blob: 79e4306ffcad29a54dd7d665a00a09d15e7f8ad2 [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 macro instantiation with substitutions.
84 MemoryBuffer *Instantiation;
85
86 /// The location of the instantiation.
87 SMLoc InstantiationLoc;
88
Daniel Dunbar40f1d852012-12-01 01:38:48 +000089 /// The buffer where parsing should resume upon instantiation completion.
90 int ExitBuffer;
91
Daniel Dunbar43235712010-07-18 18:54:11 +000092 /// The location where parsing should resume upon instantiation completion.
93 SMLoc ExitLoc;
94
Nico Weber155dccd12014-07-24 17:08:39 +000095 /// The depth of TheCondStack at the start of the instantiation.
96 size_t CondStackDepth;
97
Daniel Dunbar43235712010-07-18 18:54:11 +000098public:
Nico Weber155dccd12014-07-24 17:08:39 +000099 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, MemoryBuffer *I,
100 size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +0000101};
102
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +0000105 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106
Jim Grosbach4b905842013-09-20 23:08:21 +0000107 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 unsigned Opcode;
109
Jim Grosbach4b905842013-09-20 23:08:21 +0000110 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 bool ParseError;
112
Eli Friedman0f4871d2012-10-22 23:58:19 +0000113 SmallVectorImpl<AsmRewrite> *AsmRewrites;
114
Craig Topper353eda42014-04-24 06:44:33 +0000115 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000117 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000118};
119
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000120/// \brief The concrete assembly parser instance.
121class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000124private:
125 AsmLexer Lexer;
126 MCContext &Ctx;
127 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000128 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000130 SourceMgr::DiagHandlerTy SavedDiagHandler;
131 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000132 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000133
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000136 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000142 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000143 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000147 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000148
Jim Grosbach4b905842013-09-20 23:08:21 +0000149 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000150 std::vector<MacroInstantiation*> ActiveMacros;
151
Jim Grosbach4b905842013-09-20 23:08:21 +0000152 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000153 std::deque<MCAsmMacro> MacroLikeBodies;
154
Daniel Dunbar828984f2010-07-18 18:38:02 +0000155 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000156 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000157
Daniel Dunbar43325c42010-09-09 22:42:56 +0000158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
160
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
164 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000165 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000168 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000171 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000172 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000173
Devang Patela173ee52012-01-31 18:14:05 +0000174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
176
Jim Grosbach4b905842013-09-20 23:08:21 +0000177 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000178 bool IsDarwin;
179
Jim Grosbach4b905842013-09-20 23:08:21 +0000180 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000181 bool ParsingInlineAsm;
182
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000186 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000187
Craig Topper59be68f2014-03-08 07:14:16 +0000188 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000189
Craig Topper59be68f2014-03-08 07:14:16 +0000190 void addDirectiveHandler(StringRef Directive,
191 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000193 }
194
195public:
196 /// @name MCAsmParser Interface
197 /// {
198
Craig Topper59be68f2014-03-08 07:14:16 +0000199 SourceMgr &getSourceManager() override { return SrcMgr; }
200 MCAsmLexer &getLexer() override { return Lexer; }
201 MCContext &getContext() override { return Ctx; }
202 MCStreamer &getStreamer() override { return Out; }
203 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000204 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000205 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000206 else
207 return AssemblerDialect;
208 }
Craig Topper59be68f2014-03-08 07:14:16 +0000209 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000210 AssemblerDialect = i;
211 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000212
Craig Topper59be68f2014-03-08 07:14:16 +0000213 void Note(SMLoc L, const Twine &Msg,
214 ArrayRef<SMRange> Ranges = None) override;
215 bool Warning(SMLoc L, const Twine &Msg,
216 ArrayRef<SMRange> Ranges = None) override;
217 bool Error(SMLoc L, const Twine &Msg,
218 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper59be68f2014-03-08 07:14:16 +0000220 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000221
Craig Topper59be68f2014-03-08 07:14:16 +0000222 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
223 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000230 const MCInstrInfo *MII, const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000234 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
235 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
236 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
237 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000238
Jim Grosbach4b905842013-09-20 23:08:21 +0000239 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000240 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000241 bool parseIdentifier(StringRef &Res) override;
242 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243
Craig Topper59be68f2014-03-08 07:14:16 +0000244 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245 /// }
246
247private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 bool parseStatement(ParseStatementInfo &Info);
250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000254 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000256 ArrayRef<MCAsmMacroParameter> Parameters,
257 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
David Majnemer91fc4c22014-01-29 18:57:46 +0000289 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000290 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000291
292 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000294
Jim Grosbach4b905842013-09-20 23:08:21 +0000295 void printMacroInstantiations();
296 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000297 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000298 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000299 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000300 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000301
Jim Grosbach4b905842013-09-20 23:08:21 +0000302 /// \brief Enter the specified file. This returns true on failure.
303 bool enterIncludeFile(const std::string &Filename);
304
305 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000306 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000308
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000309 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000310 /// current token is not set; clients should ensure Lex() is called
311 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000312 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000313 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000314 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000315 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000316
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000317 /// \brief Parse up to the end of statement and a return the contents from the
318 /// current token until the end of the statement; the current token on exit
319 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000320 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000322 /// \brief Parse until the end of a statement or a comma is encountered,
323 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000324 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325
Jim Grosbach4b905842013-09-20 23:08:21 +0000326 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000327 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
330 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
331 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000334
Eli Bendersky17233942013-01-15 22:59:42 +0000335 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000336 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000337 DK_NO_DIRECTIVE, // Placeholder
338 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000339 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
340 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000341 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000343 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000344 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
345 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
346 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
347 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000348 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
349 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
350 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000351 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
352 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
353 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
354 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
355 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
356 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000357 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000358 DK_MACROS_ON, DK_MACROS_OFF,
359 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000360 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000361 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000362 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000363 };
364
Jim Grosbach4b905842013-09-20 23:08:21 +0000365 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000366 /// directives parsed by this class.
367 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000368
369 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000370 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
371 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000372 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
374 bool parseDirectiveFill(); // ".fill"
375 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000376 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
378 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000379 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000381
Eli Bendersky17233942013-01-15 22:59:42 +0000382 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveFile(SMLoc DirectiveLoc);
384 bool parseDirectiveLine();
385 bool parseDirectiveLoc();
386 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000387
388 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000390 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000391 bool parseDirectiveCFISections();
392 bool parseDirectiveCFIStartProc();
393 bool parseDirectiveCFIEndProc();
394 bool parseDirectiveCFIDefCfaOffset();
395 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIAdjustCfaOffset();
397 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
401 bool parseDirectiveCFIRememberState();
402 bool parseDirectiveCFIRestoreState();
403 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
405 bool parseDirectiveCFIEscape();
406 bool parseDirectiveCFISignalFrame();
407 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000408
409 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000410 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000411 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000412 bool parseDirectiveEndMacro(StringRef Directive);
413 bool parseDirectiveMacro(SMLoc DirectiveLoc);
414 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000415
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422
Eli Bendersky17233942013-01-15 22:59:42 +0000423 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000425
426 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000428
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveAbort(); // ".abort"
436 bool parseDirectiveInclude(); // ".include"
437 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000438
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000439 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
440 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000441 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000443 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000445 // ".ifeqs"
446 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000447 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
449 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
450 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
451 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000452 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000453
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000455 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000456
Rafael Espindola34b9c512012-06-03 23:57:14 +0000457 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
459 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000460 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000461 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000462 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
463 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
464 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000465
Chad Rosierc7f552c2013-02-12 21:33:51 +0000466 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000467 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000468 size_t Len);
469
470 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000471 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000472
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000473 // "end"
474 bool parseDirectiveEnd(SMLoc DirectiveLoc);
475
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000476 // ".err" or ".error"
477 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000478
Nico Weber404012b2014-07-24 16:26:06 +0000479 // ".warning"
480 bool parseDirectiveWarning(SMLoc DirectiveLoc);
481
Eli Bendersky17233942013-01-15 22:59:42 +0000482 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000483};
Daniel Dunbar86033402010-07-12 17:54:38 +0000484}
485
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000486namespace llvm {
487
488extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000489extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000490extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000491
492}
493
Chris Lattnerc35681b2010-01-19 19:46:13 +0000494enum { DEFAULT_ADDRSPACE = 0 };
495
Jim Grosbach4b905842013-09-20 23:08:21 +0000496AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
497 const MCAsmInfo &_MAI)
498 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000499 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
500 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
501 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000502 // Save the old handler.
503 SavedDiagHandler = SrcMgr.getDiagHandler();
504 SavedDiagContext = SrcMgr.getDiagContext();
505 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000506 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000507 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000508
Daniel Dunbarc5011082010-07-12 18:12:02 +0000509 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000510 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
511 case MCObjectFileInfo::IsCOFF:
512 PlatformParser = createCOFFAsmParser();
513 PlatformParser->Initialize(*this);
514 break;
515 case MCObjectFileInfo::IsMachO:
516 PlatformParser = createDarwinAsmParser();
517 PlatformParser->Initialize(*this);
518 IsDarwin = true;
519 break;
520 case MCObjectFileInfo::IsELF:
521 PlatformParser = createELFAsmParser();
522 PlatformParser->Initialize(*this);
523 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000524 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000525
Eli Bendersky17233942013-01-15 22:59:42 +0000526 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000527}
528
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000529AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000530 assert((HadError || ActiveMacros.empty()) &&
531 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000532
533 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000534 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
535 ie = MacroMap.end();
536 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000537 delete it->getValue();
538
Daniel Dunbarc5011082010-07-12 18:12:02 +0000539 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000540}
541
Jim Grosbach4b905842013-09-20 23:08:21 +0000542void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000543 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000544 for (std::vector<MacroInstantiation *>::const_reverse_iterator
545 it = ActiveMacros.rbegin(),
546 ie = ActiveMacros.rend();
547 it != ie; ++it)
548 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000549 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000550}
551
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000552void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
553 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
554 printMacroInstantiations();
555}
556
Chris Lattnera3a06812011-10-16 04:47:35 +0000557bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000558 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000559 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000560 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
561 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000562 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000563}
564
Chris Lattnera3a06812011-10-16 04:47:35 +0000565bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000566 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000567 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
568 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000569 return true;
570}
571
Jim Grosbach4b905842013-09-20 23:08:21 +0000572bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000573 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000574 unsigned NewBuf =
575 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
576 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000577 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000578
Sean Callanan7a77eae2010-01-21 00:19:58 +0000579 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000580 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000581 return false;
582}
Daniel Dunbar43235712010-07-18 18:54:11 +0000583
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000584/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000585/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000586/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000587bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000588 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000589 unsigned NewBuf =
590 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
591 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000592 return true;
593
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000594 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000595 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000596 return false;
597}
598
Alp Tokera55b95b2014-07-06 10:33:31 +0000599void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
600 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000601 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
602 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000603}
604
Sean Callanan7a77eae2010-01-21 00:19:58 +0000605const AsmToken &AsmParser::Lex() {
606 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 if (tok->is(AsmToken::Eof)) {
609 // If this is the end of an included file, pop the parent file off the
610 // include stack.
611 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
612 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000613 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000614 tok = &Lexer.Lex();
615 }
616 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000617
Sean Callanan7a77eae2010-01-21 00:19:58 +0000618 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000619 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000620
Sean Callanan7a77eae2010-01-21 00:19:58 +0000621 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000622}
623
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000624bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000625 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000626 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000627 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000628
Chris Lattner36e02122009-06-21 20:54:55 +0000629 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000630 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000631
632 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000633 AsmCond StartingCondState = TheCondState;
634
Kevin Enderby6469fc22011-11-01 22:27:22 +0000635 // If we are generating dwarf for assembly source files save the initial text
636 // section and generate a .file directive.
637 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000638 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
639 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000640 auto InsertResult = getContext().addGenDwarfSection(
641 getStreamer().getCurrentSection().first);
642 assert(InsertResult.second && ".text section should not have debug info yet");
643 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000644 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
645 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000646 }
647
Chris Lattner73f36112009-07-02 21:53:43 +0000648 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000649 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000650 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000651 if (!parseStatement(Info))
652 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000653
Daniel Dunbar43325c42010-09-09 22:42:56 +0000654 // We had an error, validate that one was emitted and recover by skipping to
655 // the next line.
656 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000657 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000658 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000659
660 if (TheCondState.TheCond != StartingCondState.TheCond ||
661 TheCondState.Ignore != StartingCondState.Ignore)
662 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000663
664 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000665 const auto &LineTables = getContext().getMCDwarfLineTables();
666 if (!LineTables.empty()) {
667 unsigned Index = 0;
668 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
669 if (File.Name.empty() && Index != 0)
670 TokError("unassigned file number: " + Twine(Index) +
671 " for .file directives");
672 ++Index;
673 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000674 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000675
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000676 // Check to see that all assembler local symbols were actually defined.
677 // Targets that don't do subsections via symbols may not want this, though,
678 // so conservatively exclude them. Only do this if we're finalizing, though,
679 // as otherwise we won't necessarilly have seen everything yet.
680 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
681 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
682 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000683 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000684 i != e; ++i) {
685 MCSymbol *Sym = i->getValue();
686 // Variable symbols may not be marked as defined, so check those
687 // explicitly. If we know it's a variable, we have a definition for
688 // the purposes of this check.
689 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
690 // FIXME: We would really like to refer back to where the symbol was
691 // first referenced for a source location. We need to add something
692 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000693 printMessage(
694 getLexer().getLoc(), SourceMgr::DK_Error,
695 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000696 }
697 }
698
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000699 // Finalize the output stream if there are no errors and if the client wants
700 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000701 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000702 Out.Finish();
703
Chris Lattner73f36112009-07-02 21:53:43 +0000704 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000705}
706
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000707void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000708 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000709 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000710 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000711 }
712}
713
Jim Grosbach4b905842013-09-20 23:08:21 +0000714/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000715void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000716 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000717 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000718
Chris Lattnere5074c42009-06-22 01:29:09 +0000719 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000720 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000721 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000722}
723
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000724StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000725 const char *Start = getTok().getLoc().getPointer();
726
Jim Grosbach4b905842013-09-20 23:08:21 +0000727 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000728 Lex();
729
730 const char *End = getTok().getLoc().getPointer();
731 return StringRef(Start, End - Start);
732}
Chris Lattner78db3622009-06-22 05:51:26 +0000733
Jim Grosbach4b905842013-09-20 23:08:21 +0000734StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000735 const char *Start = getTok().getLoc().getPointer();
736
737 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000738 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000739 Lex();
740
741 const char *End = getTok().getLoc().getPointer();
742 return StringRef(Start, End - Start);
743}
744
Jim Grosbach4b905842013-09-20 23:08:21 +0000745/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000746/// NOTE: This assumes the leading '(' has already been consumed.
747///
748/// parenexpr ::= expr)
749///
Jim Grosbach4b905842013-09-20 23:08:21 +0000750bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
751 if (parseExpression(Res))
752 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000753 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000754 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000755 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000756 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000757 return false;
758}
Chris Lattner78db3622009-06-22 05:51:26 +0000759
Jim Grosbach4b905842013-09-20 23:08:21 +0000760/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000761/// NOTE: This assumes the leading '[' has already been consumed.
762///
763/// bracketexpr ::= expr]
764///
Jim Grosbach4b905842013-09-20 23:08:21 +0000765bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
766 if (parseExpression(Res))
767 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000768 if (Lexer.isNot(AsmToken::RBrac))
769 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000770 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000771 Lex();
772 return false;
773}
774
Jim Grosbach4b905842013-09-20 23:08:21 +0000775/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000776/// primaryexpr ::= (parenexpr
777/// primaryexpr ::= symbol
778/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000779/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000780/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000781bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000782 SMLoc FirstTokenLoc = getLexer().getLoc();
783 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
784 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000785 default:
786 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000787 // If we have an error assume that we've already handled it.
788 case AsmToken::Error:
789 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000790 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000791 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000792 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000793 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000794 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000795 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000796 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000797 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000798 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000799 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000800 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000801 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000802 if (FirstTokenKind == AsmToken::Dollar) {
803 if (Lexer.getMAI().getDollarIsPC()) {
804 // This is a '$' reference, which references the current PC. Emit a
805 // temporary label to the streamer and refer to it.
806 MCSymbol *Sym = Ctx.CreateTempSymbol();
807 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000808 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
809 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000810 EndLoc = FirstTokenLoc;
811 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000812 }
813 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000814 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000815 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000816 // Parse symbol variant
817 std::pair<StringRef, StringRef> Split;
818 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000819 if (FirstTokenKind == AsmToken::String) {
820 if (Lexer.is(AsmToken::At)) {
821 Lexer.Lex(); // eat @
822 SMLoc AtLoc = getLexer().getLoc();
823 StringRef VName;
824 if (parseIdentifier(VName))
825 return Error(AtLoc, "expected symbol variant after '@'");
826
827 Split = std::make_pair(Identifier, VName);
828 }
829 } else {
830 Split = Identifier.split('@');
831 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000832 } else if (Lexer.is(AsmToken::LParen)) {
833 Lexer.Lex(); // eat (
834 StringRef VName;
835 parseIdentifier(VName);
836 if (Lexer.isNot(AsmToken::RParen)) {
837 return Error(Lexer.getTok().getLoc(),
838 "unexpected token in variant, expected ')'");
839 }
840 Lexer.Lex(); // eat )
841 Split = std::make_pair(Identifier, VName);
842 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000843
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000844 EndLoc = SMLoc::getFromPointer(Identifier.end());
845
Daniel Dunbard20cda02009-10-16 01:34:54 +0000846 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000847 StringRef SymbolName = Identifier;
848 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000849
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000850 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000851 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000852 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000853 if (Variant != MCSymbolRefExpr::VK_Invalid) {
854 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000855 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000856 Variant = MCSymbolRefExpr::VK_None;
857 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000858 return Error(SMLoc::getFromPointer(Split.second.begin()),
859 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000860 }
861 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000862
Hans Wennborgce69d772013-10-18 20:46:28 +0000863 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
864
Daniel Dunbard20cda02009-10-16 01:34:54 +0000865 // If this is an absolute variable reference, substitute it now to preserve
866 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000867 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000868 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000869 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000870
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000871 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000872 return false;
873 }
874
875 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000876 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000877 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000878 }
David Woodhousef42a6662014-02-01 16:20:54 +0000879 case AsmToken::BigNum:
880 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000881 case AsmToken::Integer: {
882 SMLoc Loc = getTok().getLoc();
883 int64_t IntVal = getTok().getIntVal();
884 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000885 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000886 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000887 // Look for 'b' or 'f' following an Integer as a directional label
888 if (Lexer.getKind() == AsmToken::Identifier) {
889 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000890 // Lookup the symbol variant if used.
891 std::pair<StringRef, StringRef> Split = IDVal.split('@');
892 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
893 if (Split.first.size() != IDVal.size()) {
894 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000895 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000896 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000897 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000898 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000899 if (IDVal == "f" || IDVal == "b") {
900 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000901 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000902 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000903 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000904 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000905 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000906 Lex(); // Eat identifier.
907 }
908 }
Chris Lattner78db3622009-06-22 05:51:26 +0000909 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000910 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000911 case AsmToken::Real: {
912 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000913 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000914 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000915 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000916 Lex(); // Eat token.
917 return false;
918 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000919 case AsmToken::Dot: {
920 // This is a '.' reference, which references the current PC. Emit a
921 // temporary label to the streamer and refer to it.
922 MCSymbol *Sym = Ctx.CreateTempSymbol();
923 Out.EmitLabel(Sym);
924 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000925 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000926 Lex(); // Eat identifier.
927 return false;
928 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000929 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000930 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000932 case AsmToken::LBrac:
933 if (!PlatformParser->HasBracketExpressions())
934 return TokError("brackets expression not supported on this target");
935 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000936 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000937 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000938 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000939 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000941 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000942 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000943 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000944 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000945 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000947 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000948 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000949 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000950 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000951 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000952 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000953 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000954 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000955 }
956}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000957
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000958bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000959 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000960 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000961}
962
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000964AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000965 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000966 // Ask the target implementation about this expression first.
967 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
968 if (NewE)
969 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970 // Recurse over the given expression, rebuilding it to apply the given variant
971 // if there is exactly one symbol.
972 switch (E->getKind()) {
973 case MCExpr::Target:
974 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000975 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000976
977 case MCExpr::SymbolRef: {
978 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
979
980 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000981 TokError("invalid variant on expression '" + getTok().getIdentifier() +
982 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000983 return E;
984 }
985
986 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
987 }
988
989 case MCExpr::Unary: {
990 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000991 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000992 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000993 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000994 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
995 }
996
997 case MCExpr::Binary: {
998 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000999 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
1000 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001
1002 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +00001003 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +00001004
Jim Grosbach4b905842013-09-20 23:08:21 +00001005 if (!LHS)
1006 LHS = BE->getLHS();
1007 if (!RHS)
1008 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001009
1010 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1011 }
1012 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001013
Craig Toppera2886c22012-02-07 05:05:23 +00001014 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001015}
1016
Jim Grosbach4b905842013-09-20 23:08:21 +00001017/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001018///
Jim Grosbachbd164242011-08-20 16:24:13 +00001019/// expr ::= expr &&,|| expr -> lowest.
1020/// expr ::= expr |,^,&,! expr
1021/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1022/// expr ::= expr <<,>> expr
1023/// expr ::= expr +,- expr
1024/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001025/// expr ::= primaryexpr
1026///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001027bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001028 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001029 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001030 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001031 return true;
1032
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033 // As a special case, we support 'a op b @ modifier' by rewriting the
1034 // expression to include the modifier. This is inefficient, but in general we
1035 // expect users to use 'a@modifier op b'.
1036 if (Lexer.getKind() == AsmToken::At) {
1037 Lex();
1038
1039 if (Lexer.isNot(AsmToken::Identifier))
1040 return TokError("unexpected symbol modifier following '@'");
1041
1042 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001043 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001044 if (Variant == MCSymbolRefExpr::VK_Invalid)
1045 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1046
Jim Grosbach4b905842013-09-20 23:08:21 +00001047 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001048 if (!ModifiedRes) {
1049 return TokError("invalid modifier '" + getTok().getIdentifier() +
1050 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001051 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001052
Daniel Dunbar55f16672010-09-17 02:47:07 +00001053 Res = ModifiedRes;
1054 Lex();
1055 }
1056
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001057 // Try to constant fold it up front, if possible.
1058 int64_t Value;
1059 if (Res->EvaluateAsAbsolute(Value))
1060 Res = MCConstantExpr::Create(Value, getContext());
1061
1062 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001063}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001064
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001065bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001066 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001067 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001068}
1069
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001070bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001071 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001072
Daniel Dunbar75630b32009-06-30 02:10:03 +00001073 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001074 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001075 return true;
1076
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001077 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001078 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001079
1080 return false;
1081}
1082
Michael J. Spencer530ce852010-10-09 11:00:50 +00001083static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001085 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001086 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001087 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001088
Jim Grosbach4b905842013-09-20 23:08:21 +00001089 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001092 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001093 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001095 return 1;
1096
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 // Low Precedence: |, &, ^
1098 //
1099 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001100 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001101 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001102 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001103 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001104 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001105 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001106 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001107 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001108 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001109
Jim Grosbach4b905842013-09-20 23:08:21 +00001110 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001111 case AsmToken::EqualEqual:
1112 Kind = MCBinaryExpr::EQ;
1113 return 3;
1114 case AsmToken::ExclaimEqual:
1115 case AsmToken::LessGreater:
1116 Kind = MCBinaryExpr::NE;
1117 return 3;
1118 case AsmToken::Less:
1119 Kind = MCBinaryExpr::LT;
1120 return 3;
1121 case AsmToken::LessEqual:
1122 Kind = MCBinaryExpr::LTE;
1123 return 3;
1124 case AsmToken::Greater:
1125 Kind = MCBinaryExpr::GT;
1126 return 3;
1127 case AsmToken::GreaterEqual:
1128 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001129 return 3;
1130
Jim Grosbach4b905842013-09-20 23:08:21 +00001131 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001132 case AsmToken::LessLess:
1133 Kind = MCBinaryExpr::Shl;
1134 return 4;
1135 case AsmToken::GreaterGreater:
1136 Kind = MCBinaryExpr::Shr;
1137 return 4;
1138
Jim Grosbach4b905842013-09-20 23:08:21 +00001139 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001140 case AsmToken::Plus:
1141 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001142 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001143 case AsmToken::Minus:
1144 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001145 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001146
Jim Grosbach4b905842013-09-20 23:08:21 +00001147 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001148 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001149 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001150 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001151 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001152 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001153 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001154 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001155 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001156 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001157 }
1158}
1159
Jim Grosbach4b905842013-09-20 23:08:21 +00001160/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001161/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001162bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001163 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001165 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001166 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001167
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001168 // If the next token is lower precedence than we are allowed to eat, return
1169 // successfully with what we ate already.
1170 if (TokPrec < Precedence)
1171 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001172
Sean Callanan686ed8d2010-01-19 20:22:31 +00001173 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001174
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001175 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001176 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001177 if (parsePrimaryExpr(RHS, EndLoc))
1178 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001179
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001180 // If BinOp binds less tightly with RHS than the operator after RHS, let
1181 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001182 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001183 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001184 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1185 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001186
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001187 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001188 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001189 }
1190}
1191
Chris Lattner36e02122009-06-21 20:54:55 +00001192/// ParseStatement:
1193/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001194/// ::= Label* Directive ...Operands... EndOfStatement
1195/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001196bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001197 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001198 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001199 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001200 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001201 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001202
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001203 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001204 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001205 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001206 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001207 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001208 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001209 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001210 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001211
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001212 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001213 if (Lexer.is(AsmToken::Integer)) {
1214 LocalLabelVal = getTok().getIntVal();
1215 if (LocalLabelVal < 0) {
1216 if (!TheCondState.Ignore)
1217 return TokError("unexpected token at start of statement");
1218 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001219 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001220 IDVal = getTok().getString();
1221 Lex(); // Consume the integer token to be used as an identifier token.
1222 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001223 if (!TheCondState.Ignore)
1224 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001225 }
1226 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001227 } else if (Lexer.is(AsmToken::Dot)) {
1228 // Treat '.' as a valid identifier in this context.
1229 Lex();
1230 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001231 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001232 if (!TheCondState.Ignore)
1233 return TokError("unexpected token at start of statement");
1234 IDVal = "";
1235 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001236
Chris Lattner926885c2010-04-17 18:14:27 +00001237 // Handle conditional assembly here before checking for skipping. We
1238 // have to do this so that .endif isn't skipped in a ".if 0" block for
1239 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001240 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001241 DirectiveKindMap.find(IDVal);
1242 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1243 ? DK_NO_DIRECTIVE
1244 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001245 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001246 default:
1247 break;
1248 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001249 case DK_IFEQ:
1250 case DK_IFGE:
1251 case DK_IFGT:
1252 case DK_IFLE:
1253 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001254 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001255 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001256 case DK_IFB:
1257 return parseDirectiveIfb(IDLoc, true);
1258 case DK_IFNB:
1259 return parseDirectiveIfb(IDLoc, false);
1260 case DK_IFC:
1261 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001262 case DK_IFEQS:
1263 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001264 case DK_IFNC:
1265 return parseDirectiveIfc(IDLoc, false);
1266 case DK_IFDEF:
1267 return parseDirectiveIfdef(IDLoc, true);
1268 case DK_IFNDEF:
1269 case DK_IFNOTDEF:
1270 return parseDirectiveIfdef(IDLoc, false);
1271 case DK_ELSEIF:
1272 return parseDirectiveElseIf(IDLoc);
1273 case DK_ELSE:
1274 return parseDirectiveElse(IDLoc);
1275 case DK_ENDIF:
1276 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001277 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001278
Eli Bendersky88024712013-01-16 19:32:36 +00001279 // Ignore the statement if in the middle of inactive conditional
1280 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001281 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001282 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001283 return false;
1284 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001285
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001286 // FIXME: Recurse on local labels?
1287
1288 // See what kind of statement we have.
1289 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001290 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001291 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001292
Chris Lattner36e02122009-06-21 20:54:55 +00001293 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001294 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001295
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001296 // Diagnose attempt to use '.' as a label.
1297 if (IDVal == ".")
1298 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1299
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001300 // Diagnose attempt to use a variable as a label.
1301 //
1302 // FIXME: Diagnostics. Note the location of the definition as a label.
1303 // FIXME: This doesn't diagnose assignment to a symbol which has been
1304 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001305 MCSymbol *Sym;
1306 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001307 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001308 else
1309 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001310 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001311 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001312
Daniel Dunbare73b2672009-08-26 22:13:22 +00001313 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001314 if (!ParsingInlineAsm)
1315 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001316
Kevin Enderbye7739d42011-12-09 18:09:40 +00001317 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001318 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001319 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001320 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1321 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001322
Tim Northover1744d0a2013-10-25 12:49:50 +00001323 getTargetParser().onLabelParsed(Sym);
1324
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001325 // Consume any end of statement token, if present, to avoid spurious
1326 // AddBlankLine calls().
1327 if (Lexer.is(AsmToken::EndOfStatement)) {
1328 Lex();
1329 if (Lexer.is(AsmToken::Eof))
1330 return false;
1331 }
1332
Eli Friedman0f4871d2012-10-22 23:58:19 +00001333 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001334 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001335
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001336 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001337 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001338 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001339
Jim Grosbach4b905842013-09-20 23:08:21 +00001340 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001341
1342 default: // Normal instruction or directive.
1343 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001344 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001345
1346 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001347 if (areMacrosEnabled())
1348 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1349 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001350 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001351
Michael J. Spencer530ce852010-10-09 11:00:50 +00001352 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001353
Eli Bendersky17233942013-01-15 22:59:42 +00001354 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001355 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001356 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001357 //
Eli Bendersky17233942013-01-15 22:59:42 +00001358 // 1. The target-specific assembly parser. Some directives are target
1359 // specific or may potentially behave differently on certain targets.
1360 // 2. Asm parser extensions. For example, platform-specific parsers
1361 // (like the ELF parser) register themselves as extensions.
1362 // 3. The generic directive parser implemented by this class. These are
1363 // all the directives that behave in a target and platform independent
1364 // manner, or at least have a default behavior that's shared between
1365 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001366
Eli Bendersky17233942013-01-15 22:59:42 +00001367 // First query the target-specific parser. It will return 'true' if it
1368 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001369 if (!getTargetParser().ParseDirective(ID))
1370 return false;
1371
Alp Tokercb402912014-01-24 17:20:08 +00001372 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001373 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001374 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1375 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001376 if (Handler.first)
1377 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1378
1379 // Finally, if no one else is interested in this directive, it must be
1380 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001381 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001382 default:
1383 break;
1384 case DK_SET:
1385 case DK_EQU:
1386 return parseDirectiveSet(IDVal, true);
1387 case DK_EQUIV:
1388 return parseDirectiveSet(IDVal, false);
1389 case DK_ASCII:
1390 return parseDirectiveAscii(IDVal, false);
1391 case DK_ASCIZ:
1392 case DK_STRING:
1393 return parseDirectiveAscii(IDVal, true);
1394 case DK_BYTE:
1395 return parseDirectiveValue(1);
1396 case DK_SHORT:
1397 case DK_VALUE:
1398 case DK_2BYTE:
1399 return parseDirectiveValue(2);
1400 case DK_LONG:
1401 case DK_INT:
1402 case DK_4BYTE:
1403 return parseDirectiveValue(4);
1404 case DK_QUAD:
1405 case DK_8BYTE:
1406 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001407 case DK_OCTA:
1408 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001409 case DK_SINGLE:
1410 case DK_FLOAT:
1411 return parseDirectiveRealValue(APFloat::IEEEsingle);
1412 case DK_DOUBLE:
1413 return parseDirectiveRealValue(APFloat::IEEEdouble);
1414 case DK_ALIGN: {
1415 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1416 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1417 }
1418 case DK_ALIGN32: {
1419 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1420 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1421 }
1422 case DK_BALIGN:
1423 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1424 case DK_BALIGNW:
1425 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1426 case DK_BALIGNL:
1427 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1428 case DK_P2ALIGN:
1429 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1430 case DK_P2ALIGNW:
1431 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1432 case DK_P2ALIGNL:
1433 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1434 case DK_ORG:
1435 return parseDirectiveOrg();
1436 case DK_FILL:
1437 return parseDirectiveFill();
1438 case DK_ZERO:
1439 return parseDirectiveZero();
1440 case DK_EXTERN:
1441 eatToEndOfStatement(); // .extern is the default, ignore it.
1442 return false;
1443 case DK_GLOBL:
1444 case DK_GLOBAL:
1445 return parseDirectiveSymbolAttribute(MCSA_Global);
1446 case DK_LAZY_REFERENCE:
1447 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1448 case DK_NO_DEAD_STRIP:
1449 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1450 case DK_SYMBOL_RESOLVER:
1451 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1452 case DK_PRIVATE_EXTERN:
1453 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1454 case DK_REFERENCE:
1455 return parseDirectiveSymbolAttribute(MCSA_Reference);
1456 case DK_WEAK_DEFINITION:
1457 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1458 case DK_WEAK_REFERENCE:
1459 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1460 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1461 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1462 case DK_COMM:
1463 case DK_COMMON:
1464 return parseDirectiveComm(/*IsLocal=*/false);
1465 case DK_LCOMM:
1466 return parseDirectiveComm(/*IsLocal=*/true);
1467 case DK_ABORT:
1468 return parseDirectiveAbort();
1469 case DK_INCLUDE:
1470 return parseDirectiveInclude();
1471 case DK_INCBIN:
1472 return parseDirectiveIncbin();
1473 case DK_CODE16:
1474 case DK_CODE16GCC:
1475 return TokError(Twine(IDVal) + " not supported yet");
1476 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001477 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001478 case DK_IRP:
1479 return parseDirectiveIrp(IDLoc);
1480 case DK_IRPC:
1481 return parseDirectiveIrpc(IDLoc);
1482 case DK_ENDR:
1483 return parseDirectiveEndr(IDLoc);
1484 case DK_BUNDLE_ALIGN_MODE:
1485 return parseDirectiveBundleAlignMode();
1486 case DK_BUNDLE_LOCK:
1487 return parseDirectiveBundleLock();
1488 case DK_BUNDLE_UNLOCK:
1489 return parseDirectiveBundleUnlock();
1490 case DK_SLEB128:
1491 return parseDirectiveLEB128(true);
1492 case DK_ULEB128:
1493 return parseDirectiveLEB128(false);
1494 case DK_SPACE:
1495 case DK_SKIP:
1496 return parseDirectiveSpace(IDVal);
1497 case DK_FILE:
1498 return parseDirectiveFile(IDLoc);
1499 case DK_LINE:
1500 return parseDirectiveLine();
1501 case DK_LOC:
1502 return parseDirectiveLoc();
1503 case DK_STABS:
1504 return parseDirectiveStabs();
1505 case DK_CFI_SECTIONS:
1506 return parseDirectiveCFISections();
1507 case DK_CFI_STARTPROC:
1508 return parseDirectiveCFIStartProc();
1509 case DK_CFI_ENDPROC:
1510 return parseDirectiveCFIEndProc();
1511 case DK_CFI_DEF_CFA:
1512 return parseDirectiveCFIDefCfa(IDLoc);
1513 case DK_CFI_DEF_CFA_OFFSET:
1514 return parseDirectiveCFIDefCfaOffset();
1515 case DK_CFI_ADJUST_CFA_OFFSET:
1516 return parseDirectiveCFIAdjustCfaOffset();
1517 case DK_CFI_DEF_CFA_REGISTER:
1518 return parseDirectiveCFIDefCfaRegister(IDLoc);
1519 case DK_CFI_OFFSET:
1520 return parseDirectiveCFIOffset(IDLoc);
1521 case DK_CFI_REL_OFFSET:
1522 return parseDirectiveCFIRelOffset(IDLoc);
1523 case DK_CFI_PERSONALITY:
1524 return parseDirectiveCFIPersonalityOrLsda(true);
1525 case DK_CFI_LSDA:
1526 return parseDirectiveCFIPersonalityOrLsda(false);
1527 case DK_CFI_REMEMBER_STATE:
1528 return parseDirectiveCFIRememberState();
1529 case DK_CFI_RESTORE_STATE:
1530 return parseDirectiveCFIRestoreState();
1531 case DK_CFI_SAME_VALUE:
1532 return parseDirectiveCFISameValue(IDLoc);
1533 case DK_CFI_RESTORE:
1534 return parseDirectiveCFIRestore(IDLoc);
1535 case DK_CFI_ESCAPE:
1536 return parseDirectiveCFIEscape();
1537 case DK_CFI_SIGNAL_FRAME:
1538 return parseDirectiveCFISignalFrame();
1539 case DK_CFI_UNDEFINED:
1540 return parseDirectiveCFIUndefined(IDLoc);
1541 case DK_CFI_REGISTER:
1542 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001543 case DK_CFI_WINDOW_SAVE:
1544 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001545 case DK_MACROS_ON:
1546 case DK_MACROS_OFF:
1547 return parseDirectiveMacrosOnOff(IDVal);
1548 case DK_MACRO:
1549 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001550 case DK_EXITM:
1551 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001552 case DK_ENDM:
1553 case DK_ENDMACRO:
1554 return parseDirectiveEndMacro(IDVal);
1555 case DK_PURGEM:
1556 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001557 case DK_END:
1558 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001559 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001560 return parseDirectiveError(IDLoc, false);
1561 case DK_ERROR:
1562 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001563 case DK_WARNING:
1564 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001565 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001566
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001567 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001568 }
Chris Lattner36e02122009-06-21 20:54:55 +00001569
Chad Rosierc7f552c2013-02-12 21:33:51 +00001570 // __asm _emit or __asm __emit
1571 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1572 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001573 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001574
1575 // __asm align
1576 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001577 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001578
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001579 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001580
Chris Lattner7cbfa442010-05-19 23:34:33 +00001581 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001582 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001583 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001584 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001585 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001586 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001587
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001588 // Dump the parsed representation, if requested.
1589 if (getShowParsedOperands()) {
1590 SmallString<256> Str;
1591 raw_svector_ostream OS(Str);
1592 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001593 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001594 if (i != 0)
1595 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001596 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001597 }
1598 OS << "]";
1599
Jim Grosbach4b905842013-09-20 23:08:21 +00001600 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001601 }
1602
Oliver Stannard8b273082014-06-19 15:52:37 +00001603 // If we are generating dwarf for the current section then generate a .loc
1604 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001605 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001606 getContext().getGenDwarfSectionSyms().count(
1607 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001608
Eli Bendersky88024712013-01-16 19:32:36 +00001609 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001610
Eli Bendersky88024712013-01-16 19:32:36 +00001611 // If we previously parsed a cpp hash file line comment then make sure the
1612 // current Dwarf File is for the CppHashFilename if not then emit the
1613 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001614 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001615 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1616 0, StringRef(), CppHashFilename);
1617 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001618
Jim Grosbach4b905842013-09-20 23:08:21 +00001619 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1620 // cache with the different Loc from the call above we save the last
1621 // info we queried here with SrcMgr.FindLineNumber().
1622 unsigned CppHashLocLineNo;
1623 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1624 CppHashLocLineNo = LastQueryLine;
1625 else {
1626 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1627 LastQueryLine = CppHashLocLineNo;
1628 LastQueryIDLoc = CppHashLoc;
1629 LastQueryBuffer = CppHashBuf;
1630 }
1631 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001632 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001633
Jim Grosbach4b905842013-09-20 23:08:21 +00001634 getStreamer().EmitDwarfLocDirective(
1635 getContext().getGenDwarfFileNumber(), Line, 0,
1636 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1637 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001638 }
1639
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001640 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001641 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001642 unsigned ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001643 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1644 Info.ParsedOperands, Out,
1645 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001646 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001647
Chris Lattnera2a9d162010-09-11 16:18:25 +00001648 // Don't skip the rest of the line, the instruction parser is responsible for
1649 // that.
1650 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001651}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001652
Jim Grosbach4b905842013-09-20 23:08:21 +00001653/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001654/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001655void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001656 if (!Lexer.is(AsmToken::EndOfStatement))
1657 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001658 // Eat EOL.
1659 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001660}
1661
Jim Grosbach4b905842013-09-20 23:08:21 +00001662/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001663/// ::= # number "filename"
1664/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001665bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001666 Lex(); // Eat the hash token.
1667
1668 if (getLexer().isNot(AsmToken::Integer)) {
1669 // Consume the line since in cases it is not a well-formed line directive,
1670 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001671 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001672 return false;
1673 }
1674
1675 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001676 Lex();
1677
1678 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001679 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001680 return false;
1681 }
1682
1683 StringRef Filename = getTok().getString();
1684 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001685 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001686
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001687 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1688 CppHashLoc = L;
1689 CppHashFilename = Filename;
1690 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001691 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001692
1693 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001694 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001695 return false;
1696}
1697
Jim Grosbach4b905842013-09-20 23:08:21 +00001698/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699/// for the Filename and LineNo if any in the diagnostic.
1700void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001701 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702 raw_ostream &OS = errs();
1703
1704 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1705 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001706 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1707 unsigned CppHashBuf =
1708 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709
Jim Grosbach4b905842013-09-20 23:08:21 +00001710 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001712 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1713 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1714 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1716 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001717 }
1718
Eric Christophera7c32732012-12-18 00:30:54 +00001719 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720 // manager changed or buffer changed (like in a nested include) then just
1721 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001722 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001723 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001724 if (Parser->SavedDiagHandler)
1725 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1726 else
Craig Topper353eda42014-04-24 06:44:33 +00001727 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001728 return;
1729 }
1730
Eric Christophera7c32732012-12-18 00:30:54 +00001731 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001732 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1733 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001734 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001735
1736 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1737 int CppHashLocLineNo =
1738 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001739 int LineNo =
1740 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001741
Jim Grosbach4b905842013-09-20 23:08:21 +00001742 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1743 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001744 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001745
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001746 if (Parser->SavedDiagHandler)
1747 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1748 else
Craig Topper353eda42014-04-24 06:44:33 +00001749 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001750}
1751
Rafael Espindola2c064482012-08-21 18:29:30 +00001752// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1753// difference being that that function accepts '@' as part of identifiers and
1754// we can't do that. AsmLexer.cpp should probably be changed to handle
1755// '@' as a special case when needed.
1756static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001757 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1758 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001759}
1760
Rafael Espindola34b9c512012-06-03 23:57:14 +00001761bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001762 ArrayRef<MCAsmMacroParameter> Parameters,
1763 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001765 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001766 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001767 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001768
Preston Gurd05500642012-09-19 20:36:12 +00001769 // A macro without parameters is handled differently on Darwin:
1770 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001771 while (!Body.empty()) {
1772 // Scan for the next substitution.
1773 std::size_t End = Body.size(), Pos = 0;
1774 for (; Pos != End; ++Pos) {
1775 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001776 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 // This macro has no parameters, look for $0, $1, etc.
1778 if (Body[Pos] != '$' || Pos + 1 == End)
1779 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001780
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001782 if (Next == '$' || Next == 'n' ||
1783 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001784 break;
1785 } else {
1786 // This macro has parameters, look for \foo, \bar, etc.
1787 if (Body[Pos] == '\\' && Pos + 1 != End)
1788 break;
1789 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001790 }
1791
1792 // Add the prefix.
1793 OS << Body.slice(0, Pos);
1794
1795 // Check if we reached the end.
1796 if (Pos == End)
1797 break;
1798
Benjamin Kramer513e7442014-02-20 13:36:32 +00001799 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001800 switch (Body[Pos + 1]) {
1801 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 case '$':
1803 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001804 break;
1805
Jim Grosbach4b905842013-09-20 23:08:21 +00001806 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001807 case 'n':
1808 OS << A.size();
1809 break;
1810
Jim Grosbach4b905842013-09-20 23:08:21 +00001811 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001812 default: {
1813 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001814 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001815 if (Index >= A.size())
1816 break;
1817
1818 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001819 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001820 ie = A[Index].end();
1821 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 OS << it->getString();
1823 break;
1824 }
1825 }
1826 Pos += 2;
1827 } else {
1828 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001829 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001830 ++I;
1831
Jim Grosbach4b905842013-09-20 23:08:21 +00001832 const char *Begin = Body.data() + Pos + 1;
1833 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001834 unsigned Index = 0;
1835 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001836 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001837 break;
1838
Preston Gurd05500642012-09-19 20:36:12 +00001839 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001840 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1841 Pos += 3;
1842 else {
1843 OS << '\\' << Argument;
1844 Pos = I;
1845 }
Preston Gurd05500642012-09-19 20:36:12 +00001846 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001847 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001848 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001849 ie = A[Index].end();
1850 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001851 // We expect no quotes around the string's contents when
1852 // parsing for varargs.
1853 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001854 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001855 else
1856 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001857
Preston Gurd05500642012-09-19 20:36:12 +00001858 Pos += 1 + Argument.size();
1859 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001860 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001861 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001862 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001863 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001864
Rafael Espindola1134ab232011-06-05 02:43:45 +00001865 return false;
1866}
Daniel Dunbar43235712010-07-18 18:54:11 +00001867
Nico Weber2a8f9222014-07-24 16:29:04 +00001868MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Nico Weber155dccd12014-07-24 17:08:39 +00001869 MemoryBuffer *I, size_t CondStackDepth)
1870 : Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
1871 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001872
Jim Grosbach4b905842013-09-20 23:08:21 +00001873static bool isOperator(AsmToken::TokenKind kind) {
1874 switch (kind) {
1875 default:
1876 return false;
1877 case AsmToken::Plus:
1878 case AsmToken::Minus:
1879 case AsmToken::Tilde:
1880 case AsmToken::Slash:
1881 case AsmToken::Star:
1882 case AsmToken::Dot:
1883 case AsmToken::Equal:
1884 case AsmToken::EqualEqual:
1885 case AsmToken::Pipe:
1886 case AsmToken::PipePipe:
1887 case AsmToken::Caret:
1888 case AsmToken::Amp:
1889 case AsmToken::AmpAmp:
1890 case AsmToken::Exclaim:
1891 case AsmToken::ExclaimEqual:
1892 case AsmToken::Percent:
1893 case AsmToken::Less:
1894 case AsmToken::LessEqual:
1895 case AsmToken::LessLess:
1896 case AsmToken::LessGreater:
1897 case AsmToken::Greater:
1898 case AsmToken::GreaterEqual:
1899 case AsmToken::GreaterGreater:
1900 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001901 }
1902}
1903
David Majnemer16252452014-01-29 00:07:39 +00001904namespace {
1905class AsmLexerSkipSpaceRAII {
1906public:
1907 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1908 Lexer.setSkipSpace(SkipSpace);
1909 }
1910
1911 ~AsmLexerSkipSpaceRAII() {
1912 Lexer.setSkipSpace(true);
1913 }
1914
1915private:
1916 AsmLexer &Lexer;
1917};
1918}
1919
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001920bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1921
1922 if (Vararg) {
1923 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1924 StringRef Str = parseStringToEndOfStatement();
1925 MA.push_back(AsmToken(AsmToken::String, Str));
1926 }
1927 return false;
1928 }
1929
Rafael Espindola768b41c2012-06-15 14:02:34 +00001930 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001931 unsigned AddTokens = 0;
1932
David Majnemer16252452014-01-29 00:07:39 +00001933 // Darwin doesn't use spaces to delmit arguments.
1934 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001935
1936 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001937 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001938 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001939
David Majnemer91fc4c22014-01-29 18:57:46 +00001940 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001941 break;
Preston Gurd05500642012-09-19 20:36:12 +00001942
1943 if (Lexer.is(AsmToken::Space)) {
1944 Lex(); // Eat spaces
1945
1946 // Spaces can delimit parameters, but could also be part an expression.
1947 // If the token after a space is an operator, add the token and the next
1948 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001949 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001950 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001951 // Check to see whether the token is used as an operator,
1952 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001953 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001954 if (*NextChar == ' ')
1955 AddTokens = 2;
1956 }
1957
1958 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001959 break;
1960 }
1961 }
1962 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001963
Jim Grosbach4b905842013-09-20 23:08:21 +00001964 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001965 // to be able to fill in the remaining default parameter values
1966 if (Lexer.is(AsmToken::EndOfStatement))
1967 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001968
1969 // Adjust the current parentheses level.
1970 if (Lexer.is(AsmToken::LParen))
1971 ++ParenLevel;
1972 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1973 --ParenLevel;
1974
1975 // Append the token to the current argument list.
1976 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001977 if (AddTokens)
1978 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001979 Lex();
1980 }
Preston Gurd05500642012-09-19 20:36:12 +00001981
Rafael Espindola768b41c2012-06-15 14:02:34 +00001982 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001983 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001984 return false;
1985}
1986
1987// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001988bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001989 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001990 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001991 bool NamedParametersFound = false;
1992 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001993
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001994 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001995 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001996
Rafael Espindola768b41c2012-06-15 14:02:34 +00001997 // Parse two kinds of macro invocations:
1998 // - macros defined without any parameters accept an arbitrary number of them
1999 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002000 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002001 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2002 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002003 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002004 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00002005
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002006 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002007 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002008 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002009 eatToEndOfStatement();
2010 return true;
2011 }
2012
2013 if (!Lexer.is(AsmToken::Equal)) {
2014 TokError("expected '=' after formal parameter identifier");
2015 eatToEndOfStatement();
2016 return true;
2017 }
2018 Lex();
2019
2020 NamedParametersFound = true;
2021 }
2022
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002023 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002024 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002025 eatToEndOfStatement();
2026 return true;
2027 }
2028
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002029 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2030 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002031 return true;
2032
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002033 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002034 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002035 unsigned FAI = 0;
2036 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002037 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002038 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002039
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002040 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002041 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002042 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002043 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002044 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002045 return true;
2046 }
2047 PI = FAI;
2048 }
2049
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002050 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002051 if (A.size() <= PI)
2052 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002053 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002054
2055 if (FALocs.size() <= PI)
2056 FALocs.resize(PI + 1);
2057
2058 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002059 }
Jim Grosbach206661622012-07-30 22:44:17 +00002060
Preston Gurd242ed3152012-09-19 20:29:04 +00002061 // At the end of the statement, fill in remaining arguments that have
2062 // default values. If there aren't any, then the next argument is
2063 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002064 if (Lexer.is(AsmToken::EndOfStatement)) {
2065 bool Failure = false;
2066 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2067 if (A[FAI].empty()) {
2068 if (M->Parameters[FAI].Required) {
2069 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2070 "missing value for required parameter "
2071 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2072 Failure = true;
2073 }
2074
2075 if (!M->Parameters[FAI].Value.empty())
2076 A[FAI] = M->Parameters[FAI].Value;
2077 }
2078 }
2079 return Failure;
2080 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002081
2082 if (Lexer.is(AsmToken::Comma))
2083 Lex();
2084 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002085
2086 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002087}
2088
Jim Grosbach4b905842013-09-20 23:08:21 +00002089const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2090 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002091 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002092}
2093
Jim Grosbach4b905842013-09-20 23:08:21 +00002094void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002095 MacroMap[Name] = new MCAsmMacro(Macro);
2096}
2097
Jim Grosbach4b905842013-09-20 23:08:21 +00002098void AsmParser::undefineMacro(StringRef Name) {
2099 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002100 if (I != MacroMap.end()) {
2101 delete I->getValue();
2102 MacroMap.erase(I);
2103 }
2104}
2105
Jim Grosbach4b905842013-09-20 23:08:21 +00002106bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002107 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2108 // this, although we should protect against infinite loops.
2109 if (ActiveMacros.size() == 20)
2110 return TokError("macros cannot be nested more than 20 levels deep");
2111
Eli Bendersky38274122013-01-14 23:22:36 +00002112 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002113 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002114 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002115
Rafael Espindola1134ab232011-06-05 02:43:45 +00002116 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2117 // to hold the macro body with substitutions.
2118 SmallString<256> Buf;
2119 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002120 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002121
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002122 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002123 return true;
2124
Eli Bendersky38274122013-01-14 23:22:36 +00002125 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002126 // instantiation.
2127 OS << ".endmacro\n";
2128
Rafael Espindola1134ab232011-06-05 02:43:45 +00002129 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002130 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002131
Daniel Dunbar43235712010-07-18 18:54:11 +00002132 // Create the macro instantiation object and add to the current macro
2133 // instantiation stack.
Nico Weber155dccd12014-07-24 17:08:39 +00002134 MacroInstantiation *MI =
2135 new MacroInstantiation(NameLoc, CurBuffer, getTok().getLoc(),
2136 Instantiation, TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002137 ActiveMacros.push_back(MI);
2138
2139 // Jump to the macro instantiation and prime the lexer.
2140 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002141 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002142 Lex();
2143
2144 return false;
2145}
2146
Jim Grosbach4b905842013-09-20 23:08:21 +00002147void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002148 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002149 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002150 Lex();
2151
2152 // Pop the instantiation entry.
2153 delete ActiveMacros.back();
2154 ActiveMacros.pop_back();
2155}
2156
Jim Grosbach4b905842013-09-20 23:08:21 +00002157static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002158 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002159 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002160 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2161 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002162 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002163 case MCExpr::Target:
2164 case MCExpr::Constant:
2165 return false;
2166 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002167 const MCSymbol &S =
2168 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002169 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002170 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002171 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002172 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002173 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002174 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002175 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002176
2177 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002178}
2179
Jim Grosbach4b905842013-09-20 23:08:21 +00002180bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002181 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002182 // FIXME: Use better location, we should use proper tokens.
2183 SMLoc EqualLoc = Lexer.getLoc();
2184
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002185 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002186 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002187 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002188
Rafael Espindola72f5f172012-01-28 05:57:00 +00002189 // Note: we don't count b as used in "a = b". This is to allow
2190 // a = b
2191 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002192
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002193 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002194 return TokError("unexpected token in assignment");
2195
2196 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002197 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002198
Daniel Dunbar5f339242009-10-16 01:57:39 +00002199 // Validate that the LHS is allowed to be a variable (either it has not been
2200 // used as a symbol, or it is an absolute symbol).
2201 MCSymbol *Sym = getContext().LookupSymbol(Name);
2202 if (Sym) {
2203 // Diagnose assignment to a label.
2204 //
2205 // FIXME: Diagnostics. Note the location of the definition as a label.
2206 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002207 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002208 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2209 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002210 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002211 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2212 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002213 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002214 return Error(EqualLoc, "redefinition of '" + Name + "'");
2215 else if (!Sym->isVariable())
2216 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002217 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002218 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002219 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002220
2221 // Don't count these checks as uses.
2222 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002223 } else if (Name == ".") {
2224 if (Out.EmitValueToOffset(Value, 0)) {
2225 Error(EqualLoc, "expected absolute expression");
2226 eatToEndOfStatement();
2227 }
2228 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002229 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002230 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002231
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002232 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002233 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002234 if (NoDeadStrip)
2235 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2236
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002237 return false;
2238}
2239
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002240/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002241/// ::= identifier
2242/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002243bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002245 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2246 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002247 // handle this as a context dependent token, instead we detect adjacent tokens
2248 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002249 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2250 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002251
Hans Wennborgce69d772013-10-18 20:46:28 +00002252 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002253 Lex();
2254 if (Lexer.isNot(AsmToken::Identifier))
2255 return true;
2256
Hans Wennborgce69d772013-10-18 20:46:28 +00002257 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2258 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002259 return true;
2260
2261 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002262 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002263 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002264 Lex();
2265 return false;
2266 }
2267
Jim Grosbach4b905842013-09-20 23:08:21 +00002268 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002269 return true;
2270
Sean Callanan936b0d32010-01-19 21:44:56 +00002271 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002272
Sean Callanan686ed8d2010-01-19 20:22:31 +00002273 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002274
2275 return false;
2276}
2277
Jim Grosbach4b905842013-09-20 23:08:21 +00002278/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002279/// ::= .equ identifier ',' expression
2280/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002281/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002282bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002283 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002285 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002286 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002287
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002288 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002289 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002290 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002291
Jim Grosbach4b905842013-09-20 23:08:21 +00002292 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002293}
2294
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002295bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002296 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002297
2298 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002299 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002300 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2301 if (Str[i] != '\\') {
2302 Data += Str[i];
2303 continue;
2304 }
2305
2306 // Recognize escaped characters. Note that this escape semantics currently
2307 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2308 ++i;
2309 if (i == e)
2310 return TokError("unexpected backslash at end of string");
2311
2312 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002313 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002314 // Consume up to three octal characters.
2315 unsigned Value = Str[i] - '0';
2316
Jim Grosbach4b905842013-09-20 23:08:21 +00002317 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002318 ++i;
2319 Value = Value * 8 + (Str[i] - '0');
2320
Jim Grosbach4b905842013-09-20 23:08:21 +00002321 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 ++i;
2323 Value = Value * 8 + (Str[i] - '0');
2324 }
2325 }
2326
2327 if (Value > 255)
2328 return TokError("invalid octal escape sequence (out of range)");
2329
Jim Grosbach4b905842013-09-20 23:08:21 +00002330 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002331 continue;
2332 }
2333
2334 // Otherwise recognize individual escapes.
2335 switch (Str[i]) {
2336 default:
2337 // Just reject invalid escape sequences for now.
2338 return TokError("invalid escape sequence (unrecognized character)");
2339
2340 case 'b': Data += '\b'; break;
2341 case 'f': Data += '\f'; break;
2342 case 'n': Data += '\n'; break;
2343 case 'r': Data += '\r'; break;
2344 case 't': Data += '\t'; break;
2345 case '"': Data += '"'; break;
2346 case '\\': Data += '\\'; break;
2347 }
2348 }
2349
2350 return false;
2351}
2352
Jim Grosbach4b905842013-09-20 23:08:21 +00002353/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002354/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002355bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002356 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002357 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002358
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002360 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002361 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002362
Daniel Dunbaref668c12009-08-14 18:19:52 +00002363 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002364 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002365 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002366
Rafael Espindola64e1af82013-07-02 15:49:13 +00002367 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002368 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002369 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002370
Sean Callanan686ed8d2010-01-19 20:22:31 +00002371 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002372
2373 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 break;
2375
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002376 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002377 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002378 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002379 }
2380 }
2381
Sean Callanan686ed8d2010-01-19 20:22:31 +00002382 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 return false;
2384}
2385
Jim Grosbach4b905842013-09-20 23:08:21 +00002386/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002388bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002389 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002390 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002391
Daniel Dunbara10e5192009-06-24 23:30:00 +00002392 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002393 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002394 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002395 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002396 return true;
2397
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002398 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002399 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2400 assert(Size <= 8 && "Invalid size");
2401 uint64_t IntValue = MCE->getValue();
2402 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2403 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002404 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002405 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002406 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002407
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002408 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002409 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002410
Daniel Dunbara10e5192009-06-24 23:30:00 +00002411 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002412 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002413 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002414 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002415 }
2416 }
2417
Sean Callanan686ed8d2010-01-19 20:22:31 +00002418 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002419 return false;
2420}
2421
David Woodhoused6de0d92014-02-01 16:20:59 +00002422/// ParseDirectiveOctaValue
2423/// ::= .octa [ hexconstant (, hexconstant)* ]
2424bool AsmParser::parseDirectiveOctaValue() {
2425 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2426 checkForValidSection();
2427
2428 for (;;) {
2429 if (Lexer.getKind() == AsmToken::Error)
2430 return true;
2431 if (Lexer.getKind() != AsmToken::Integer &&
2432 Lexer.getKind() != AsmToken::BigNum)
2433 return TokError("unknown token in expression");
2434
2435 SMLoc ExprLoc = getLexer().getLoc();
2436 APInt IntValue = getTok().getAPIntVal();
2437 Lex();
2438
2439 uint64_t hi, lo;
2440 if (IntValue.isIntN(64)) {
2441 hi = 0;
2442 lo = IntValue.getZExtValue();
2443 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002444 // It might actually have more than 128 bits, but the top ones are zero.
2445 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002446 lo = IntValue.getLoBits(64).getZExtValue();
2447 } else
2448 return Error(ExprLoc, "literal value out of range for directive");
2449
2450 if (MAI.isLittleEndian()) {
2451 getStreamer().EmitIntValue(lo, 8);
2452 getStreamer().EmitIntValue(hi, 8);
2453 } else {
2454 getStreamer().EmitIntValue(hi, 8);
2455 getStreamer().EmitIntValue(lo, 8);
2456 }
2457
2458 if (getLexer().is(AsmToken::EndOfStatement))
2459 break;
2460
2461 // FIXME: Improve diagnostic.
2462 if (getLexer().isNot(AsmToken::Comma))
2463 return TokError("unexpected token in directive");
2464 Lex();
2465 }
2466 }
2467
2468 Lex();
2469 return false;
2470}
2471
Jim Grosbach4b905842013-09-20 23:08:21 +00002472/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002473/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002474bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002475 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002476 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002477
2478 for (;;) {
2479 // We don't truly support arithmetic on floating point expressions, so we
2480 // have to manually parse unary prefixes.
2481 bool IsNeg = false;
2482 if (getLexer().is(AsmToken::Minus)) {
2483 Lex();
2484 IsNeg = true;
2485 } else if (getLexer().is(AsmToken::Plus))
2486 Lex();
2487
Michael J. Spencer530ce852010-10-09 11:00:50 +00002488 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002489 getLexer().isNot(AsmToken::Real) &&
2490 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002491 return TokError("unexpected token in directive");
2492
2493 // Convert to an APFloat.
2494 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002495 StringRef IDVal = getTok().getString();
2496 if (getLexer().is(AsmToken::Identifier)) {
2497 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2498 Value = APFloat::getInf(Semantics);
2499 else if (!IDVal.compare_lower("nan"))
2500 Value = APFloat::getNaN(Semantics, false, ~0);
2501 else
2502 return TokError("invalid floating point literal");
2503 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002504 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002505 return TokError("invalid floating point literal");
2506 if (IsNeg)
2507 Value.changeSign();
2508
2509 // Consume the numeric token.
2510 Lex();
2511
2512 // Emit the value as an integer.
2513 APInt AsInt = Value.bitcastToAPInt();
2514 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002515 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002516
2517 if (getLexer().is(AsmToken::EndOfStatement))
2518 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002519
Daniel Dunbar2af16532010-09-24 01:59:56 +00002520 if (getLexer().isNot(AsmToken::Comma))
2521 return TokError("unexpected token in directive");
2522 Lex();
2523 }
2524 }
2525
2526 Lex();
2527 return false;
2528}
2529
Jim Grosbach4b905842013-09-20 23:08:21 +00002530/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002531/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002532bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002533 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002534
2535 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002536 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002537 return true;
2538
Rafael Espindolab91bac62010-10-05 19:42:57 +00002539 int64_t Val = 0;
2540 if (getLexer().is(AsmToken::Comma)) {
2541 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002542 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002543 return true;
2544 }
2545
Rafael Espindola922e3f42010-09-16 15:03:59 +00002546 if (getLexer().isNot(AsmToken::EndOfStatement))
2547 return TokError("unexpected token in '.zero' directive");
2548
2549 Lex();
2550
Rafael Espindola64e1af82013-07-02 15:49:13 +00002551 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002552
2553 return false;
2554}
2555
Jim Grosbach4b905842013-09-20 23:08:21 +00002556/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002557/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002558bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002559 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002560
David Majnemer522d3db2014-02-01 07:19:38 +00002561 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002562 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002563 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002564 return true;
2565
David Majnemer522d3db2014-02-01 07:19:38 +00002566 if (NumValues < 0) {
2567 Warning(RepeatLoc,
2568 "'.fill' directive with negative repeat count has no effect");
2569 NumValues = 0;
2570 }
2571
Roman Divackye33098f2013-09-24 17:44:41 +00002572 int64_t FillSize = 1;
2573 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002574
David Majnemer522d3db2014-02-01 07:19:38 +00002575 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002576 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2577 if (getLexer().isNot(AsmToken::Comma))
2578 return TokError("unexpected token in '.fill' directive");
2579 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002580
David Majnemer522d3db2014-02-01 07:19:38 +00002581 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002582 if (parseAbsoluteExpression(FillSize))
2583 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002584
Roman Divackye33098f2013-09-24 17:44:41 +00002585 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2586 if (getLexer().isNot(AsmToken::Comma))
2587 return TokError("unexpected token in '.fill' directive");
2588 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002589
David Majnemer522d3db2014-02-01 07:19:38 +00002590 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002591 if (parseAbsoluteExpression(FillExpr))
2592 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002593
Roman Divackye33098f2013-09-24 17:44:41 +00002594 if (getLexer().isNot(AsmToken::EndOfStatement))
2595 return TokError("unexpected token in '.fill' directive");
2596
2597 Lex();
2598 }
2599 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002600
David Majnemer522d3db2014-02-01 07:19:38 +00002601 if (FillSize < 0) {
2602 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2603 NumValues = 0;
2604 }
2605 if (FillSize > 8) {
2606 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2607 FillSize = 8;
2608 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002609
David Majnemer522d3db2014-02-01 07:19:38 +00002610 if (!isUInt<32>(FillExpr) && FillSize > 4)
2611 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2612
2613 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2614 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2615
2616 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2617 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2618 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2619 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002620
2621 return false;
2622}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002623
Jim Grosbach4b905842013-09-20 23:08:21 +00002624/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002625/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002626bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002627 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002628
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002629 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002630 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002631 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002632 return true;
2633
2634 // Parse optional fill expression.
2635 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002636 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2637 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002638 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002639 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002640
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002641 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002642 return true;
2643
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002644 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002645 return TokError("unexpected token in '.org' directive");
2646 }
2647
Sean Callanan686ed8d2010-01-19 20:22:31 +00002648 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002649
Jim Grosbachb5912772012-01-27 00:37:08 +00002650 // Only limited forms of relocatable expressions are accepted here, it
2651 // has to be relative to the current section. The streamer will return
2652 // 'true' if the expression wasn't evaluatable.
2653 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2654 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002655
2656 return false;
2657}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002658
Jim Grosbach4b905842013-09-20 23:08:21 +00002659/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002661bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002662 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002663
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002664 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002665 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002666 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002667 return true;
2668
2669 SMLoc MaxBytesLoc;
2670 bool HasFillExpr = false;
2671 int64_t FillExpr = 0;
2672 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002673 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2674 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002675 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002676 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677
2678 // The fill expression can be omitted while specifying a maximum number of
2679 // alignment bytes, e.g:
2680 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002681 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002683 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002684 return true;
2685 }
2686
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002687 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2688 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002689 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002690 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002691
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002692 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002693 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002694 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002695
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002696 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697 return TokError("unexpected token in directive");
2698 }
2699 }
2700
Sean Callanan686ed8d2010-01-19 20:22:31 +00002701 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002702
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002703 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002704 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002705
2706 // Compute alignment in bytes.
2707 if (IsPow2) {
2708 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002709 if (Alignment >= 32) {
2710 Error(AlignmentLoc, "invalid alignment value");
2711 Alignment = 31;
2712 }
2713
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002714 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002715 } else {
2716 // Reject alignments that aren't a power of two, for gas compatibility.
2717 if (!isPowerOf2_64(Alignment))
2718 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002719 }
2720
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002721 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002722 if (MaxBytesLoc.isValid()) {
2723 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002724 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002725 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002726 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002727 }
2728
2729 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002730 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002731 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002732 MaxBytesToFill = 0;
2733 }
2734 }
2735
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002736 // Check whether we should use optimal code alignment for this .align
2737 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002738 const MCSection *Section = getStreamer().getCurrentSection().first;
2739 assert(Section && "must have section to emit alignment");
2740 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002741 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2742 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002743 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002744 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002745 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002746 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2747 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002748 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002749
2750 return false;
2751}
2752
Jim Grosbach4b905842013-09-20 23:08:21 +00002753/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002754/// ::= .file [number] filename
2755/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002756bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002757 // FIXME: I'm not sure what this is.
2758 int64_t FileNumber = -1;
2759 SMLoc FileNumberLoc = getLexer().getLoc();
2760 if (getLexer().is(AsmToken::Integer)) {
2761 FileNumber = getTok().getIntVal();
2762 Lex();
2763
2764 if (FileNumber < 1)
2765 return TokError("file number less than one");
2766 }
2767
2768 if (getLexer().isNot(AsmToken::String))
2769 return TokError("unexpected token in '.file' directive");
2770
2771 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002772 // Allow the strings to have escaped octal character sequence.
2773 std::string Path = getTok().getString();
2774 if (parseEscapedString(Path))
2775 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002776 Lex();
2777
2778 StringRef Directory;
2779 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002780 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002781 if (getLexer().is(AsmToken::String)) {
2782 if (FileNumber == -1)
2783 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002784 if (parseEscapedString(FilenameData))
2785 return true;
2786 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002787 Directory = Path;
2788 Lex();
2789 } else {
2790 Filename = Path;
2791 }
2792
2793 if (getLexer().isNot(AsmToken::EndOfStatement))
2794 return TokError("unexpected token in '.file' directive");
2795
2796 if (FileNumber == -1)
2797 getStreamer().EmitFileDirective(Filename);
2798 else {
2799 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002800 Error(DirectiveLoc,
2801 "input can't have .file dwarf directives when -g is "
2802 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002803
David Blaikiec714ef42014-03-17 01:52:11 +00002804 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2805 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002806 Error(FileNumberLoc, "file number already allocated");
2807 }
2808
2809 return false;
2810}
2811
Jim Grosbach4b905842013-09-20 23:08:21 +00002812/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002813/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002814bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002815 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2816 if (getLexer().isNot(AsmToken::Integer))
2817 return TokError("unexpected token in '.line' directive");
2818
2819 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002820 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002821 Lex();
2822
2823 // FIXME: Do something with the .line.
2824 }
2825
2826 if (getLexer().isNot(AsmToken::EndOfStatement))
2827 return TokError("unexpected token in '.line' directive");
2828
2829 return false;
2830}
2831
Jim Grosbach4b905842013-09-20 23:08:21 +00002832/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002833/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2834/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2835/// The first number is a file number, must have been previously assigned with
2836/// a .file directive, the second number is the line number and optionally the
2837/// third number is a column position (zero if not specified). The remaining
2838/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002839bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002840 if (getLexer().isNot(AsmToken::Integer))
2841 return TokError("unexpected token in '.loc' directive");
2842 int64_t FileNumber = getTok().getIntVal();
2843 if (FileNumber < 1)
2844 return TokError("file number less than one in '.loc' directive");
2845 if (!getContext().isValidDwarfFileNumber(FileNumber))
2846 return TokError("unassigned file number in '.loc' directive");
2847 Lex();
2848
2849 int64_t LineNumber = 0;
2850 if (getLexer().is(AsmToken::Integer)) {
2851 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002852 if (LineNumber < 0)
2853 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002854 Lex();
2855 }
2856
2857 int64_t ColumnPos = 0;
2858 if (getLexer().is(AsmToken::Integer)) {
2859 ColumnPos = getTok().getIntVal();
2860 if (ColumnPos < 0)
2861 return TokError("column position less than zero in '.loc' directive");
2862 Lex();
2863 }
2864
2865 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2866 unsigned Isa = 0;
2867 int64_t Discriminator = 0;
2868 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2869 for (;;) {
2870 if (getLexer().is(AsmToken::EndOfStatement))
2871 break;
2872
2873 StringRef Name;
2874 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002875 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002876 return TokError("unexpected token in '.loc' directive");
2877
2878 if (Name == "basic_block")
2879 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2880 else if (Name == "prologue_end")
2881 Flags |= DWARF2_FLAG_PROLOGUE_END;
2882 else if (Name == "epilogue_begin")
2883 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2884 else if (Name == "is_stmt") {
2885 Loc = getTok().getLoc();
2886 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002887 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002888 return true;
2889 // The expression must be the constant 0 or 1.
2890 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2891 int Value = MCE->getValue();
2892 if (Value == 0)
2893 Flags &= ~DWARF2_FLAG_IS_STMT;
2894 else if (Value == 1)
2895 Flags |= DWARF2_FLAG_IS_STMT;
2896 else
2897 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002898 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002899 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2900 }
Craig Topperf15655b2013-04-22 04:22:40 +00002901 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002902 Loc = getTok().getLoc();
2903 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002904 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002905 return true;
2906 // The expression must be a constant greater or equal to 0.
2907 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2908 int Value = MCE->getValue();
2909 if (Value < 0)
2910 return Error(Loc, "isa number less than zero");
2911 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002912 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002913 return Error(Loc, "isa number not a constant value");
2914 }
Craig Topperf15655b2013-04-22 04:22:40 +00002915 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002916 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002917 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002918 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002919 return Error(Loc, "unknown sub-directive in '.loc' directive");
2920 }
2921
2922 if (getLexer().is(AsmToken::EndOfStatement))
2923 break;
2924 }
2925 }
2926
2927 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2928 Isa, Discriminator, StringRef());
2929
2930 return false;
2931}
2932
Jim Grosbach4b905842013-09-20 23:08:21 +00002933/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002934/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002935bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002936 return TokError("unsupported directive '.stabs'");
2937}
2938
Jim Grosbach4b905842013-09-20 23:08:21 +00002939/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002940/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002941bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002942 StringRef Name;
2943 bool EH = false;
2944 bool Debug = false;
2945
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002946 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002947 return TokError("Expected an identifier");
2948
2949 if (Name == ".eh_frame")
2950 EH = true;
2951 else if (Name == ".debug_frame")
2952 Debug = true;
2953
2954 if (getLexer().is(AsmToken::Comma)) {
2955 Lex();
2956
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002957 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002958 return TokError("Expected an identifier");
2959
2960 if (Name == ".eh_frame")
2961 EH = true;
2962 else if (Name == ".debug_frame")
2963 Debug = true;
2964 }
2965
2966 getStreamer().EmitCFISections(EH, Debug);
2967 return false;
2968}
2969
Jim Grosbach4b905842013-09-20 23:08:21 +00002970/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002971/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002972bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002973 StringRef Simple;
2974 if (getLexer().isNot(AsmToken::EndOfStatement))
2975 if (parseIdentifier(Simple) || Simple != "simple")
2976 return TokError("unexpected token in .cfi_startproc directive");
2977
2978 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002979 return false;
2980}
2981
Jim Grosbach4b905842013-09-20 23:08:21 +00002982/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002983/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002984bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002985 getStreamer().EmitCFIEndProc();
2986 return false;
2987}
2988
Jim Grosbach4b905842013-09-20 23:08:21 +00002989/// \brief parse register name or number.
2990bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002991 SMLoc DirectiveLoc) {
2992 unsigned RegNo;
2993
2994 if (getLexer().isNot(AsmToken::Integer)) {
2995 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2996 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002997 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002998 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002999 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00003000
3001 return false;
3002}
3003
Jim Grosbach4b905842013-09-20 23:08:21 +00003004/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00003005/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003006bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003007 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003008 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003009 return true;
3010
3011 if (getLexer().isNot(AsmToken::Comma))
3012 return TokError("unexpected token in directive");
3013 Lex();
3014
3015 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003016 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003017 return true;
3018
3019 getStreamer().EmitCFIDefCfa(Register, Offset);
3020 return false;
3021}
3022
Jim Grosbach4b905842013-09-20 23:08:21 +00003023/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003024/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003025bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003026 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003027 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003028 return true;
3029
3030 getStreamer().EmitCFIDefCfaOffset(Offset);
3031 return false;
3032}
3033
Jim Grosbach4b905842013-09-20 23:08:21 +00003034/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003035/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003036bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003037 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003038 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003039 return true;
3040
3041 if (getLexer().isNot(AsmToken::Comma))
3042 return TokError("unexpected token in directive");
3043 Lex();
3044
3045 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003046 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003047 return true;
3048
3049 getStreamer().EmitCFIRegister(Register1, Register2);
3050 return false;
3051}
3052
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003053/// parseDirectiveCFIWindowSave
3054/// ::= .cfi_window_save
3055bool AsmParser::parseDirectiveCFIWindowSave() {
3056 getStreamer().EmitCFIWindowSave();
3057 return false;
3058}
3059
Jim Grosbach4b905842013-09-20 23:08:21 +00003060/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003061/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003062bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003063 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003064 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003065 return true;
3066
3067 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3068 return false;
3069}
3070
Jim Grosbach4b905842013-09-20 23:08:21 +00003071/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003072/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003073bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003074 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003075 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003076 return true;
3077
3078 getStreamer().EmitCFIDefCfaRegister(Register);
3079 return false;
3080}
3081
Jim Grosbach4b905842013-09-20 23:08:21 +00003082/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003083/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003084bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003085 int64_t Register = 0;
3086 int64_t Offset = 0;
3087
Jim Grosbach4b905842013-09-20 23:08:21 +00003088 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003089 return true;
3090
3091 if (getLexer().isNot(AsmToken::Comma))
3092 return TokError("unexpected token in directive");
3093 Lex();
3094
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003095 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003096 return true;
3097
3098 getStreamer().EmitCFIOffset(Register, Offset);
3099 return false;
3100}
3101
Jim Grosbach4b905842013-09-20 23:08:21 +00003102/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003103/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003104bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003105 int64_t Register = 0;
3106
Jim Grosbach4b905842013-09-20 23:08:21 +00003107 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003108 return true;
3109
3110 if (getLexer().isNot(AsmToken::Comma))
3111 return TokError("unexpected token in directive");
3112 Lex();
3113
3114 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003115 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003116 return true;
3117
3118 getStreamer().EmitCFIRelOffset(Register, Offset);
3119 return false;
3120}
3121
3122static bool isValidEncoding(int64_t Encoding) {
3123 if (Encoding & ~0xff)
3124 return false;
3125
3126 if (Encoding == dwarf::DW_EH_PE_omit)
3127 return true;
3128
3129 const unsigned Format = Encoding & 0xf;
3130 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3131 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3132 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3133 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3134 return false;
3135
3136 const unsigned Application = Encoding & 0x70;
3137 if (Application != dwarf::DW_EH_PE_absptr &&
3138 Application != dwarf::DW_EH_PE_pcrel)
3139 return false;
3140
3141 return true;
3142}
3143
Jim Grosbach4b905842013-09-20 23:08:21 +00003144/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003145/// IsPersonality true for cfi_personality, false for cfi_lsda
3146/// ::= .cfi_personality encoding, [symbol_name]
3147/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003148bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003149 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003150 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003151 return true;
3152 if (Encoding == dwarf::DW_EH_PE_omit)
3153 return false;
3154
3155 if (!isValidEncoding(Encoding))
3156 return TokError("unsupported encoding.");
3157
3158 if (getLexer().isNot(AsmToken::Comma))
3159 return TokError("unexpected token in directive");
3160 Lex();
3161
3162 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003163 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003164 return TokError("expected identifier in directive");
3165
3166 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3167
3168 if (IsPersonality)
3169 getStreamer().EmitCFIPersonality(Sym, Encoding);
3170 else
3171 getStreamer().EmitCFILsda(Sym, Encoding);
3172 return false;
3173}
3174
Jim Grosbach4b905842013-09-20 23:08:21 +00003175/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003176/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003177bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003178 getStreamer().EmitCFIRememberState();
3179 return false;
3180}
3181
Jim Grosbach4b905842013-09-20 23:08:21 +00003182/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003183/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003184bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003185 getStreamer().EmitCFIRestoreState();
3186 return false;
3187}
3188
Jim Grosbach4b905842013-09-20 23:08:21 +00003189/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003190/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003191bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003192 int64_t Register = 0;
3193
Jim Grosbach4b905842013-09-20 23:08:21 +00003194 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003195 return true;
3196
3197 getStreamer().EmitCFISameValue(Register);
3198 return false;
3199}
3200
Jim Grosbach4b905842013-09-20 23:08:21 +00003201/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003202/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003203bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003204 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003205 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003206 return true;
3207
3208 getStreamer().EmitCFIRestore(Register);
3209 return false;
3210}
3211
Jim Grosbach4b905842013-09-20 23:08:21 +00003212/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003213/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003214bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003215 std::string Values;
3216 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003217 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003218 return true;
3219
3220 Values.push_back((uint8_t)CurrValue);
3221
3222 while (getLexer().is(AsmToken::Comma)) {
3223 Lex();
3224
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003225 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003226 return true;
3227
3228 Values.push_back((uint8_t)CurrValue);
3229 }
3230
3231 getStreamer().EmitCFIEscape(Values);
3232 return false;
3233}
3234
Jim Grosbach4b905842013-09-20 23:08:21 +00003235/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003236/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003237bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003238 if (getLexer().isNot(AsmToken::EndOfStatement))
3239 return Error(getLexer().getLoc(),
3240 "unexpected token in '.cfi_signal_frame'");
3241
3242 getStreamer().EmitCFISignalFrame();
3243 return false;
3244}
3245
Jim Grosbach4b905842013-09-20 23:08:21 +00003246/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003247/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003248bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003249 int64_t Register = 0;
3250
Jim Grosbach4b905842013-09-20 23:08:21 +00003251 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003252 return true;
3253
3254 getStreamer().EmitCFIUndefined(Register);
3255 return false;
3256}
3257
Jim Grosbach4b905842013-09-20 23:08:21 +00003258/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003259/// ::= .macros_on
3260/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003261bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003262 if (getLexer().isNot(AsmToken::EndOfStatement))
3263 return Error(getLexer().getLoc(),
3264 "unexpected token in '" + Directive + "' directive");
3265
Jim Grosbach4b905842013-09-20 23:08:21 +00003266 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003267 return false;
3268}
3269
Jim Grosbach4b905842013-09-20 23:08:21 +00003270/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003271/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003272bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003273 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003274 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003275 return TokError("expected identifier in '.macro' directive");
3276
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003277 if (getLexer().is(AsmToken::Comma))
3278 Lex();
3279
Eli Bendersky17233942013-01-15 22:59:42 +00003280 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003281 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003282
3283 if (Parameters.size() && Parameters.back().Vararg)
3284 return Error(Lexer.getLoc(),
3285 "Vararg parameter '" + Parameters.back().Name +
3286 "' should be last one in the list of parameters.");
3287
David Majnemer91fc4c22014-01-29 18:57:46 +00003288 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003289 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003290 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003291
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003292 if (Lexer.is(AsmToken::Colon)) {
3293 Lex(); // consume ':'
3294
3295 SMLoc QualLoc;
3296 StringRef Qualifier;
3297
3298 QualLoc = Lexer.getLoc();
3299 if (parseIdentifier(Qualifier))
3300 return Error(QualLoc, "missing parameter qualifier for "
3301 "'" + Parameter.Name + "' in macro '" + Name + "'");
3302
3303 if (Qualifier == "req")
3304 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003305 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003306 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003307 else
3308 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3309 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3310 }
3311
David Majnemer91fc4c22014-01-29 18:57:46 +00003312 if (getLexer().is(AsmToken::Equal)) {
3313 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003314
3315 SMLoc ParamLoc;
3316
3317 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003318 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003319 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003320
3321 if (Parameter.Required)
3322 Warning(ParamLoc, "pointless default value for required parameter "
3323 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003324 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003325
3326 Parameters.push_back(Parameter);
3327
3328 if (getLexer().is(AsmToken::Comma))
3329 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003330 }
3331
3332 // Eat the end of statement.
3333 Lex();
3334
3335 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003336 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003337
3338 // Lex the macro definition.
3339 for (;;) {
3340 // Check whether we have reached the end of the file.
3341 if (getLexer().is(AsmToken::Eof))
3342 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3343
3344 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003345 if (getLexer().is(AsmToken::Identifier)) {
3346 if (getTok().getIdentifier() == ".endm" ||
3347 getTok().getIdentifier() == ".endmacro") {
3348 if (MacroDepth == 0) { // Outermost macro.
3349 EndToken = getTok();
3350 Lex();
3351 if (getLexer().isNot(AsmToken::EndOfStatement))
3352 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3353 "' directive");
3354 break;
3355 } else {
3356 // Otherwise we just found the end of an inner macro.
3357 --MacroDepth;
3358 }
3359 } else if (getTok().getIdentifier() == ".macro") {
3360 // We allow nested macros. Those aren't instantiated until the outermost
3361 // macro is expanded so just ignore them for now.
3362 ++MacroDepth;
3363 }
Eli Bendersky17233942013-01-15 22:59:42 +00003364 }
3365
3366 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003367 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003368 }
3369
Jim Grosbach4b905842013-09-20 23:08:21 +00003370 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003371 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3372 }
3373
3374 const char *BodyStart = StartToken.getLoc().getPointer();
3375 const char *BodyEnd = EndToken.getLoc().getPointer();
3376 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003377 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3378 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003379 return false;
3380}
3381
Jim Grosbach4b905842013-09-20 23:08:21 +00003382/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003383///
3384/// With the support added for named parameters there may be code out there that
3385/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003386/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003387/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003388/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003389/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3390/// warning that the positional parameter found in body which have no effect.
3391/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003392/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003393/// intended or change the macro to use the named parameters. It is possible
3394/// this warning will trigger when the none of the named parameters are used
3395/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003396void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003397 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003398 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003399 // If this macro is not defined with named parameters the warning we are
3400 // checking for here doesn't apply.
3401 unsigned NParameters = Parameters.size();
3402 if (NParameters == 0)
3403 return;
3404
3405 bool NamedParametersFound = false;
3406 bool PositionalParametersFound = false;
3407
3408 // Look at the body of the macro for use of both the named parameters and what
3409 // are likely to be positional parameters. This is what expandMacro() is
3410 // doing when it finds the parameters in the body.
3411 while (!Body.empty()) {
3412 // Scan for the next possible parameter.
3413 std::size_t End = Body.size(), Pos = 0;
3414 for (; Pos != End; ++Pos) {
3415 // Check for a substitution or escape.
3416 // This macro is defined with parameters, look for \foo, \bar, etc.
3417 if (Body[Pos] == '\\' && Pos + 1 != End)
3418 break;
3419
3420 // This macro should have parameters, but look for $0, $1, ..., $n too.
3421 if (Body[Pos] != '$' || Pos + 1 == End)
3422 continue;
3423 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003424 if (Next == '$' || Next == 'n' ||
3425 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003426 break;
3427 }
3428
3429 // Check if we reached the end.
3430 if (Pos == End)
3431 break;
3432
3433 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003434 switch (Body[Pos + 1]) {
3435 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003436 case '$':
3437 break;
3438
Jim Grosbach4b905842013-09-20 23:08:21 +00003439 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003440 case 'n':
3441 PositionalParametersFound = true;
3442 break;
3443
Jim Grosbach4b905842013-09-20 23:08:21 +00003444 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003445 default: {
3446 PositionalParametersFound = true;
3447 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003448 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003449 }
3450 Pos += 2;
3451 } else {
3452 unsigned I = Pos + 1;
3453 while (isIdentifierChar(Body[I]) && I + 1 != End)
3454 ++I;
3455
Jim Grosbach4b905842013-09-20 23:08:21 +00003456 const char *Begin = Body.data() + Pos + 1;
3457 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003458 unsigned Index = 0;
3459 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003460 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003461 break;
3462
3463 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003464 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3465 Pos += 3;
3466 else {
3467 Pos = I;
3468 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003469 } else {
3470 NamedParametersFound = true;
3471 Pos += 1 + Argument.size();
3472 }
3473 }
3474 // Update the scan point.
3475 Body = Body.substr(Pos);
3476 }
3477
3478 if (!NamedParametersFound && PositionalParametersFound)
3479 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3480 "used in macro body, possible positional parameter "
3481 "found in body which will have no effect");
3482}
3483
Nico Weber155dccd12014-07-24 17:08:39 +00003484/// parseDirectiveExitMacro
3485/// ::= .exitm
3486bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3487 if (getLexer().isNot(AsmToken::EndOfStatement))
3488 return TokError("unexpected token in '" + Directive + "' directive");
3489
3490 if (!isInsideMacroInstantiation())
3491 return TokError("unexpected '" + Directive + "' in file, "
3492 "no current macro definition");
3493
3494 // Exit all conditionals that are active in the current macro.
3495 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3496 TheCondState = TheCondStack.back();
3497 TheCondStack.pop_back();
3498 }
3499
3500 handleMacroExit();
3501 return false;
3502}
3503
Jim Grosbach4b905842013-09-20 23:08:21 +00003504/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003505/// ::= .endm
3506/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003507bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003508 if (getLexer().isNot(AsmToken::EndOfStatement))
3509 return TokError("unexpected token in '" + Directive + "' directive");
3510
3511 // If we are inside a macro instantiation, terminate the current
3512 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003513 if (isInsideMacroInstantiation()) {
3514 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003515 return false;
3516 }
3517
3518 // Otherwise, this .endmacro is a stray entry in the file; well formed
3519 // .endmacro directives are handled during the macro definition parsing.
3520 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003521 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003522}
3523
Jim Grosbach4b905842013-09-20 23:08:21 +00003524/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003525/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003526bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003527 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003528 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003529 return TokError("expected identifier in '.purgem' directive");
3530
3531 if (getLexer().isNot(AsmToken::EndOfStatement))
3532 return TokError("unexpected token in '.purgem' directive");
3533
Jim Grosbach4b905842013-09-20 23:08:21 +00003534 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003535 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3536
Jim Grosbach4b905842013-09-20 23:08:21 +00003537 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003538 return false;
3539}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003540
Jim Grosbach4b905842013-09-20 23:08:21 +00003541/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003542/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003543bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003544 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003545
3546 // Expect a single argument: an expression that evaluates to a constant
3547 // in the inclusive range 0-30.
3548 SMLoc ExprLoc = getLexer().getLoc();
3549 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003550 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003551 return true;
3552 else if (getLexer().isNot(AsmToken::EndOfStatement))
3553 return TokError("unexpected token after expression in"
3554 " '.bundle_align_mode' directive");
3555 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3556 return Error(ExprLoc,
3557 "invalid bundle alignment size (expected between 0 and 30)");
3558
3559 Lex();
3560
3561 // Because of AlignSizePow2's verified range we can safely truncate it to
3562 // unsigned.
3563 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3564 return false;
3565}
3566
Jim Grosbach4b905842013-09-20 23:08:21 +00003567/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003568/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003569bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003570 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003571 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003572
Eli Bendersky802b6282013-01-07 21:51:08 +00003573 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3574 StringRef Option;
3575 SMLoc Loc = getTok().getLoc();
3576 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003577 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003578
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003579 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003580 return Error(Loc, kInvalidOptionError);
3581
3582 if (Option != "align_to_end")
3583 return Error(Loc, kInvalidOptionError);
3584 else if (getLexer().isNot(AsmToken::EndOfStatement))
3585 return Error(Loc,
3586 "unexpected token after '.bundle_lock' directive option");
3587 AlignToEnd = true;
3588 }
3589
Eli Benderskyf483ff92012-12-20 19:05:53 +00003590 Lex();
3591
Eli Bendersky802b6282013-01-07 21:51:08 +00003592 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003593 return false;
3594}
3595
Jim Grosbach4b905842013-09-20 23:08:21 +00003596/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003597/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003598bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003599 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003600
3601 if (getLexer().isNot(AsmToken::EndOfStatement))
3602 return TokError("unexpected token in '.bundle_unlock' directive");
3603 Lex();
3604
3605 getStreamer().EmitBundleUnlock();
3606 return false;
3607}
3608
Jim Grosbach4b905842013-09-20 23:08:21 +00003609/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003610/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003611bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003612 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003613
3614 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003615 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003616 return true;
3617
3618 int64_t FillExpr = 0;
3619 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3620 if (getLexer().isNot(AsmToken::Comma))
3621 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3622 Lex();
3623
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003624 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003625 return true;
3626
3627 if (getLexer().isNot(AsmToken::EndOfStatement))
3628 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3629 }
3630
3631 Lex();
3632
3633 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003634 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3635 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003636
3637 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003638 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003639
3640 return false;
3641}
3642
Jim Grosbach4b905842013-09-20 23:08:21 +00003643/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003644/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003645bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003646 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003647 const MCExpr *Value;
3648
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003649 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003650 return true;
3651
3652 if (getLexer().isNot(AsmToken::EndOfStatement))
3653 return TokError("unexpected token in directive");
3654
3655 if (Signed)
3656 getStreamer().EmitSLEB128Value(Value);
3657 else
3658 getStreamer().EmitULEB128Value(Value);
3659
3660 return false;
3661}
3662
Jim Grosbach4b905842013-09-20 23:08:21 +00003663/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003664/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003665bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003666 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003667 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003668 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003669 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003670
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003671 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003672 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003673
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003674 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003675
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003676 // Assembler local symbols don't make any sense here. Complain loudly.
3677 if (Sym->isTemporary())
3678 return Error(Loc, "non-local symbol required in directive");
3679
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003680 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3681 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003682
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003683 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003684 break;
3685
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003686 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003687 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003688 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003689 }
3690 }
3691
Sean Callanan686ed8d2010-01-19 20:22:31 +00003692 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003693 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003694}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003695
Jim Grosbach4b905842013-09-20 23:08:21 +00003696/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003697/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003698bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003699 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003700
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003701 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003702 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003703 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003704 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003705
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003706 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003707 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003708
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003709 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003710 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003711 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003712
3713 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003714 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003715 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003716 return true;
3717
3718 int64_t Pow2Alignment = 0;
3719 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003720 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003721 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003722 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003723 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003724 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003725
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003726 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3727 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003728 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3729
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003730 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003731 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3732 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003733 if (!isPowerOf2_64(Pow2Alignment))
3734 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3735 Pow2Alignment = Log2_64(Pow2Alignment);
3736 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003737 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003738
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003739 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003740 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003741
Sean Callanan686ed8d2010-01-19 20:22:31 +00003742 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003743
Chris Lattner28ad7542009-07-09 17:25:12 +00003744 // NOTE: a size of zero for a .comm should create a undefined symbol
3745 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003746 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003747 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003748 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003749
Eric Christopherbc818852010-05-14 01:38:54 +00003750 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003751 // may internally end up wanting an alignment in bytes.
3752 // FIXME: Diagnose overflow.
3753 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003754 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003755 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003756
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003757 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003758 return Error(IDLoc, "invalid symbol redefinition");
3759
Chris Lattner28ad7542009-07-09 17:25:12 +00003760 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003761 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003762 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003763 return false;
3764 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003765
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003766 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003767 return false;
3768}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003769
Jim Grosbach4b905842013-09-20 23:08:21 +00003770/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003771/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003772bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003773 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003774 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003775
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003776 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003777 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003778 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003779
Sean Callanan686ed8d2010-01-19 20:22:31 +00003780 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003781
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003782 if (Str.empty())
3783 Error(Loc, ".abort detected. Assembly stopping.");
3784 else
3785 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003786 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003787
3788 return false;
3789}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003790
Jim Grosbach4b905842013-09-20 23:08:21 +00003791/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003792/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003793bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003794 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003795 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003796
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003797 // Allow the strings to have escaped octal character sequence.
3798 std::string Filename;
3799 if (parseEscapedString(Filename))
3800 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003801 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003802 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003803
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003804 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003805 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003806
Chris Lattner693fbb82009-07-16 06:14:39 +00003807 // Attempt to switch the lexer to the included file before consuming the end
3808 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003809 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003810 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003811 return true;
3812 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003813
3814 return false;
3815}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003816
Jim Grosbach4b905842013-09-20 23:08:21 +00003817/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003818/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003819bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003820 if (getLexer().isNot(AsmToken::String))
3821 return TokError("expected string in '.incbin' directive");
3822
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003823 // Allow the strings to have escaped octal character sequence.
3824 std::string Filename;
3825 if (parseEscapedString(Filename))
3826 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003827 SMLoc IncbinLoc = getLexer().getLoc();
3828 Lex();
3829
3830 if (getLexer().isNot(AsmToken::EndOfStatement))
3831 return TokError("unexpected token in '.incbin' directive");
3832
Kevin Enderby109f25c2011-12-14 21:47:48 +00003833 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003834 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003835 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3836 return true;
3837 }
3838
3839 return false;
3840}
3841
Jim Grosbach4b905842013-09-20 23:08:21 +00003842/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003843/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3844bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003845 TheCondStack.push_back(TheCondState);
3846 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003847 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003848 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003849 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003850 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003851 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003852 return true;
3853
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003854 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003855 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003856
Sean Callanan686ed8d2010-01-19 20:22:31 +00003857 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003858
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003859 switch (DirKind) {
3860 default:
3861 llvm_unreachable("unsupported directive");
3862 case DK_IF:
3863 case DK_IFNE:
3864 break;
3865 case DK_IFEQ:
3866 ExprValue = ExprValue == 0;
3867 break;
3868 case DK_IFGE:
3869 ExprValue = ExprValue >= 0;
3870 break;
3871 case DK_IFGT:
3872 ExprValue = ExprValue > 0;
3873 break;
3874 case DK_IFLE:
3875 ExprValue = ExprValue <= 0;
3876 break;
3877 case DK_IFLT:
3878 ExprValue = ExprValue < 0;
3879 break;
3880 }
3881
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003882 TheCondState.CondMet = ExprValue;
3883 TheCondState.Ignore = !TheCondState.CondMet;
3884 }
3885
3886 return false;
3887}
3888
Jim Grosbach4b905842013-09-20 23:08:21 +00003889/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003890/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003891bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003892 TheCondStack.push_back(TheCondState);
3893 TheCondState.TheCond = AsmCond::IfCond;
3894
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003895 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003896 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003897 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003898 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003899
3900 if (getLexer().isNot(AsmToken::EndOfStatement))
3901 return TokError("unexpected token in '.ifb' directive");
3902
3903 Lex();
3904
3905 TheCondState.CondMet = ExpectBlank == Str.empty();
3906 TheCondState.Ignore = !TheCondState.CondMet;
3907 }
3908
3909 return false;
3910}
3911
Jim Grosbach4b905842013-09-20 23:08:21 +00003912/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003913/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003914/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003915bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003916 TheCondStack.push_back(TheCondState);
3917 TheCondState.TheCond = AsmCond::IfCond;
3918
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003919 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003920 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003921 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003922 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003923
3924 if (getLexer().isNot(AsmToken::Comma))
3925 return TokError("unexpected token in '.ifc' directive");
3926
3927 Lex();
3928
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003929 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003930
3931 if (getLexer().isNot(AsmToken::EndOfStatement))
3932 return TokError("unexpected token in '.ifc' directive");
3933
3934 Lex();
3935
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003936 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003937 TheCondState.Ignore = !TheCondState.CondMet;
3938 }
3939
3940 return false;
3941}
3942
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003943/// parseDirectiveIfeqs
3944/// ::= .ifeqs string1, string2
3945bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3946 if (Lexer.isNot(AsmToken::String)) {
3947 TokError("expected string parameter for '.ifeqs' directive");
3948 eatToEndOfStatement();
3949 return true;
3950 }
3951
3952 StringRef String1 = getTok().getStringContents();
3953 Lex();
3954
3955 if (Lexer.isNot(AsmToken::Comma)) {
3956 TokError("expected comma after first string for '.ifeqs' directive");
3957 eatToEndOfStatement();
3958 return true;
3959 }
3960
3961 Lex();
3962
3963 if (Lexer.isNot(AsmToken::String)) {
3964 TokError("expected string parameter for '.ifeqs' directive");
3965 eatToEndOfStatement();
3966 return true;
3967 }
3968
3969 StringRef String2 = getTok().getStringContents();
3970 Lex();
3971
3972 TheCondStack.push_back(TheCondState);
3973 TheCondState.TheCond = AsmCond::IfCond;
3974 TheCondState.CondMet = String1 == String2;
3975 TheCondState.Ignore = !TheCondState.CondMet;
3976
3977 return false;
3978}
3979
Jim Grosbach4b905842013-09-20 23:08:21 +00003980/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003981/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003982bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003983 StringRef Name;
3984 TheCondStack.push_back(TheCondState);
3985 TheCondState.TheCond = AsmCond::IfCond;
3986
3987 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003988 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003989 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003990 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003991 return TokError("expected identifier after '.ifdef'");
3992
3993 Lex();
3994
3995 MCSymbol *Sym = getContext().LookupSymbol(Name);
3996
3997 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003998 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003999 else
Craig Topper353eda42014-04-24 06:44:33 +00004000 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00004001 TheCondState.Ignore = !TheCondState.CondMet;
4002 }
4003
4004 return false;
4005}
4006
Jim Grosbach4b905842013-09-20 23:08:21 +00004007/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004008/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004009bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004010 if (TheCondState.TheCond != AsmCond::IfCond &&
4011 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004012 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4013 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004014 TheCondState.TheCond = AsmCond::ElseIfCond;
4015
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004016 bool LastIgnoreState = false;
4017 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004018 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004019 if (LastIgnoreState || TheCondState.CondMet) {
4020 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004021 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004022 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004023 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004024 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004025 return true;
4026
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004027 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004028 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004029
Sean Callanan686ed8d2010-01-19 20:22:31 +00004030 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004031 TheCondState.CondMet = ExprValue;
4032 TheCondState.Ignore = !TheCondState.CondMet;
4033 }
4034
4035 return false;
4036}
4037
Jim Grosbach4b905842013-09-20 23:08:21 +00004038/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004039/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004040bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004041 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004042 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004043
Sean Callanan686ed8d2010-01-19 20:22:31 +00004044 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004045
4046 if (TheCondState.TheCond != AsmCond::IfCond &&
4047 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004048 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4049 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004050 TheCondState.TheCond = AsmCond::ElseCond;
4051 bool LastIgnoreState = false;
4052 if (!TheCondStack.empty())
4053 LastIgnoreState = TheCondStack.back().Ignore;
4054 if (LastIgnoreState || TheCondState.CondMet)
4055 TheCondState.Ignore = true;
4056 else
4057 TheCondState.Ignore = false;
4058
4059 return false;
4060}
4061
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004062/// parseDirectiveEnd
4063/// ::= .end
4064bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4065 if (getLexer().isNot(AsmToken::EndOfStatement))
4066 return TokError("unexpected token in '.end' directive");
4067
4068 Lex();
4069
4070 while (Lexer.isNot(AsmToken::Eof))
4071 Lex();
4072
4073 return false;
4074}
4075
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004076/// parseDirectiveError
4077/// ::= .err
4078/// ::= .error [string]
4079bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4080 if (!TheCondStack.empty()) {
4081 if (TheCondStack.back().Ignore) {
4082 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004083 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004084 }
4085 }
4086
4087 if (!WithMessage)
4088 return Error(L, ".err encountered");
4089
4090 StringRef Message = ".error directive invoked in source file";
4091 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4092 if (Lexer.isNot(AsmToken::String)) {
4093 TokError(".error argument must be a string");
4094 eatToEndOfStatement();
4095 return true;
4096 }
4097
4098 Message = getTok().getStringContents();
4099 Lex();
4100 }
4101
4102 Error(L, Message);
4103 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004104}
4105
Nico Weber404012b2014-07-24 16:26:06 +00004106/// parseDirectiveWarning
4107/// ::= .warning [string]
4108bool AsmParser::parseDirectiveWarning(SMLoc L) {
4109 if (!TheCondStack.empty()) {
4110 if (TheCondStack.back().Ignore) {
4111 eatToEndOfStatement();
4112 return false;
4113 }
4114 }
4115
4116 StringRef Message = ".warning directive invoked in source file";
4117 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4118 if (Lexer.isNot(AsmToken::String)) {
4119 TokError(".warning argument must be a string");
4120 eatToEndOfStatement();
4121 return true;
4122 }
4123
4124 Message = getTok().getStringContents();
4125 Lex();
4126 }
4127
4128 Warning(L, Message);
4129 return false;
4130}
4131
Jim Grosbach4b905842013-09-20 23:08:21 +00004132/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004133/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004134bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004135 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004136 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004137
Sean Callanan686ed8d2010-01-19 20:22:31 +00004138 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004139
Jim Grosbach4b905842013-09-20 23:08:21 +00004140 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004141 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4142 ".else");
4143 if (!TheCondStack.empty()) {
4144 TheCondState = TheCondStack.back();
4145 TheCondStack.pop_back();
4146 }
4147
4148 return false;
4149}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004150
Eli Bendersky17233942013-01-15 22:59:42 +00004151void AsmParser::initializeDirectiveKindMap() {
4152 DirectiveKindMap[".set"] = DK_SET;
4153 DirectiveKindMap[".equ"] = DK_EQU;
4154 DirectiveKindMap[".equiv"] = DK_EQUIV;
4155 DirectiveKindMap[".ascii"] = DK_ASCII;
4156 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4157 DirectiveKindMap[".string"] = DK_STRING;
4158 DirectiveKindMap[".byte"] = DK_BYTE;
4159 DirectiveKindMap[".short"] = DK_SHORT;
4160 DirectiveKindMap[".value"] = DK_VALUE;
4161 DirectiveKindMap[".2byte"] = DK_2BYTE;
4162 DirectiveKindMap[".long"] = DK_LONG;
4163 DirectiveKindMap[".int"] = DK_INT;
4164 DirectiveKindMap[".4byte"] = DK_4BYTE;
4165 DirectiveKindMap[".quad"] = DK_QUAD;
4166 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004167 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004168 DirectiveKindMap[".single"] = DK_SINGLE;
4169 DirectiveKindMap[".float"] = DK_FLOAT;
4170 DirectiveKindMap[".double"] = DK_DOUBLE;
4171 DirectiveKindMap[".align"] = DK_ALIGN;
4172 DirectiveKindMap[".align32"] = DK_ALIGN32;
4173 DirectiveKindMap[".balign"] = DK_BALIGN;
4174 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4175 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4176 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4177 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4178 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4179 DirectiveKindMap[".org"] = DK_ORG;
4180 DirectiveKindMap[".fill"] = DK_FILL;
4181 DirectiveKindMap[".zero"] = DK_ZERO;
4182 DirectiveKindMap[".extern"] = DK_EXTERN;
4183 DirectiveKindMap[".globl"] = DK_GLOBL;
4184 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004185 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4186 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4187 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4188 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4189 DirectiveKindMap[".reference"] = DK_REFERENCE;
4190 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4191 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4192 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4193 DirectiveKindMap[".comm"] = DK_COMM;
4194 DirectiveKindMap[".common"] = DK_COMMON;
4195 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4196 DirectiveKindMap[".abort"] = DK_ABORT;
4197 DirectiveKindMap[".include"] = DK_INCLUDE;
4198 DirectiveKindMap[".incbin"] = DK_INCBIN;
4199 DirectiveKindMap[".code16"] = DK_CODE16;
4200 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4201 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004202 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004203 DirectiveKindMap[".irp"] = DK_IRP;
4204 DirectiveKindMap[".irpc"] = DK_IRPC;
4205 DirectiveKindMap[".endr"] = DK_ENDR;
4206 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4207 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4208 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4209 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004210 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4211 DirectiveKindMap[".ifge"] = DK_IFGE;
4212 DirectiveKindMap[".ifgt"] = DK_IFGT;
4213 DirectiveKindMap[".ifle"] = DK_IFLE;
4214 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004215 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004216 DirectiveKindMap[".ifb"] = DK_IFB;
4217 DirectiveKindMap[".ifnb"] = DK_IFNB;
4218 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004219 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004220 DirectiveKindMap[".ifnc"] = DK_IFNC;
4221 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4222 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4223 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4224 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4225 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004226 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004227 DirectiveKindMap[".endif"] = DK_ENDIF;
4228 DirectiveKindMap[".skip"] = DK_SKIP;
4229 DirectiveKindMap[".space"] = DK_SPACE;
4230 DirectiveKindMap[".file"] = DK_FILE;
4231 DirectiveKindMap[".line"] = DK_LINE;
4232 DirectiveKindMap[".loc"] = DK_LOC;
4233 DirectiveKindMap[".stabs"] = DK_STABS;
4234 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4235 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4236 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4237 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4238 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4239 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4240 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4241 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4242 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4243 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4244 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4245 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4246 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4247 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4248 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4249 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4250 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4251 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4252 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4253 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4254 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004255 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004256 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4257 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4258 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004259 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004260 DirectiveKindMap[".endm"] = DK_ENDM;
4261 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4262 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004263 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004264 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004265 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004266}
4267
Jim Grosbach4b905842013-09-20 23:08:21 +00004268MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004269 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004270
Rafael Espindola34b9c512012-06-03 23:57:14 +00004271 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004272 for (;;) {
4273 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004274 if (getLexer().is(AsmToken::Eof)) {
4275 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004276 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004277 }
4278
Rafael Espindola34b9c512012-06-03 23:57:14 +00004279 if (Lexer.is(AsmToken::Identifier) &&
4280 (getTok().getIdentifier() == ".rept")) {
4281 ++NestLevel;
4282 }
4283
4284 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004285 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004286 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004287 EndToken = getTok();
4288 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004289 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4290 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004291 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004292 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004293 break;
4294 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004295 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004296 }
4297
Rafael Espindola34b9c512012-06-03 23:57:14 +00004298 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004299 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004300 }
4301
4302 const char *BodyStart = StartToken.getLoc().getPointer();
4303 const char *BodyEnd = EndToken.getLoc().getPointer();
4304 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4305
Rafael Espindola34b9c512012-06-03 23:57:14 +00004306 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004307 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004308 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004309}
4310
Jim Grosbach4b905842013-09-20 23:08:21 +00004311void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004312 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004313 OS << ".endr\n";
4314
4315 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004316 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004317
Rafael Espindola34b9c512012-06-03 23:57:14 +00004318 // Create the macro instantiation object and add to the current macro
4319 // instantiation stack.
Nico Weber155dccd12014-07-24 17:08:39 +00004320 MacroInstantiation *MI =
4321 new MacroInstantiation(DirectiveLoc, CurBuffer, getTok().getLoc(),
4322 Instantiation, TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004323 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004324
Rafael Espindola34b9c512012-06-03 23:57:14 +00004325 // Jump to the macro instantiation and prime the lexer.
4326 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004327 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004328 Lex();
4329}
4330
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004331/// parseDirectiveRept
4332/// ::= .rep | .rept count
4333bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004334 const MCExpr *CountExpr;
4335 SMLoc CountLoc = getTok().getLoc();
4336 if (parseExpression(CountExpr))
4337 return true;
4338
Rafael Espindola34b9c512012-06-03 23:57:14 +00004339 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004340 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4341 eatToEndOfStatement();
4342 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4343 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004344
4345 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004346 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004347
4348 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004349 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004350
4351 // Eat the end of statement.
4352 Lex();
4353
4354 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004355 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004356 if (!M)
4357 return true;
4358
4359 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4360 // to hold the macro body with substitutions.
4361 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004362 raw_svector_ostream OS(Buf);
4363 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004364 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004365 return true;
4366 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004367 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004368
4369 return false;
4370}
4371
Jim Grosbach4b905842013-09-20 23:08:21 +00004372/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004373/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004374bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004375 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004376
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004377 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004378 return TokError("expected identifier in '.irp' directive");
4379
Rafael Espindola768b41c2012-06-15 14:02:34 +00004380 if (Lexer.isNot(AsmToken::Comma))
4381 return TokError("expected comma in '.irp' directive");
4382
4383 Lex();
4384
Eli Bendersky38274122013-01-14 23:22:36 +00004385 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004386 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004387 return true;
4388
4389 // Eat the end of statement.
4390 Lex();
4391
4392 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004393 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004394 if (!M)
4395 return true;
4396
4397 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4398 // to hold the macro body with substitutions.
4399 SmallString<256> Buf;
4400 raw_svector_ostream OS(Buf);
4401
Eli Bendersky38274122013-01-14 23:22:36 +00004402 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004403 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004404 return true;
4405 }
4406
Jim Grosbach4b905842013-09-20 23:08:21 +00004407 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004408
4409 return false;
4410}
4411
Jim Grosbach4b905842013-09-20 23:08:21 +00004412/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004413/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004414bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004415 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004416
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004417 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004418 return TokError("expected identifier in '.irpc' directive");
4419
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004420 if (Lexer.isNot(AsmToken::Comma))
4421 return TokError("expected comma in '.irpc' directive");
4422
4423 Lex();
4424
Eli Bendersky38274122013-01-14 23:22:36 +00004425 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004426 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004427 return true;
4428
4429 if (A.size() != 1 || A.front().size() != 1)
4430 return TokError("unexpected token in '.irpc' directive");
4431
4432 // Eat the end of statement.
4433 Lex();
4434
4435 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004436 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004437 if (!M)
4438 return true;
4439
4440 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4441 // to hold the macro body with substitutions.
4442 SmallString<256> Buf;
4443 raw_svector_ostream OS(Buf);
4444
4445 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004446 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004447 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004448 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004449
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004450 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004451 return true;
4452 }
4453
Jim Grosbach4b905842013-09-20 23:08:21 +00004454 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004455
4456 return false;
4457}
4458
Jim Grosbach4b905842013-09-20 23:08:21 +00004459bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004460 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004461 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004462
4463 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004464 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004465 assert(getLexer().is(AsmToken::EndOfStatement));
4466
Jim Grosbach4b905842013-09-20 23:08:21 +00004467 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004468 return false;
4469}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004470
Jim Grosbach4b905842013-09-20 23:08:21 +00004471bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004472 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004473 const MCExpr *Value;
4474 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004475 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004476 return true;
4477 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4478 if (!MCE)
4479 return Error(ExprLoc, "unexpected expression in _emit");
4480 uint64_t IntValue = MCE->getValue();
4481 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4482 return Error(ExprLoc, "literal value out of range for directive");
4483
Chad Rosierc7f552c2013-02-12 21:33:51 +00004484 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4485 return false;
4486}
4487
Jim Grosbach4b905842013-09-20 23:08:21 +00004488bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004489 const MCExpr *Value;
4490 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004491 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004492 return true;
4493 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4494 if (!MCE)
4495 return Error(ExprLoc, "unexpected expression in align");
4496 uint64_t IntValue = MCE->getValue();
4497 if (!isPowerOf2_64(IntValue))
4498 return Error(ExprLoc, "literal value not a power of two greater then zero");
4499
Jim Grosbach4b905842013-09-20 23:08:21 +00004500 Info.AsmRewrites->push_back(
4501 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004502 return false;
4503}
4504
Chad Rosierf43fcf52013-02-13 21:27:17 +00004505// We are comparing pointers, but the pointers are relative to a single string.
4506// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004507static int rewritesSort(const AsmRewrite *AsmRewriteA,
4508 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004509 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4510 return -1;
4511 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4512 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004513
Chad Rosierfce4fab2013-04-08 17:43:47 +00004514 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4515 // rewrite to the same location. Make sure the SizeDirective rewrite is
4516 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4517 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004518 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4519 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004520 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004521
Jim Grosbach4b905842013-09-20 23:08:21 +00004522 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4523 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004524 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004525 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004526}
4527
Jim Grosbach4b905842013-09-20 23:08:21 +00004528bool AsmParser::parseMSInlineAsm(
4529 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4530 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4531 SmallVectorImpl<std::string> &Constraints,
4532 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4533 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004534 SmallVector<void *, 4> InputDecls;
4535 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004536 SmallVector<bool, 4> InputDeclsAddressOf;
4537 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004538 SmallVector<std::string, 4> InputConstraints;
4539 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004540 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004541
Benjamin Kramer1a136112013-02-15 20:37:21 +00004542 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004543
4544 // Prime the lexer.
4545 Lex();
4546
4547 // While we have input, parse each statement.
4548 unsigned InputIdx = 0;
4549 unsigned OutputIdx = 0;
4550 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004551 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004552 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004553 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004554
Chad Rosier149e8e02012-12-12 22:45:52 +00004555 if (Info.ParseError)
4556 return true;
4557
Benjamin Kramer1a136112013-02-15 20:37:21 +00004558 if (Info.Opcode == ~0U)
4559 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004560
Benjamin Kramer1a136112013-02-15 20:37:21 +00004561 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004562
Benjamin Kramer1a136112013-02-15 20:37:21 +00004563 // Build the list of clobbers, outputs and inputs.
4564 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004565 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004566
Benjamin Kramer1a136112013-02-15 20:37:21 +00004567 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004568 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004569 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004570
Benjamin Kramer1a136112013-02-15 20:37:21 +00004571 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004572 if (Operand.isReg() && !Operand.needAddressOf() &&
4573 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004574 unsigned NumDefs = Desc.getNumDefs();
4575 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004576 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4577 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004578 continue;
4579 }
4580
4581 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004582 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004583 if (SymName.empty())
4584 continue;
4585
David Blaikie960ea3f2014-06-08 16:18:35 +00004586 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004587 if (!OpDecl)
4588 continue;
4589
4590 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004591 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004592 if (isOutput) {
4593 ++InputIdx;
4594 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004595 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4596 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004597 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004598 } else {
4599 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004600 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4601 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004602 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004603 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004604 }
Reid Kleckneree088972013-12-10 18:27:32 +00004605
4606 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004607 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4608 Desc.getNumImplicitDefs());
4609 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004610 }
4611
4612 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004613 NumOutputs = OutputDecls.size();
4614 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004615
4616 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004617 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4618 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4619 ClobberRegs.end());
4620 Clobbers.assign(ClobberRegs.size(), std::string());
4621 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4622 raw_string_ostream OS(Clobbers[I]);
4623 IP->printRegName(OS, ClobberRegs[I]);
4624 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004625
4626 // Merge the various outputs and inputs. Output are expected first.
4627 if (NumOutputs || NumInputs) {
4628 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004629 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004630 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004631 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004632 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004633 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004634 }
4635 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004636 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004637 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004638 }
4639 }
4640
4641 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004642 std::string AsmStringIR;
4643 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004644 StringRef ASMString =
4645 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4646 const char *AsmStart = ASMString.begin();
4647 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004648 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004649 for (const AsmRewrite &AR : AsmStrRewrites) {
4650 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004651 if (Kind == AOK_Delete)
4652 continue;
4653
David Majnemer8114c1a2014-06-23 02:17:16 +00004654 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004655 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004656
Chad Rosier120eefd2013-03-19 17:32:17 +00004657 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004658 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004659 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004660
Chad Rosier37e755c2012-10-23 17:43:43 +00004661 // Skip the original expression.
4662 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004663 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004664 continue;
4665 }
4666
Chad Rosierff10ed12013-04-12 16:26:42 +00004667 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004668 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004669 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004670 default:
4671 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004672 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004673 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004674 break;
4675 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004676 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004677 break;
4678 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004679 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004680 break;
4681 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004682 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004683 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004684 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004685 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004686 default: break;
4687 case 8: OS << "byte ptr "; break;
4688 case 16: OS << "word ptr "; break;
4689 case 32: OS << "dword ptr "; break;
4690 case 64: OS << "qword ptr "; break;
4691 case 80: OS << "xword ptr "; break;
4692 case 128: OS << "xmmword ptr "; break;
4693 case 256: OS << "ymmword ptr "; break;
4694 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004695 break;
4696 case AOK_Emit:
4697 OS << ".byte";
4698 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004699 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004700 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004701 OS << ".align " << Val;
4702
4703 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004704 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004705 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4706 break;
4707 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004708 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004709 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004710 OS.flush();
4711 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004712 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004713 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004714 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004715 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004716
Chad Rosier8bce6642012-10-18 15:49:34 +00004717 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004718 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004719 }
4720
4721 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004722 if (AsmStart != AsmEnd)
4723 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004724
4725 AsmString = OS.str();
4726 return false;
4727}
4728
Daniel Dunbar01e36072010-07-17 02:26:10 +00004729/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004730MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4731 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004732 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004733}