blob: a1b469d6797e89fdcd6c68e96c1e131488afdc99 [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
Eric Christophera7c32732012-12-18 00:30:54 +000048MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000049
Daniel Dunbar86033402010-07-12 17:54:38 +000050namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000051/// \brief Helper types for tracking macro definitions.
52typedef std::vector<AsmToken> MCAsmMacroArgument;
53typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000054
55struct MCAsmMacroParameter {
56 StringRef Name;
57 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000058 bool Required;
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000059 bool Vararg;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000060
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +000061 MCAsmMacroParameter() : Required(false), Vararg(false) {}
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000062};
63
Eli Benderskya313ae62013-01-16 18:56:50 +000064typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
65
66struct MCAsmMacro {
67 StringRef Name;
68 StringRef Body;
69 MCAsmMacroParameters Parameters;
70
71public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000072 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000073 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000074};
75
Daniel Dunbar43235712010-07-18 18:54:11 +000076/// \brief Helper class for storing information about an active macro
77/// instantiation.
78struct MacroInstantiation {
Daniel Dunbar43235712010-07-18 18:54:11 +000079 /// The location of the instantiation.
80 SMLoc InstantiationLoc;
81
Daniel Dunbar40f1d852012-12-01 01:38:48 +000082 /// The buffer where parsing should resume upon instantiation completion.
83 int ExitBuffer;
84
Daniel Dunbar43235712010-07-18 18:54:11 +000085 /// The location where parsing should resume upon instantiation completion.
86 SMLoc ExitLoc;
87
Nico Weber155dccd12014-07-24 17:08:39 +000088 /// The depth of TheCondStack at the start of the instantiation.
89 size_t CondStackDepth;
90
Daniel Dunbar43235712010-07-18 18:54:11 +000091public:
Rafael Espindola9eef18c2014-08-27 19:49:03 +000092 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth);
Daniel Dunbar43235712010-07-18 18:54:11 +000093};
94
Eli Friedman0f4871d2012-10-22 23:58:19 +000095struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000096 /// \brief The parsed operands from the last parsed statement.
David Blaikie960ea3f2014-06-08 16:18:35 +000097 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
Eli Friedman0f4871d2012-10-22 23:58:19 +000098
Jim Grosbach4b905842013-09-20 23:08:21 +000099 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000100 unsigned Opcode;
101
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000103 bool ParseError;
104
Eli Friedman0f4871d2012-10-22 23:58:19 +0000105 SmallVectorImpl<AsmRewrite> *AsmRewrites;
106
Craig Topper353eda42014-04-24 06:44:33 +0000107 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000108 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110};
111
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000112/// \brief The concrete assembly parser instance.
113class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000114 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
115 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000116private:
117 AsmLexer Lexer;
118 MCContext &Ctx;
119 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000120 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000122 SourceMgr::DiagHandlerTy SavedDiagHandler;
123 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000124 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000125
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000126 /// This is the current buffer index we're lexing from as managed by the
127 /// SourceMgr object.
Alp Tokera55b95b2014-07-06 10:33:31 +0000128 unsigned CurBuffer;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129
130 AsmCond TheCondState;
131 std::vector<AsmCond> TheCondStack;
132
Jim Grosbach4b905842013-09-20 23:08:21 +0000133 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000134 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000135 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000136 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000137
Jim Grosbach4b905842013-09-20 23:08:21 +0000138 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000139 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000140
Jim Grosbach4b905842013-09-20 23:08:21 +0000141 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000142 std::vector<MacroInstantiation*> ActiveMacros;
143
Jim Grosbach4b905842013-09-20 23:08:21 +0000144 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000145 std::deque<MCAsmMacro> MacroLikeBodies;
146
Daniel Dunbar828984f2010-07-18 18:38:02 +0000147 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000148 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000149
Daniel Dunbar43325c42010-09-09 22:42:56 +0000150 /// Flag tracking whether any errors have been encountered.
151 unsigned HadError : 1;
152
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000153 /// The values from the last parsed cpp hash file line comment if any.
154 StringRef CppHashFilename;
155 int64_t CppHashLineNumber;
156 SMLoc CppHashLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000157 unsigned CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000158 /// When generating dwarf for assembly source files we need to calculate the
159 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000160 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000161 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
162 SMLoc LastQueryIDLoc;
Alp Tokera55b95b2014-07-06 10:33:31 +0000163 unsigned LastQueryBuffer;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000164 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000165
Devang Patela173ee52012-01-31 18:14:05 +0000166 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
167 unsigned AssemblerDialect;
168
Jim Grosbach4b905842013-09-20 23:08:21 +0000169 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000170 bool IsDarwin;
171
Jim Grosbach4b905842013-09-20 23:08:21 +0000172 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000173 bool ParsingInlineAsm;
174
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000175public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000176 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000177 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000178 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000179
Craig Topper59be68f2014-03-08 07:14:16 +0000180 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000181
Craig Topper59be68f2014-03-08 07:14:16 +0000182 void addDirectiveHandler(StringRef Directive,
183 ExtensionDirectiveHandler Handler) override {
Eli Bendersky29b9f472013-01-16 00:50:52 +0000184 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185 }
186
187public:
188 /// @name MCAsmParser Interface
189 /// {
190
Craig Topper59be68f2014-03-08 07:14:16 +0000191 SourceMgr &getSourceManager() override { return SrcMgr; }
192 MCAsmLexer &getLexer() override { return Lexer; }
193 MCContext &getContext() override { return Ctx; }
194 MCStreamer &getStreamer() override { return Out; }
195 unsigned getAssemblerDialect() override {
Devang Patela173ee52012-01-31 18:14:05 +0000196 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000197 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000198 else
199 return AssemblerDialect;
200 }
Craig Topper59be68f2014-03-08 07:14:16 +0000201 void setAssemblerDialect(unsigned i) override {
Devang Patela173ee52012-01-31 18:14:05 +0000202 AssemblerDialect = i;
203 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000204
Craig Topper59be68f2014-03-08 07:14:16 +0000205 void Note(SMLoc L, const Twine &Msg,
206 ArrayRef<SMRange> Ranges = None) override;
207 bool Warning(SMLoc L, const Twine &Msg,
208 ArrayRef<SMRange> Ranges = None) override;
209 bool Error(SMLoc L, const Twine &Msg,
210 ArrayRef<SMRange> Ranges = None) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000211
Craig Topper59be68f2014-03-08 07:14:16 +0000212 const AsmToken &Lex() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Craig Topper59be68f2014-03-08 07:14:16 +0000214 void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; }
215 bool isParsingInlineAsm() override { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000216
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000217 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000218 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000219 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000220 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000221 SmallVectorImpl<std::string> &Clobbers,
Craig Topper59be68f2014-03-08 07:14:16 +0000222 const MCInstrInfo *MII, const MCInstPrinter *IP,
223 MCAsmParserSemaCallback &SI) override;
Chad Rosier49963552012-10-13 00:26:04 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseExpression(const MCExpr *&Res);
Craig Topper59be68f2014-03-08 07:14:16 +0000226 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
227 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override;
228 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
229 bool parseAbsoluteExpression(int64_t &Res) override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000230
Jim Grosbach4b905842013-09-20 23:08:21 +0000231 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000232 /// and set \p Res to the identifier contents.
Craig Topper59be68f2014-03-08 07:14:16 +0000233 bool parseIdentifier(StringRef &Res) override;
234 void eatToEndOfStatement() override;
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000235
Craig Topper59be68f2014-03-08 07:14:16 +0000236 void checkForValidSection() override;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000237 /// }
238
239private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000240
Jim Grosbach4b905842013-09-20 23:08:21 +0000241 bool parseStatement(ParseStatementInfo &Info);
242 void eatToEndOfLine();
243 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000244
Jim Grosbach4b905842013-09-20 23:08:21 +0000245 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000246 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000247 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000248 ArrayRef<MCAsmMacroParameter> Parameters,
249 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000250 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000251
Eli Benderskya313ae62013-01-16 18:56:50 +0000252 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000254
255 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000256 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000257
258 /// \brief Lookup a previously defined macro.
259 /// \param Name Macro name.
260 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000267 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000268
269 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000270 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000271
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000272 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000273 ///
274 /// \param M The macro.
275 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000276 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
278 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000279 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000280
David Majnemer91fc4c22014-01-29 18:57:46 +0000281 /// \brief Extract AsmTokens for a macro argument.
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +0000282 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg);
Eli Benderskya313ae62013-01-16 18:56:50 +0000283
284 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000285 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000286
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void printMacroInstantiations();
288 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000289 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000290 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000291 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000292 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000293
Jim Grosbach4b905842013-09-20 23:08:21 +0000294 /// \brief Enter the specified file. This returns true on failure.
295 bool enterIncludeFile(const std::string &Filename);
296
297 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000298 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000300
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000301 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000302 /// current token is not set; clients should ensure Lex() is called
303 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000304 ///
Alp Tokera55b95b2014-07-06 10:33:31 +0000305 /// \param InBuffer If not 0, should be the known buffer id that contains the
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000306 /// location.
Alp Tokera55b95b2014-07-06 10:33:31 +0000307 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0);
Daniel Dunbar43235712010-07-18 18:54:11 +0000308
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000309 /// \brief Parse up to the end of statement and a return the contents from the
310 /// current token until the end of the statement; the current token on exit
311 /// will be either the EndOfStatement or EOF.
Craig Topper59be68f2014-03-08 07:14:16 +0000312 StringRef parseStringToEndOfStatement() override;
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000313
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000314 /// \brief Parse until the end of a statement or a comma is encountered,
315 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000316 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000317
Jim Grosbach4b905842013-09-20 23:08:21 +0000318 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000319 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000320
Jim Grosbach4b905842013-09-20 23:08:21 +0000321 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
322 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
323 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000324
Jim Grosbach4b905842013-09-20 23:08:21 +0000325 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000326
Eli Bendersky17233942013-01-15 22:59:42 +0000327 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000328 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000329 DK_NO_DIRECTIVE, // Placeholder
330 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000331 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
332 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000333 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000334 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000335 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000336 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
337 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
338 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
339 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000340 DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB,
341 DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
342 DK_ELSEIF, DK_ELSE, DK_ENDIF,
Eli Bendersky17233942013-01-15 22:59:42 +0000343 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
344 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
345 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
346 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
347 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
348 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000349 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Nico Weber155dccd12014-07-24 17:08:39 +0000350 DK_MACROS_ON, DK_MACROS_OFF,
351 DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000352 DK_SLEB128, DK_ULEB128,
Nico Weber404012b2014-07-24 16:26:06 +0000353 DK_ERR, DK_ERROR, DK_WARNING,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000354 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000355 };
356
Jim Grosbach4b905842013-09-20 23:08:21 +0000357 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000358 /// directives parsed by this class.
359 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000360
361 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000362 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
363 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000364 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000365 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
366 bool parseDirectiveFill(); // ".fill"
367 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000368 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
370 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000371 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000372 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000373
Eli Bendersky17233942013-01-15 22:59:42 +0000374 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseDirectiveFile(SMLoc DirectiveLoc);
376 bool parseDirectiveLine();
377 bool parseDirectiveLoc();
378 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000379
380 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000381 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000382 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveCFISections();
384 bool parseDirectiveCFIStartProc();
385 bool parseDirectiveCFIEndProc();
386 bool parseDirectiveCFIDefCfaOffset();
387 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
388 bool parseDirectiveCFIAdjustCfaOffset();
389 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
390 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
391 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
392 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
393 bool parseDirectiveCFIRememberState();
394 bool parseDirectiveCFIRestoreState();
395 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIEscape();
398 bool parseDirectiveCFISignalFrame();
399 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000400
401 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000402 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
Nico Weber155dccd12014-07-24 17:08:39 +0000403 bool parseDirectiveExitMacro(StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000404 bool parseDirectiveEndMacro(StringRef Directive);
405 bool parseDirectiveMacro(SMLoc DirectiveLoc);
406 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000407
Eli Benderskyf483ff92012-12-20 19:05:53 +0000408 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000409 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000410 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000411 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000412 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414
Eli Bendersky17233942013-01-15 22:59:42 +0000415 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000417
418 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000420
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000422 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000424
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000426
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveAbort(); // ".abort"
428 bool parseDirectiveInclude(); // ".include"
429 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +0000431 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne"
432 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000433 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000435 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +0000437 // ".ifeqs"
438 bool parseDirectiveIfeqs(SMLoc DirectiveLoc);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000439 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
441 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
442 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
443 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Craig Topper59be68f2014-03-08 07:14:16 +0000444 bool parseEscapedString(std::string &Data) override;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000445
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000447 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000448
Rafael Espindola34b9c512012-06-03 23:57:14 +0000449 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
451 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000452 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000453 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
455 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
456 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000457
Chad Rosierc7f552c2013-02-12 21:33:51 +0000458 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000459 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000460 size_t Len);
461
462 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000463 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000464
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000465 // "end"
466 bool parseDirectiveEnd(SMLoc DirectiveLoc);
467
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +0000468 // ".err" or ".error"
469 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000470
Nico Weber404012b2014-07-24 16:26:06 +0000471 // ".warning"
472 bool parseDirectiveWarning(SMLoc DirectiveLoc);
473
Eli Bendersky17233942013-01-15 22:59:42 +0000474 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000475};
Daniel Dunbar86033402010-07-12 17:54:38 +0000476}
477
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000478namespace llvm {
479
480extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000481extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000482extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000483
484}
485
Chris Lattnerc35681b2010-01-19 19:46:13 +0000486enum { DEFAULT_ADDRSPACE = 0 };
487
Jim Grosbach4b905842013-09-20 23:08:21 +0000488AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
489 const MCAsmInfo &_MAI)
490 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Alp Tokera55b95b2014-07-06 10:33:31 +0000491 PlatformParser(nullptr), CurBuffer(_SM.getMainFileID()),
492 MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0),
493 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000494 // Save the old handler.
495 SavedDiagHandler = SrcMgr.getDiagHandler();
496 SavedDiagContext = SrcMgr.getDiagContext();
497 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000498 SrcMgr.setDiagHandler(DiagHandler, this);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000499 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar86033402010-07-12 17:54:38 +0000500
Daniel Dunbarc5011082010-07-12 18:12:02 +0000501 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000502 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
503 case MCObjectFileInfo::IsCOFF:
504 PlatformParser = createCOFFAsmParser();
505 PlatformParser->Initialize(*this);
506 break;
507 case MCObjectFileInfo::IsMachO:
508 PlatformParser = createDarwinAsmParser();
509 PlatformParser->Initialize(*this);
510 IsDarwin = true;
511 break;
512 case MCObjectFileInfo::IsELF:
513 PlatformParser = createELFAsmParser();
514 PlatformParser->Initialize(*this);
515 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000516 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000517
Eli Bendersky17233942013-01-15 22:59:42 +0000518 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000519}
520
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000521AsmParser::~AsmParser() {
Saleem Abdulrasool6eae1e62014-05-21 17:53:18 +0000522 assert((HadError || ActiveMacros.empty()) &&
523 "Unexpected active macro instantiation!");
Daniel Dunbarb759a132010-07-29 01:51:55 +0000524
525 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000526 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
527 ie = MacroMap.end();
528 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000529 delete it->getValue();
530
Daniel Dunbarc5011082010-07-12 18:12:02 +0000531 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000532}
533
Jim Grosbach4b905842013-09-20 23:08:21 +0000534void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000535 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000536 for (std::vector<MacroInstantiation *>::const_reverse_iterator
537 it = ActiveMacros.rbegin(),
538 ie = ActiveMacros.rend();
539 it != ie; ++it)
540 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000541 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000542}
543
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000544void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
545 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
546 printMacroInstantiations();
547}
548
Chris Lattnera3a06812011-10-16 04:47:35 +0000549bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger29815912014-08-26 18:39:50 +0000550 if (getTargetParser().getTargetOptions().MCFatalWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000551 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000552 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
553 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000554 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000555}
556
Chris Lattnera3a06812011-10-16 04:47:35 +0000557bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000558 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000559 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
560 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000561 return true;
562}
563
Jim Grosbach4b905842013-09-20 23:08:21 +0000564bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000565 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000566 unsigned NewBuf =
567 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
568 if (!NewBuf)
Sean Callanan7a77eae2010-01-21 00:19:58 +0000569 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000570
Sean Callanan7a77eae2010-01-21 00:19:58 +0000571 CurBuffer = NewBuf;
Rafael Espindola8026bd02014-07-06 14:17:29 +0000572 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Sean Callanan7a77eae2010-01-21 00:19:58 +0000573 return false;
574}
Daniel Dunbar43235712010-07-18 18:54:11 +0000575
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000576/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000577/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000578/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000579bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000580 std::string IncludedFile;
Alp Tokera55b95b2014-07-06 10:33:31 +0000581 unsigned NewBuf =
582 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
583 if (!NewBuf)
Kevin Enderby109f25c2011-12-14 21:47:48 +0000584 return true;
585
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000586 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000587 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000588 return false;
589}
590
Alp Tokera55b95b2014-07-06 10:33:31 +0000591void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) {
592 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
Rafael Espindola8026bd02014-07-06 14:17:29 +0000593 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
594 Loc.getPointer());
Daniel Dunbar43235712010-07-18 18:54:11 +0000595}
596
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597const AsmToken &AsmParser::Lex() {
598 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000599
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600 if (tok->is(AsmToken::Eof)) {
601 // If this is the end of an included file, pop the parent file off the
602 // include stack.
603 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
604 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000605 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606 tok = &Lexer.Lex();
607 }
608 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000609
Sean Callanan7a77eae2010-01-21 00:19:58 +0000610 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000611 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000612
Sean Callanan7a77eae2010-01-21 00:19:58 +0000613 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000614}
615
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000616bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000617 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000618 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000619 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000620
Chris Lattner36e02122009-06-21 20:54:55 +0000621 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000622 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000623
624 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000625 AsmCond StartingCondState = TheCondState;
626
Kevin Enderby6469fc22011-11-01 22:27:22 +0000627 // If we are generating dwarf for assembly source files save the initial text
628 // section and generate a .file directive.
629 if (getContext().getGenDwarfForAssembly()) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000630 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
631 getStreamer().EmitLabel(SectionStartSym);
Oliver Stannard8b273082014-06-19 15:52:37 +0000632 auto InsertResult = getContext().addGenDwarfSection(
633 getStreamer().getCurrentSection().first);
634 assert(InsertResult.second && ".text section should not have debug info yet");
635 InsertResult.first->second.first = SectionStartSym;
David Blaikiec714ef42014-03-17 01:52:11 +0000636 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective(
637 0, StringRef(), getContext().getMainFileName()));
Kevin Enderby6469fc22011-11-01 22:27:22 +0000638 }
639
Chris Lattner73f36112009-07-02 21:53:43 +0000640 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000641 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000642 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000643 if (!parseStatement(Info))
644 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000645
Daniel Dunbar43325c42010-09-09 22:42:56 +0000646 // We had an error, validate that one was emitted and recover by skipping to
647 // the next line.
648 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000649 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000650 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000651
652 if (TheCondState.TheCond != StartingCondState.TheCond ||
653 TheCondState.Ignore != StartingCondState.Ignore)
654 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000655
656 // Check to see there are no empty DwarfFile slots.
David Blaikie8bf66c42014-04-01 07:35:52 +0000657 const auto &LineTables = getContext().getMCDwarfLineTables();
658 if (!LineTables.empty()) {
659 unsigned Index = 0;
660 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) {
661 if (File.Name.empty() && Index != 0)
662 TokError("unassigned file number: " + Twine(Index) +
663 " for .file directives");
664 ++Index;
665 }
Kevin Enderbye5930f12010-07-28 20:55:35 +0000666 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000667
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000668 // Check to see that all assembler local symbols were actually defined.
669 // Targets that don't do subsections via symbols may not want this, though,
670 // so conservatively exclude them. Only do this if we're finalizing, though,
671 // as otherwise we won't necessarilly have seen everything yet.
672 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
673 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
674 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000675 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000676 i != e; ++i) {
677 MCSymbol *Sym = i->getValue();
678 // Variable symbols may not be marked as defined, so check those
679 // explicitly. If we know it's a variable, we have a definition for
680 // the purposes of this check.
681 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
682 // FIXME: We would really like to refer back to where the symbol was
683 // first referenced for a source location. We need to add something
684 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000685 printMessage(
686 getLexer().getLoc(), SourceMgr::DK_Error,
687 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000688 }
689 }
690
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000691 // Finalize the output stream if there are no errors and if the client wants
692 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000693 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000694 Out.Finish();
695
Chris Lattner73f36112009-07-02 21:53:43 +0000696 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000697}
698
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000699void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000700 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000701 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000702 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000703 }
704}
705
Jim Grosbach4b905842013-09-20 23:08:21 +0000706/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000707void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000708 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000709 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000710
Chris Lattnere5074c42009-06-22 01:29:09 +0000711 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000712 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000713 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000714}
715
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000716StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000717 const char *Start = getTok().getLoc().getPointer();
718
Jim Grosbach4b905842013-09-20 23:08:21 +0000719 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000720 Lex();
721
722 const char *End = getTok().getLoc().getPointer();
723 return StringRef(Start, End - Start);
724}
Chris Lattner78db3622009-06-22 05:51:26 +0000725
Jim Grosbach4b905842013-09-20 23:08:21 +0000726StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000727 const char *Start = getTok().getLoc().getPointer();
728
729 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000730 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000731 Lex();
732
733 const char *End = getTok().getLoc().getPointer();
734 return StringRef(Start, End - Start);
735}
736
Jim Grosbach4b905842013-09-20 23:08:21 +0000737/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000738/// NOTE: This assumes the leading '(' has already been consumed.
739///
740/// parenexpr ::= expr)
741///
Jim Grosbach4b905842013-09-20 23:08:21 +0000742bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
743 if (parseExpression(Res))
744 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000745 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000746 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000747 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000748 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000749 return false;
750}
Chris Lattner78db3622009-06-22 05:51:26 +0000751
Jim Grosbach4b905842013-09-20 23:08:21 +0000752/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000753/// NOTE: This assumes the leading '[' has already been consumed.
754///
755/// bracketexpr ::= expr]
756///
Jim Grosbach4b905842013-09-20 23:08:21 +0000757bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
758 if (parseExpression(Res))
759 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000760 if (Lexer.isNot(AsmToken::RBrac))
761 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000762 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000763 Lex();
764 return false;
765}
766
Jim Grosbach4b905842013-09-20 23:08:21 +0000767/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000768/// primaryexpr ::= (parenexpr
769/// primaryexpr ::= symbol
770/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000771/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000772/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000773bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000774 SMLoc FirstTokenLoc = getLexer().getLoc();
775 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
776 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000777 default:
778 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000779 // If we have an error assume that we've already handled it.
780 case AsmToken::Error:
781 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000782 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000783 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000784 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000785 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000786 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000787 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000788 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000789 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000790 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000791 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000792 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000793 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000794 if (FirstTokenKind == AsmToken::Dollar) {
795 if (Lexer.getMAI().getDollarIsPC()) {
796 // This is a '$' reference, which references the current PC. Emit a
797 // temporary label to the streamer and refer to it.
798 MCSymbol *Sym = Ctx.CreateTempSymbol();
799 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000800 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
801 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000802 EndLoc = FirstTokenLoc;
803 return false;
Ted Kremenek297febe2014-03-06 22:13:17 +0000804 }
805 return Error(FirstTokenLoc, "invalid token in expression");
David Majnemer0c58bc62013-09-25 10:47:21 +0000806 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000807 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000808 // Parse symbol variant
809 std::pair<StringRef, StringRef> Split;
810 if (!MAI.useParensForSymbolVariant()) {
David Majnemer6a5b8122014-06-19 01:25:43 +0000811 if (FirstTokenKind == AsmToken::String) {
812 if (Lexer.is(AsmToken::At)) {
813 Lexer.Lex(); // eat @
814 SMLoc AtLoc = getLexer().getLoc();
815 StringRef VName;
816 if (parseIdentifier(VName))
817 return Error(AtLoc, "expected symbol variant after '@'");
818
819 Split = std::make_pair(Identifier, VName);
820 }
821 } else {
822 Split = Identifier.split('@');
823 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000824 } else if (Lexer.is(AsmToken::LParen)) {
825 Lexer.Lex(); // eat (
826 StringRef VName;
827 parseIdentifier(VName);
828 if (Lexer.isNot(AsmToken::RParen)) {
829 return Error(Lexer.getTok().getLoc(),
830 "unexpected token in variant, expected ')'");
831 }
832 Lexer.Lex(); // eat )
833 Split = std::make_pair(Identifier, VName);
834 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000835
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000836 EndLoc = SMLoc::getFromPointer(Identifier.end());
837
Daniel Dunbard20cda02009-10-16 01:34:54 +0000838 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000839 StringRef SymbolName = Identifier;
840 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000841
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000842 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000843 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000844 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000845 if (Variant != MCSymbolRefExpr::VK_Invalid) {
846 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000847 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000848 Variant = MCSymbolRefExpr::VK_None;
849 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000850 return Error(SMLoc::getFromPointer(Split.second.begin()),
851 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000852 }
853 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000854
Hans Wennborgce69d772013-10-18 20:46:28 +0000855 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
856
Daniel Dunbard20cda02009-10-16 01:34:54 +0000857 // If this is an absolute variable reference, substitute it now to preserve
858 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000859 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000860 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000861 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000862
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000863 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000864 return false;
865 }
866
867 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000868 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000869 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000870 }
David Woodhousef42a6662014-02-01 16:20:54 +0000871 case AsmToken::BigNum:
872 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000873 case AsmToken::Integer: {
874 SMLoc Loc = getTok().getLoc();
875 int64_t IntVal = getTok().getIntVal();
876 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000877 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000878 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000879 // Look for 'b' or 'f' following an Integer as a directional label
880 if (Lexer.getKind() == AsmToken::Identifier) {
881 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000882 // Lookup the symbol variant if used.
883 std::pair<StringRef, StringRef> Split = IDVal.split('@');
884 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
885 if (Split.first.size() != IDVal.size()) {
886 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +0000887 if (Variant == MCSymbolRefExpr::VK_Invalid)
Ulrich Weigandd4120982013-06-20 16:24:17 +0000888 return TokError("invalid variant '" + Split.second + "'");
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000889 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000890 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000891 if (IDVal == "f" || IDVal == "b") {
892 MCSymbol *Sym =
Rafael Espindola4269b9e2014-03-13 18:09:26 +0000893 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "b");
Ulrich Weigandd4120982013-06-20 16:24:17 +0000894 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000895 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000896 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000897 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000898 Lex(); // Eat identifier.
899 }
900 }
Chris Lattner78db3622009-06-22 05:51:26 +0000901 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000902 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000903 case AsmToken::Real: {
904 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000905 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000906 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000907 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000908 Lex(); // Eat token.
909 return false;
910 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000911 case AsmToken::Dot: {
912 // This is a '.' reference, which references the current PC. Emit a
913 // temporary label to the streamer and refer to it.
914 MCSymbol *Sym = Ctx.CreateTempSymbol();
915 Out.EmitLabel(Sym);
916 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000917 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000918 Lex(); // Eat identifier.
919 return false;
920 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000921 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000922 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000924 case AsmToken::LBrac:
925 if (!PlatformParser->HasBracketExpressions())
926 return TokError("brackets expression not supported on this target");
927 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000928 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000929 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000930 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000933 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000934 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000935 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000936 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000937 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000939 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000940 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000941 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000942 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000943 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000944 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000945 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000946 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000947 }
948}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000949
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000950bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000951 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000952 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000953}
954
Daniel Dunbar55f16672010-09-17 02:47:07 +0000955const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000956AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000958 // Ask the target implementation about this expression first.
959 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
960 if (NewE)
961 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000962 // Recurse over the given expression, rebuilding it to apply the given variant
963 // if there is exactly one symbol.
964 switch (E->getKind()) {
965 case MCExpr::Target:
966 case MCExpr::Constant:
Craig Topper353eda42014-04-24 06:44:33 +0000967 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000968
969 case MCExpr::SymbolRef: {
970 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
971
972 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000973 TokError("invalid variant on expression '" + getTok().getIdentifier() +
974 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975 return E;
976 }
977
978 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
979 }
980
981 case MCExpr::Unary: {
982 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000983 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000984 if (!Sub)
Craig Topper353eda42014-04-24 06:44:33 +0000985 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000986 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
987 }
988
989 case MCExpr::Binary: {
990 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000991 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
992 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993
994 if (!LHS && !RHS)
Craig Topper353eda42014-04-24 06:44:33 +0000995 return nullptr;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996
Jim Grosbach4b905842013-09-20 23:08:21 +0000997 if (!LHS)
998 LHS = BE->getLHS();
999 if (!RHS)
1000 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +00001001
1002 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1003 }
1004 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +00001005
Craig Toppera2886c22012-02-07 05:05:23 +00001006 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007}
1008
Jim Grosbach4b905842013-09-20 23:08:21 +00001009/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001010///
Jim Grosbachbd164242011-08-20 16:24:13 +00001011/// expr ::= expr &&,|| expr -> lowest.
1012/// expr ::= expr |,^,&,! expr
1013/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1014/// expr ::= expr <<,>> expr
1015/// expr ::= expr +,- expr
1016/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001017/// expr ::= primaryexpr
1018///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001019bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001020 // Parse the expression.
Craig Topper353eda42014-04-24 06:44:33 +00001021 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001022 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001023 return true;
1024
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 // As a special case, we support 'a op b @ modifier' by rewriting the
1026 // expression to include the modifier. This is inefficient, but in general we
1027 // expect users to use 'a@modifier op b'.
1028 if (Lexer.getKind() == AsmToken::At) {
1029 Lex();
1030
1031 if (Lexer.isNot(AsmToken::Identifier))
1032 return TokError("unexpected symbol modifier following '@'");
1033
1034 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001035 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001036 if (Variant == MCSymbolRefExpr::VK_Invalid)
1037 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1038
Jim Grosbach4b905842013-09-20 23:08:21 +00001039 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001040 if (!ModifiedRes) {
1041 return TokError("invalid modifier '" + getTok().getIdentifier() +
1042 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001043 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001044
Daniel Dunbar55f16672010-09-17 02:47:07 +00001045 Res = ModifiedRes;
1046 Lex();
1047 }
1048
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001049 // Try to constant fold it up front, if possible.
1050 int64_t Value;
1051 if (Res->EvaluateAsAbsolute(Value))
1052 Res = MCConstantExpr::Create(Value, getContext());
1053
1054 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001055}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001056
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001057bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Craig Topper353eda42014-04-24 06:44:33 +00001058 Res = nullptr;
Jim Grosbach4b905842013-09-20 23:08:21 +00001059 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001060}
1061
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001062bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001063 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001064
Daniel Dunbar75630b32009-06-30 02:10:03 +00001065 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001066 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001067 return true;
1068
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001069 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001070 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001071
1072 return false;
1073}
1074
Michael J. Spencer530ce852010-10-09 11:00:50 +00001075static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001076 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001077 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001078 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001079 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001080
Jim Grosbach4b905842013-09-20 23:08:21 +00001081 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001082 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001083 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001084 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001085 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001086 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001087 return 1;
1088
Jim Grosbach4b905842013-09-20 23:08:21 +00001089 // Low Precedence: |, &, ^
1090 //
1091 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001092 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001093 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001094 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001095 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001096 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001097 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001098 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001100 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001101
Jim Grosbach4b905842013-09-20 23:08:21 +00001102 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001103 case AsmToken::EqualEqual:
1104 Kind = MCBinaryExpr::EQ;
1105 return 3;
1106 case AsmToken::ExclaimEqual:
1107 case AsmToken::LessGreater:
1108 Kind = MCBinaryExpr::NE;
1109 return 3;
1110 case AsmToken::Less:
1111 Kind = MCBinaryExpr::LT;
1112 return 3;
1113 case AsmToken::LessEqual:
1114 Kind = MCBinaryExpr::LTE;
1115 return 3;
1116 case AsmToken::Greater:
1117 Kind = MCBinaryExpr::GT;
1118 return 3;
1119 case AsmToken::GreaterEqual:
1120 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001121 return 3;
1122
Jim Grosbach4b905842013-09-20 23:08:21 +00001123 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001124 case AsmToken::LessLess:
1125 Kind = MCBinaryExpr::Shl;
1126 return 4;
1127 case AsmToken::GreaterGreater:
1128 Kind = MCBinaryExpr::Shr;
1129 return 4;
1130
Jim Grosbach4b905842013-09-20 23:08:21 +00001131 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001132 case AsmToken::Plus:
1133 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001135 case AsmToken::Minus:
1136 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001138
Jim Grosbach4b905842013-09-20 23:08:21 +00001139 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001140 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001141 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001142 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001143 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001144 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001145 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001146 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001147 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001148 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149 }
1150}
1151
Jim Grosbach4b905842013-09-20 23:08:21 +00001152/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001153/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001154bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001155 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001156 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001157 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001158 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160 // If the next token is lower precedence than we are allowed to eat, return
1161 // successfully with what we ate already.
1162 if (TokPrec < Precedence)
1163 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001164
Sean Callanan686ed8d2010-01-19 20:22:31 +00001165 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001166
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001168 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001169 if (parsePrimaryExpr(RHS, EndLoc))
1170 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001171
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001172 // If BinOp binds less tightly with RHS than the operator after RHS, let
1173 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001174 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001175 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001176 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1177 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001178
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001179 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001180 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001181 }
1182}
1183
Chris Lattner36e02122009-06-21 20:54:55 +00001184/// ParseStatement:
1185/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001186/// ::= Label* Directive ...Operands... EndOfStatement
1187/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001188bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001189 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001190 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001191 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001192 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001193 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001194
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001195 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001196 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001197 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001198 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001199 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001200 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001201 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001202 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001203
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001204 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001205 if (Lexer.is(AsmToken::Integer)) {
1206 LocalLabelVal = getTok().getIntVal();
1207 if (LocalLabelVal < 0) {
1208 if (!TheCondState.Ignore)
1209 return TokError("unexpected token at start of statement");
1210 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001211 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001212 IDVal = getTok().getString();
1213 Lex(); // Consume the integer token to be used as an identifier token.
1214 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001215 if (!TheCondState.Ignore)
1216 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001217 }
1218 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001219 } else if (Lexer.is(AsmToken::Dot)) {
1220 // Treat '.' as a valid identifier in this context.
1221 Lex();
1222 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001223 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001224 if (!TheCondState.Ignore)
1225 return TokError("unexpected token at start of statement");
1226 IDVal = "";
1227 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001228
Chris Lattner926885c2010-04-17 18:14:27 +00001229 // Handle conditional assembly here before checking for skipping. We
1230 // have to do this so that .endif isn't skipped in a ".if 0" block for
1231 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001232 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001233 DirectiveKindMap.find(IDVal);
1234 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1235 ? DK_NO_DIRECTIVE
1236 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001237 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001238 default:
1239 break;
1240 case DK_IF:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001241 case DK_IFEQ:
1242 case DK_IFGE:
1243 case DK_IFGT:
1244 case DK_IFLE:
1245 case DK_IFLT:
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00001246 case DK_IFNE:
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00001247 return parseDirectiveIf(IDLoc, DirKind);
Jim Grosbach4b905842013-09-20 23:08:21 +00001248 case DK_IFB:
1249 return parseDirectiveIfb(IDLoc, true);
1250 case DK_IFNB:
1251 return parseDirectiveIfb(IDLoc, false);
1252 case DK_IFC:
1253 return parseDirectiveIfc(IDLoc, true);
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00001254 case DK_IFEQS:
1255 return parseDirectiveIfeqs(IDLoc);
Jim Grosbach4b905842013-09-20 23:08:21 +00001256 case DK_IFNC:
1257 return parseDirectiveIfc(IDLoc, false);
1258 case DK_IFDEF:
1259 return parseDirectiveIfdef(IDLoc, true);
1260 case DK_IFNDEF:
1261 case DK_IFNOTDEF:
1262 return parseDirectiveIfdef(IDLoc, false);
1263 case DK_ELSEIF:
1264 return parseDirectiveElseIf(IDLoc);
1265 case DK_ELSE:
1266 return parseDirectiveElse(IDLoc);
1267 case DK_ENDIF:
1268 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001269 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001270
Eli Bendersky88024712013-01-16 19:32:36 +00001271 // Ignore the statement if in the middle of inactive conditional
1272 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001273 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001274 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001275 return false;
1276 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001277
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001278 // FIXME: Recurse on local labels?
1279
1280 // See what kind of statement we have.
1281 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001282 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001283 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001284
Chris Lattner36e02122009-06-21 20:54:55 +00001285 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001286 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001287
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001288 // Diagnose attempt to use '.' as a label.
1289 if (IDVal == ".")
1290 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1291
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001292 // Diagnose attempt to use a variable as a label.
1293 //
1294 // FIXME: Diagnostics. Note the location of the definition as a label.
1295 // FIXME: This doesn't diagnose assignment to a symbol which has been
1296 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001297 MCSymbol *Sym;
1298 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001299 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001300 else
1301 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001302 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001303 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001304
Daniel Dunbare73b2672009-08-26 22:13:22 +00001305 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001306 if (!ParsingInlineAsm)
1307 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001308
Kevin Enderbye7739d42011-12-09 18:09:40 +00001309 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001310 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001311 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001312 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1313 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001314
Tim Northover1744d0a2013-10-25 12:49:50 +00001315 getTargetParser().onLabelParsed(Sym);
1316
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001317 // Consume any end of statement token, if present, to avoid spurious
1318 // AddBlankLine calls().
1319 if (Lexer.is(AsmToken::EndOfStatement)) {
1320 Lex();
1321 if (Lexer.is(AsmToken::Eof))
1322 return false;
1323 }
1324
Eli Friedman0f4871d2012-10-22 23:58:19 +00001325 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001326 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001327
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001328 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001329 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001330 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001331
Jim Grosbach4b905842013-09-20 23:08:21 +00001332 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001333
1334 default: // Normal instruction or directive.
1335 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001336 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001337
1338 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001339 if (areMacrosEnabled())
1340 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1341 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001342 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001343
Michael J. Spencer530ce852010-10-09 11:00:50 +00001344 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001345
Eli Bendersky17233942013-01-15 22:59:42 +00001346 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001347 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001348 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001349 //
Eli Bendersky17233942013-01-15 22:59:42 +00001350 // 1. The target-specific assembly parser. Some directives are target
1351 // specific or may potentially behave differently on certain targets.
1352 // 2. Asm parser extensions. For example, platform-specific parsers
1353 // (like the ELF parser) register themselves as extensions.
1354 // 3. The generic directive parser implemented by this class. These are
1355 // all the directives that behave in a target and platform independent
1356 // manner, or at least have a default behavior that's shared between
1357 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001358
Eli Bendersky17233942013-01-15 22:59:42 +00001359 // First query the target-specific parser. It will return 'true' if it
1360 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001361 if (!getTargetParser().ParseDirective(ID))
1362 return false;
1363
Alp Tokercb402912014-01-24 17:20:08 +00001364 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001365 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001366 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1367 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001368 if (Handler.first)
1369 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1370
1371 // Finally, if no one else is interested in this directive, it must be
1372 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001373 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001374 default:
1375 break;
1376 case DK_SET:
1377 case DK_EQU:
1378 return parseDirectiveSet(IDVal, true);
1379 case DK_EQUIV:
1380 return parseDirectiveSet(IDVal, false);
1381 case DK_ASCII:
1382 return parseDirectiveAscii(IDVal, false);
1383 case DK_ASCIZ:
1384 case DK_STRING:
1385 return parseDirectiveAscii(IDVal, true);
1386 case DK_BYTE:
1387 return parseDirectiveValue(1);
1388 case DK_SHORT:
1389 case DK_VALUE:
1390 case DK_2BYTE:
1391 return parseDirectiveValue(2);
1392 case DK_LONG:
1393 case DK_INT:
1394 case DK_4BYTE:
1395 return parseDirectiveValue(4);
1396 case DK_QUAD:
1397 case DK_8BYTE:
1398 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001399 case DK_OCTA:
1400 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001401 case DK_SINGLE:
1402 case DK_FLOAT:
1403 return parseDirectiveRealValue(APFloat::IEEEsingle);
1404 case DK_DOUBLE:
1405 return parseDirectiveRealValue(APFloat::IEEEdouble);
1406 case DK_ALIGN: {
1407 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1408 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1409 }
1410 case DK_ALIGN32: {
1411 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1412 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1413 }
1414 case DK_BALIGN:
1415 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1416 case DK_BALIGNW:
1417 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1418 case DK_BALIGNL:
1419 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1420 case DK_P2ALIGN:
1421 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1422 case DK_P2ALIGNW:
1423 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1424 case DK_P2ALIGNL:
1425 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1426 case DK_ORG:
1427 return parseDirectiveOrg();
1428 case DK_FILL:
1429 return parseDirectiveFill();
1430 case DK_ZERO:
1431 return parseDirectiveZero();
1432 case DK_EXTERN:
1433 eatToEndOfStatement(); // .extern is the default, ignore it.
1434 return false;
1435 case DK_GLOBL:
1436 case DK_GLOBAL:
1437 return parseDirectiveSymbolAttribute(MCSA_Global);
1438 case DK_LAZY_REFERENCE:
1439 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1440 case DK_NO_DEAD_STRIP:
1441 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1442 case DK_SYMBOL_RESOLVER:
1443 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1444 case DK_PRIVATE_EXTERN:
1445 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1446 case DK_REFERENCE:
1447 return parseDirectiveSymbolAttribute(MCSA_Reference);
1448 case DK_WEAK_DEFINITION:
1449 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1450 case DK_WEAK_REFERENCE:
1451 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1452 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1453 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1454 case DK_COMM:
1455 case DK_COMMON:
1456 return parseDirectiveComm(/*IsLocal=*/false);
1457 case DK_LCOMM:
1458 return parseDirectiveComm(/*IsLocal=*/true);
1459 case DK_ABORT:
1460 return parseDirectiveAbort();
1461 case DK_INCLUDE:
1462 return parseDirectiveInclude();
1463 case DK_INCBIN:
1464 return parseDirectiveIncbin();
1465 case DK_CODE16:
1466 case DK_CODE16GCC:
1467 return TokError(Twine(IDVal) + " not supported yet");
1468 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001469 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001470 case DK_IRP:
1471 return parseDirectiveIrp(IDLoc);
1472 case DK_IRPC:
1473 return parseDirectiveIrpc(IDLoc);
1474 case DK_ENDR:
1475 return parseDirectiveEndr(IDLoc);
1476 case DK_BUNDLE_ALIGN_MODE:
1477 return parseDirectiveBundleAlignMode();
1478 case DK_BUNDLE_LOCK:
1479 return parseDirectiveBundleLock();
1480 case DK_BUNDLE_UNLOCK:
1481 return parseDirectiveBundleUnlock();
1482 case DK_SLEB128:
1483 return parseDirectiveLEB128(true);
1484 case DK_ULEB128:
1485 return parseDirectiveLEB128(false);
1486 case DK_SPACE:
1487 case DK_SKIP:
1488 return parseDirectiveSpace(IDVal);
1489 case DK_FILE:
1490 return parseDirectiveFile(IDLoc);
1491 case DK_LINE:
1492 return parseDirectiveLine();
1493 case DK_LOC:
1494 return parseDirectiveLoc();
1495 case DK_STABS:
1496 return parseDirectiveStabs();
1497 case DK_CFI_SECTIONS:
1498 return parseDirectiveCFISections();
1499 case DK_CFI_STARTPROC:
1500 return parseDirectiveCFIStartProc();
1501 case DK_CFI_ENDPROC:
1502 return parseDirectiveCFIEndProc();
1503 case DK_CFI_DEF_CFA:
1504 return parseDirectiveCFIDefCfa(IDLoc);
1505 case DK_CFI_DEF_CFA_OFFSET:
1506 return parseDirectiveCFIDefCfaOffset();
1507 case DK_CFI_ADJUST_CFA_OFFSET:
1508 return parseDirectiveCFIAdjustCfaOffset();
1509 case DK_CFI_DEF_CFA_REGISTER:
1510 return parseDirectiveCFIDefCfaRegister(IDLoc);
1511 case DK_CFI_OFFSET:
1512 return parseDirectiveCFIOffset(IDLoc);
1513 case DK_CFI_REL_OFFSET:
1514 return parseDirectiveCFIRelOffset(IDLoc);
1515 case DK_CFI_PERSONALITY:
1516 return parseDirectiveCFIPersonalityOrLsda(true);
1517 case DK_CFI_LSDA:
1518 return parseDirectiveCFIPersonalityOrLsda(false);
1519 case DK_CFI_REMEMBER_STATE:
1520 return parseDirectiveCFIRememberState();
1521 case DK_CFI_RESTORE_STATE:
1522 return parseDirectiveCFIRestoreState();
1523 case DK_CFI_SAME_VALUE:
1524 return parseDirectiveCFISameValue(IDLoc);
1525 case DK_CFI_RESTORE:
1526 return parseDirectiveCFIRestore(IDLoc);
1527 case DK_CFI_ESCAPE:
1528 return parseDirectiveCFIEscape();
1529 case DK_CFI_SIGNAL_FRAME:
1530 return parseDirectiveCFISignalFrame();
1531 case DK_CFI_UNDEFINED:
1532 return parseDirectiveCFIUndefined(IDLoc);
1533 case DK_CFI_REGISTER:
1534 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001535 case DK_CFI_WINDOW_SAVE:
1536 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001537 case DK_MACROS_ON:
1538 case DK_MACROS_OFF:
1539 return parseDirectiveMacrosOnOff(IDVal);
1540 case DK_MACRO:
1541 return parseDirectiveMacro(IDLoc);
Nico Weber155dccd12014-07-24 17:08:39 +00001542 case DK_EXITM:
1543 return parseDirectiveExitMacro(IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001544 case DK_ENDM:
1545 case DK_ENDMACRO:
1546 return parseDirectiveEndMacro(IDVal);
1547 case DK_PURGEM:
1548 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001549 case DK_END:
1550 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001551 case DK_ERR:
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00001552 return parseDirectiveError(IDLoc, false);
1553 case DK_ERROR:
1554 return parseDirectiveError(IDLoc, true);
Nico Weber404012b2014-07-24 16:26:06 +00001555 case DK_WARNING:
1556 return parseDirectiveWarning(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001557 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001558
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001559 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001560 }
Chris Lattner36e02122009-06-21 20:54:55 +00001561
Chad Rosierc7f552c2013-02-12 21:33:51 +00001562 // __asm _emit or __asm __emit
1563 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1564 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001565 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001566
1567 // __asm align
1568 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001569 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001570
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001571 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001572
Chris Lattner7cbfa442010-05-19 23:34:33 +00001573 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001574 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001575 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001576 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001577 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001578 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001579
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001580 // Dump the parsed representation, if requested.
1581 if (getShowParsedOperands()) {
1582 SmallString<256> Str;
1583 raw_svector_ostream OS(Str);
1584 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001585 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001586 if (i != 0)
1587 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001588 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001589 }
1590 OS << "]";
1591
Jim Grosbach4b905842013-09-20 23:08:21 +00001592 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001593 }
1594
Oliver Stannard8b273082014-06-19 15:52:37 +00001595 // If we are generating dwarf for the current section then generate a .loc
1596 // directive for the instruction.
Kevin Enderby6469fc22011-11-01 22:27:22 +00001597 if (!HadError && getContext().getGenDwarfForAssembly() &&
Oliver Stannard8b273082014-06-19 15:52:37 +00001598 getContext().getGenDwarfSectionSyms().count(
1599 getStreamer().getCurrentSection().first)) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001600
Eli Bendersky88024712013-01-16 19:32:36 +00001601 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001602
Eli Bendersky88024712013-01-16 19:32:36 +00001603 // If we previously parsed a cpp hash file line comment then make sure the
1604 // current Dwarf File is for the CppHashFilename if not then emit the
1605 // Dwarf File table for it and adjust the line number for the .loc.
Eli Bendersky88024712013-01-16 19:32:36 +00001606 if (CppHashFilename.size() != 0) {
David Blaikiec714ef42014-03-17 01:52:11 +00001607 unsigned FileNumber = getStreamer().EmitDwarfFileDirective(
1608 0, StringRef(), CppHashFilename);
1609 getContext().setGenDwarfFileNumber(FileNumber);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001610
Jim Grosbach4b905842013-09-20 23:08:21 +00001611 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1612 // cache with the different Loc from the call above we save the last
1613 // info we queried here with SrcMgr.FindLineNumber().
1614 unsigned CppHashLocLineNo;
1615 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1616 CppHashLocLineNo = LastQueryLine;
1617 else {
1618 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1619 LastQueryLine = CppHashLocLineNo;
1620 LastQueryIDLoc = CppHashLoc;
1621 LastQueryBuffer = CppHashBuf;
1622 }
1623 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001624 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001625
Jim Grosbach4b905842013-09-20 23:08:21 +00001626 getStreamer().EmitDwarfLocDirective(
1627 getContext().getGenDwarfFileNumber(), Line, 0,
1628 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1629 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001630 }
1631
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001632 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001633 if (!HadError) {
Tim Northover26bb14e2014-08-18 11:49:42 +00001634 uint64_t ErrorInfo;
Arnaud A. de Grandmaisonc97727a2014-03-21 21:54:46 +00001635 getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1636 Info.ParsedOperands, Out,
1637 ErrorInfo, ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001638 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001639
Chris Lattnera2a9d162010-09-11 16:18:25 +00001640 // Don't skip the rest of the line, the instruction parser is responsible for
1641 // that.
1642 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001643}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001644
Jim Grosbach4b905842013-09-20 23:08:21 +00001645/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001646/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001647void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001648 if (!Lexer.is(AsmToken::EndOfStatement))
1649 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001650 // Eat EOL.
1651 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001652}
1653
Jim Grosbach4b905842013-09-20 23:08:21 +00001654/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001655/// ::= # number "filename"
1656/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001657bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001658 Lex(); // Eat the hash token.
1659
1660 if (getLexer().isNot(AsmToken::Integer)) {
1661 // Consume the line since in cases it is not a well-formed line directive,
1662 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001663 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001664 return false;
1665 }
1666
1667 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001668 Lex();
1669
1670 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001671 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001672 return false;
1673 }
1674
1675 StringRef Filename = getTok().getString();
1676 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001677 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001678
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001679 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1680 CppHashLoc = L;
1681 CppHashFilename = Filename;
1682 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001683 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001684
1685 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001686 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001687 return false;
1688}
1689
Jim Grosbach4b905842013-09-20 23:08:21 +00001690/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001691/// for the Filename and LineNo if any in the diagnostic.
1692void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001693 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001694 raw_ostream &OS = errs();
1695
1696 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1697 const SMLoc &DiagLoc = Diag.getLoc();
Alp Tokera55b95b2014-07-06 10:33:31 +00001698 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1699 unsigned CppHashBuf =
1700 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001701
Jim Grosbach4b905842013-09-20 23:08:21 +00001702 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001703 // before printing the message.
Alp Tokera55b95b2014-07-06 10:33:31 +00001704 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1705 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
1706 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001707 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1708 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709 }
1710
Eric Christophera7c32732012-12-18 00:30:54 +00001711 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001712 // manager changed or buffer changed (like in a nested include) then just
1713 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001714 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001716 if (Parser->SavedDiagHandler)
1717 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1718 else
Craig Topper353eda42014-04-24 06:44:33 +00001719 Diag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001720 return;
1721 }
1722
Eric Christophera7c32732012-12-18 00:30:54 +00001723 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001724 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1725 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001726 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001727
1728 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1729 int CppHashLocLineNo =
1730 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001731 int LineNo =
1732 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001733
Jim Grosbach4b905842013-09-20 23:08:21 +00001734 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1735 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001736 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001737
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001738 if (Parser->SavedDiagHandler)
1739 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1740 else
Craig Topper353eda42014-04-24 06:44:33 +00001741 NewDiag.print(nullptr, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001742}
1743
Rafael Espindola2c064482012-08-21 18:29:30 +00001744// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1745// difference being that that function accepts '@' as part of identifiers and
1746// we can't do that. AsmLexer.cpp should probably be changed to handle
1747// '@' as a special case when needed.
1748static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001749 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1750 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001751}
1752
Rafael Espindola34b9c512012-06-03 23:57:14 +00001753bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001754 ArrayRef<MCAsmMacroParameter> Parameters,
1755 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001756 unsigned NParameters = Parameters.size();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001757 bool HasVararg = NParameters ? Parameters.back().Vararg : false;
Benjamin Kramer513e7442014-02-20 13:36:32 +00001758 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001759 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001760
Preston Gurd05500642012-09-19 20:36:12 +00001761 // A macro without parameters is handled differently on Darwin:
1762 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001763 while (!Body.empty()) {
1764 // Scan for the next substitution.
1765 std::size_t End = Body.size(), Pos = 0;
1766 for (; Pos != End; ++Pos) {
1767 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001768 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001769 // This macro has no parameters, look for $0, $1, etc.
1770 if (Body[Pos] != '$' || Pos + 1 == End)
1771 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001772
Rafael Espindola1134ab232011-06-05 02:43:45 +00001773 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001774 if (Next == '$' || Next == 'n' ||
1775 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001776 break;
1777 } else {
1778 // This macro has parameters, look for \foo, \bar, etc.
1779 if (Body[Pos] == '\\' && Pos + 1 != End)
1780 break;
1781 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001782 }
1783
1784 // Add the prefix.
1785 OS << Body.slice(0, Pos);
1786
1787 // Check if we reached the end.
1788 if (Pos == End)
1789 break;
1790
Benjamin Kramer513e7442014-02-20 13:36:32 +00001791 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001792 switch (Body[Pos + 1]) {
1793 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 case '$':
1795 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001796 break;
1797
Jim Grosbach4b905842013-09-20 23:08:21 +00001798 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001799 case 'n':
1800 OS << A.size();
1801 break;
1802
Jim Grosbach4b905842013-09-20 23:08:21 +00001803 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001804 default: {
1805 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001806 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001807 if (Index >= A.size())
1808 break;
1809
1810 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001811 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 ie = A[Index].end();
1813 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001814 OS << it->getString();
1815 break;
1816 }
1817 }
1818 Pos += 2;
1819 } else {
1820 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001821 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 ++I;
1823
Jim Grosbach4b905842013-09-20 23:08:21 +00001824 const char *Begin = Body.data() + Pos + 1;
1825 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 unsigned Index = 0;
1827 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001828 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001829 break;
1830
Preston Gurd05500642012-09-19 20:36:12 +00001831 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001832 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1833 Pos += 3;
1834 else {
1835 OS << '\\' << Argument;
1836 Pos = I;
1837 }
Preston Gurd05500642012-09-19 20:36:12 +00001838 } else {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001839 bool VarargParameter = HasVararg && Index == (NParameters - 1);
Eli Benderskya7b905e2013-01-14 19:00:26 +00001840 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001841 ie = A[Index].end();
1842 it != ie; ++it)
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001843 // We expect no quotes around the string's contents when
1844 // parsing for varargs.
1845 if (it->getKind() != AsmToken::String || VarargParameter)
Preston Gurd05500642012-09-19 20:36:12 +00001846 OS << it->getString();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001847 else
1848 OS << it->getStringContents();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001849
Preston Gurd05500642012-09-19 20:36:12 +00001850 Pos += 1 + Argument.size();
1851 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001852 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001853 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001854 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001855 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001856
Rafael Espindola1134ab232011-06-05 02:43:45 +00001857 return false;
1858}
Daniel Dunbar43235712010-07-18 18:54:11 +00001859
Nico Weber2a8f9222014-07-24 16:29:04 +00001860MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL,
Rafael Espindola9eef18c2014-08-27 19:49:03 +00001861 size_t CondStackDepth)
Rafael Espindolaf43a94e2014-08-17 22:48:55 +00001862 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL),
Nico Weber155dccd12014-07-24 17:08:39 +00001863 CondStackDepth(CondStackDepth) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001864
Jim Grosbach4b905842013-09-20 23:08:21 +00001865static bool isOperator(AsmToken::TokenKind kind) {
1866 switch (kind) {
1867 default:
1868 return false;
1869 case AsmToken::Plus:
1870 case AsmToken::Minus:
1871 case AsmToken::Tilde:
1872 case AsmToken::Slash:
1873 case AsmToken::Star:
1874 case AsmToken::Dot:
1875 case AsmToken::Equal:
1876 case AsmToken::EqualEqual:
1877 case AsmToken::Pipe:
1878 case AsmToken::PipePipe:
1879 case AsmToken::Caret:
1880 case AsmToken::Amp:
1881 case AsmToken::AmpAmp:
1882 case AsmToken::Exclaim:
1883 case AsmToken::ExclaimEqual:
1884 case AsmToken::Percent:
1885 case AsmToken::Less:
1886 case AsmToken::LessEqual:
1887 case AsmToken::LessLess:
1888 case AsmToken::LessGreater:
1889 case AsmToken::Greater:
1890 case AsmToken::GreaterEqual:
1891 case AsmToken::GreaterGreater:
1892 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001893 }
1894}
1895
David Majnemer16252452014-01-29 00:07:39 +00001896namespace {
1897class AsmLexerSkipSpaceRAII {
1898public:
1899 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1900 Lexer.setSkipSpace(SkipSpace);
1901 }
1902
1903 ~AsmLexerSkipSpaceRAII() {
1904 Lexer.setSkipSpace(true);
1905 }
1906
1907private:
1908 AsmLexer &Lexer;
1909};
1910}
1911
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001912bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) {
1913
1914 if (Vararg) {
1915 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1916 StringRef Str = parseStringToEndOfStatement();
1917 MA.push_back(AsmToken(AsmToken::String, Str));
1918 }
1919 return false;
1920 }
1921
Rafael Espindola768b41c2012-06-15 14:02:34 +00001922 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001923 unsigned AddTokens = 0;
1924
David Majnemer16252452014-01-29 00:07:39 +00001925 // Darwin doesn't use spaces to delmit arguments.
1926 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001927
1928 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001929 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001930 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001931
David Majnemer91fc4c22014-01-29 18:57:46 +00001932 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001933 break;
Preston Gurd05500642012-09-19 20:36:12 +00001934
1935 if (Lexer.is(AsmToken::Space)) {
1936 Lex(); // Eat spaces
1937
1938 // Spaces can delimit parameters, but could also be part an expression.
1939 // If the token after a space is an operator, add the token and the next
1940 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001941 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001942 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001943 // Check to see whether the token is used as an operator,
1944 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001945 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001946 if (*NextChar == ' ')
1947 AddTokens = 2;
1948 }
1949
1950 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001951 break;
1952 }
1953 }
1954 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001955
Jim Grosbach4b905842013-09-20 23:08:21 +00001956 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 // to be able to fill in the remaining default parameter values
1958 if (Lexer.is(AsmToken::EndOfStatement))
1959 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001960
1961 // Adjust the current parentheses level.
1962 if (Lexer.is(AsmToken::LParen))
1963 ++ParenLevel;
1964 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1965 --ParenLevel;
1966
1967 // Append the token to the current argument list.
1968 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001969 if (AddTokens)
1970 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001971 Lex();
1972 }
Preston Gurd05500642012-09-19 20:36:12 +00001973
Rafael Espindola768b41c2012-06-15 14:02:34 +00001974 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001975 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001976 return false;
1977}
1978
1979// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001980bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001981 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001982 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001983 bool NamedParametersFound = false;
1984 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001985
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001986 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001987 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001988
Rafael Espindola768b41c2012-06-15 14:02:34 +00001989 // Parse two kinds of macro invocations:
1990 // - macros defined without any parameters accept an arbitrary number of them
1991 // - macros defined with parameters accept at most that many of them
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00001992 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001993 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1994 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001995 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001996 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001997
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001998 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001999 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002000 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002001 eatToEndOfStatement();
2002 return true;
2003 }
2004
2005 if (!Lexer.is(AsmToken::Equal)) {
2006 TokError("expected '=' after formal parameter identifier");
2007 eatToEndOfStatement();
2008 return true;
2009 }
2010 Lex();
2011
2012 NamedParametersFound = true;
2013 }
2014
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002015 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002016 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002017 eatToEndOfStatement();
2018 return true;
2019 }
2020
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00002021 bool Vararg = HasVararg && Parameter == (NParameters - 1);
2022 if (parseMacroArgument(FA.Value, Vararg))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002023 return true;
2024
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002025 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002026 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002027 unsigned FAI = 0;
2028 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002029 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002030 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002031
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002032 if (FAI >= NParameters) {
Oliver Stannard8b273082014-06-19 15:52:37 +00002033 assert(M && "expected macro to be defined");
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002034 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002035 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool3f44cd72014-03-17 17:13:57 +00002036 M->Name + "'");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002037 return true;
2038 }
2039 PI = FAI;
2040 }
2041
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002042 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002043 if (A.size() <= PI)
2044 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002045 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002046
2047 if (FALocs.size() <= PI)
2048 FALocs.resize(PI + 1);
2049
2050 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002051 }
Jim Grosbach206661622012-07-30 22:44:17 +00002052
Preston Gurd242ed3152012-09-19 20:29:04 +00002053 // At the end of the statement, fill in remaining arguments that have
2054 // default values. If there aren't any, then the next argument is
2055 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002056 if (Lexer.is(AsmToken::EndOfStatement)) {
2057 bool Failure = false;
2058 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2059 if (A[FAI].empty()) {
2060 if (M->Parameters[FAI].Required) {
2061 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2062 "missing value for required parameter "
2063 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2064 Failure = true;
2065 }
2066
2067 if (!M->Parameters[FAI].Value.empty())
2068 A[FAI] = M->Parameters[FAI].Value;
2069 }
2070 }
2071 return Failure;
2072 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002073
2074 if (Lexer.is(AsmToken::Comma))
2075 Lex();
2076 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002077
2078 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002079}
2080
Jim Grosbach4b905842013-09-20 23:08:21 +00002081const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2082 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Craig Topper353eda42014-04-24 06:44:33 +00002083 return (I == MacroMap.end()) ? nullptr : I->getValue();
Eli Bendersky38274122013-01-14 23:22:36 +00002084}
2085
Jim Grosbach4b905842013-09-20 23:08:21 +00002086void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002087 MacroMap[Name] = new MCAsmMacro(Macro);
2088}
2089
Jim Grosbach4b905842013-09-20 23:08:21 +00002090void AsmParser::undefineMacro(StringRef Name) {
2091 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002092 if (I != MacroMap.end()) {
2093 delete I->getValue();
2094 MacroMap.erase(I);
2095 }
2096}
2097
Jim Grosbach4b905842013-09-20 23:08:21 +00002098bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002099 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2100 // this, although we should protect against infinite loops.
2101 if (ActiveMacros.size() == 20)
2102 return TokError("macros cannot be nested more than 20 levels deep");
2103
Eli Bendersky38274122013-01-14 23:22:36 +00002104 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002105 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002106 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002107
Rafael Espindola1134ab232011-06-05 02:43:45 +00002108 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2109 // to hold the macro body with substitutions.
2110 SmallString<256> Buf;
2111 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002112 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002113
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002114 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002115 return true;
2116
Eli Bendersky38274122013-01-14 23:22:36 +00002117 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002118 // instantiation.
2119 OS << ".endmacro\n";
2120
Rafael Espindola3560ff22014-08-27 20:03:13 +00002121 std::unique_ptr<MemoryBuffer> Instantiation =
2122 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002123
Daniel Dunbar43235712010-07-18 18:54:11 +00002124 // Create the macro instantiation object and add to the current macro
2125 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00002126 MacroInstantiation *MI = new MacroInstantiation(
2127 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Daniel Dunbar43235712010-07-18 18:54:11 +00002128 ActiveMacros.push_back(MI);
2129
2130 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00002131 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00002132 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Daniel Dunbar43235712010-07-18 18:54:11 +00002133 Lex();
2134
2135 return false;
2136}
2137
Jim Grosbach4b905842013-09-20 23:08:21 +00002138void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002139 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002140 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002141 Lex();
2142
2143 // Pop the instantiation entry.
2144 delete ActiveMacros.back();
2145 ActiveMacros.pop_back();
2146}
2147
Jim Grosbach4b905842013-09-20 23:08:21 +00002148static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002149 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002150 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002151 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2152 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002153 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002154 case MCExpr::Target:
2155 case MCExpr::Constant:
2156 return false;
2157 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002158 const MCSymbol &S =
2159 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002160 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002161 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002162 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002163 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002164 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002165 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002166 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002167
2168 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002169}
2170
Jim Grosbach4b905842013-09-20 23:08:21 +00002171bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002172 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002173 // FIXME: Use better location, we should use proper tokens.
2174 SMLoc EqualLoc = Lexer.getLoc();
2175
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002176 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002177 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002178 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002179
Rafael Espindola72f5f172012-01-28 05:57:00 +00002180 // Note: we don't count b as used in "a = b". This is to allow
2181 // a = b
2182 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002183
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002184 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002185 return TokError("unexpected token in assignment");
2186
2187 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002188 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002189
Daniel Dunbar5f339242009-10-16 01:57:39 +00002190 // Validate that the LHS is allowed to be a variable (either it has not been
2191 // used as a symbol, or it is an absolute symbol).
2192 MCSymbol *Sym = getContext().LookupSymbol(Name);
2193 if (Sym) {
2194 // Diagnose assignment to a label.
2195 //
2196 // FIXME: Diagnostics. Note the location of the definition as a label.
2197 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002198 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002199 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2200 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002201 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002202 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2203 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002204 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002205 return Error(EqualLoc, "redefinition of '" + Name + "'");
2206 else if (!Sym->isVariable())
2207 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002208 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002209 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002210 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002211
2212 // Don't count these checks as uses.
2213 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002214 } else if (Name == ".") {
2215 if (Out.EmitValueToOffset(Value, 0)) {
2216 Error(EqualLoc, "expected absolute expression");
2217 eatToEndOfStatement();
2218 }
2219 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002220 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002221 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002222
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002223 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002224 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002225 if (NoDeadStrip)
2226 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2227
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002228 return false;
2229}
2230
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002231/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002232/// ::= identifier
2233/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002234bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002235 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002236 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2237 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002238 // handle this as a context dependent token, instead we detect adjacent tokens
2239 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002240 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2241 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002242
Hans Wennborgce69d772013-10-18 20:46:28 +00002243 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002244 Lex();
2245 if (Lexer.isNot(AsmToken::Identifier))
2246 return true;
2247
Hans Wennborgce69d772013-10-18 20:46:28 +00002248 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2249 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002250 return true;
2251
2252 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002253 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002254 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002255 Lex();
2256 return false;
2257 }
2258
Jim Grosbach4b905842013-09-20 23:08:21 +00002259 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002260 return true;
2261
Sean Callanan936b0d32010-01-19 21:44:56 +00002262 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002263
Sean Callanan686ed8d2010-01-19 20:22:31 +00002264 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002265
2266 return false;
2267}
2268
Jim Grosbach4b905842013-09-20 23:08:21 +00002269/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002270/// ::= .equ identifier ',' expression
2271/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002272/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002273bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002274 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002275
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002276 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002277 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002278
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002279 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002280 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002281 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002282
Jim Grosbach4b905842013-09-20 23:08:21 +00002283 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002284}
2285
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002286bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002287 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002288
2289 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002290 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002291 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2292 if (Str[i] != '\\') {
2293 Data += Str[i];
2294 continue;
2295 }
2296
2297 // Recognize escaped characters. Note that this escape semantics currently
2298 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2299 ++i;
2300 if (i == e)
2301 return TokError("unexpected backslash at end of string");
2302
2303 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002304 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002305 // Consume up to three octal characters.
2306 unsigned Value = Str[i] - '0';
2307
Jim Grosbach4b905842013-09-20 23:08:21 +00002308 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002309 ++i;
2310 Value = Value * 8 + (Str[i] - '0');
2311
Jim Grosbach4b905842013-09-20 23:08:21 +00002312 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002313 ++i;
2314 Value = Value * 8 + (Str[i] - '0');
2315 }
2316 }
2317
2318 if (Value > 255)
2319 return TokError("invalid octal escape sequence (out of range)");
2320
Jim Grosbach4b905842013-09-20 23:08:21 +00002321 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002322 continue;
2323 }
2324
2325 // Otherwise recognize individual escapes.
2326 switch (Str[i]) {
2327 default:
2328 // Just reject invalid escape sequences for now.
2329 return TokError("invalid escape sequence (unrecognized character)");
2330
2331 case 'b': Data += '\b'; break;
2332 case 'f': Data += '\f'; break;
2333 case 'n': Data += '\n'; break;
2334 case 'r': Data += '\r'; break;
2335 case 't': Data += '\t'; break;
2336 case '"': Data += '"'; break;
2337 case '\\': Data += '\\'; break;
2338 }
2339 }
2340
2341 return false;
2342}
2343
Jim Grosbach4b905842013-09-20 23:08:21 +00002344/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002345/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002346bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002347 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002348 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002349
Daniel Dunbara10e5192009-06-24 23:30:00 +00002350 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002351 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002352 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002353
Daniel Dunbaref668c12009-08-14 18:19:52 +00002354 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002355 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002356 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002357
Rafael Espindola64e1af82013-07-02 15:49:13 +00002358 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002360 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002361
Sean Callanan686ed8d2010-01-19 20:22:31 +00002362 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002363
2364 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365 break;
2366
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002367 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002368 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002369 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002370 }
2371 }
2372
Sean Callanan686ed8d2010-01-19 20:22:31 +00002373 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002374 return false;
2375}
2376
Jim Grosbach4b905842013-09-20 23:08:21 +00002377/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002378/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002379bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002382
Daniel Dunbara10e5192009-06-24 23:30:00 +00002383 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002384 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002385 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002386 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002387 return true;
2388
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002389 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002390 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2391 assert(Size <= 8 && "Invalid size");
2392 uint64_t IntValue = MCE->getValue();
2393 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2394 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002395 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002396 } else
Kevin Enderby96918bc2014-04-22 17:27:29 +00002397 getStreamer().EmitValue(Value, Size, ExprLoc);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002398
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002399 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002400 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002401
Daniel Dunbara10e5192009-06-24 23:30:00 +00002402 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002403 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002404 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002405 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002406 }
2407 }
2408
Sean Callanan686ed8d2010-01-19 20:22:31 +00002409 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410 return false;
2411}
2412
David Woodhoused6de0d92014-02-01 16:20:59 +00002413/// ParseDirectiveOctaValue
2414/// ::= .octa [ hexconstant (, hexconstant)* ]
2415bool AsmParser::parseDirectiveOctaValue() {
2416 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2417 checkForValidSection();
2418
2419 for (;;) {
2420 if (Lexer.getKind() == AsmToken::Error)
2421 return true;
2422 if (Lexer.getKind() != AsmToken::Integer &&
2423 Lexer.getKind() != AsmToken::BigNum)
2424 return TokError("unknown token in expression");
2425
2426 SMLoc ExprLoc = getLexer().getLoc();
2427 APInt IntValue = getTok().getAPIntVal();
2428 Lex();
2429
2430 uint64_t hi, lo;
2431 if (IntValue.isIntN(64)) {
2432 hi = 0;
2433 lo = IntValue.getZExtValue();
2434 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002435 // It might actually have more than 128 bits, but the top ones are zero.
2436 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002437 lo = IntValue.getLoBits(64).getZExtValue();
2438 } else
2439 return Error(ExprLoc, "literal value out of range for directive");
2440
2441 if (MAI.isLittleEndian()) {
2442 getStreamer().EmitIntValue(lo, 8);
2443 getStreamer().EmitIntValue(hi, 8);
2444 } else {
2445 getStreamer().EmitIntValue(hi, 8);
2446 getStreamer().EmitIntValue(lo, 8);
2447 }
2448
2449 if (getLexer().is(AsmToken::EndOfStatement))
2450 break;
2451
2452 // FIXME: Improve diagnostic.
2453 if (getLexer().isNot(AsmToken::Comma))
2454 return TokError("unexpected token in directive");
2455 Lex();
2456 }
2457 }
2458
2459 Lex();
2460 return false;
2461}
2462
Jim Grosbach4b905842013-09-20 23:08:21 +00002463/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002464/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002465bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002467 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002468
2469 for (;;) {
2470 // We don't truly support arithmetic on floating point expressions, so we
2471 // have to manually parse unary prefixes.
2472 bool IsNeg = false;
2473 if (getLexer().is(AsmToken::Minus)) {
2474 Lex();
2475 IsNeg = true;
2476 } else if (getLexer().is(AsmToken::Plus))
2477 Lex();
2478
Michael J. Spencer530ce852010-10-09 11:00:50 +00002479 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002480 getLexer().isNot(AsmToken::Real) &&
2481 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002482 return TokError("unexpected token in directive");
2483
2484 // Convert to an APFloat.
2485 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002486 StringRef IDVal = getTok().getString();
2487 if (getLexer().is(AsmToken::Identifier)) {
2488 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2489 Value = APFloat::getInf(Semantics);
2490 else if (!IDVal.compare_lower("nan"))
2491 Value = APFloat::getNaN(Semantics, false, ~0);
2492 else
2493 return TokError("invalid floating point literal");
2494 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002495 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002496 return TokError("invalid floating point literal");
2497 if (IsNeg)
2498 Value.changeSign();
2499
2500 // Consume the numeric token.
2501 Lex();
2502
2503 // Emit the value as an integer.
2504 APInt AsInt = Value.bitcastToAPInt();
2505 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002506 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002507
2508 if (getLexer().is(AsmToken::EndOfStatement))
2509 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002510
Daniel Dunbar2af16532010-09-24 01:59:56 +00002511 if (getLexer().isNot(AsmToken::Comma))
2512 return TokError("unexpected token in directive");
2513 Lex();
2514 }
2515 }
2516
2517 Lex();
2518 return false;
2519}
2520
Jim Grosbach4b905842013-09-20 23:08:21 +00002521/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002522/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002523bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002524 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002525
2526 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002527 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002528 return true;
2529
Rafael Espindolab91bac62010-10-05 19:42:57 +00002530 int64_t Val = 0;
2531 if (getLexer().is(AsmToken::Comma)) {
2532 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002533 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002534 return true;
2535 }
2536
Rafael Espindola922e3f42010-09-16 15:03:59 +00002537 if (getLexer().isNot(AsmToken::EndOfStatement))
2538 return TokError("unexpected token in '.zero' directive");
2539
2540 Lex();
2541
Rafael Espindola64e1af82013-07-02 15:49:13 +00002542 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002543
2544 return false;
2545}
2546
Jim Grosbach4b905842013-09-20 23:08:21 +00002547/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002548/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002549bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002550 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002551
David Majnemer522d3db2014-02-01 07:19:38 +00002552 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002553 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002554 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002555 return true;
2556
David Majnemer522d3db2014-02-01 07:19:38 +00002557 if (NumValues < 0) {
2558 Warning(RepeatLoc,
2559 "'.fill' directive with negative repeat count has no effect");
2560 NumValues = 0;
2561 }
2562
Roman Divackye33098f2013-09-24 17:44:41 +00002563 int64_t FillSize = 1;
2564 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002565
David Majnemer522d3db2014-02-01 07:19:38 +00002566 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2568 if (getLexer().isNot(AsmToken::Comma))
2569 return TokError("unexpected token in '.fill' directive");
2570 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002571
David Majnemer522d3db2014-02-01 07:19:38 +00002572 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002573 if (parseAbsoluteExpression(FillSize))
2574 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002575
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 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002582 if (parseAbsoluteExpression(FillExpr))
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 return TokError("unexpected token in '.fill' directive");
2587
2588 Lex();
2589 }
2590 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002591
David Majnemer522d3db2014-02-01 07:19:38 +00002592 if (FillSize < 0) {
2593 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2594 NumValues = 0;
2595 }
2596 if (FillSize > 8) {
2597 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2598 FillSize = 8;
2599 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002600
David Majnemer522d3db2014-02-01 07:19:38 +00002601 if (!isUInt<32>(FillExpr) && FillSize > 4)
2602 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2603
2604 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2605 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2606
2607 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2608 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
Alexey Samsonove5864c62014-08-20 22:46:38 +00002609 if (NonZeroFillSize < FillSize)
2610 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
David Majnemer522d3db2014-02-01 07:19:38 +00002611 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002612
2613 return false;
2614}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002615
Jim Grosbach4b905842013-09-20 23:08:21 +00002616/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002617/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002618bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002619 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002620
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002621 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002622 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002623 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002624 return true;
2625
2626 // Parse optional fill expression.
2627 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002628 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2629 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002630 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002631 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002632
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002633 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002634 return true;
2635
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002636 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002637 return TokError("unexpected token in '.org' directive");
2638 }
2639
Sean Callanan686ed8d2010-01-19 20:22:31 +00002640 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002641
Jim Grosbachb5912772012-01-27 00:37:08 +00002642 // Only limited forms of relocatable expressions are accepted here, it
2643 // has to be relative to the current section. The streamer will return
2644 // 'true' if the expression wasn't evaluatable.
2645 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2646 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002647
2648 return false;
2649}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002650
Jim Grosbach4b905842013-09-20 23:08:21 +00002651/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002652/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002653bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002654 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002655
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002656 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002657 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002658 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002659 return true;
2660
2661 SMLoc MaxBytesLoc;
2662 bool HasFillExpr = false;
2663 int64_t FillExpr = 0;
2664 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002665 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2666 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002667 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002668 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002669
2670 // The fill expression can be omitted while specifying a maximum number of
2671 // alignment bytes, e.g:
2672 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002673 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002674 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002675 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002676 return true;
2677 }
2678
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002679 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2680 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002681 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002682 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002684 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002685 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002686 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002687
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002688 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002689 return TokError("unexpected token in directive");
2690 }
2691 }
2692
Sean Callanan686ed8d2010-01-19 20:22:31 +00002693 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002694
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002695 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002696 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697
2698 // Compute alignment in bytes.
2699 if (IsPow2) {
2700 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002701 if (Alignment >= 32) {
2702 Error(AlignmentLoc, "invalid alignment value");
2703 Alignment = 31;
2704 }
2705
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002706 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002707 } else {
2708 // Reject alignments that aren't a power of two, for gas compatibility.
2709 if (!isPowerOf2_64(Alignment))
2710 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002711 }
2712
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002713 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002714 if (MaxBytesLoc.isValid()) {
2715 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002716 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002717 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002718 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002719 }
2720
2721 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002722 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002723 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002724 MaxBytesToFill = 0;
2725 }
2726 }
2727
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002728 // Check whether we should use optimal code alignment for this .align
2729 // directive.
Saleem Abdulrasool7f2f9f42014-03-21 05:13:23 +00002730 const MCSection *Section = getStreamer().getCurrentSection().first;
2731 assert(Section && "must have section to emit alignment");
2732 bool UseCodeAlign = Section->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002733 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2734 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002735 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002736 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002737 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002738 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2739 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002740 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002741
2742 return false;
2743}
2744
Jim Grosbach4b905842013-09-20 23:08:21 +00002745/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002746/// ::= .file [number] filename
2747/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002748bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002749 // FIXME: I'm not sure what this is.
2750 int64_t FileNumber = -1;
2751 SMLoc FileNumberLoc = getLexer().getLoc();
2752 if (getLexer().is(AsmToken::Integer)) {
2753 FileNumber = getTok().getIntVal();
2754 Lex();
2755
2756 if (FileNumber < 1)
2757 return TokError("file number less than one");
2758 }
2759
2760 if (getLexer().isNot(AsmToken::String))
2761 return TokError("unexpected token in '.file' directive");
2762
2763 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002764 // Allow the strings to have escaped octal character sequence.
2765 std::string Path = getTok().getString();
2766 if (parseEscapedString(Path))
2767 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002768 Lex();
2769
2770 StringRef Directory;
2771 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002772 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002773 if (getLexer().is(AsmToken::String)) {
2774 if (FileNumber == -1)
2775 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002776 if (parseEscapedString(FilenameData))
2777 return true;
2778 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002779 Directory = Path;
2780 Lex();
2781 } else {
2782 Filename = Path;
2783 }
2784
2785 if (getLexer().isNot(AsmToken::EndOfStatement))
2786 return TokError("unexpected token in '.file' directive");
2787
2788 if (FileNumber == -1)
2789 getStreamer().EmitFileDirective(Filename);
2790 else {
2791 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002792 Error(DirectiveLoc,
2793 "input can't have .file dwarf directives when -g is "
2794 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002795
David Blaikiec714ef42014-03-17 01:52:11 +00002796 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) ==
2797 0)
Eli Bendersky17233942013-01-15 22:59:42 +00002798 Error(FileNumberLoc, "file number already allocated");
2799 }
2800
2801 return false;
2802}
2803
Jim Grosbach4b905842013-09-20 23:08:21 +00002804/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002805/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002806bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002807 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2808 if (getLexer().isNot(AsmToken::Integer))
2809 return TokError("unexpected token in '.line' directive");
2810
2811 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002812 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002813 Lex();
2814
2815 // FIXME: Do something with the .line.
2816 }
2817
2818 if (getLexer().isNot(AsmToken::EndOfStatement))
2819 return TokError("unexpected token in '.line' directive");
2820
2821 return false;
2822}
2823
Jim Grosbach4b905842013-09-20 23:08:21 +00002824/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002825/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2826/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2827/// The first number is a file number, must have been previously assigned with
2828/// a .file directive, the second number is the line number and optionally the
2829/// third number is a column position (zero if not specified). The remaining
2830/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002831bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002832 if (getLexer().isNot(AsmToken::Integer))
2833 return TokError("unexpected token in '.loc' directive");
2834 int64_t FileNumber = getTok().getIntVal();
2835 if (FileNumber < 1)
2836 return TokError("file number less than one in '.loc' directive");
2837 if (!getContext().isValidDwarfFileNumber(FileNumber))
2838 return TokError("unassigned file number in '.loc' directive");
2839 Lex();
2840
2841 int64_t LineNumber = 0;
2842 if (getLexer().is(AsmToken::Integer)) {
2843 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002844 if (LineNumber < 0)
2845 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002846 Lex();
2847 }
2848
2849 int64_t ColumnPos = 0;
2850 if (getLexer().is(AsmToken::Integer)) {
2851 ColumnPos = getTok().getIntVal();
2852 if (ColumnPos < 0)
2853 return TokError("column position less than zero in '.loc' directive");
2854 Lex();
2855 }
2856
2857 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2858 unsigned Isa = 0;
2859 int64_t Discriminator = 0;
2860 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2861 for (;;) {
2862 if (getLexer().is(AsmToken::EndOfStatement))
2863 break;
2864
2865 StringRef Name;
2866 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002867 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002868 return TokError("unexpected token in '.loc' directive");
2869
2870 if (Name == "basic_block")
2871 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2872 else if (Name == "prologue_end")
2873 Flags |= DWARF2_FLAG_PROLOGUE_END;
2874 else if (Name == "epilogue_begin")
2875 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2876 else if (Name == "is_stmt") {
2877 Loc = getTok().getLoc();
2878 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002879 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002880 return true;
2881 // The expression must be the constant 0 or 1.
2882 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2883 int Value = MCE->getValue();
2884 if (Value == 0)
2885 Flags &= ~DWARF2_FLAG_IS_STMT;
2886 else if (Value == 1)
2887 Flags |= DWARF2_FLAG_IS_STMT;
2888 else
2889 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002890 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002891 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2892 }
Craig Topperf15655b2013-04-22 04:22:40 +00002893 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002894 Loc = getTok().getLoc();
2895 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002896 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002897 return true;
2898 // The expression must be a constant greater or equal to 0.
2899 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2900 int Value = MCE->getValue();
2901 if (Value < 0)
2902 return Error(Loc, "isa number less than zero");
2903 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002904 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002905 return Error(Loc, "isa number not a constant value");
2906 }
Craig Topperf15655b2013-04-22 04:22:40 +00002907 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002908 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002909 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002910 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002911 return Error(Loc, "unknown sub-directive in '.loc' directive");
2912 }
2913
2914 if (getLexer().is(AsmToken::EndOfStatement))
2915 break;
2916 }
2917 }
2918
2919 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2920 Isa, Discriminator, StringRef());
2921
2922 return false;
2923}
2924
Jim Grosbach4b905842013-09-20 23:08:21 +00002925/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002926/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002927bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002928 return TokError("unsupported directive '.stabs'");
2929}
2930
Jim Grosbach4b905842013-09-20 23:08:21 +00002931/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002932/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002933bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002934 StringRef Name;
2935 bool EH = false;
2936 bool Debug = false;
2937
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002938 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002939 return TokError("Expected an identifier");
2940
2941 if (Name == ".eh_frame")
2942 EH = true;
2943 else if (Name == ".debug_frame")
2944 Debug = true;
2945
2946 if (getLexer().is(AsmToken::Comma)) {
2947 Lex();
2948
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002949 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002950 return TokError("Expected an identifier");
2951
2952 if (Name == ".eh_frame")
2953 EH = true;
2954 else if (Name == ".debug_frame")
2955 Debug = true;
2956 }
2957
2958 getStreamer().EmitCFISections(EH, Debug);
2959 return false;
2960}
2961
Jim Grosbach4b905842013-09-20 23:08:21 +00002962/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002963/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002964bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002965 StringRef Simple;
2966 if (getLexer().isNot(AsmToken::EndOfStatement))
2967 if (parseIdentifier(Simple) || Simple != "simple")
2968 return TokError("unexpected token in .cfi_startproc directive");
2969
2970 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002971 return false;
2972}
2973
Jim Grosbach4b905842013-09-20 23:08:21 +00002974/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002975/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002976bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002977 getStreamer().EmitCFIEndProc();
2978 return false;
2979}
2980
Jim Grosbach4b905842013-09-20 23:08:21 +00002981/// \brief parse register name or number.
2982bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002983 SMLoc DirectiveLoc) {
2984 unsigned RegNo;
2985
2986 if (getLexer().isNot(AsmToken::Integer)) {
2987 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2988 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002989 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002990 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002991 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002992
2993 return false;
2994}
2995
Jim Grosbach4b905842013-09-20 23:08:21 +00002996/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002997/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002998bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002999 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003000 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003001 return true;
3002
3003 if (getLexer().isNot(AsmToken::Comma))
3004 return TokError("unexpected token in directive");
3005 Lex();
3006
3007 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003008 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003009 return true;
3010
3011 getStreamer().EmitCFIDefCfa(Register, Offset);
3012 return false;
3013}
3014
Jim Grosbach4b905842013-09-20 23:08:21 +00003015/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003016/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003017bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003018 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003019 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003020 return true;
3021
3022 getStreamer().EmitCFIDefCfaOffset(Offset);
3023 return false;
3024}
3025
Jim Grosbach4b905842013-09-20 23:08:21 +00003026/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003027/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00003028bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003029 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003030 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003031 return true;
3032
3033 if (getLexer().isNot(AsmToken::Comma))
3034 return TokError("unexpected token in directive");
3035 Lex();
3036
3037 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003038 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003039 return true;
3040
3041 getStreamer().EmitCFIRegister(Register1, Register2);
3042 return false;
3043}
3044
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003045/// parseDirectiveCFIWindowSave
3046/// ::= .cfi_window_save
3047bool AsmParser::parseDirectiveCFIWindowSave() {
3048 getStreamer().EmitCFIWindowSave();
3049 return false;
3050}
3051
Jim Grosbach4b905842013-09-20 23:08:21 +00003052/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003053/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003054bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003055 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003056 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003057 return true;
3058
3059 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3060 return false;
3061}
3062
Jim Grosbach4b905842013-09-20 23:08:21 +00003063/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003064/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003065bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003066 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003067 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003068 return true;
3069
3070 getStreamer().EmitCFIDefCfaRegister(Register);
3071 return false;
3072}
3073
Jim Grosbach4b905842013-09-20 23:08:21 +00003074/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003075/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003076bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003077 int64_t Register = 0;
3078 int64_t Offset = 0;
3079
Jim Grosbach4b905842013-09-20 23:08:21 +00003080 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003081 return true;
3082
3083 if (getLexer().isNot(AsmToken::Comma))
3084 return TokError("unexpected token in directive");
3085 Lex();
3086
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003087 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003088 return true;
3089
3090 getStreamer().EmitCFIOffset(Register, Offset);
3091 return false;
3092}
3093
Jim Grosbach4b905842013-09-20 23:08:21 +00003094/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003095/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003096bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003097 int64_t Register = 0;
3098
Jim Grosbach4b905842013-09-20 23:08:21 +00003099 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003100 return true;
3101
3102 if (getLexer().isNot(AsmToken::Comma))
3103 return TokError("unexpected token in directive");
3104 Lex();
3105
3106 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003107 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003108 return true;
3109
3110 getStreamer().EmitCFIRelOffset(Register, Offset);
3111 return false;
3112}
3113
3114static bool isValidEncoding(int64_t Encoding) {
3115 if (Encoding & ~0xff)
3116 return false;
3117
3118 if (Encoding == dwarf::DW_EH_PE_omit)
3119 return true;
3120
3121 const unsigned Format = Encoding & 0xf;
3122 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3123 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3124 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3125 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3126 return false;
3127
3128 const unsigned Application = Encoding & 0x70;
3129 if (Application != dwarf::DW_EH_PE_absptr &&
3130 Application != dwarf::DW_EH_PE_pcrel)
3131 return false;
3132
3133 return true;
3134}
3135
Jim Grosbach4b905842013-09-20 23:08:21 +00003136/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003137/// IsPersonality true for cfi_personality, false for cfi_lsda
3138/// ::= .cfi_personality encoding, [symbol_name]
3139/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003140bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003141 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003142 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003143 return true;
3144 if (Encoding == dwarf::DW_EH_PE_omit)
3145 return false;
3146
3147 if (!isValidEncoding(Encoding))
3148 return TokError("unsupported encoding.");
3149
3150 if (getLexer().isNot(AsmToken::Comma))
3151 return TokError("unexpected token in directive");
3152 Lex();
3153
3154 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003155 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003156 return TokError("expected identifier in directive");
3157
3158 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3159
3160 if (IsPersonality)
3161 getStreamer().EmitCFIPersonality(Sym, Encoding);
3162 else
3163 getStreamer().EmitCFILsda(Sym, Encoding);
3164 return false;
3165}
3166
Jim Grosbach4b905842013-09-20 23:08:21 +00003167/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003168/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003169bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003170 getStreamer().EmitCFIRememberState();
3171 return false;
3172}
3173
Jim Grosbach4b905842013-09-20 23:08:21 +00003174/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003175/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003176bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003177 getStreamer().EmitCFIRestoreState();
3178 return false;
3179}
3180
Jim Grosbach4b905842013-09-20 23:08:21 +00003181/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003182/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003183bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003184 int64_t Register = 0;
3185
Jim Grosbach4b905842013-09-20 23:08:21 +00003186 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003187 return true;
3188
3189 getStreamer().EmitCFISameValue(Register);
3190 return false;
3191}
3192
Jim Grosbach4b905842013-09-20 23:08:21 +00003193/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003194/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003195bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003196 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003197 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003198 return true;
3199
3200 getStreamer().EmitCFIRestore(Register);
3201 return false;
3202}
3203
Jim Grosbach4b905842013-09-20 23:08:21 +00003204/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003205/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003206bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003207 std::string Values;
3208 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003209 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003210 return true;
3211
3212 Values.push_back((uint8_t)CurrValue);
3213
3214 while (getLexer().is(AsmToken::Comma)) {
3215 Lex();
3216
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
3223 getStreamer().EmitCFIEscape(Values);
3224 return false;
3225}
3226
Jim Grosbach4b905842013-09-20 23:08:21 +00003227/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003228/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003229bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003230 if (getLexer().isNot(AsmToken::EndOfStatement))
3231 return Error(getLexer().getLoc(),
3232 "unexpected token in '.cfi_signal_frame'");
3233
3234 getStreamer().EmitCFISignalFrame();
3235 return false;
3236}
3237
Jim Grosbach4b905842013-09-20 23:08:21 +00003238/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003239/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003240bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003241 int64_t Register = 0;
3242
Jim Grosbach4b905842013-09-20 23:08:21 +00003243 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003244 return true;
3245
3246 getStreamer().EmitCFIUndefined(Register);
3247 return false;
3248}
3249
Jim Grosbach4b905842013-09-20 23:08:21 +00003250/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003251/// ::= .macros_on
3252/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003253bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003254 if (getLexer().isNot(AsmToken::EndOfStatement))
3255 return Error(getLexer().getLoc(),
3256 "unexpected token in '" + Directive + "' directive");
3257
Jim Grosbach4b905842013-09-20 23:08:21 +00003258 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003259 return false;
3260}
3261
Jim Grosbach4b905842013-09-20 23:08:21 +00003262/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003263/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003264bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003265 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003266 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003267 return TokError("expected identifier in '.macro' directive");
3268
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003269 if (getLexer().is(AsmToken::Comma))
3270 Lex();
3271
Eli Bendersky17233942013-01-15 22:59:42 +00003272 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003273 while (getLexer().isNot(AsmToken::EndOfStatement)) {
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003274
3275 if (Parameters.size() && Parameters.back().Vararg)
3276 return Error(Lexer.getLoc(),
3277 "Vararg parameter '" + Parameters.back().Name +
3278 "' should be last one in the list of parameters.");
3279
David Majnemer91fc4c22014-01-29 18:57:46 +00003280 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003281 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003282 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003283
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003284 if (Lexer.is(AsmToken::Colon)) {
3285 Lex(); // consume ':'
3286
3287 SMLoc QualLoc;
3288 StringRef Qualifier;
3289
3290 QualLoc = Lexer.getLoc();
3291 if (parseIdentifier(Qualifier))
3292 return Error(QualLoc, "missing parameter qualifier for "
3293 "'" + Parameter.Name + "' in macro '" + Name + "'");
3294
3295 if (Qualifier == "req")
3296 Parameter.Required = true;
Kevin Enderbye3c13462014-08-04 23:14:37 +00003297 else if (Qualifier == "vararg")
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003298 Parameter.Vararg = true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003299 else
3300 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3301 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3302 }
3303
David Majnemer91fc4c22014-01-29 18:57:46 +00003304 if (getLexer().is(AsmToken::Equal)) {
3305 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003306
3307 SMLoc ParamLoc;
3308
3309 ParamLoc = Lexer.getLoc();
Stepan Dyatkovskiyafc364b2014-04-23 06:56:28 +00003310 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false ))
David Majnemer91fc4c22014-01-29 18:57:46 +00003311 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003312
3313 if (Parameter.Required)
3314 Warning(ParamLoc, "pointless default value for required parameter "
3315 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003316 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003317
3318 Parameters.push_back(Parameter);
3319
3320 if (getLexer().is(AsmToken::Comma))
3321 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003322 }
3323
3324 // Eat the end of statement.
3325 Lex();
3326
3327 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003328 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003329
3330 // Lex the macro definition.
3331 for (;;) {
3332 // Check whether we have reached the end of the file.
3333 if (getLexer().is(AsmToken::Eof))
3334 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3335
3336 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003337 if (getLexer().is(AsmToken::Identifier)) {
3338 if (getTok().getIdentifier() == ".endm" ||
3339 getTok().getIdentifier() == ".endmacro") {
3340 if (MacroDepth == 0) { // Outermost macro.
3341 EndToken = getTok();
3342 Lex();
3343 if (getLexer().isNot(AsmToken::EndOfStatement))
3344 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3345 "' directive");
3346 break;
3347 } else {
3348 // Otherwise we just found the end of an inner macro.
3349 --MacroDepth;
3350 }
3351 } else if (getTok().getIdentifier() == ".macro") {
3352 // We allow nested macros. Those aren't instantiated until the outermost
3353 // macro is expanded so just ignore them for now.
3354 ++MacroDepth;
3355 }
Eli Bendersky17233942013-01-15 22:59:42 +00003356 }
3357
3358 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003359 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003360 }
3361
Jim Grosbach4b905842013-09-20 23:08:21 +00003362 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003363 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3364 }
3365
3366 const char *BodyStart = StartToken.getLoc().getPointer();
3367 const char *BodyEnd = EndToken.getLoc().getPointer();
3368 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003369 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3370 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003371 return false;
3372}
3373
Jim Grosbach4b905842013-09-20 23:08:21 +00003374/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003375///
3376/// With the support added for named parameters there may be code out there that
3377/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003378/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003379/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003380/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003381/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3382/// warning that the positional parameter found in body which have no effect.
3383/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003384/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003385/// intended or change the macro to use the named parameters. It is possible
3386/// this warning will trigger when the none of the named parameters are used
3387/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003388void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003389 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003390 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003391 // If this macro is not defined with named parameters the warning we are
3392 // checking for here doesn't apply.
3393 unsigned NParameters = Parameters.size();
3394 if (NParameters == 0)
3395 return;
3396
3397 bool NamedParametersFound = false;
3398 bool PositionalParametersFound = false;
3399
3400 // Look at the body of the macro for use of both the named parameters and what
3401 // are likely to be positional parameters. This is what expandMacro() is
3402 // doing when it finds the parameters in the body.
3403 while (!Body.empty()) {
3404 // Scan for the next possible parameter.
3405 std::size_t End = Body.size(), Pos = 0;
3406 for (; Pos != End; ++Pos) {
3407 // Check for a substitution or escape.
3408 // This macro is defined with parameters, look for \foo, \bar, etc.
3409 if (Body[Pos] == '\\' && Pos + 1 != End)
3410 break;
3411
3412 // This macro should have parameters, but look for $0, $1, ..., $n too.
3413 if (Body[Pos] != '$' || Pos + 1 == End)
3414 continue;
3415 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003416 if (Next == '$' || Next == 'n' ||
3417 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003418 break;
3419 }
3420
3421 // Check if we reached the end.
3422 if (Pos == End)
3423 break;
3424
3425 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003426 switch (Body[Pos + 1]) {
3427 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003428 case '$':
3429 break;
3430
Jim Grosbach4b905842013-09-20 23:08:21 +00003431 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003432 case 'n':
3433 PositionalParametersFound = true;
3434 break;
3435
Jim Grosbach4b905842013-09-20 23:08:21 +00003436 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003437 default: {
3438 PositionalParametersFound = true;
3439 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003440 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003441 }
3442 Pos += 2;
3443 } else {
3444 unsigned I = Pos + 1;
3445 while (isIdentifierChar(Body[I]) && I + 1 != End)
3446 ++I;
3447
Jim Grosbach4b905842013-09-20 23:08:21 +00003448 const char *Begin = Body.data() + Pos + 1;
3449 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003450 unsigned Index = 0;
3451 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003452 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003453 break;
3454
3455 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003456 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3457 Pos += 3;
3458 else {
3459 Pos = I;
3460 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003461 } else {
3462 NamedParametersFound = true;
3463 Pos += 1 + Argument.size();
3464 }
3465 }
3466 // Update the scan point.
3467 Body = Body.substr(Pos);
3468 }
3469
3470 if (!NamedParametersFound && PositionalParametersFound)
3471 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3472 "used in macro body, possible positional parameter "
3473 "found in body which will have no effect");
3474}
3475
Nico Weber155dccd12014-07-24 17:08:39 +00003476/// parseDirectiveExitMacro
3477/// ::= .exitm
3478bool AsmParser::parseDirectiveExitMacro(StringRef Directive) {
3479 if (getLexer().isNot(AsmToken::EndOfStatement))
3480 return TokError("unexpected token in '" + Directive + "' directive");
3481
3482 if (!isInsideMacroInstantiation())
3483 return TokError("unexpected '" + Directive + "' in file, "
3484 "no current macro definition");
3485
3486 // Exit all conditionals that are active in the current macro.
3487 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
3488 TheCondState = TheCondStack.back();
3489 TheCondStack.pop_back();
3490 }
3491
3492 handleMacroExit();
3493 return false;
3494}
3495
Jim Grosbach4b905842013-09-20 23:08:21 +00003496/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003497/// ::= .endm
3498/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003499bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003500 if (getLexer().isNot(AsmToken::EndOfStatement))
3501 return TokError("unexpected token in '" + Directive + "' directive");
3502
3503 // If we are inside a macro instantiation, terminate the current
3504 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003505 if (isInsideMacroInstantiation()) {
3506 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003507 return false;
3508 }
3509
3510 // Otherwise, this .endmacro is a stray entry in the file; well formed
3511 // .endmacro directives are handled during the macro definition parsing.
3512 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003513 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003514}
3515
Jim Grosbach4b905842013-09-20 23:08:21 +00003516/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003517/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003518bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003519 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003520 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003521 return TokError("expected identifier in '.purgem' directive");
3522
3523 if (getLexer().isNot(AsmToken::EndOfStatement))
3524 return TokError("unexpected token in '.purgem' directive");
3525
Jim Grosbach4b905842013-09-20 23:08:21 +00003526 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003527 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3528
Jim Grosbach4b905842013-09-20 23:08:21 +00003529 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003530 return false;
3531}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003532
Jim Grosbach4b905842013-09-20 23:08:21 +00003533/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003534/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003535bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003536 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003537
3538 // Expect a single argument: an expression that evaluates to a constant
3539 // in the inclusive range 0-30.
3540 SMLoc ExprLoc = getLexer().getLoc();
3541 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003542 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003543 return true;
3544 else if (getLexer().isNot(AsmToken::EndOfStatement))
3545 return TokError("unexpected token after expression in"
3546 " '.bundle_align_mode' directive");
3547 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3548 return Error(ExprLoc,
3549 "invalid bundle alignment size (expected between 0 and 30)");
3550
3551 Lex();
3552
3553 // Because of AlignSizePow2's verified range we can safely truncate it to
3554 // unsigned.
3555 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3556 return false;
3557}
3558
Jim Grosbach4b905842013-09-20 23:08:21 +00003559/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003560/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003561bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003562 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003563 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003564
Eli Bendersky802b6282013-01-07 21:51:08 +00003565 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3566 StringRef Option;
3567 SMLoc Loc = getTok().getLoc();
3568 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003569 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003570
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003571 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003572 return Error(Loc, kInvalidOptionError);
3573
3574 if (Option != "align_to_end")
3575 return Error(Loc, kInvalidOptionError);
3576 else if (getLexer().isNot(AsmToken::EndOfStatement))
3577 return Error(Loc,
3578 "unexpected token after '.bundle_lock' directive option");
3579 AlignToEnd = true;
3580 }
3581
Eli Benderskyf483ff92012-12-20 19:05:53 +00003582 Lex();
3583
Eli Bendersky802b6282013-01-07 21:51:08 +00003584 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003585 return false;
3586}
3587
Jim Grosbach4b905842013-09-20 23:08:21 +00003588/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003589/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003590bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003591 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003592
3593 if (getLexer().isNot(AsmToken::EndOfStatement))
3594 return TokError("unexpected token in '.bundle_unlock' directive");
3595 Lex();
3596
3597 getStreamer().EmitBundleUnlock();
3598 return false;
3599}
3600
Jim Grosbach4b905842013-09-20 23:08:21 +00003601/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003602/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003603bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003604 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003605
3606 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003607 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003608 return true;
3609
3610 int64_t FillExpr = 0;
3611 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3612 if (getLexer().isNot(AsmToken::Comma))
3613 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3614 Lex();
3615
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003616 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003617 return true;
3618
3619 if (getLexer().isNot(AsmToken::EndOfStatement))
3620 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3621 }
3622
3623 Lex();
3624
3625 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003626 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3627 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003628
3629 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003630 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003631
3632 return false;
3633}
3634
Jim Grosbach4b905842013-09-20 23:08:21 +00003635/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003636/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003637bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003638 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003639 const MCExpr *Value;
3640
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003641 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003642 return true;
3643
3644 if (getLexer().isNot(AsmToken::EndOfStatement))
3645 return TokError("unexpected token in directive");
3646
3647 if (Signed)
3648 getStreamer().EmitSLEB128Value(Value);
3649 else
3650 getStreamer().EmitULEB128Value(Value);
3651
3652 return false;
3653}
3654
Jim Grosbach4b905842013-09-20 23:08:21 +00003655/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003656/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003657bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003658 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003659 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003660 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003661 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003662
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003663 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003664 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003665
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003666 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003667
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003668 // Assembler local symbols don't make any sense here. Complain loudly.
3669 if (Sym->isTemporary())
3670 return Error(Loc, "non-local symbol required in directive");
3671
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003672 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3673 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003674
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003675 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003676 break;
3677
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003678 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003679 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003680 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003681 }
3682 }
3683
Sean Callanan686ed8d2010-01-19 20:22:31 +00003684 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003685 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003686}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003687
Jim Grosbach4b905842013-09-20 23:08:21 +00003688/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003689/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003690bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003691 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003692
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003693 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003694 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003695 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003696 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003697
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003698 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003699 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003700
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003701 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003702 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003703 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003704
3705 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003706 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003707 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003708 return true;
3709
3710 int64_t Pow2Alignment = 0;
3711 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003712 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003713 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003714 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003715 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003716 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003717
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003718 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3719 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003720 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3721
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003722 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003723 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3724 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003725 if (!isPowerOf2_64(Pow2Alignment))
3726 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3727 Pow2Alignment = Log2_64(Pow2Alignment);
3728 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003729 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003730
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003731 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003732 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003733
Sean Callanan686ed8d2010-01-19 20:22:31 +00003734 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003735
Chris Lattner28ad7542009-07-09 17:25:12 +00003736 // NOTE: a size of zero for a .comm should create a undefined symbol
3737 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003738 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003739 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003740 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003741
Eric Christopherbc818852010-05-14 01:38:54 +00003742 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003743 // may internally end up wanting an alignment in bytes.
3744 // FIXME: Diagnose overflow.
3745 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003746 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003747 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003748
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003749 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003750 return Error(IDLoc, "invalid symbol redefinition");
3751
Chris Lattner28ad7542009-07-09 17:25:12 +00003752 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003753 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003754 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003755 return false;
3756 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003757
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003758 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003759 return false;
3760}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003761
Jim Grosbach4b905842013-09-20 23:08:21 +00003762/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003763/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003764bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003765 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003766 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003767
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003768 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003769 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003770 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003771
Sean Callanan686ed8d2010-01-19 20:22:31 +00003772 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003773
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003774 if (Str.empty())
3775 Error(Loc, ".abort detected. Assembly stopping.");
3776 else
3777 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003778 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003779
3780 return false;
3781}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003782
Jim Grosbach4b905842013-09-20 23:08:21 +00003783/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003784/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003785bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003786 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003787 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003788
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003789 // Allow the strings to have escaped octal character sequence.
3790 std::string Filename;
3791 if (parseEscapedString(Filename))
3792 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003793 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003794 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003795
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003796 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003797 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003798
Chris Lattner693fbb82009-07-16 06:14:39 +00003799 // Attempt to switch the lexer to the included file before consuming the end
3800 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003801 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003802 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003803 return true;
3804 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003805
3806 return false;
3807}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003808
Jim Grosbach4b905842013-09-20 23:08:21 +00003809/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003810/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003811bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003812 if (getLexer().isNot(AsmToken::String))
3813 return TokError("expected string in '.incbin' directive");
3814
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003815 // Allow the strings to have escaped octal character sequence.
3816 std::string Filename;
3817 if (parseEscapedString(Filename))
3818 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003819 SMLoc IncbinLoc = getLexer().getLoc();
3820 Lex();
3821
3822 if (getLexer().isNot(AsmToken::EndOfStatement))
3823 return TokError("unexpected token in '.incbin' directive");
3824
Kevin Enderby109f25c2011-12-14 21:47:48 +00003825 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003826 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003827 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3828 return true;
3829 }
3830
3831 return false;
3832}
3833
Jim Grosbach4b905842013-09-20 23:08:21 +00003834/// parseDirectiveIf
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003835/// ::= .if{,eq,ge,gt,le,lt,ne} expression
3836bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003837 TheCondStack.push_back(TheCondState);
3838 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003839 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003840 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003841 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003842 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003843 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003844 return true;
3845
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003846 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003847 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003848
Sean Callanan686ed8d2010-01-19 20:22:31 +00003849 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003850
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00003851 switch (DirKind) {
3852 default:
3853 llvm_unreachable("unsupported directive");
3854 case DK_IF:
3855 case DK_IFNE:
3856 break;
3857 case DK_IFEQ:
3858 ExprValue = ExprValue == 0;
3859 break;
3860 case DK_IFGE:
3861 ExprValue = ExprValue >= 0;
3862 break;
3863 case DK_IFGT:
3864 ExprValue = ExprValue > 0;
3865 break;
3866 case DK_IFLE:
3867 ExprValue = ExprValue <= 0;
3868 break;
3869 case DK_IFLT:
3870 ExprValue = ExprValue < 0;
3871 break;
3872 }
3873
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003874 TheCondState.CondMet = ExprValue;
3875 TheCondState.Ignore = !TheCondState.CondMet;
3876 }
3877
3878 return false;
3879}
3880
Jim Grosbach4b905842013-09-20 23:08:21 +00003881/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003882/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003883bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003884 TheCondStack.push_back(TheCondState);
3885 TheCondState.TheCond = AsmCond::IfCond;
3886
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003887 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003888 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003889 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003890 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003891
3892 if (getLexer().isNot(AsmToken::EndOfStatement))
3893 return TokError("unexpected token in '.ifb' directive");
3894
3895 Lex();
3896
3897 TheCondState.CondMet = ExpectBlank == Str.empty();
3898 TheCondState.Ignore = !TheCondState.CondMet;
3899 }
3900
3901 return false;
3902}
3903
Jim Grosbach4b905842013-09-20 23:08:21 +00003904/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003905/// ::= .ifc string1, string2
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003906/// ::= .ifnc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003907bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003908 TheCondStack.push_back(TheCondState);
3909 TheCondState.TheCond = AsmCond::IfCond;
3910
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003911 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003912 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003913 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003914 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003915
3916 if (getLexer().isNot(AsmToken::Comma))
3917 return TokError("unexpected token in '.ifc' directive");
3918
3919 Lex();
3920
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003921 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003922
3923 if (getLexer().isNot(AsmToken::EndOfStatement))
3924 return TokError("unexpected token in '.ifc' directive");
3925
3926 Lex();
3927
Saleem Abdulrasool5db52982014-02-23 15:53:36 +00003928 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim());
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003929 TheCondState.Ignore = !TheCondState.CondMet;
3930 }
3931
3932 return false;
3933}
3934
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00003935/// parseDirectiveIfeqs
3936/// ::= .ifeqs string1, string2
3937bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc) {
3938 if (Lexer.isNot(AsmToken::String)) {
3939 TokError("expected string parameter for '.ifeqs' directive");
3940 eatToEndOfStatement();
3941 return true;
3942 }
3943
3944 StringRef String1 = getTok().getStringContents();
3945 Lex();
3946
3947 if (Lexer.isNot(AsmToken::Comma)) {
3948 TokError("expected comma after first string for '.ifeqs' directive");
3949 eatToEndOfStatement();
3950 return true;
3951 }
3952
3953 Lex();
3954
3955 if (Lexer.isNot(AsmToken::String)) {
3956 TokError("expected string parameter for '.ifeqs' directive");
3957 eatToEndOfStatement();
3958 return true;
3959 }
3960
3961 StringRef String2 = getTok().getStringContents();
3962 Lex();
3963
3964 TheCondStack.push_back(TheCondState);
3965 TheCondState.TheCond = AsmCond::IfCond;
3966 TheCondState.CondMet = String1 == String2;
3967 TheCondState.Ignore = !TheCondState.CondMet;
3968
3969 return false;
3970}
3971
Jim Grosbach4b905842013-09-20 23:08:21 +00003972/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003973/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003974bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003975 StringRef Name;
3976 TheCondStack.push_back(TheCondState);
3977 TheCondState.TheCond = AsmCond::IfCond;
3978
3979 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003980 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003981 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003982 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003983 return TokError("expected identifier after '.ifdef'");
3984
3985 Lex();
3986
3987 MCSymbol *Sym = getContext().LookupSymbol(Name);
3988
3989 if (expect_defined)
Craig Topper353eda42014-04-24 06:44:33 +00003990 TheCondState.CondMet = (Sym && !Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003991 else
Craig Topper353eda42014-04-24 06:44:33 +00003992 TheCondState.CondMet = (!Sym || Sym->isUndefined());
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003993 TheCondState.Ignore = !TheCondState.CondMet;
3994 }
3995
3996 return false;
3997}
3998
Jim Grosbach4b905842013-09-20 23:08:21 +00003999/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004000/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00004001bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004002 if (TheCondState.TheCond != AsmCond::IfCond &&
4003 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004004 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
4005 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004006 TheCondState.TheCond = AsmCond::ElseIfCond;
4007
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004008 bool LastIgnoreState = false;
4009 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00004010 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004011 if (LastIgnoreState || TheCondState.CondMet) {
4012 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004013 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00004014 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004015 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004016 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004017 return true;
4018
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004019 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004020 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004021
Sean Callanan686ed8d2010-01-19 20:22:31 +00004022 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004023 TheCondState.CondMet = ExprValue;
4024 TheCondState.Ignore = !TheCondState.CondMet;
4025 }
4026
4027 return false;
4028}
4029
Jim Grosbach4b905842013-09-20 23:08:21 +00004030/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004031/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00004032bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004033 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004034 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004035
Sean Callanan686ed8d2010-01-19 20:22:31 +00004036 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004037
4038 if (TheCondState.TheCond != AsmCond::IfCond &&
4039 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00004040 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
4041 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004042 TheCondState.TheCond = AsmCond::ElseCond;
4043 bool LastIgnoreState = false;
4044 if (!TheCondStack.empty())
4045 LastIgnoreState = TheCondStack.back().Ignore;
4046 if (LastIgnoreState || TheCondState.CondMet)
4047 TheCondState.Ignore = true;
4048 else
4049 TheCondState.Ignore = false;
4050
4051 return false;
4052}
4053
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004054/// parseDirectiveEnd
4055/// ::= .end
4056bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4057 if (getLexer().isNot(AsmToken::EndOfStatement))
4058 return TokError("unexpected token in '.end' directive");
4059
4060 Lex();
4061
4062 while (Lexer.isNot(AsmToken::Eof))
4063 Lex();
4064
4065 return false;
4066}
4067
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004068/// parseDirectiveError
4069/// ::= .err
4070/// ::= .error [string]
4071bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) {
4072 if (!TheCondStack.empty()) {
4073 if (TheCondStack.back().Ignore) {
4074 eatToEndOfStatement();
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004075 return false;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004076 }
4077 }
4078
4079 if (!WithMessage)
4080 return Error(L, ".err encountered");
4081
4082 StringRef Message = ".error directive invoked in source file";
4083 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4084 if (Lexer.isNot(AsmToken::String)) {
4085 TokError(".error argument must be a string");
4086 eatToEndOfStatement();
4087 return true;
4088 }
4089
4090 Message = getTok().getStringContents();
4091 Lex();
4092 }
4093
4094 Error(L, Message);
4095 return true;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004096}
4097
Nico Weber404012b2014-07-24 16:26:06 +00004098/// parseDirectiveWarning
4099/// ::= .warning [string]
4100bool AsmParser::parseDirectiveWarning(SMLoc L) {
4101 if (!TheCondStack.empty()) {
4102 if (TheCondStack.back().Ignore) {
4103 eatToEndOfStatement();
4104 return false;
4105 }
4106 }
4107
4108 StringRef Message = ".warning directive invoked in source file";
4109 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4110 if (Lexer.isNot(AsmToken::String)) {
4111 TokError(".warning argument must be a string");
4112 eatToEndOfStatement();
4113 return true;
4114 }
4115
4116 Message = getTok().getStringContents();
4117 Lex();
4118 }
4119
4120 Warning(L, Message);
4121 return false;
4122}
4123
Jim Grosbach4b905842013-09-20 23:08:21 +00004124/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004125/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00004126bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00004127 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004128 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00004129
Sean Callanan686ed8d2010-01-19 20:22:31 +00004130 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004131
Jim Grosbach4b905842013-09-20 23:08:21 +00004132 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00004133 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
4134 ".else");
4135 if (!TheCondStack.empty()) {
4136 TheCondState = TheCondStack.back();
4137 TheCondStack.pop_back();
4138 }
4139
4140 return false;
4141}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00004142
Eli Bendersky17233942013-01-15 22:59:42 +00004143void AsmParser::initializeDirectiveKindMap() {
4144 DirectiveKindMap[".set"] = DK_SET;
4145 DirectiveKindMap[".equ"] = DK_EQU;
4146 DirectiveKindMap[".equiv"] = DK_EQUIV;
4147 DirectiveKindMap[".ascii"] = DK_ASCII;
4148 DirectiveKindMap[".asciz"] = DK_ASCIZ;
4149 DirectiveKindMap[".string"] = DK_STRING;
4150 DirectiveKindMap[".byte"] = DK_BYTE;
4151 DirectiveKindMap[".short"] = DK_SHORT;
4152 DirectiveKindMap[".value"] = DK_VALUE;
4153 DirectiveKindMap[".2byte"] = DK_2BYTE;
4154 DirectiveKindMap[".long"] = DK_LONG;
4155 DirectiveKindMap[".int"] = DK_INT;
4156 DirectiveKindMap[".4byte"] = DK_4BYTE;
4157 DirectiveKindMap[".quad"] = DK_QUAD;
4158 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00004159 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00004160 DirectiveKindMap[".single"] = DK_SINGLE;
4161 DirectiveKindMap[".float"] = DK_FLOAT;
4162 DirectiveKindMap[".double"] = DK_DOUBLE;
4163 DirectiveKindMap[".align"] = DK_ALIGN;
4164 DirectiveKindMap[".align32"] = DK_ALIGN32;
4165 DirectiveKindMap[".balign"] = DK_BALIGN;
4166 DirectiveKindMap[".balignw"] = DK_BALIGNW;
4167 DirectiveKindMap[".balignl"] = DK_BALIGNL;
4168 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
4169 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
4170 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
4171 DirectiveKindMap[".org"] = DK_ORG;
4172 DirectiveKindMap[".fill"] = DK_FILL;
4173 DirectiveKindMap[".zero"] = DK_ZERO;
4174 DirectiveKindMap[".extern"] = DK_EXTERN;
4175 DirectiveKindMap[".globl"] = DK_GLOBL;
4176 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004177 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4178 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4179 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4180 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4181 DirectiveKindMap[".reference"] = DK_REFERENCE;
4182 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4183 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4184 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4185 DirectiveKindMap[".comm"] = DK_COMM;
4186 DirectiveKindMap[".common"] = DK_COMMON;
4187 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4188 DirectiveKindMap[".abort"] = DK_ABORT;
4189 DirectiveKindMap[".include"] = DK_INCLUDE;
4190 DirectiveKindMap[".incbin"] = DK_INCBIN;
4191 DirectiveKindMap[".code16"] = DK_CODE16;
4192 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4193 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004194 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004195 DirectiveKindMap[".irp"] = DK_IRP;
4196 DirectiveKindMap[".irpc"] = DK_IRPC;
4197 DirectiveKindMap[".endr"] = DK_ENDR;
4198 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4199 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4200 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4201 DirectiveKindMap[".if"] = DK_IF;
Saleem Abdulrasool763e2cb2014-06-18 20:57:28 +00004202 DirectiveKindMap[".ifeq"] = DK_IFEQ;
4203 DirectiveKindMap[".ifge"] = DK_IFGE;
4204 DirectiveKindMap[".ifgt"] = DK_IFGT;
4205 DirectiveKindMap[".ifle"] = DK_IFLE;
4206 DirectiveKindMap[".iflt"] = DK_IFLT;
Saleem Abdulrasool5852d6b2014-02-23 15:53:41 +00004207 DirectiveKindMap[".ifne"] = DK_IFNE;
Eli Bendersky17233942013-01-15 22:59:42 +00004208 DirectiveKindMap[".ifb"] = DK_IFB;
4209 DirectiveKindMap[".ifnb"] = DK_IFNB;
4210 DirectiveKindMap[".ifc"] = DK_IFC;
Saleem Abdulrasool00f53c12014-02-23 23:02:18 +00004211 DirectiveKindMap[".ifeqs"] = DK_IFEQS;
Eli Bendersky17233942013-01-15 22:59:42 +00004212 DirectiveKindMap[".ifnc"] = DK_IFNC;
4213 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4214 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4215 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4216 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4217 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004218 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004219 DirectiveKindMap[".endif"] = DK_ENDIF;
4220 DirectiveKindMap[".skip"] = DK_SKIP;
4221 DirectiveKindMap[".space"] = DK_SPACE;
4222 DirectiveKindMap[".file"] = DK_FILE;
4223 DirectiveKindMap[".line"] = DK_LINE;
4224 DirectiveKindMap[".loc"] = DK_LOC;
4225 DirectiveKindMap[".stabs"] = DK_STABS;
4226 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4227 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4228 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4229 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4230 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4231 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4232 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4233 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4234 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4235 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4236 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4237 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4238 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4239 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4240 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4241 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4242 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4243 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4244 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4245 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4246 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004247 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004248 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4249 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4250 DirectiveKindMap[".macro"] = DK_MACRO;
Nico Weber155dccd12014-07-24 17:08:39 +00004251 DirectiveKindMap[".exitm"] = DK_EXITM;
Eli Bendersky17233942013-01-15 22:59:42 +00004252 DirectiveKindMap[".endm"] = DK_ENDM;
4253 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4254 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004255 DirectiveKindMap[".err"] = DK_ERR;
Saleem Abdulrasool7ecc5492014-02-23 23:02:23 +00004256 DirectiveKindMap[".error"] = DK_ERROR;
Nico Weber404012b2014-07-24 16:26:06 +00004257 DirectiveKindMap[".warning"] = DK_WARNING;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004258}
4259
Jim Grosbach4b905842013-09-20 23:08:21 +00004260MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004261 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004262
Rafael Espindola34b9c512012-06-03 23:57:14 +00004263 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004264 for (;;) {
4265 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004266 if (getLexer().is(AsmToken::Eof)) {
4267 Error(DirectiveLoc, "no matching '.endr' in definition");
Craig Topper353eda42014-04-24 06:44:33 +00004268 return nullptr;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004269 }
4270
Rafael Espindola34b9c512012-06-03 23:57:14 +00004271 if (Lexer.is(AsmToken::Identifier) &&
4272 (getTok().getIdentifier() == ".rept")) {
4273 ++NestLevel;
4274 }
4275
4276 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004277 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004278 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004279 EndToken = getTok();
4280 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004281 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4282 TokError("unexpected token in '.endr' directive");
Craig Topper353eda42014-04-24 06:44:33 +00004283 return nullptr;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004284 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004285 break;
4286 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004287 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004288 }
4289
Rafael Espindola34b9c512012-06-03 23:57:14 +00004290 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004291 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004292 }
4293
4294 const char *BodyStart = StartToken.getLoc().getPointer();
4295 const char *BodyEnd = EndToken.getLoc().getPointer();
4296 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4297
Rafael Espindola34b9c512012-06-03 23:57:14 +00004298 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004299 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004300 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004301}
4302
Jim Grosbach4b905842013-09-20 23:08:21 +00004303void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004304 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004305 OS << ".endr\n";
4306
Rafael Espindola3560ff22014-08-27 20:03:13 +00004307 std::unique_ptr<MemoryBuffer> Instantiation =
4308 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004309
Rafael Espindola34b9c512012-06-03 23:57:14 +00004310 // Create the macro instantiation object and add to the current macro
4311 // instantiation stack.
Rafael Espindola9eef18c2014-08-27 19:49:03 +00004312 MacroInstantiation *MI = new MacroInstantiation(
4313 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004314 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004315
Rafael Espindola34b9c512012-06-03 23:57:14 +00004316 // Jump to the macro instantiation and prime the lexer.
David Blaikie1961f142014-08-21 20:44:56 +00004317 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
Rafael Espindola8026bd02014-07-06 14:17:29 +00004318 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
Rafael Espindola34b9c512012-06-03 23:57:14 +00004319 Lex();
4320}
4321
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004322/// parseDirectiveRept
4323/// ::= .rep | .rept count
4324bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004325 const MCExpr *CountExpr;
4326 SMLoc CountLoc = getTok().getLoc();
4327 if (parseExpression(CountExpr))
4328 return true;
4329
Rafael Espindola34b9c512012-06-03 23:57:14 +00004330 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004331 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4332 eatToEndOfStatement();
4333 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4334 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004335
4336 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004337 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004338
4339 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004340 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004341
4342 // Eat the end of statement.
4343 Lex();
4344
4345 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004346 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004347 if (!M)
4348 return true;
4349
4350 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4351 // to hold the macro body with substitutions.
4352 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004353 raw_svector_ostream OS(Buf);
4354 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004355 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004356 return true;
4357 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004358 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004359
4360 return false;
4361}
4362
Jim Grosbach4b905842013-09-20 23:08:21 +00004363/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004364/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004365bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004366 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004367
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004368 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004369 return TokError("expected identifier in '.irp' directive");
4370
Rafael Espindola768b41c2012-06-15 14:02:34 +00004371 if (Lexer.isNot(AsmToken::Comma))
4372 return TokError("expected comma in '.irp' directive");
4373
4374 Lex();
4375
Eli Bendersky38274122013-01-14 23:22:36 +00004376 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004377 if (parseMacroArguments(nullptr, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004378 return true;
4379
4380 // Eat the end of statement.
4381 Lex();
4382
4383 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004384 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004385 if (!M)
4386 return true;
4387
4388 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4389 // to hold the macro body with substitutions.
4390 SmallString<256> Buf;
4391 raw_svector_ostream OS(Buf);
4392
Eli Bendersky38274122013-01-14 23:22:36 +00004393 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004394 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004395 return true;
4396 }
4397
Jim Grosbach4b905842013-09-20 23:08:21 +00004398 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004399
4400 return false;
4401}
4402
Jim Grosbach4b905842013-09-20 23:08:21 +00004403/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004404/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004405bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004406 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004407
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004408 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004409 return TokError("expected identifier in '.irpc' directive");
4410
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004411 if (Lexer.isNot(AsmToken::Comma))
4412 return TokError("expected comma in '.irpc' directive");
4413
4414 Lex();
4415
Eli Bendersky38274122013-01-14 23:22:36 +00004416 MCAsmMacroArguments A;
Craig Topper353eda42014-04-24 06:44:33 +00004417 if (parseMacroArguments(nullptr, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004418 return true;
4419
4420 if (A.size() != 1 || A.front().size() != 1)
4421 return TokError("unexpected token in '.irpc' directive");
4422
4423 // Eat the end of statement.
4424 Lex();
4425
4426 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004427 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004428 if (!M)
4429 return true;
4430
4431 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4432 // to hold the macro body with substitutions.
4433 SmallString<256> Buf;
4434 raw_svector_ostream OS(Buf);
4435
4436 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004437 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004438 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004439 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004440
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004441 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004442 return true;
4443 }
4444
Jim Grosbach4b905842013-09-20 23:08:21 +00004445 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004446
4447 return false;
4448}
4449
Jim Grosbach4b905842013-09-20 23:08:21 +00004450bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004451 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004452 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004453
4454 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004455 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004456 assert(getLexer().is(AsmToken::EndOfStatement));
4457
Jim Grosbach4b905842013-09-20 23:08:21 +00004458 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004459 return false;
4460}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004461
Jim Grosbach4b905842013-09-20 23:08:21 +00004462bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004463 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004464 const MCExpr *Value;
4465 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004466 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004467 return true;
4468 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4469 if (!MCE)
4470 return Error(ExprLoc, "unexpected expression in _emit");
4471 uint64_t IntValue = MCE->getValue();
4472 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4473 return Error(ExprLoc, "literal value out of range for directive");
4474
Chad Rosierc7f552c2013-02-12 21:33:51 +00004475 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4476 return false;
4477}
4478
Jim Grosbach4b905842013-09-20 23:08:21 +00004479bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004480 const MCExpr *Value;
4481 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004482 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004483 return true;
4484 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4485 if (!MCE)
4486 return Error(ExprLoc, "unexpected expression in align");
4487 uint64_t IntValue = MCE->getValue();
4488 if (!isPowerOf2_64(IntValue))
4489 return Error(ExprLoc, "literal value not a power of two greater then zero");
4490
Jim Grosbach4b905842013-09-20 23:08:21 +00004491 Info.AsmRewrites->push_back(
4492 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004493 return false;
4494}
4495
Chad Rosierf43fcf52013-02-13 21:27:17 +00004496// We are comparing pointers, but the pointers are relative to a single string.
4497// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004498static int rewritesSort(const AsmRewrite *AsmRewriteA,
4499 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004500 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4501 return -1;
4502 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4503 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004504
Chad Rosierfce4fab2013-04-08 17:43:47 +00004505 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4506 // rewrite to the same location. Make sure the SizeDirective rewrite is
4507 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4508 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004509 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4510 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004511 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004512
Jim Grosbach4b905842013-09-20 23:08:21 +00004513 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4514 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004515 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004516 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004517}
4518
Jim Grosbach4b905842013-09-20 23:08:21 +00004519bool AsmParser::parseMSInlineAsm(
4520 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4521 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4522 SmallVectorImpl<std::string> &Constraints,
4523 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4524 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004525 SmallVector<void *, 4> InputDecls;
4526 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004527 SmallVector<bool, 4> InputDeclsAddressOf;
4528 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004529 SmallVector<std::string, 4> InputConstraints;
4530 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004531 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004532
Benjamin Kramer1a136112013-02-15 20:37:21 +00004533 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004534
4535 // Prime the lexer.
4536 Lex();
4537
4538 // While we have input, parse each statement.
4539 unsigned InputIdx = 0;
4540 unsigned OutputIdx = 0;
4541 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004542 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004543 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004544 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004545
Chad Rosier149e8e02012-12-12 22:45:52 +00004546 if (Info.ParseError)
4547 return true;
4548
Benjamin Kramer1a136112013-02-15 20:37:21 +00004549 if (Info.Opcode == ~0U)
4550 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004551
Benjamin Kramer1a136112013-02-15 20:37:21 +00004552 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004553
Benjamin Kramer1a136112013-02-15 20:37:21 +00004554 // Build the list of clobbers, outputs and inputs.
4555 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
David Blaikie960ea3f2014-06-08 16:18:35 +00004556 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004557
Benjamin Kramer1a136112013-02-15 20:37:21 +00004558 // Immediate.
David Blaikie960ea3f2014-06-08 16:18:35 +00004559 if (Operand.isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004560 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004561
Benjamin Kramer1a136112013-02-15 20:37:21 +00004562 // Register operand.
Nico Weber42f79db2014-07-17 20:24:55 +00004563 if (Operand.isReg() && !Operand.needAddressOf() &&
4564 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) {
Benjamin Kramer1a136112013-02-15 20:37:21 +00004565 unsigned NumDefs = Desc.getNumDefs();
4566 // Clobber.
David Blaikie960ea3f2014-06-08 16:18:35 +00004567 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
4568 ClobberRegs.push_back(Operand.getReg());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004569 continue;
4570 }
4571
4572 // Expr/Input or Output.
David Blaikie960ea3f2014-06-08 16:18:35 +00004573 StringRef SymName = Operand.getSymName();
Chad Rosiere81309b2013-04-09 17:53:49 +00004574 if (SymName.empty())
4575 continue;
4576
David Blaikie960ea3f2014-06-08 16:18:35 +00004577 void *OpDecl = Operand.getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004578 if (!OpDecl)
4579 continue;
4580
4581 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004582 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004583 if (isOutput) {
4584 ++InputIdx;
4585 OutputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004586 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
4587 OutputConstraints.push_back('=' + Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004588 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004589 } else {
4590 InputDecls.push_back(OpDecl);
David Blaikie960ea3f2014-06-08 16:18:35 +00004591 InputDeclsAddressOf.push_back(Operand.needAddressOf());
4592 InputConstraints.push_back(Operand.getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004593 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004594 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004595 }
Reid Kleckneree088972013-12-10 18:27:32 +00004596
4597 // Consider implicit defs to be clobbers. Think of cpuid and push.
David Majnemer8114c1a2014-06-23 02:17:16 +00004598 ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(),
4599 Desc.getNumImplicitDefs());
4600 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end());
Chad Rosier8bce6642012-10-18 15:49:34 +00004601 }
4602
4603 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004604 NumOutputs = OutputDecls.size();
4605 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004606
4607 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004608 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4609 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4610 ClobberRegs.end());
4611 Clobbers.assign(ClobberRegs.size(), std::string());
4612 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4613 raw_string_ostream OS(Clobbers[I]);
4614 IP->printRegName(OS, ClobberRegs[I]);
4615 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004616
4617 // Merge the various outputs and inputs. Output are expected first.
4618 if (NumOutputs || NumInputs) {
4619 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004620 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004621 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004622 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004623 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004624 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004625 }
4626 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004627 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004628 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004629 }
4630 }
4631
4632 // Build the IR assembly string.
Alp Tokere69170a2014-06-26 22:52:05 +00004633 std::string AsmStringIR;
4634 raw_string_ostream OS(AsmStringIR);
Alp Tokera55b95b2014-07-06 10:33:31 +00004635 StringRef ASMString =
4636 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer();
4637 const char *AsmStart = ASMString.begin();
4638 const char *AsmEnd = ASMString.end();
Jim Grosbach4b905842013-09-20 23:08:21 +00004639 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
David Majnemer8114c1a2014-06-23 02:17:16 +00004640 for (const AsmRewrite &AR : AsmStrRewrites) {
4641 AsmRewriteKind Kind = AR.Kind;
Chad Rosierff10ed12013-04-12 16:26:42 +00004642 if (Kind == AOK_Delete)
4643 continue;
4644
David Majnemer8114c1a2014-06-23 02:17:16 +00004645 const char *Loc = AR.Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004646 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004647
Chad Rosier120eefd2013-03-19 17:32:17 +00004648 // Emit everything up to the immediate/expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004649 if (unsigned Len = Loc - AsmStart)
Chad Rosier17d37992013-03-19 21:12:14 +00004650 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004651
Chad Rosier37e755c2012-10-23 17:43:43 +00004652 // Skip the original expression.
4653 if (Kind == AOK_Skip) {
David Majnemer8114c1a2014-06-23 02:17:16 +00004654 AsmStart = Loc + AR.Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004655 continue;
4656 }
4657
Chad Rosierff10ed12013-04-12 16:26:42 +00004658 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004659 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004660 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004661 default:
4662 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004663 case AOK_Imm:
David Majnemer8114c1a2014-06-23 02:17:16 +00004664 OS << "$$" << AR.Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004665 break;
4666 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004667 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004668 break;
4669 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004670 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004671 break;
4672 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004673 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004674 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004675 case AOK_SizeDirective:
David Majnemer8114c1a2014-06-23 02:17:16 +00004676 switch (AR.Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004677 default: break;
4678 case 8: OS << "byte ptr "; break;
4679 case 16: OS << "word ptr "; break;
4680 case 32: OS << "dword ptr "; break;
4681 case 64: OS << "qword ptr "; break;
4682 case 80: OS << "xword ptr "; break;
4683 case 128: OS << "xmmword ptr "; break;
4684 case 256: OS << "ymmword ptr "; break;
4685 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004686 break;
4687 case AOK_Emit:
4688 OS << ".byte";
4689 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004690 case AOK_Align: {
David Majnemer8114c1a2014-06-23 02:17:16 +00004691 unsigned Val = AR.Val;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004692 OS << ".align " << Val;
4693
4694 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004695 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004696 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4697 break;
4698 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004699 case AOK_DotOperator:
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004700 // Insert the dot if the user omitted it.
Alp Tokere69170a2014-06-26 22:52:05 +00004701 OS.flush();
4702 if (AsmStringIR.back() != '.')
Reid Kleckner94a1c4d2014-03-06 19:19:12 +00004703 OS << '.';
David Majnemer8114c1a2014-06-23 02:17:16 +00004704 OS << AR.Val;
Chad Rosierf0e87202012-10-25 20:41:34 +00004705 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004706 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004707
Chad Rosier8bce6642012-10-18 15:49:34 +00004708 // Skip the original expression.
David Majnemer8114c1a2014-06-23 02:17:16 +00004709 AsmStart = Loc + AR.Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004710 }
4711
4712 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004713 if (AsmStart != AsmEnd)
4714 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004715
4716 AsmString = OS.str();
4717 return false;
4718}
4719
Daniel Dunbar01e36072010-07-17 02:26:10 +00004720/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004721MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4722 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004723 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004724}