blob: c2998ab8ccc1e6282ee985bd8c60ce46213a11b2 [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>
Chad Rosier8bce6642012-10-18 15:49:34 +000042#include <set>
43#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000044#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000045using namespace llvm;
46
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000047static cl::opt<bool>
48FatalAssemblerWarnings("fatal-assembler-warnings",
49 cl::desc("Consider warnings as error"));
50
Eric Christophera7c32732012-12-18 00:30:54 +000051MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000052
Daniel Dunbar86033402010-07-12 17:54:38 +000053namespace {
Eli Benderskya313ae62013-01-16 18:56:50 +000054/// \brief Helper types for tracking macro definitions.
55typedef std::vector<AsmToken> MCAsmMacroArgument;
56typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000057
58struct MCAsmMacroParameter {
59 StringRef Name;
60 MCAsmMacroArgument Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +000061 bool Required;
62
63 MCAsmMacroParameter() : Required(false) { }
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +000064};
65
Eli Benderskya313ae62013-01-16 18:56:50 +000066typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
67
68struct MCAsmMacro {
69 StringRef Name;
70 StringRef Body;
71 MCAsmMacroParameters Parameters;
72
73public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000074 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000075 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000076};
77
Daniel Dunbar43235712010-07-18 18:54:11 +000078/// \brief Helper class for storing information about an active macro
79/// instantiation.
80struct MacroInstantiation {
81 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000082 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000083
84 /// The macro instantiation with substitutions.
85 MemoryBuffer *Instantiation;
86
87 /// The location of the instantiation.
88 SMLoc InstantiationLoc;
89
Daniel Dunbar40f1d852012-12-01 01:38:48 +000090 /// The buffer where parsing should resume upon instantiation completion.
91 int ExitBuffer;
92
Daniel Dunbar43235712010-07-18 18:54:11 +000093 /// The location where parsing should resume upon instantiation completion.
94 SMLoc ExitLoc;
95
96public:
Eli Bendersky38274122013-01-14 23:22:36 +000097 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000098 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000099};
100
Eli Friedman0f4871d2012-10-22 23:58:19 +0000101struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +0000102 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000103 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
104
Jim Grosbach4b905842013-09-20 23:08:21 +0000105 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000106 unsigned Opcode;
107
Jim Grosbach4b905842013-09-20 23:08:21 +0000108 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 bool ParseError;
110
Eli Friedman0f4871d2012-10-22 23:58:19 +0000111 SmallVectorImpl<AsmRewrite> *AsmRewrites;
112
Chad Rosier149e8e02012-12-12 22:45:52 +0000113 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000114 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000115 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000116
117 ~ParseStatementInfo() {
118 // Free any parsed operands.
119 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
120 delete ParsedOperands[i];
121 ParsedOperands.clear();
122 }
123};
124
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125/// \brief The concrete assembly parser instance.
126class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000127 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
128 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000129private:
130 AsmLexer Lexer;
131 MCContext &Ctx;
132 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000133 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000134 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000135 SourceMgr::DiagHandlerTy SavedDiagHandler;
136 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000137 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000138
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000139 /// This is the current buffer index we're lexing from as managed by the
140 /// SourceMgr object.
141 int CurBuffer;
142
143 AsmCond TheCondState;
144 std::vector<AsmCond> TheCondStack;
145
Jim Grosbach4b905842013-09-20 23:08:21 +0000146 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000147 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000148 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000149 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000150
Jim Grosbach4b905842013-09-20 23:08:21 +0000151 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000152 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000153
Jim Grosbach4b905842013-09-20 23:08:21 +0000154 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000155 std::vector<MacroInstantiation*> ActiveMacros;
156
Jim Grosbach4b905842013-09-20 23:08:21 +0000157 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000158 std::deque<MCAsmMacro> MacroLikeBodies;
159
Daniel Dunbar828984f2010-07-18 18:38:02 +0000160 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000161 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000162
Daniel Dunbar43325c42010-09-09 22:42:56 +0000163 /// Flag tracking whether any errors have been encountered.
164 unsigned HadError : 1;
165
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000166 /// The values from the last parsed cpp hash file line comment if any.
167 StringRef CppHashFilename;
168 int64_t CppHashLineNumber;
169 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000170 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000171 /// When generating dwarf for assembly source files we need to calculate the
172 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000173 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000174 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
175 SMLoc LastQueryIDLoc;
176 int LastQueryBuffer;
177 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000178
Devang Patela173ee52012-01-31 18:14:05 +0000179 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
180 unsigned AssemblerDialect;
181
Jim Grosbach4b905842013-09-20 23:08:21 +0000182 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000183 bool IsDarwin;
184
Jim Grosbach4b905842013-09-20 23:08:21 +0000185 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000186 bool ParsingInlineAsm;
187
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000189 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000190 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000191 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000192
193 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
194
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000195 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000196 ExtensionDirectiveHandler Handler) {
197 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000198 }
199
200public:
201 /// @name MCAsmParser Interface
202 /// {
203
204 virtual SourceMgr &getSourceManager() { return SrcMgr; }
205 virtual MCAsmLexer &getLexer() { return Lexer; }
206 virtual MCContext &getContext() { return Ctx; }
207 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000208 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000209 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000210 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000211 else
212 return AssemblerDialect;
213 }
214 virtual void setAssemblerDialect(unsigned i) {
215 AssemblerDialect = i;
216 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000217
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000218 virtual void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000219 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000220 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000221 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000222 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000223
Craig Topper5f96ca52012-08-29 05:48:09 +0000224 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000225
Chad Rosier49963552012-10-13 00:26:04 +0000226 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000227 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000228
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000229 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000230 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000231 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000232 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000233 SmallVectorImpl<std::string> &Clobbers,
234 const MCInstrInfo *MII,
235 const MCInstPrinter *IP,
236 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000237
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000238 bool parseExpression(const MCExpr *&Res);
239 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000240 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000241 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
242 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000243
Jim Grosbach4b905842013-09-20 23:08:21 +0000244 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000245 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000246 virtual bool parseIdentifier(StringRef &Res);
247 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000248
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000249 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000250 /// }
251
252private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000253
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 bool parseStatement(ParseStatementInfo &Info);
255 void eatToEndOfLine();
256 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000257
Jim Grosbach4b905842013-09-20 23:08:21 +0000258 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000259 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000260 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000261 ArrayRef<MCAsmMacroParameter> Parameters,
262 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000263 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000264
Eli Benderskya313ae62013-01-16 18:56:50 +0000265 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000266 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000267
268 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Lookup a previously defined macro.
272 /// \param Name Macro name.
273 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000274 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000275
276 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000277 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000278
279 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000280 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000281
282 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000283 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000284
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000285 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000286 ///
287 /// \param M The macro.
288 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000289 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000290
291 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000293
David Majnemer91fc4c22014-01-29 18:57:46 +0000294 /// \brief Extract AsmTokens for a macro argument.
295 bool parseMacroArgument(MCAsmMacroArgument &MA);
Eli Benderskya313ae62013-01-16 18:56:50 +0000296
297 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000299
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 void printMacroInstantiations();
301 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000302 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000303 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000304 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000305 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000306
Jim Grosbach4b905842013-09-20 23:08:21 +0000307 /// \brief Enter the specified file. This returns true on failure.
308 bool enterIncludeFile(const std::string &Filename);
309
310 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000311 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000312 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000313
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000314 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000315 /// current token is not set; clients should ensure Lex() is called
316 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000317 ///
318 /// \param InBuffer If not -1, should be the known buffer id that contains the
319 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000320 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000321
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000322 /// \brief Parse up to the end of statement and a return the contents from the
323 /// current token until the end of the statement; the current token on exit
324 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000325 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000326
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000327 /// \brief Parse until the end of a statement or a comma is encountered,
328 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000330
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000332 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000333
Jim Grosbach4b905842013-09-20 23:08:21 +0000334 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
335 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
336 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000337
Jim Grosbach4b905842013-09-20 23:08:21 +0000338 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000339
Eli Bendersky17233942013-01-15 22:59:42 +0000340 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000341 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_NO_DIRECTIVE, // Placeholder
343 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000344 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
345 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000346 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000347 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000348 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000349 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
350 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
351 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
352 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
353 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky17233942013-01-15 22:59:42 +0000354 DK_ELSEIF, DK_ELSE, DK_ENDIF,
355 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
356 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
357 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
358 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
359 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
360 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000361 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000362 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000363 DK_SLEB128, DK_ULEB128,
364 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000365 };
366
Jim Grosbach4b905842013-09-20 23:08:21 +0000367 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000368 /// directives parsed by this class.
369 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000370
371 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000372 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
373 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000374 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
376 bool parseDirectiveFill(); // ".fill"
377 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000378 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000379 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
380 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000381 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000382 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000383
Eli Bendersky17233942013-01-15 22:59:42 +0000384 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000385 bool parseDirectiveFile(SMLoc DirectiveLoc);
386 bool parseDirectiveLine();
387 bool parseDirectiveLoc();
388 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000389
390 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000391 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000392 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000393 bool parseDirectiveCFISections();
394 bool parseDirectiveCFIStartProc();
395 bool parseDirectiveCFIEndProc();
396 bool parseDirectiveCFIDefCfaOffset();
397 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIAdjustCfaOffset();
399 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
403 bool parseDirectiveCFIRememberState();
404 bool parseDirectiveCFIRestoreState();
405 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
406 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
407 bool parseDirectiveCFIEscape();
408 bool parseDirectiveCFISignalFrame();
409 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000410
411 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000412 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
413 bool parseDirectiveEndMacro(StringRef Directive);
414 bool parseDirectiveMacro(SMLoc DirectiveLoc);
415 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000416
Eli Benderskyf483ff92012-12-20 19:05:53 +0000417 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000421 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000422 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000423
Eli Bendersky17233942013-01-15 22:59:42 +0000424 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000426
427 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000429
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000433
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveAbort(); // ".abort"
437 bool parseDirectiveInclude(); // ".include"
438 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000439
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000441 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000443 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000445 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000446 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
447 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
448 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
449 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000450 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000451
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000453 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000454
Rafael Espindola34b9c512012-06-03 23:57:14 +0000455 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
457 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000458 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000459 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
461 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
462 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000463
Chad Rosierc7f552c2013-02-12 21:33:51 +0000464 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000466 size_t Len);
467
468 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000469 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000470
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000471 // "end"
472 bool parseDirectiveEnd(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),
491 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
492 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
493 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);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000499 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
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() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000522 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
523
524 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000525 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
526 ie = MacroMap.end();
527 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000528 delete it->getValue();
529
Daniel Dunbarc5011082010-07-12 18:12:02 +0000530 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000531}
532
Jim Grosbach4b905842013-09-20 23:08:21 +0000533void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000534 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000535 for (std::vector<MacroInstantiation *>::const_reverse_iterator
536 it = ActiveMacros.rbegin(),
537 ie = ActiveMacros.rend();
538 it != ie; ++it)
539 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000540 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000541}
542
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000543void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
544 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
545 printMacroInstantiations();
546}
547
Chris Lattnera3a06812011-10-16 04:47:35 +0000548bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000549 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000550 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000551 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
552 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000553 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000554}
555
Chris Lattnera3a06812011-10-16 04:47:35 +0000556bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000557 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000558 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
559 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000560 return true;
561}
562
Jim Grosbach4b905842013-09-20 23:08:21 +0000563bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000564 std::string IncludedFile;
565 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000566 if (NewBuf == -1)
567 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000568
Sean Callanan7a77eae2010-01-21 00:19:58 +0000569 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000570
Sean Callanan7a77eae2010-01-21 00:19:58 +0000571 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000572
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;
581 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
582 if (NewBuf == -1)
583 return true;
584
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000585 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000586 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000587 return false;
588}
589
Jim Grosbach4b905842013-09-20 23:08:21 +0000590void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000591 if (InBuffer != -1) {
592 CurBuffer = InBuffer;
593 } else {
594 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
595 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000596 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
597}
598
Sean Callanan7a77eae2010-01-21 00:19:58 +0000599const AsmToken &AsmParser::Lex() {
600 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000601
Sean Callanan7a77eae2010-01-21 00:19:58 +0000602 if (tok->is(AsmToken::Eof)) {
603 // If this is the end of an included file, pop the parent file off the
604 // include stack.
605 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
606 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000607 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 tok = &Lexer.Lex();
609 }
610 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000611
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000613 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000614
Sean Callanan7a77eae2010-01-21 00:19:58 +0000615 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000616}
617
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000618bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000619 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000620 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000621 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000622
Chris Lattner36e02122009-06-21 20:54:55 +0000623 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000624 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000625
626 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000627 AsmCond StartingCondState = TheCondState;
628
Kevin Enderby6469fc22011-11-01 22:27:22 +0000629 // If we are generating dwarf for assembly source files save the initial text
630 // section and generate a .file directive.
631 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000632 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000633 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
634 getStreamer().EmitLabel(SectionStartSym);
635 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000636 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000637 StringRef(),
638 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000639 }
640
Chris Lattner73f36112009-07-02 21:53:43 +0000641 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000642 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000643 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000644 if (!parseStatement(Info))
645 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000646
Daniel Dunbar43325c42010-09-09 22:42:56 +0000647 // We had an error, validate that one was emitted and recover by skipping to
648 // the next line.
649 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000650 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000651 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000652
653 if (TheCondState.TheCond != StartingCondState.TheCond ||
654 TheCondState.Ignore != StartingCondState.Ignore)
655 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000656
657 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000658 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000659 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000660 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000661 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000662 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000663 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000664
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000665 // Check to see that all assembler local symbols were actually defined.
666 // Targets that don't do subsections via symbols may not want this, though,
667 // so conservatively exclude them. Only do this if we're finalizing, though,
668 // as otherwise we won't necessarilly have seen everything yet.
669 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
670 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
671 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000672 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000673 i != e; ++i) {
674 MCSymbol *Sym = i->getValue();
675 // Variable symbols may not be marked as defined, so check those
676 // explicitly. If we know it's a variable, we have a definition for
677 // the purposes of this check.
678 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
679 // FIXME: We would really like to refer back to where the symbol was
680 // first referenced for a source location. We need to add something
681 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000682 printMessage(
683 getLexer().getLoc(), SourceMgr::DK_Error,
684 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000685 }
686 }
687
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000688 // Finalize the output stream if there are no errors and if the client wants
689 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000690 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000691 Out.Finish();
692
Chris Lattner73f36112009-07-02 21:53:43 +0000693 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000694}
695
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000696void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000697 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000698 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000699 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000700 }
701}
702
Jim Grosbach4b905842013-09-20 23:08:21 +0000703/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000704void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000705 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000706 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000707
Chris Lattnere5074c42009-06-22 01:29:09 +0000708 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000709 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000710 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000711}
712
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000713StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000714 const char *Start = getTok().getLoc().getPointer();
715
Jim Grosbach4b905842013-09-20 23:08:21 +0000716 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000717 Lex();
718
719 const char *End = getTok().getLoc().getPointer();
720 return StringRef(Start, End - Start);
721}
Chris Lattner78db3622009-06-22 05:51:26 +0000722
Jim Grosbach4b905842013-09-20 23:08:21 +0000723StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000724 const char *Start = getTok().getLoc().getPointer();
725
726 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000727 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000728 Lex();
729
730 const char *End = getTok().getLoc().getPointer();
731 return StringRef(Start, End - Start);
732}
733
Jim Grosbach4b905842013-09-20 23:08:21 +0000734/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000735/// NOTE: This assumes the leading '(' has already been consumed.
736///
737/// parenexpr ::= expr)
738///
Jim Grosbach4b905842013-09-20 23:08:21 +0000739bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
740 if (parseExpression(Res))
741 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000742 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000743 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000744 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000745 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000746 return false;
747}
Chris Lattner78db3622009-06-22 05:51:26 +0000748
Jim Grosbach4b905842013-09-20 23:08:21 +0000749/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000750/// NOTE: This assumes the leading '[' has already been consumed.
751///
752/// bracketexpr ::= expr]
753///
Jim Grosbach4b905842013-09-20 23:08:21 +0000754bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
755 if (parseExpression(Res))
756 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000757 if (Lexer.isNot(AsmToken::RBrac))
758 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000759 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000760 Lex();
761 return false;
762}
763
Jim Grosbach4b905842013-09-20 23:08:21 +0000764/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000765/// primaryexpr ::= (parenexpr
766/// primaryexpr ::= symbol
767/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000768/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000769/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000770bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000771 SMLoc FirstTokenLoc = getLexer().getLoc();
772 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
773 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000774 default:
775 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000776 // If we have an error assume that we've already handled it.
777 case AsmToken::Error:
778 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000779 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000780 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000781 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000782 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000783 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000784 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000785 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000786 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000787 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000788 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000789 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000790 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000791 if (FirstTokenKind == AsmToken::Dollar) {
792 if (Lexer.getMAI().getDollarIsPC()) {
793 // This is a '$' reference, which references the current PC. Emit a
794 // temporary label to the streamer and refer to it.
795 MCSymbol *Sym = Ctx.CreateTempSymbol();
796 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000797 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
798 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000799 EndLoc = FirstTokenLoc;
800 return false;
801 } else
802 return Error(FirstTokenLoc, "invalid token in expression");
803 return true;
804 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000805 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000806 // Parse symbol variant
807 std::pair<StringRef, StringRef> Split;
808 if (!MAI.useParensForSymbolVariant()) {
809 Split = Identifier.split('@');
810 } else if (Lexer.is(AsmToken::LParen)) {
811 Lexer.Lex(); // eat (
812 StringRef VName;
813 parseIdentifier(VName);
814 if (Lexer.isNot(AsmToken::RParen)) {
815 return Error(Lexer.getTok().getLoc(),
816 "unexpected token in variant, expected ')'");
817 }
818 Lexer.Lex(); // eat )
819 Split = std::make_pair(Identifier, VName);
820 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000821
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000822 EndLoc = SMLoc::getFromPointer(Identifier.end());
823
Daniel Dunbard20cda02009-10-16 01:34:54 +0000824 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000825 StringRef SymbolName = Identifier;
826 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000827
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000828 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000829 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000830 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000831 if (Variant != MCSymbolRefExpr::VK_Invalid) {
832 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000833 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000834 Variant = MCSymbolRefExpr::VK_None;
835 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000836 return Error(SMLoc::getFromPointer(Split.second.begin()),
837 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000838 }
839 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000840
Hans Wennborgce69d772013-10-18 20:46:28 +0000841 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
842
Daniel Dunbard20cda02009-10-16 01:34:54 +0000843 // If this is an absolute variable reference, substitute it now to preserve
844 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000845 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000846 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000847 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000848
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000849 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000850 return false;
851 }
852
853 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000854 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000855 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000856 }
David Woodhousef42a6662014-02-01 16:20:54 +0000857 case AsmToken::BigNum:
858 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000859 case AsmToken::Integer: {
860 SMLoc Loc = getTok().getLoc();
861 int64_t IntVal = getTok().getIntVal();
862 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000863 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000864 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000865 // Look for 'b' or 'f' following an Integer as a directional label
866 if (Lexer.getKind() == AsmToken::Identifier) {
867 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000868 // Lookup the symbol variant if used.
869 std::pair<StringRef, StringRef> Split = IDVal.split('@');
870 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
871 if (Split.first.size() != IDVal.size()) {
872 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
873 if (Variant == MCSymbolRefExpr::VK_Invalid) {
874 Variant = MCSymbolRefExpr::VK_None;
875 return TokError("invalid variant '" + Split.second + "'");
876 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000877 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000878 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000879 if (IDVal == "f" || IDVal == "b") {
880 MCSymbol *Sym =
881 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000882 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000883 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000885 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000886 Lex(); // Eat identifier.
887 }
888 }
Chris Lattner78db3622009-06-22 05:51:26 +0000889 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000890 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000891 case AsmToken::Real: {
892 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000893 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000894 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000895 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000896 Lex(); // Eat token.
897 return false;
898 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000899 case AsmToken::Dot: {
900 // This is a '.' reference, which references the current PC. Emit a
901 // temporary label to the streamer and refer to it.
902 MCSymbol *Sym = Ctx.CreateTempSymbol();
903 Out.EmitLabel(Sym);
904 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000905 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000906 Lex(); // Eat identifier.
907 return false;
908 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000909 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000910 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000911 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000912 case AsmToken::LBrac:
913 if (!PlatformParser->HasBracketExpressions())
914 return TokError("brackets expression not supported on this target");
915 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000916 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000917 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000918 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000921 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000922 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000923 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000924 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000925 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000927 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000928 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000929 case AsmToken::Tilde:
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::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000934 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000935 }
936}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000937
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000938bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000939 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000940 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000941}
942
Daniel Dunbar55f16672010-09-17 02:47:07 +0000943const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000944AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000945 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000946 // Ask the target implementation about this expression first.
947 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
948 if (NewE)
949 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000950 // Recurse over the given expression, rebuilding it to apply the given variant
951 // if there is exactly one symbol.
952 switch (E->getKind()) {
953 case MCExpr::Target:
954 case MCExpr::Constant:
955 return 0;
956
957 case MCExpr::SymbolRef: {
958 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
959
960 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000961 TokError("invalid variant on expression '" + getTok().getIdentifier() +
962 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963 return E;
964 }
965
966 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
967 }
968
969 case MCExpr::Unary: {
970 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000971 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000972 if (!Sub)
973 return 0;
974 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
975 }
976
977 case MCExpr::Binary: {
978 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
980 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981
982 if (!LHS && !RHS)
983 return 0;
984
Jim Grosbach4b905842013-09-20 23:08:21 +0000985 if (!LHS)
986 LHS = BE->getLHS();
987 if (!RHS)
988 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989
990 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
991 }
992 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000993
Craig Toppera2886c22012-02-07 05:05:23 +0000994 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000995}
996
Jim Grosbach4b905842013-09-20 23:08:21 +0000997/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000998///
Jim Grosbachbd164242011-08-20 16:24:13 +0000999/// expr ::= expr &&,|| expr -> lowest.
1000/// expr ::= expr |,^,&,! expr
1001/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1002/// expr ::= expr <<,>> expr
1003/// expr ::= expr +,- expr
1004/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001005/// expr ::= primaryexpr
1006///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001007bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001008 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001009 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001010 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001011 return true;
1012
Daniel Dunbar55f16672010-09-17 02:47:07 +00001013 // As a special case, we support 'a op b @ modifier' by rewriting the
1014 // expression to include the modifier. This is inefficient, but in general we
1015 // expect users to use 'a@modifier op b'.
1016 if (Lexer.getKind() == AsmToken::At) {
1017 Lex();
1018
1019 if (Lexer.isNot(AsmToken::Identifier))
1020 return TokError("unexpected symbol modifier following '@'");
1021
1022 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001023 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001024 if (Variant == MCSymbolRefExpr::VK_Invalid)
1025 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1026
Jim Grosbach4b905842013-09-20 23:08:21 +00001027 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001028 if (!ModifiedRes) {
1029 return TokError("invalid modifier '" + getTok().getIdentifier() +
1030 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001032
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033 Res = ModifiedRes;
1034 Lex();
1035 }
1036
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001037 // Try to constant fold it up front, if possible.
1038 int64_t Value;
1039 if (Res->EvaluateAsAbsolute(Value))
1040 Res = MCConstantExpr::Create(Value, getContext());
1041
1042 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001043}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001044
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001045bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001046 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001047 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001048}
1049
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001050bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001051 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001052
Daniel Dunbar75630b32009-06-30 02:10:03 +00001053 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001054 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001055 return true;
1056
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001057 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001058 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001059
1060 return false;
1061}
1062
Michael J. Spencer530ce852010-10-09 11:00:50 +00001063static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001064 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001065 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001066 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001067 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001068
Jim Grosbach4b905842013-09-20 23:08:21 +00001069 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001070 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001071 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001072 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001073 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001074 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001075 return 1;
1076
Jim Grosbach4b905842013-09-20 23:08:21 +00001077 // Low Precedence: |, &, ^
1078 //
1079 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001082 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001085 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001088 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001089
Jim Grosbach4b905842013-09-20 23:08:21 +00001090 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001091 case AsmToken::EqualEqual:
1092 Kind = MCBinaryExpr::EQ;
1093 return 3;
1094 case AsmToken::ExclaimEqual:
1095 case AsmToken::LessGreater:
1096 Kind = MCBinaryExpr::NE;
1097 return 3;
1098 case AsmToken::Less:
1099 Kind = MCBinaryExpr::LT;
1100 return 3;
1101 case AsmToken::LessEqual:
1102 Kind = MCBinaryExpr::LTE;
1103 return 3;
1104 case AsmToken::Greater:
1105 Kind = MCBinaryExpr::GT;
1106 return 3;
1107 case AsmToken::GreaterEqual:
1108 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001109 return 3;
1110
Jim Grosbach4b905842013-09-20 23:08:21 +00001111 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001112 case AsmToken::LessLess:
1113 Kind = MCBinaryExpr::Shl;
1114 return 4;
1115 case AsmToken::GreaterGreater:
1116 Kind = MCBinaryExpr::Shr;
1117 return 4;
1118
Jim Grosbach4b905842013-09-20 23:08:21 +00001119 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001120 case AsmToken::Plus:
1121 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001122 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001123 case AsmToken::Minus:
1124 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001125 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001126
Jim Grosbach4b905842013-09-20 23:08:21 +00001127 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001133 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001134 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001135 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001136 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001137 }
1138}
1139
Jim Grosbach4b905842013-09-20 23:08:21 +00001140/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001141/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001142bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001143 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001145 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001146 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001147
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001148 // If the next token is lower precedence than we are allowed to eat, return
1149 // successfully with what we ate already.
1150 if (TokPrec < Precedence)
1151 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001152
Sean Callanan686ed8d2010-01-19 20:22:31 +00001153 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001154
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001156 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001157 if (parsePrimaryExpr(RHS, EndLoc))
1158 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160 // If BinOp binds less tightly with RHS than the operator after RHS, let
1161 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001162 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001163 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1165 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001166
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001167 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001168 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001169 }
1170}
1171
Chris Lattner36e02122009-06-21 20:54:55 +00001172/// ParseStatement:
1173/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001174/// ::= Label* Directive ...Operands... EndOfStatement
1175/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001176bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001177 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001178 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001179 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001180 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001181 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001182
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001183 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001184 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001185 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001187 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001188 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001189 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001190 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001191
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001192 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001193 if (Lexer.is(AsmToken::Integer)) {
1194 LocalLabelVal = getTok().getIntVal();
1195 if (LocalLabelVal < 0) {
1196 if (!TheCondState.Ignore)
1197 return TokError("unexpected token at start of statement");
1198 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001199 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001200 IDVal = getTok().getString();
1201 Lex(); // Consume the integer token to be used as an identifier token.
1202 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001203 if (!TheCondState.Ignore)
1204 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001205 }
1206 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001207 } else if (Lexer.is(AsmToken::Dot)) {
1208 // Treat '.' as a valid identifier in this context.
1209 Lex();
1210 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001211 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001212 if (!TheCondState.Ignore)
1213 return TokError("unexpected token at start of statement");
1214 IDVal = "";
1215 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001216
Chris Lattner926885c2010-04-17 18:14:27 +00001217 // Handle conditional assembly here before checking for skipping. We
1218 // have to do this so that .endif isn't skipped in a ".if 0" block for
1219 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001220 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001221 DirectiveKindMap.find(IDVal);
1222 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1223 ? DK_NO_DIRECTIVE
1224 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001225 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001226 default:
1227 break;
1228 case DK_IF:
1229 return parseDirectiveIf(IDLoc);
1230 case DK_IFB:
1231 return parseDirectiveIfb(IDLoc, true);
1232 case DK_IFNB:
1233 return parseDirectiveIfb(IDLoc, false);
1234 case DK_IFC:
1235 return parseDirectiveIfc(IDLoc, true);
1236 case DK_IFNC:
1237 return parseDirectiveIfc(IDLoc, false);
1238 case DK_IFDEF:
1239 return parseDirectiveIfdef(IDLoc, true);
1240 case DK_IFNDEF:
1241 case DK_IFNOTDEF:
1242 return parseDirectiveIfdef(IDLoc, false);
1243 case DK_ELSEIF:
1244 return parseDirectiveElseIf(IDLoc);
1245 case DK_ELSE:
1246 return parseDirectiveElse(IDLoc);
1247 case DK_ENDIF:
1248 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001249 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001250
Eli Bendersky88024712013-01-16 19:32:36 +00001251 // Ignore the statement if in the middle of inactive conditional
1252 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001253 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001254 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001255 return false;
1256 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001257
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001258 // FIXME: Recurse on local labels?
1259
1260 // See what kind of statement we have.
1261 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001262 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001263 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001264
Chris Lattner36e02122009-06-21 20:54:55 +00001265 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001266 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001267
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001268 // Diagnose attempt to use '.' as a label.
1269 if (IDVal == ".")
1270 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1271
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001272 // Diagnose attempt to use a variable as a label.
1273 //
1274 // FIXME: Diagnostics. Note the location of the definition as a label.
1275 // FIXME: This doesn't diagnose assignment to a symbol which has been
1276 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001277 MCSymbol *Sym;
1278 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001279 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001280 else
1281 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001282 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001283 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001284
Daniel Dunbare73b2672009-08-26 22:13:22 +00001285 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001286 if (!ParsingInlineAsm)
1287 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001288
Kevin Enderbye7739d42011-12-09 18:09:40 +00001289 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001290 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001291 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001292 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1293 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001294
Tim Northover1744d0a2013-10-25 12:49:50 +00001295 getTargetParser().onLabelParsed(Sym);
1296
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001297 // Consume any end of statement token, if present, to avoid spurious
1298 // AddBlankLine calls().
1299 if (Lexer.is(AsmToken::EndOfStatement)) {
1300 Lex();
1301 if (Lexer.is(AsmToken::Eof))
1302 return false;
1303 }
1304
Eli Friedman0f4871d2012-10-22 23:58:19 +00001305 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001306 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001307
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001308 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001309 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001310 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001311
Jim Grosbach4b905842013-09-20 23:08:21 +00001312 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001313
1314 default: // Normal instruction or directive.
1315 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001316 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001317
1318 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001319 if (areMacrosEnabled())
1320 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1321 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001322 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001323
Michael J. Spencer530ce852010-10-09 11:00:50 +00001324 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001325
Eli Bendersky17233942013-01-15 22:59:42 +00001326 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001327 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001328 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001329 //
Eli Bendersky17233942013-01-15 22:59:42 +00001330 // 1. The target-specific assembly parser. Some directives are target
1331 // specific or may potentially behave differently on certain targets.
1332 // 2. Asm parser extensions. For example, platform-specific parsers
1333 // (like the ELF parser) register themselves as extensions.
1334 // 3. The generic directive parser implemented by this class. These are
1335 // all the directives that behave in a target and platform independent
1336 // manner, or at least have a default behavior that's shared between
1337 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001338
Eli Bendersky17233942013-01-15 22:59:42 +00001339 // First query the target-specific parser. It will return 'true' if it
1340 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001341 if (!getTargetParser().ParseDirective(ID))
1342 return false;
1343
Alp Tokercb402912014-01-24 17:20:08 +00001344 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001345 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001346 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1347 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001348 if (Handler.first)
1349 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1350
1351 // Finally, if no one else is interested in this directive, it must be
1352 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001353 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001354 default:
1355 break;
1356 case DK_SET:
1357 case DK_EQU:
1358 return parseDirectiveSet(IDVal, true);
1359 case DK_EQUIV:
1360 return parseDirectiveSet(IDVal, false);
1361 case DK_ASCII:
1362 return parseDirectiveAscii(IDVal, false);
1363 case DK_ASCIZ:
1364 case DK_STRING:
1365 return parseDirectiveAscii(IDVal, true);
1366 case DK_BYTE:
1367 return parseDirectiveValue(1);
1368 case DK_SHORT:
1369 case DK_VALUE:
1370 case DK_2BYTE:
1371 return parseDirectiveValue(2);
1372 case DK_LONG:
1373 case DK_INT:
1374 case DK_4BYTE:
1375 return parseDirectiveValue(4);
1376 case DK_QUAD:
1377 case DK_8BYTE:
1378 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001379 case DK_OCTA:
1380 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001381 case DK_SINGLE:
1382 case DK_FLOAT:
1383 return parseDirectiveRealValue(APFloat::IEEEsingle);
1384 case DK_DOUBLE:
1385 return parseDirectiveRealValue(APFloat::IEEEdouble);
1386 case DK_ALIGN: {
1387 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1388 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1389 }
1390 case DK_ALIGN32: {
1391 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1392 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1393 }
1394 case DK_BALIGN:
1395 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1396 case DK_BALIGNW:
1397 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1398 case DK_BALIGNL:
1399 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1400 case DK_P2ALIGN:
1401 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1402 case DK_P2ALIGNW:
1403 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1404 case DK_P2ALIGNL:
1405 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1406 case DK_ORG:
1407 return parseDirectiveOrg();
1408 case DK_FILL:
1409 return parseDirectiveFill();
1410 case DK_ZERO:
1411 return parseDirectiveZero();
1412 case DK_EXTERN:
1413 eatToEndOfStatement(); // .extern is the default, ignore it.
1414 return false;
1415 case DK_GLOBL:
1416 case DK_GLOBAL:
1417 return parseDirectiveSymbolAttribute(MCSA_Global);
1418 case DK_LAZY_REFERENCE:
1419 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1420 case DK_NO_DEAD_STRIP:
1421 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1422 case DK_SYMBOL_RESOLVER:
1423 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1424 case DK_PRIVATE_EXTERN:
1425 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1426 case DK_REFERENCE:
1427 return parseDirectiveSymbolAttribute(MCSA_Reference);
1428 case DK_WEAK_DEFINITION:
1429 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1430 case DK_WEAK_REFERENCE:
1431 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1432 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1433 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1434 case DK_COMM:
1435 case DK_COMMON:
1436 return parseDirectiveComm(/*IsLocal=*/false);
1437 case DK_LCOMM:
1438 return parseDirectiveComm(/*IsLocal=*/true);
1439 case DK_ABORT:
1440 return parseDirectiveAbort();
1441 case DK_INCLUDE:
1442 return parseDirectiveInclude();
1443 case DK_INCBIN:
1444 return parseDirectiveIncbin();
1445 case DK_CODE16:
1446 case DK_CODE16GCC:
1447 return TokError(Twine(IDVal) + " not supported yet");
1448 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001449 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001450 case DK_IRP:
1451 return parseDirectiveIrp(IDLoc);
1452 case DK_IRPC:
1453 return parseDirectiveIrpc(IDLoc);
1454 case DK_ENDR:
1455 return parseDirectiveEndr(IDLoc);
1456 case DK_BUNDLE_ALIGN_MODE:
1457 return parseDirectiveBundleAlignMode();
1458 case DK_BUNDLE_LOCK:
1459 return parseDirectiveBundleLock();
1460 case DK_BUNDLE_UNLOCK:
1461 return parseDirectiveBundleUnlock();
1462 case DK_SLEB128:
1463 return parseDirectiveLEB128(true);
1464 case DK_ULEB128:
1465 return parseDirectiveLEB128(false);
1466 case DK_SPACE:
1467 case DK_SKIP:
1468 return parseDirectiveSpace(IDVal);
1469 case DK_FILE:
1470 return parseDirectiveFile(IDLoc);
1471 case DK_LINE:
1472 return parseDirectiveLine();
1473 case DK_LOC:
1474 return parseDirectiveLoc();
1475 case DK_STABS:
1476 return parseDirectiveStabs();
1477 case DK_CFI_SECTIONS:
1478 return parseDirectiveCFISections();
1479 case DK_CFI_STARTPROC:
1480 return parseDirectiveCFIStartProc();
1481 case DK_CFI_ENDPROC:
1482 return parseDirectiveCFIEndProc();
1483 case DK_CFI_DEF_CFA:
1484 return parseDirectiveCFIDefCfa(IDLoc);
1485 case DK_CFI_DEF_CFA_OFFSET:
1486 return parseDirectiveCFIDefCfaOffset();
1487 case DK_CFI_ADJUST_CFA_OFFSET:
1488 return parseDirectiveCFIAdjustCfaOffset();
1489 case DK_CFI_DEF_CFA_REGISTER:
1490 return parseDirectiveCFIDefCfaRegister(IDLoc);
1491 case DK_CFI_OFFSET:
1492 return parseDirectiveCFIOffset(IDLoc);
1493 case DK_CFI_REL_OFFSET:
1494 return parseDirectiveCFIRelOffset(IDLoc);
1495 case DK_CFI_PERSONALITY:
1496 return parseDirectiveCFIPersonalityOrLsda(true);
1497 case DK_CFI_LSDA:
1498 return parseDirectiveCFIPersonalityOrLsda(false);
1499 case DK_CFI_REMEMBER_STATE:
1500 return parseDirectiveCFIRememberState();
1501 case DK_CFI_RESTORE_STATE:
1502 return parseDirectiveCFIRestoreState();
1503 case DK_CFI_SAME_VALUE:
1504 return parseDirectiveCFISameValue(IDLoc);
1505 case DK_CFI_RESTORE:
1506 return parseDirectiveCFIRestore(IDLoc);
1507 case DK_CFI_ESCAPE:
1508 return parseDirectiveCFIEscape();
1509 case DK_CFI_SIGNAL_FRAME:
1510 return parseDirectiveCFISignalFrame();
1511 case DK_CFI_UNDEFINED:
1512 return parseDirectiveCFIUndefined(IDLoc);
1513 case DK_CFI_REGISTER:
1514 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001515 case DK_CFI_WINDOW_SAVE:
1516 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001517 case DK_MACROS_ON:
1518 case DK_MACROS_OFF:
1519 return parseDirectiveMacrosOnOff(IDVal);
1520 case DK_MACRO:
1521 return parseDirectiveMacro(IDLoc);
1522 case DK_ENDM:
1523 case DK_ENDMACRO:
1524 return parseDirectiveEndMacro(IDVal);
1525 case DK_PURGEM:
1526 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001527 case DK_END:
1528 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001529 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001530
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001531 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001532 }
Chris Lattner36e02122009-06-21 20:54:55 +00001533
Chad Rosierc7f552c2013-02-12 21:33:51 +00001534 // __asm _emit or __asm __emit
1535 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1536 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001537 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001538
1539 // __asm align
1540 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001541 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001542
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001543 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001544
Chris Lattner7cbfa442010-05-19 23:34:33 +00001545 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001546 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001547 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001548 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001549 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001550 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001551
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001552 // Dump the parsed representation, if requested.
1553 if (getShowParsedOperands()) {
1554 SmallString<256> Str;
1555 raw_svector_ostream OS(Str);
1556 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001557 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001558 if (i != 0)
1559 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001560 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001561 }
1562 OS << "]";
1563
Jim Grosbach4b905842013-09-20 23:08:21 +00001564 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001565 }
1566
Kevin Enderby6469fc22011-11-01 22:27:22 +00001567 // If we are generating dwarf for assembly source files and the current
1568 // section is the initial text section then generate a .loc directive for
1569 // the instruction.
1570 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001571 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001572 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001573
Eli Bendersky88024712013-01-16 19:32:36 +00001574 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001575
Eli Bendersky88024712013-01-16 19:32:36 +00001576 // If we previously parsed a cpp hash file line comment then make sure the
1577 // current Dwarf File is for the CppHashFilename if not then emit the
1578 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001579 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001580 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001581 if (CppHashFilename.size() != 0) {
1582 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001583 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001584 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001585 getContext().nextGenDwarfFileNumber(), StringRef(),
1586 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001587
Jim Grosbach4b905842013-09-20 23:08:21 +00001588 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1589 // cache with the different Loc from the call above we save the last
1590 // info we queried here with SrcMgr.FindLineNumber().
1591 unsigned CppHashLocLineNo;
1592 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1593 CppHashLocLineNo = LastQueryLine;
1594 else {
1595 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1596 LastQueryLine = CppHashLocLineNo;
1597 LastQueryIDLoc = CppHashLoc;
1598 LastQueryBuffer = CppHashBuf;
1599 }
1600 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001601 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001602
Jim Grosbach4b905842013-09-20 23:08:21 +00001603 getStreamer().EmitDwarfLocDirective(
1604 getContext().getGenDwarfFileNumber(), Line, 0,
1605 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1606 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001607 }
1608
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001609 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001610 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001611 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001612 HadError = getTargetParser().MatchAndEmitInstruction(
1613 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1614 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001615 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001616
Chris Lattnera2a9d162010-09-11 16:18:25 +00001617 // Don't skip the rest of the line, the instruction parser is responsible for
1618 // that.
1619 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001620}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001621
Jim Grosbach4b905842013-09-20 23:08:21 +00001622/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001623/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001624void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001625 if (!Lexer.is(AsmToken::EndOfStatement))
1626 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001627 // Eat EOL.
1628 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001629}
1630
Jim Grosbach4b905842013-09-20 23:08:21 +00001631/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001632/// ::= # number "filename"
1633/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001634bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001635 Lex(); // Eat the hash token.
1636
1637 if (getLexer().isNot(AsmToken::Integer)) {
1638 // Consume the line since in cases it is not a well-formed line directive,
1639 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001640 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001641 return false;
1642 }
1643
1644 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001645 Lex();
1646
1647 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001648 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001649 return false;
1650 }
1651
1652 StringRef Filename = getTok().getString();
1653 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001654 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001655
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001656 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1657 CppHashLoc = L;
1658 CppHashFilename = Filename;
1659 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001660 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001661
1662 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001663 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001664 return false;
1665}
1666
Jim Grosbach4b905842013-09-20 23:08:21 +00001667/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001668/// for the Filename and LineNo if any in the diagnostic.
1669void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001670 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001671 raw_ostream &OS = errs();
1672
1673 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1674 const SMLoc &DiagLoc = Diag.getLoc();
1675 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1676 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1677
Jim Grosbach4b905842013-09-20 23:08:21 +00001678 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001679 // before printing the message.
1680 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001681 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001682 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1683 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001684 }
1685
Eric Christophera7c32732012-12-18 00:30:54 +00001686 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001687 // manager changed or buffer changed (like in a nested include) then just
1688 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001689 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001690 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001691 if (Parser->SavedDiagHandler)
1692 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1693 else
1694 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001695 return;
1696 }
1697
Eric Christophera7c32732012-12-18 00:30:54 +00001698 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1700 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001701 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702
1703 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1704 int CppHashLocLineNo =
1705 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001706 int LineNo =
1707 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708
Jim Grosbach4b905842013-09-20 23:08:21 +00001709 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1710 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001711 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001712
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001713 if (Parser->SavedDiagHandler)
1714 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1715 else
1716 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001717}
1718
Rafael Espindola2c064482012-08-21 18:29:30 +00001719// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1720// difference being that that function accepts '@' as part of identifiers and
1721// we can't do that. AsmLexer.cpp should probably be changed to handle
1722// '@' as a special case when needed.
1723static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001724 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1725 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001726}
1727
Rafael Espindola34b9c512012-06-03 23:57:14 +00001728bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001729 ArrayRef<MCAsmMacroParameter> Parameters,
1730 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001731 unsigned NParameters = Parameters.size();
Benjamin Kramer513e7442014-02-20 13:36:32 +00001732 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001733 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001734
Preston Gurd05500642012-09-19 20:36:12 +00001735 // A macro without parameters is handled differently on Darwin:
1736 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001737 while (!Body.empty()) {
1738 // Scan for the next substitution.
1739 std::size_t End = Body.size(), Pos = 0;
1740 for (; Pos != End; ++Pos) {
1741 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001742 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001743 // This macro has no parameters, look for $0, $1, etc.
1744 if (Body[Pos] != '$' || Pos + 1 == End)
1745 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001746
Rafael Espindola1134ab232011-06-05 02:43:45 +00001747 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001748 if (Next == '$' || Next == 'n' ||
1749 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001750 break;
1751 } else {
1752 // This macro has parameters, look for \foo, \bar, etc.
1753 if (Body[Pos] == '\\' && Pos + 1 != End)
1754 break;
1755 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001756 }
1757
1758 // Add the prefix.
1759 OS << Body.slice(0, Pos);
1760
1761 // Check if we reached the end.
1762 if (Pos == End)
1763 break;
1764
Benjamin Kramer513e7442014-02-20 13:36:32 +00001765 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001766 switch (Body[Pos + 1]) {
1767 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001768 case '$':
1769 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001770 break;
1771
Jim Grosbach4b905842013-09-20 23:08:21 +00001772 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001773 case 'n':
1774 OS << A.size();
1775 break;
1776
Jim Grosbach4b905842013-09-20 23:08:21 +00001777 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001778 default: {
1779 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001780 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001781 if (Index >= A.size())
1782 break;
1783
1784 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001785 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001786 ie = A[Index].end();
1787 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001788 OS << it->getString();
1789 break;
1790 }
1791 }
1792 Pos += 2;
1793 } else {
1794 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001795 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001796 ++I;
1797
Jim Grosbach4b905842013-09-20 23:08:21 +00001798 const char *Begin = Body.data() + Pos + 1;
1799 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001800 unsigned Index = 0;
1801 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001802 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001803 break;
1804
Preston Gurd05500642012-09-19 20:36:12 +00001805 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001806 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1807 Pos += 3;
1808 else {
1809 OS << '\\' << Argument;
1810 Pos = I;
1811 }
Preston Gurd05500642012-09-19 20:36:12 +00001812 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001813 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001814 ie = A[Index].end();
1815 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001816 if (it->getKind() == AsmToken::String)
1817 OS << it->getStringContents();
1818 else
1819 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001820
Preston Gurd05500642012-09-19 20:36:12 +00001821 Pos += 1 + Argument.size();
1822 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001823 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001824 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001825 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001826 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001827
Rafael Espindola1134ab232011-06-05 02:43:45 +00001828 return false;
1829}
Daniel Dunbar43235712010-07-18 18:54:11 +00001830
Jim Grosbach4b905842013-09-20 23:08:21 +00001831MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1832 SMLoc EL, MemoryBuffer *I)
1833 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1834 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001835
Jim Grosbach4b905842013-09-20 23:08:21 +00001836static bool isOperator(AsmToken::TokenKind kind) {
1837 switch (kind) {
1838 default:
1839 return false;
1840 case AsmToken::Plus:
1841 case AsmToken::Minus:
1842 case AsmToken::Tilde:
1843 case AsmToken::Slash:
1844 case AsmToken::Star:
1845 case AsmToken::Dot:
1846 case AsmToken::Equal:
1847 case AsmToken::EqualEqual:
1848 case AsmToken::Pipe:
1849 case AsmToken::PipePipe:
1850 case AsmToken::Caret:
1851 case AsmToken::Amp:
1852 case AsmToken::AmpAmp:
1853 case AsmToken::Exclaim:
1854 case AsmToken::ExclaimEqual:
1855 case AsmToken::Percent:
1856 case AsmToken::Less:
1857 case AsmToken::LessEqual:
1858 case AsmToken::LessLess:
1859 case AsmToken::LessGreater:
1860 case AsmToken::Greater:
1861 case AsmToken::GreaterEqual:
1862 case AsmToken::GreaterGreater:
1863 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001864 }
1865}
1866
David Majnemer16252452014-01-29 00:07:39 +00001867namespace {
1868class AsmLexerSkipSpaceRAII {
1869public:
1870 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1871 Lexer.setSkipSpace(SkipSpace);
1872 }
1873
1874 ~AsmLexerSkipSpaceRAII() {
1875 Lexer.setSkipSpace(true);
1876 }
1877
1878private:
1879 AsmLexer &Lexer;
1880};
1881}
1882
David Majnemer91fc4c22014-01-29 18:57:46 +00001883bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001884 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001885 unsigned AddTokens = 0;
1886
David Majnemer16252452014-01-29 00:07:39 +00001887 // Darwin doesn't use spaces to delmit arguments.
1888 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001889
1890 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001891 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001892 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001893
David Majnemer91fc4c22014-01-29 18:57:46 +00001894 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001895 break;
Preston Gurd05500642012-09-19 20:36:12 +00001896
1897 if (Lexer.is(AsmToken::Space)) {
1898 Lex(); // Eat spaces
1899
1900 // Spaces can delimit parameters, but could also be part an expression.
1901 // If the token after a space is an operator, add the token and the next
1902 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001903 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001904 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001905 // Check to see whether the token is used as an operator,
1906 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001907 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001908 if (*NextChar == ' ')
1909 AddTokens = 2;
1910 }
1911
1912 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001913 break;
1914 }
1915 }
1916 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001917
Jim Grosbach4b905842013-09-20 23:08:21 +00001918 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001919 // to be able to fill in the remaining default parameter values
1920 if (Lexer.is(AsmToken::EndOfStatement))
1921 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001922
1923 // Adjust the current parentheses level.
1924 if (Lexer.is(AsmToken::LParen))
1925 ++ParenLevel;
1926 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1927 --ParenLevel;
1928
1929 // Append the token to the current argument list.
1930 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001931 if (AddTokens)
1932 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001933 Lex();
1934 }
Preston Gurd05500642012-09-19 20:36:12 +00001935
Rafael Espindola768b41c2012-06-15 14:02:34 +00001936 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001937 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001938 return false;
1939}
1940
1941// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001942bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001943 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001944 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001945 bool NamedParametersFound = false;
1946 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001947
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001948 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001949 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001950
Rafael Espindola768b41c2012-06-15 14:02:34 +00001951 // Parse two kinds of macro invocations:
1952 // - macros defined without any parameters accept an arbitrary number of them
1953 // - macros defined with parameters accept at most that many of them
1954 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1955 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001956 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001957 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001958
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001959 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001960 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001961 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001962 eatToEndOfStatement();
1963 return true;
1964 }
1965
1966 if (!Lexer.is(AsmToken::Equal)) {
1967 TokError("expected '=' after formal parameter identifier");
1968 eatToEndOfStatement();
1969 return true;
1970 }
1971 Lex();
1972
1973 NamedParametersFound = true;
1974 }
1975
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001976 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001977 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001978 eatToEndOfStatement();
1979 return true;
1980 }
1981
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001982 if (parseMacroArgument(FA.Value))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001983 return true;
1984
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001985 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001986 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001987 unsigned FAI = 0;
1988 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001989 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001990 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001991
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001992 if (FAI >= NParameters) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001993 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001994 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001995 M->Name + "'");
1996 return true;
1997 }
1998 PI = FAI;
1999 }
2000
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002001 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002002 if (A.size() <= PI)
2003 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002004 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002005
2006 if (FALocs.size() <= PI)
2007 FALocs.resize(PI + 1);
2008
2009 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002010 }
Jim Grosbach206661622012-07-30 22:44:17 +00002011
Preston Gurd242ed3152012-09-19 20:29:04 +00002012 // At the end of the statement, fill in remaining arguments that have
2013 // default values. If there aren't any, then the next argument is
2014 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002015 if (Lexer.is(AsmToken::EndOfStatement)) {
2016 bool Failure = false;
2017 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2018 if (A[FAI].empty()) {
2019 if (M->Parameters[FAI].Required) {
2020 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2021 "missing value for required parameter "
2022 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2023 Failure = true;
2024 }
2025
2026 if (!M->Parameters[FAI].Value.empty())
2027 A[FAI] = M->Parameters[FAI].Value;
2028 }
2029 }
2030 return Failure;
2031 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002032
2033 if (Lexer.is(AsmToken::Comma))
2034 Lex();
2035 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002036
2037 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002038}
2039
Jim Grosbach4b905842013-09-20 23:08:21 +00002040const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2041 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002042 return (I == MacroMap.end()) ? NULL : I->getValue();
2043}
2044
Jim Grosbach4b905842013-09-20 23:08:21 +00002045void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002046 MacroMap[Name] = new MCAsmMacro(Macro);
2047}
2048
Jim Grosbach4b905842013-09-20 23:08:21 +00002049void AsmParser::undefineMacro(StringRef Name) {
2050 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002051 if (I != MacroMap.end()) {
2052 delete I->getValue();
2053 MacroMap.erase(I);
2054 }
2055}
2056
Jim Grosbach4b905842013-09-20 23:08:21 +00002057bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002058 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2059 // this, although we should protect against infinite loops.
2060 if (ActiveMacros.size() == 20)
2061 return TokError("macros cannot be nested more than 20 levels deep");
2062
Eli Bendersky38274122013-01-14 23:22:36 +00002063 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002064 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002065 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002066
Rafael Espindola1134ab232011-06-05 02:43:45 +00002067 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2068 // to hold the macro body with substitutions.
2069 SmallString<256> Buf;
2070 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002071 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002072
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002073 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002074 return true;
2075
Eli Bendersky38274122013-01-14 23:22:36 +00002076 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002077 // instantiation.
2078 OS << ".endmacro\n";
2079
Rafael Espindola1134ab232011-06-05 02:43:45 +00002080 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002081 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002082
Daniel Dunbar43235712010-07-18 18:54:11 +00002083 // Create the macro instantiation object and add to the current macro
2084 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002085 MacroInstantiation *MI = new MacroInstantiation(
2086 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002087 ActiveMacros.push_back(MI);
2088
2089 // Jump to the macro instantiation and prime the lexer.
2090 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2091 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2092 Lex();
2093
2094 return false;
2095}
2096
Jim Grosbach4b905842013-09-20 23:08:21 +00002097void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002098 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002099 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002100 Lex();
2101
2102 // Pop the instantiation entry.
2103 delete ActiveMacros.back();
2104 ActiveMacros.pop_back();
2105}
2106
Jim Grosbach4b905842013-09-20 23:08:21 +00002107static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002108 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002109 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002110 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2111 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002112 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002113 case MCExpr::Target:
2114 case MCExpr::Constant:
2115 return false;
2116 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002117 const MCSymbol &S =
2118 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002119 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002120 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002121 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002122 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002123 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002124 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002125 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002126
2127 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002128}
2129
Jim Grosbach4b905842013-09-20 23:08:21 +00002130bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002131 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002132 // FIXME: Use better location, we should use proper tokens.
2133 SMLoc EqualLoc = Lexer.getLoc();
2134
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002135 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002136 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002137 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002138
Rafael Espindola72f5f172012-01-28 05:57:00 +00002139 // Note: we don't count b as used in "a = b". This is to allow
2140 // a = b
2141 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002142
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002143 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002144 return TokError("unexpected token in assignment");
2145
2146 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002147 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002148
Daniel Dunbar5f339242009-10-16 01:57:39 +00002149 // Validate that the LHS is allowed to be a variable (either it has not been
2150 // used as a symbol, or it is an absolute symbol).
2151 MCSymbol *Sym = getContext().LookupSymbol(Name);
2152 if (Sym) {
2153 // Diagnose assignment to a label.
2154 //
2155 // FIXME: Diagnostics. Note the location of the definition as a label.
2156 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002157 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002158 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2159 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002160 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002161 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2162 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002163 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002164 return Error(EqualLoc, "redefinition of '" + Name + "'");
2165 else if (!Sym->isVariable())
2166 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002167 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002168 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002169 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002170
2171 // Don't count these checks as uses.
2172 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002173 } else if (Name == ".") {
2174 if (Out.EmitValueToOffset(Value, 0)) {
2175 Error(EqualLoc, "expected absolute expression");
2176 eatToEndOfStatement();
2177 }
2178 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002179 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002180 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002181
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002182 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002183 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002184 if (NoDeadStrip)
2185 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2186
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002187 return false;
2188}
2189
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002190/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002191/// ::= identifier
2192/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002193bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002194 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002195 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2196 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002197 // handle this as a context dependent token, instead we detect adjacent tokens
2198 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002199 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2200 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002201
Hans Wennborgce69d772013-10-18 20:46:28 +00002202 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002203 Lex();
2204 if (Lexer.isNot(AsmToken::Identifier))
2205 return true;
2206
Hans Wennborgce69d772013-10-18 20:46:28 +00002207 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2208 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002209 return true;
2210
2211 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002212 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002213 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002214 Lex();
2215 return false;
2216 }
2217
Jim Grosbach4b905842013-09-20 23:08:21 +00002218 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002219 return true;
2220
Sean Callanan936b0d32010-01-19 21:44:56 +00002221 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002222
Sean Callanan686ed8d2010-01-19 20:22:31 +00002223 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002224
2225 return false;
2226}
2227
Jim Grosbach4b905842013-09-20 23:08:21 +00002228/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002229/// ::= .equ identifier ',' expression
2230/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002231/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002232bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002233 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002234
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002235 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002236 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002237
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002238 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002239 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002240 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002241
Jim Grosbach4b905842013-09-20 23:08:21 +00002242 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002243}
2244
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002245bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002246 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002247
2248 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002249 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002250 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2251 if (Str[i] != '\\') {
2252 Data += Str[i];
2253 continue;
2254 }
2255
2256 // Recognize escaped characters. Note that this escape semantics currently
2257 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2258 ++i;
2259 if (i == e)
2260 return TokError("unexpected backslash at end of string");
2261
2262 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002263 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002264 // Consume up to three octal characters.
2265 unsigned Value = Str[i] - '0';
2266
Jim Grosbach4b905842013-09-20 23:08:21 +00002267 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002268 ++i;
2269 Value = Value * 8 + (Str[i] - '0');
2270
Jim Grosbach4b905842013-09-20 23:08:21 +00002271 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002272 ++i;
2273 Value = Value * 8 + (Str[i] - '0');
2274 }
2275 }
2276
2277 if (Value > 255)
2278 return TokError("invalid octal escape sequence (out of range)");
2279
Jim Grosbach4b905842013-09-20 23:08:21 +00002280 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002281 continue;
2282 }
2283
2284 // Otherwise recognize individual escapes.
2285 switch (Str[i]) {
2286 default:
2287 // Just reject invalid escape sequences for now.
2288 return TokError("invalid escape sequence (unrecognized character)");
2289
2290 case 'b': Data += '\b'; break;
2291 case 'f': Data += '\f'; break;
2292 case 'n': Data += '\n'; break;
2293 case 'r': Data += '\r'; break;
2294 case 't': Data += '\t'; break;
2295 case '"': Data += '"'; break;
2296 case '\\': Data += '\\'; break;
2297 }
2298 }
2299
2300 return false;
2301}
2302
Jim Grosbach4b905842013-09-20 23:08:21 +00002303/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002304/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002305bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002306 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002307 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002308
Daniel Dunbara10e5192009-06-24 23:30:00 +00002309 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002310 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002311 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002312
Daniel Dunbaref668c12009-08-14 18:19:52 +00002313 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002314 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002315 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002316
Rafael Espindola64e1af82013-07-02 15:49:13 +00002317 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002318 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002319 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002320
Sean Callanan686ed8d2010-01-19 20:22:31 +00002321 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002322
2323 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002324 break;
2325
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002326 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002327 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002328 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002329 }
2330 }
2331
Sean Callanan686ed8d2010-01-19 20:22:31 +00002332 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002333 return false;
2334}
2335
Jim Grosbach4b905842013-09-20 23:08:21 +00002336/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002337/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002338bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002339 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002340 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002341
Daniel Dunbara10e5192009-06-24 23:30:00 +00002342 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002343 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002344 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002345 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002346 return true;
2347
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002348 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002349 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2350 assert(Size <= 8 && "Invalid size");
2351 uint64_t IntValue = MCE->getValue();
2352 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2353 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002354 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002355 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002356 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002357
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002358 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002359 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002360
Daniel Dunbara10e5192009-06-24 23:30:00 +00002361 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002362 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002363 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002364 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365 }
2366 }
2367
Sean Callanan686ed8d2010-01-19 20:22:31 +00002368 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002369 return false;
2370}
2371
David Woodhoused6de0d92014-02-01 16:20:59 +00002372/// ParseDirectiveOctaValue
2373/// ::= .octa [ hexconstant (, hexconstant)* ]
2374bool AsmParser::parseDirectiveOctaValue() {
2375 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2376 checkForValidSection();
2377
2378 for (;;) {
2379 if (Lexer.getKind() == AsmToken::Error)
2380 return true;
2381 if (Lexer.getKind() != AsmToken::Integer &&
2382 Lexer.getKind() != AsmToken::BigNum)
2383 return TokError("unknown token in expression");
2384
2385 SMLoc ExprLoc = getLexer().getLoc();
2386 APInt IntValue = getTok().getAPIntVal();
2387 Lex();
2388
2389 uint64_t hi, lo;
2390 if (IntValue.isIntN(64)) {
2391 hi = 0;
2392 lo = IntValue.getZExtValue();
2393 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002394 // It might actually have more than 128 bits, but the top ones are zero.
2395 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002396 lo = IntValue.getLoBits(64).getZExtValue();
2397 } else
2398 return Error(ExprLoc, "literal value out of range for directive");
2399
2400 if (MAI.isLittleEndian()) {
2401 getStreamer().EmitIntValue(lo, 8);
2402 getStreamer().EmitIntValue(hi, 8);
2403 } else {
2404 getStreamer().EmitIntValue(hi, 8);
2405 getStreamer().EmitIntValue(lo, 8);
2406 }
2407
2408 if (getLexer().is(AsmToken::EndOfStatement))
2409 break;
2410
2411 // FIXME: Improve diagnostic.
2412 if (getLexer().isNot(AsmToken::Comma))
2413 return TokError("unexpected token in directive");
2414 Lex();
2415 }
2416 }
2417
2418 Lex();
2419 return false;
2420}
2421
Jim Grosbach4b905842013-09-20 23:08:21 +00002422/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002423/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002424bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002425 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002426 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002427
2428 for (;;) {
2429 // We don't truly support arithmetic on floating point expressions, so we
2430 // have to manually parse unary prefixes.
2431 bool IsNeg = false;
2432 if (getLexer().is(AsmToken::Minus)) {
2433 Lex();
2434 IsNeg = true;
2435 } else if (getLexer().is(AsmToken::Plus))
2436 Lex();
2437
Michael J. Spencer530ce852010-10-09 11:00:50 +00002438 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002439 getLexer().isNot(AsmToken::Real) &&
2440 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002441 return TokError("unexpected token in directive");
2442
2443 // Convert to an APFloat.
2444 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002445 StringRef IDVal = getTok().getString();
2446 if (getLexer().is(AsmToken::Identifier)) {
2447 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2448 Value = APFloat::getInf(Semantics);
2449 else if (!IDVal.compare_lower("nan"))
2450 Value = APFloat::getNaN(Semantics, false, ~0);
2451 else
2452 return TokError("invalid floating point literal");
2453 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002454 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002455 return TokError("invalid floating point literal");
2456 if (IsNeg)
2457 Value.changeSign();
2458
2459 // Consume the numeric token.
2460 Lex();
2461
2462 // Emit the value as an integer.
2463 APInt AsInt = Value.bitcastToAPInt();
2464 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002465 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002466
2467 if (getLexer().is(AsmToken::EndOfStatement))
2468 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002469
Daniel Dunbar2af16532010-09-24 01:59:56 +00002470 if (getLexer().isNot(AsmToken::Comma))
2471 return TokError("unexpected token in directive");
2472 Lex();
2473 }
2474 }
2475
2476 Lex();
2477 return false;
2478}
2479
Jim Grosbach4b905842013-09-20 23:08:21 +00002480/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002481/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002482bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002483 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002484
2485 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002486 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002487 return true;
2488
Rafael Espindolab91bac62010-10-05 19:42:57 +00002489 int64_t Val = 0;
2490 if (getLexer().is(AsmToken::Comma)) {
2491 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002492 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002493 return true;
2494 }
2495
Rafael Espindola922e3f42010-09-16 15:03:59 +00002496 if (getLexer().isNot(AsmToken::EndOfStatement))
2497 return TokError("unexpected token in '.zero' directive");
2498
2499 Lex();
2500
Rafael Espindola64e1af82013-07-02 15:49:13 +00002501 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002502
2503 return false;
2504}
2505
Jim Grosbach4b905842013-09-20 23:08:21 +00002506/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002507/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002508bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002509 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002510
David Majnemer522d3db2014-02-01 07:19:38 +00002511 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002512 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002513 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002514 return true;
2515
David Majnemer522d3db2014-02-01 07:19:38 +00002516 if (NumValues < 0) {
2517 Warning(RepeatLoc,
2518 "'.fill' directive with negative repeat count has no effect");
2519 NumValues = 0;
2520 }
2521
Roman Divackye33098f2013-09-24 17:44:41 +00002522 int64_t FillSize = 1;
2523 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002524
David Majnemer522d3db2014-02-01 07:19:38 +00002525 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002526 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2527 if (getLexer().isNot(AsmToken::Comma))
2528 return TokError("unexpected token in '.fill' directive");
2529 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002530
David Majnemer522d3db2014-02-01 07:19:38 +00002531 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002532 if (parseAbsoluteExpression(FillSize))
2533 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002534
Roman Divackye33098f2013-09-24 17:44:41 +00002535 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2536 if (getLexer().isNot(AsmToken::Comma))
2537 return TokError("unexpected token in '.fill' directive");
2538 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002539
David Majnemer522d3db2014-02-01 07:19:38 +00002540 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002541 if (parseAbsoluteExpression(FillExpr))
2542 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002543
Roman Divackye33098f2013-09-24 17:44:41 +00002544 if (getLexer().isNot(AsmToken::EndOfStatement))
2545 return TokError("unexpected token in '.fill' directive");
2546
2547 Lex();
2548 }
2549 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002550
David Majnemer522d3db2014-02-01 07:19:38 +00002551 if (FillSize < 0) {
2552 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2553 NumValues = 0;
2554 }
2555 if (FillSize > 8) {
2556 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2557 FillSize = 8;
2558 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002559
David Majnemer522d3db2014-02-01 07:19:38 +00002560 if (!isUInt<32>(FillExpr) && FillSize > 4)
2561 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2562
2563 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2564 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2565
2566 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2567 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2568 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2569 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002570
2571 return false;
2572}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002573
Jim Grosbach4b905842013-09-20 23:08:21 +00002574/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002575/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002576bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002577 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002578
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002579 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002580 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002581 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002582 return true;
2583
2584 // Parse optional fill expression.
2585 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002586 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2587 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002588 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002589 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002590
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002591 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002592 return true;
2593
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002594 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002595 return TokError("unexpected token in '.org' directive");
2596 }
2597
Sean Callanan686ed8d2010-01-19 20:22:31 +00002598 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002599
Jim Grosbachb5912772012-01-27 00:37:08 +00002600 // Only limited forms of relocatable expressions are accepted here, it
2601 // has to be relative to the current section. The streamer will return
2602 // 'true' if the expression wasn't evaluatable.
2603 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2604 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002605
2606 return false;
2607}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002608
Jim Grosbach4b905842013-09-20 23:08:21 +00002609/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002610/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002611bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002612 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002613
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002614 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002615 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002616 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002617 return true;
2618
2619 SMLoc MaxBytesLoc;
2620 bool HasFillExpr = false;
2621 int64_t FillExpr = 0;
2622 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002623 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2624 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002625 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002626 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002627
2628 // The fill expression can be omitted while specifying a maximum number of
2629 // alignment bytes, e.g:
2630 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002631 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002632 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002633 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002634 return true;
2635 }
2636
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2638 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002639 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002640 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002641
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002642 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002643 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002644 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002645
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002646 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002647 return TokError("unexpected token in directive");
2648 }
2649 }
2650
Sean Callanan686ed8d2010-01-19 20:22:31 +00002651 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002652
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002653 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002654 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002655
2656 // Compute alignment in bytes.
2657 if (IsPow2) {
2658 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002659 if (Alignment >= 32) {
2660 Error(AlignmentLoc, "invalid alignment value");
2661 Alignment = 31;
2662 }
2663
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002664 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002665 } else {
2666 // Reject alignments that aren't a power of two, for gas compatibility.
2667 if (!isPowerOf2_64(Alignment))
2668 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002669 }
2670
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002671 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002672 if (MaxBytesLoc.isValid()) {
2673 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002674 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002675 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002676 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002677 }
2678
2679 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002680 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002681 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002682 MaxBytesToFill = 0;
2683 }
2684 }
2685
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002686 // Check whether we should use optimal code alignment for this .align
2687 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002688 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002689 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2690 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002691 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002692 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002693 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002694 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2695 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002696 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002697
2698 return false;
2699}
2700
Jim Grosbach4b905842013-09-20 23:08:21 +00002701/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002702/// ::= .file [number] filename
2703/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002704bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002705 // FIXME: I'm not sure what this is.
2706 int64_t FileNumber = -1;
2707 SMLoc FileNumberLoc = getLexer().getLoc();
2708 if (getLexer().is(AsmToken::Integer)) {
2709 FileNumber = getTok().getIntVal();
2710 Lex();
2711
2712 if (FileNumber < 1)
2713 return TokError("file number less than one");
2714 }
2715
2716 if (getLexer().isNot(AsmToken::String))
2717 return TokError("unexpected token in '.file' directive");
2718
2719 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002720 // Allow the strings to have escaped octal character sequence.
2721 std::string Path = getTok().getString();
2722 if (parseEscapedString(Path))
2723 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002724 Lex();
2725
2726 StringRef Directory;
2727 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002728 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002729 if (getLexer().is(AsmToken::String)) {
2730 if (FileNumber == -1)
2731 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002732 if (parseEscapedString(FilenameData))
2733 return true;
2734 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002735 Directory = Path;
2736 Lex();
2737 } else {
2738 Filename = Path;
2739 }
2740
2741 if (getLexer().isNot(AsmToken::EndOfStatement))
2742 return TokError("unexpected token in '.file' directive");
2743
2744 if (FileNumber == -1)
2745 getStreamer().EmitFileDirective(Filename);
2746 else {
2747 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002748 Error(DirectiveLoc,
2749 "input can't have .file dwarf directives when -g is "
2750 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002751
2752 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2753 Error(FileNumberLoc, "file number already allocated");
2754 }
2755
2756 return false;
2757}
2758
Jim Grosbach4b905842013-09-20 23:08:21 +00002759/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002760/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002761bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002762 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2763 if (getLexer().isNot(AsmToken::Integer))
2764 return TokError("unexpected token in '.line' directive");
2765
2766 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002767 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002768 Lex();
2769
2770 // FIXME: Do something with the .line.
2771 }
2772
2773 if (getLexer().isNot(AsmToken::EndOfStatement))
2774 return TokError("unexpected token in '.line' directive");
2775
2776 return false;
2777}
2778
Jim Grosbach4b905842013-09-20 23:08:21 +00002779/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002780/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2781/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2782/// The first number is a file number, must have been previously assigned with
2783/// a .file directive, the second number is the line number and optionally the
2784/// third number is a column position (zero if not specified). The remaining
2785/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002786bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002787 if (getLexer().isNot(AsmToken::Integer))
2788 return TokError("unexpected token in '.loc' directive");
2789 int64_t FileNumber = getTok().getIntVal();
2790 if (FileNumber < 1)
2791 return TokError("file number less than one in '.loc' directive");
2792 if (!getContext().isValidDwarfFileNumber(FileNumber))
2793 return TokError("unassigned file number in '.loc' directive");
2794 Lex();
2795
2796 int64_t LineNumber = 0;
2797 if (getLexer().is(AsmToken::Integer)) {
2798 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002799 if (LineNumber < 0)
2800 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002801 Lex();
2802 }
2803
2804 int64_t ColumnPos = 0;
2805 if (getLexer().is(AsmToken::Integer)) {
2806 ColumnPos = getTok().getIntVal();
2807 if (ColumnPos < 0)
2808 return TokError("column position less than zero in '.loc' directive");
2809 Lex();
2810 }
2811
2812 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2813 unsigned Isa = 0;
2814 int64_t Discriminator = 0;
2815 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2816 for (;;) {
2817 if (getLexer().is(AsmToken::EndOfStatement))
2818 break;
2819
2820 StringRef Name;
2821 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002822 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002823 return TokError("unexpected token in '.loc' directive");
2824
2825 if (Name == "basic_block")
2826 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2827 else if (Name == "prologue_end")
2828 Flags |= DWARF2_FLAG_PROLOGUE_END;
2829 else if (Name == "epilogue_begin")
2830 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2831 else if (Name == "is_stmt") {
2832 Loc = getTok().getLoc();
2833 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002834 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002835 return true;
2836 // The expression must be the constant 0 or 1.
2837 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2838 int Value = MCE->getValue();
2839 if (Value == 0)
2840 Flags &= ~DWARF2_FLAG_IS_STMT;
2841 else if (Value == 1)
2842 Flags |= DWARF2_FLAG_IS_STMT;
2843 else
2844 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002845 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002846 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2847 }
Craig Topperf15655b2013-04-22 04:22:40 +00002848 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002849 Loc = getTok().getLoc();
2850 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002851 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002852 return true;
2853 // The expression must be a constant greater or equal to 0.
2854 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2855 int Value = MCE->getValue();
2856 if (Value < 0)
2857 return Error(Loc, "isa number less than zero");
2858 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002859 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002860 return Error(Loc, "isa number not a constant value");
2861 }
Craig Topperf15655b2013-04-22 04:22:40 +00002862 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002863 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002864 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002865 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002866 return Error(Loc, "unknown sub-directive in '.loc' directive");
2867 }
2868
2869 if (getLexer().is(AsmToken::EndOfStatement))
2870 break;
2871 }
2872 }
2873
2874 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2875 Isa, Discriminator, StringRef());
2876
2877 return false;
2878}
2879
Jim Grosbach4b905842013-09-20 23:08:21 +00002880/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002881/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002882bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002883 return TokError("unsupported directive '.stabs'");
2884}
2885
Jim Grosbach4b905842013-09-20 23:08:21 +00002886/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002887/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002888bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002889 StringRef Name;
2890 bool EH = false;
2891 bool Debug = false;
2892
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002893 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002894 return TokError("Expected an identifier");
2895
2896 if (Name == ".eh_frame")
2897 EH = true;
2898 else if (Name == ".debug_frame")
2899 Debug = true;
2900
2901 if (getLexer().is(AsmToken::Comma)) {
2902 Lex();
2903
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002904 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002905 return TokError("Expected an identifier");
2906
2907 if (Name == ".eh_frame")
2908 EH = true;
2909 else if (Name == ".debug_frame")
2910 Debug = true;
2911 }
2912
2913 getStreamer().EmitCFISections(EH, Debug);
2914 return false;
2915}
2916
Jim Grosbach4b905842013-09-20 23:08:21 +00002917/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002918/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002919bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002920 StringRef Simple;
2921 if (getLexer().isNot(AsmToken::EndOfStatement))
2922 if (parseIdentifier(Simple) || Simple != "simple")
2923 return TokError("unexpected token in .cfi_startproc directive");
2924
2925 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002926 return false;
2927}
2928
Jim Grosbach4b905842013-09-20 23:08:21 +00002929/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002930/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002931bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002932 getStreamer().EmitCFIEndProc();
2933 return false;
2934}
2935
Jim Grosbach4b905842013-09-20 23:08:21 +00002936/// \brief parse register name or number.
2937bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002938 SMLoc DirectiveLoc) {
2939 unsigned RegNo;
2940
2941 if (getLexer().isNot(AsmToken::Integer)) {
2942 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2943 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002944 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002945 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002946 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002947
2948 return false;
2949}
2950
Jim Grosbach4b905842013-09-20 23:08:21 +00002951/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002952/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002953bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002954 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002955 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002956 return true;
2957
2958 if (getLexer().isNot(AsmToken::Comma))
2959 return TokError("unexpected token in directive");
2960 Lex();
2961
2962 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002963 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002964 return true;
2965
2966 getStreamer().EmitCFIDefCfa(Register, Offset);
2967 return false;
2968}
2969
Jim Grosbach4b905842013-09-20 23:08:21 +00002970/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002971/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002972bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002973 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002974 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002975 return true;
2976
2977 getStreamer().EmitCFIDefCfaOffset(Offset);
2978 return false;
2979}
2980
Jim Grosbach4b905842013-09-20 23:08:21 +00002981/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002982/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002983bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002984 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002985 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002986 return true;
2987
2988 if (getLexer().isNot(AsmToken::Comma))
2989 return TokError("unexpected token in directive");
2990 Lex();
2991
2992 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002993 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002994 return true;
2995
2996 getStreamer().EmitCFIRegister(Register1, Register2);
2997 return false;
2998}
2999
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003000/// parseDirectiveCFIWindowSave
3001/// ::= .cfi_window_save
3002bool AsmParser::parseDirectiveCFIWindowSave() {
3003 getStreamer().EmitCFIWindowSave();
3004 return false;
3005}
3006
Jim Grosbach4b905842013-09-20 23:08:21 +00003007/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003008/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003009bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003010 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003011 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003012 return true;
3013
3014 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3015 return false;
3016}
3017
Jim Grosbach4b905842013-09-20 23:08:21 +00003018/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003019/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003020bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003021 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003022 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003023 return true;
3024
3025 getStreamer().EmitCFIDefCfaRegister(Register);
3026 return false;
3027}
3028
Jim Grosbach4b905842013-09-20 23:08:21 +00003029/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003030/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003031bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003032 int64_t Register = 0;
3033 int64_t Offset = 0;
3034
Jim Grosbach4b905842013-09-20 23:08:21 +00003035 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003036 return true;
3037
3038 if (getLexer().isNot(AsmToken::Comma))
3039 return TokError("unexpected token in directive");
3040 Lex();
3041
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003042 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003043 return true;
3044
3045 getStreamer().EmitCFIOffset(Register, Offset);
3046 return false;
3047}
3048
Jim Grosbach4b905842013-09-20 23:08:21 +00003049/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003050/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003051bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003052 int64_t Register = 0;
3053
Jim Grosbach4b905842013-09-20 23:08:21 +00003054 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003055 return true;
3056
3057 if (getLexer().isNot(AsmToken::Comma))
3058 return TokError("unexpected token in directive");
3059 Lex();
3060
3061 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003062 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003063 return true;
3064
3065 getStreamer().EmitCFIRelOffset(Register, Offset);
3066 return false;
3067}
3068
3069static bool isValidEncoding(int64_t Encoding) {
3070 if (Encoding & ~0xff)
3071 return false;
3072
3073 if (Encoding == dwarf::DW_EH_PE_omit)
3074 return true;
3075
3076 const unsigned Format = Encoding & 0xf;
3077 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3078 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3079 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3080 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3081 return false;
3082
3083 const unsigned Application = Encoding & 0x70;
3084 if (Application != dwarf::DW_EH_PE_absptr &&
3085 Application != dwarf::DW_EH_PE_pcrel)
3086 return false;
3087
3088 return true;
3089}
3090
Jim Grosbach4b905842013-09-20 23:08:21 +00003091/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003092/// IsPersonality true for cfi_personality, false for cfi_lsda
3093/// ::= .cfi_personality encoding, [symbol_name]
3094/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003095bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003096 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003097 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003098 return true;
3099 if (Encoding == dwarf::DW_EH_PE_omit)
3100 return false;
3101
3102 if (!isValidEncoding(Encoding))
3103 return TokError("unsupported encoding.");
3104
3105 if (getLexer().isNot(AsmToken::Comma))
3106 return TokError("unexpected token in directive");
3107 Lex();
3108
3109 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003110 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003111 return TokError("expected identifier in directive");
3112
3113 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3114
3115 if (IsPersonality)
3116 getStreamer().EmitCFIPersonality(Sym, Encoding);
3117 else
3118 getStreamer().EmitCFILsda(Sym, Encoding);
3119 return false;
3120}
3121
Jim Grosbach4b905842013-09-20 23:08:21 +00003122/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003123/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003124bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003125 getStreamer().EmitCFIRememberState();
3126 return false;
3127}
3128
Jim Grosbach4b905842013-09-20 23:08:21 +00003129/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003130/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003131bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003132 getStreamer().EmitCFIRestoreState();
3133 return false;
3134}
3135
Jim Grosbach4b905842013-09-20 23:08:21 +00003136/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003137/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003138bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003139 int64_t Register = 0;
3140
Jim Grosbach4b905842013-09-20 23:08:21 +00003141 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003142 return true;
3143
3144 getStreamer().EmitCFISameValue(Register);
3145 return false;
3146}
3147
Jim Grosbach4b905842013-09-20 23:08:21 +00003148/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003149/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003150bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003151 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003152 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003153 return true;
3154
3155 getStreamer().EmitCFIRestore(Register);
3156 return false;
3157}
3158
Jim Grosbach4b905842013-09-20 23:08:21 +00003159/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003160/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003161bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003162 std::string Values;
3163 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003164 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003165 return true;
3166
3167 Values.push_back((uint8_t)CurrValue);
3168
3169 while (getLexer().is(AsmToken::Comma)) {
3170 Lex();
3171
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003172 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003173 return true;
3174
3175 Values.push_back((uint8_t)CurrValue);
3176 }
3177
3178 getStreamer().EmitCFIEscape(Values);
3179 return false;
3180}
3181
Jim Grosbach4b905842013-09-20 23:08:21 +00003182/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003183/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003184bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003185 if (getLexer().isNot(AsmToken::EndOfStatement))
3186 return Error(getLexer().getLoc(),
3187 "unexpected token in '.cfi_signal_frame'");
3188
3189 getStreamer().EmitCFISignalFrame();
3190 return false;
3191}
3192
Jim Grosbach4b905842013-09-20 23:08:21 +00003193/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003194/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003195bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003196 int64_t Register = 0;
3197
Jim Grosbach4b905842013-09-20 23:08:21 +00003198 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003199 return true;
3200
3201 getStreamer().EmitCFIUndefined(Register);
3202 return false;
3203}
3204
Jim Grosbach4b905842013-09-20 23:08:21 +00003205/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003206/// ::= .macros_on
3207/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003208bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003209 if (getLexer().isNot(AsmToken::EndOfStatement))
3210 return Error(getLexer().getLoc(),
3211 "unexpected token in '" + Directive + "' directive");
3212
Jim Grosbach4b905842013-09-20 23:08:21 +00003213 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003214 return false;
3215}
3216
Jim Grosbach4b905842013-09-20 23:08:21 +00003217/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003218/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003219bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003220 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003221 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003222 return TokError("expected identifier in '.macro' directive");
3223
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003224 if (getLexer().is(AsmToken::Comma))
3225 Lex();
3226
Eli Bendersky17233942013-01-15 22:59:42 +00003227 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003228 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3229 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003230 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003231 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003232
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003233 if (Lexer.is(AsmToken::Colon)) {
3234 Lex(); // consume ':'
3235
3236 SMLoc QualLoc;
3237 StringRef Qualifier;
3238
3239 QualLoc = Lexer.getLoc();
3240 if (parseIdentifier(Qualifier))
3241 return Error(QualLoc, "missing parameter qualifier for "
3242 "'" + Parameter.Name + "' in macro '" + Name + "'");
3243
3244 if (Qualifier == "req")
3245 Parameter.Required = true;
3246 else
3247 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3248 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3249 }
3250
David Majnemer91fc4c22014-01-29 18:57:46 +00003251 if (getLexer().is(AsmToken::Equal)) {
3252 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003253
3254 SMLoc ParamLoc;
3255
3256 ParamLoc = Lexer.getLoc();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003257 if (parseMacroArgument(Parameter.Value))
David Majnemer91fc4c22014-01-29 18:57:46 +00003258 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003259
3260 if (Parameter.Required)
3261 Warning(ParamLoc, "pointless default value for required parameter "
3262 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003263 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003264
3265 Parameters.push_back(Parameter);
3266
3267 if (getLexer().is(AsmToken::Comma))
3268 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003269 }
3270
3271 // Eat the end of statement.
3272 Lex();
3273
3274 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003275 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003276
3277 // Lex the macro definition.
3278 for (;;) {
3279 // Check whether we have reached the end of the file.
3280 if (getLexer().is(AsmToken::Eof))
3281 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3282
3283 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003284 if (getLexer().is(AsmToken::Identifier)) {
3285 if (getTok().getIdentifier() == ".endm" ||
3286 getTok().getIdentifier() == ".endmacro") {
3287 if (MacroDepth == 0) { // Outermost macro.
3288 EndToken = getTok();
3289 Lex();
3290 if (getLexer().isNot(AsmToken::EndOfStatement))
3291 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3292 "' directive");
3293 break;
3294 } else {
3295 // Otherwise we just found the end of an inner macro.
3296 --MacroDepth;
3297 }
3298 } else if (getTok().getIdentifier() == ".macro") {
3299 // We allow nested macros. Those aren't instantiated until the outermost
3300 // macro is expanded so just ignore them for now.
3301 ++MacroDepth;
3302 }
Eli Bendersky17233942013-01-15 22:59:42 +00003303 }
3304
3305 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003306 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003307 }
3308
Jim Grosbach4b905842013-09-20 23:08:21 +00003309 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003310 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3311 }
3312
3313 const char *BodyStart = StartToken.getLoc().getPointer();
3314 const char *BodyEnd = EndToken.getLoc().getPointer();
3315 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003316 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3317 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003318 return false;
3319}
3320
Jim Grosbach4b905842013-09-20 23:08:21 +00003321/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003322///
3323/// With the support added for named parameters there may be code out there that
3324/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003325/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003326/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003327/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003328/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3329/// warning that the positional parameter found in body which have no effect.
3330/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003331/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003332/// intended or change the macro to use the named parameters. It is possible
3333/// this warning will trigger when the none of the named parameters are used
3334/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003335void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003336 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003337 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003338 // If this macro is not defined with named parameters the warning we are
3339 // checking for here doesn't apply.
3340 unsigned NParameters = Parameters.size();
3341 if (NParameters == 0)
3342 return;
3343
3344 bool NamedParametersFound = false;
3345 bool PositionalParametersFound = false;
3346
3347 // Look at the body of the macro for use of both the named parameters and what
3348 // are likely to be positional parameters. This is what expandMacro() is
3349 // doing when it finds the parameters in the body.
3350 while (!Body.empty()) {
3351 // Scan for the next possible parameter.
3352 std::size_t End = Body.size(), Pos = 0;
3353 for (; Pos != End; ++Pos) {
3354 // Check for a substitution or escape.
3355 // This macro is defined with parameters, look for \foo, \bar, etc.
3356 if (Body[Pos] == '\\' && Pos + 1 != End)
3357 break;
3358
3359 // This macro should have parameters, but look for $0, $1, ..., $n too.
3360 if (Body[Pos] != '$' || Pos + 1 == End)
3361 continue;
3362 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003363 if (Next == '$' || Next == 'n' ||
3364 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003365 break;
3366 }
3367
3368 // Check if we reached the end.
3369 if (Pos == End)
3370 break;
3371
3372 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003373 switch (Body[Pos + 1]) {
3374 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003375 case '$':
3376 break;
3377
Jim Grosbach4b905842013-09-20 23:08:21 +00003378 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003379 case 'n':
3380 PositionalParametersFound = true;
3381 break;
3382
Jim Grosbach4b905842013-09-20 23:08:21 +00003383 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003384 default: {
3385 PositionalParametersFound = true;
3386 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003387 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003388 }
3389 Pos += 2;
3390 } else {
3391 unsigned I = Pos + 1;
3392 while (isIdentifierChar(Body[I]) && I + 1 != End)
3393 ++I;
3394
Jim Grosbach4b905842013-09-20 23:08:21 +00003395 const char *Begin = Body.data() + Pos + 1;
3396 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003397 unsigned Index = 0;
3398 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003399 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003400 break;
3401
3402 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003403 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3404 Pos += 3;
3405 else {
3406 Pos = I;
3407 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003408 } else {
3409 NamedParametersFound = true;
3410 Pos += 1 + Argument.size();
3411 }
3412 }
3413 // Update the scan point.
3414 Body = Body.substr(Pos);
3415 }
3416
3417 if (!NamedParametersFound && PositionalParametersFound)
3418 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3419 "used in macro body, possible positional parameter "
3420 "found in body which will have no effect");
3421}
3422
Jim Grosbach4b905842013-09-20 23:08:21 +00003423/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003424/// ::= .endm
3425/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003426bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003427 if (getLexer().isNot(AsmToken::EndOfStatement))
3428 return TokError("unexpected token in '" + Directive + "' directive");
3429
3430 // If we are inside a macro instantiation, terminate the current
3431 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003432 if (isInsideMacroInstantiation()) {
3433 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003434 return false;
3435 }
3436
3437 // Otherwise, this .endmacro is a stray entry in the file; well formed
3438 // .endmacro directives are handled during the macro definition parsing.
3439 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003440 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003441}
3442
Jim Grosbach4b905842013-09-20 23:08:21 +00003443/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003444/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003445bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003446 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003447 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003448 return TokError("expected identifier in '.purgem' directive");
3449
3450 if (getLexer().isNot(AsmToken::EndOfStatement))
3451 return TokError("unexpected token in '.purgem' directive");
3452
Jim Grosbach4b905842013-09-20 23:08:21 +00003453 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003454 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3455
Jim Grosbach4b905842013-09-20 23:08:21 +00003456 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003457 return false;
3458}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003459
Jim Grosbach4b905842013-09-20 23:08:21 +00003460/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003461/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003462bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003463 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003464
3465 // Expect a single argument: an expression that evaluates to a constant
3466 // in the inclusive range 0-30.
3467 SMLoc ExprLoc = getLexer().getLoc();
3468 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003469 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003470 return true;
3471 else if (getLexer().isNot(AsmToken::EndOfStatement))
3472 return TokError("unexpected token after expression in"
3473 " '.bundle_align_mode' directive");
3474 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3475 return Error(ExprLoc,
3476 "invalid bundle alignment size (expected between 0 and 30)");
3477
3478 Lex();
3479
3480 // Because of AlignSizePow2's verified range we can safely truncate it to
3481 // unsigned.
3482 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3483 return false;
3484}
3485
Jim Grosbach4b905842013-09-20 23:08:21 +00003486/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003487/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003488bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003489 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003490 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003491
Eli Bendersky802b6282013-01-07 21:51:08 +00003492 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3493 StringRef Option;
3494 SMLoc Loc = getTok().getLoc();
3495 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003496 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003497
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003498 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003499 return Error(Loc, kInvalidOptionError);
3500
3501 if (Option != "align_to_end")
3502 return Error(Loc, kInvalidOptionError);
3503 else if (getLexer().isNot(AsmToken::EndOfStatement))
3504 return Error(Loc,
3505 "unexpected token after '.bundle_lock' directive option");
3506 AlignToEnd = true;
3507 }
3508
Eli Benderskyf483ff92012-12-20 19:05:53 +00003509 Lex();
3510
Eli Bendersky802b6282013-01-07 21:51:08 +00003511 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003512 return false;
3513}
3514
Jim Grosbach4b905842013-09-20 23:08:21 +00003515/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003516/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003517bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003518 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003519
3520 if (getLexer().isNot(AsmToken::EndOfStatement))
3521 return TokError("unexpected token in '.bundle_unlock' directive");
3522 Lex();
3523
3524 getStreamer().EmitBundleUnlock();
3525 return false;
3526}
3527
Jim Grosbach4b905842013-09-20 23:08:21 +00003528/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003529/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003530bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003531 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003532
3533 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003534 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003535 return true;
3536
3537 int64_t FillExpr = 0;
3538 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3539 if (getLexer().isNot(AsmToken::Comma))
3540 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3541 Lex();
3542
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003543 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003544 return true;
3545
3546 if (getLexer().isNot(AsmToken::EndOfStatement))
3547 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3548 }
3549
3550 Lex();
3551
3552 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003553 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3554 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003555
3556 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003557 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003558
3559 return false;
3560}
3561
Jim Grosbach4b905842013-09-20 23:08:21 +00003562/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003563/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003564bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003565 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003566 const MCExpr *Value;
3567
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003568 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003569 return true;
3570
3571 if (getLexer().isNot(AsmToken::EndOfStatement))
3572 return TokError("unexpected token in directive");
3573
3574 if (Signed)
3575 getStreamer().EmitSLEB128Value(Value);
3576 else
3577 getStreamer().EmitULEB128Value(Value);
3578
3579 return false;
3580}
3581
Jim Grosbach4b905842013-09-20 23:08:21 +00003582/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003583/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003584bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003585 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003586 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003587 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003588 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003589
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003590 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003591 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003592
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003593 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003594
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003595 // Assembler local symbols don't make any sense here. Complain loudly.
3596 if (Sym->isTemporary())
3597 return Error(Loc, "non-local symbol required in directive");
3598
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003599 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3600 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003601
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003602 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003603 break;
3604
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003605 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003606 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003607 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003608 }
3609 }
3610
Sean Callanan686ed8d2010-01-19 20:22:31 +00003611 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003612 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003613}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003614
Jim Grosbach4b905842013-09-20 23:08:21 +00003615/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003616/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003617bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003618 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003619
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003620 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003621 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003622 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003623 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003624
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003625 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003626 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003627
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003628 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003629 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003630 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003631
3632 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003633 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003634 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003635 return true;
3636
3637 int64_t Pow2Alignment = 0;
3638 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003639 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003640 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003641 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003642 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003643 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003644
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003645 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3646 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003647 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3648
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003649 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003650 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3651 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003652 if (!isPowerOf2_64(Pow2Alignment))
3653 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3654 Pow2Alignment = Log2_64(Pow2Alignment);
3655 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003656 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003657
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003658 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003659 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003660
Sean Callanan686ed8d2010-01-19 20:22:31 +00003661 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003662
Chris Lattner28ad7542009-07-09 17:25:12 +00003663 // NOTE: a size of zero for a .comm should create a undefined symbol
3664 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003665 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003666 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003667 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003668
Eric Christopherbc818852010-05-14 01:38:54 +00003669 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003670 // may internally end up wanting an alignment in bytes.
3671 // FIXME: Diagnose overflow.
3672 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003673 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003674 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003675
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003676 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003677 return Error(IDLoc, "invalid symbol redefinition");
3678
Chris Lattner28ad7542009-07-09 17:25:12 +00003679 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003680 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003681 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003682 return false;
3683 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003684
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003685 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003686 return false;
3687}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003688
Jim Grosbach4b905842013-09-20 23:08:21 +00003689/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003690/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003691bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003692 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003693 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003694
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003695 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003696 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003697 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003698
Sean Callanan686ed8d2010-01-19 20:22:31 +00003699 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003700
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003701 if (Str.empty())
3702 Error(Loc, ".abort detected. Assembly stopping.");
3703 else
3704 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003705 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003706
3707 return false;
3708}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003709
Jim Grosbach4b905842013-09-20 23:08:21 +00003710/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003711/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003712bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003713 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003714 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003715
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003716 // Allow the strings to have escaped octal character sequence.
3717 std::string Filename;
3718 if (parseEscapedString(Filename))
3719 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003720 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003721 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003722
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003723 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003724 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003725
Chris Lattner693fbb82009-07-16 06:14:39 +00003726 // Attempt to switch the lexer to the included file before consuming the end
3727 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003728 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003729 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003730 return true;
3731 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003732
3733 return false;
3734}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003735
Jim Grosbach4b905842013-09-20 23:08:21 +00003736/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003737/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003738bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003739 if (getLexer().isNot(AsmToken::String))
3740 return TokError("expected string in '.incbin' directive");
3741
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003742 // Allow the strings to have escaped octal character sequence.
3743 std::string Filename;
3744 if (parseEscapedString(Filename))
3745 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003746 SMLoc IncbinLoc = getLexer().getLoc();
3747 Lex();
3748
3749 if (getLexer().isNot(AsmToken::EndOfStatement))
3750 return TokError("unexpected token in '.incbin' directive");
3751
Kevin Enderby109f25c2011-12-14 21:47:48 +00003752 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003753 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003754 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3755 return true;
3756 }
3757
3758 return false;
3759}
3760
Jim Grosbach4b905842013-09-20 23:08:21 +00003761/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003762/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003763bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003764 TheCondStack.push_back(TheCondState);
3765 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003766 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003767 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003768 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003769 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003770 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003771 return true;
3772
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003773 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003774 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003775
Sean Callanan686ed8d2010-01-19 20:22:31 +00003776 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003777
3778 TheCondState.CondMet = ExprValue;
3779 TheCondState.Ignore = !TheCondState.CondMet;
3780 }
3781
3782 return false;
3783}
3784
Jim Grosbach4b905842013-09-20 23:08:21 +00003785/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003786/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003787bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003788 TheCondStack.push_back(TheCondState);
3789 TheCondState.TheCond = AsmCond::IfCond;
3790
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003791 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003792 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003793 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003794 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003795
3796 if (getLexer().isNot(AsmToken::EndOfStatement))
3797 return TokError("unexpected token in '.ifb' directive");
3798
3799 Lex();
3800
3801 TheCondState.CondMet = ExpectBlank == Str.empty();
3802 TheCondState.Ignore = !TheCondState.CondMet;
3803 }
3804
3805 return false;
3806}
3807
Jim Grosbach4b905842013-09-20 23:08:21 +00003808/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003809/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003810bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003811 TheCondStack.push_back(TheCondState);
3812 TheCondState.TheCond = AsmCond::IfCond;
3813
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003814 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003815 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003816 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003817 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003818
3819 if (getLexer().isNot(AsmToken::Comma))
3820 return TokError("unexpected token in '.ifc' directive");
3821
3822 Lex();
3823
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003824 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003825
3826 if (getLexer().isNot(AsmToken::EndOfStatement))
3827 return TokError("unexpected token in '.ifc' directive");
3828
3829 Lex();
3830
3831 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3832 TheCondState.Ignore = !TheCondState.CondMet;
3833 }
3834
3835 return false;
3836}
3837
Jim Grosbach4b905842013-09-20 23:08:21 +00003838/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003839/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003840bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003841 StringRef Name;
3842 TheCondStack.push_back(TheCondState);
3843 TheCondState.TheCond = AsmCond::IfCond;
3844
3845 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003846 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003847 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003848 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003849 return TokError("expected identifier after '.ifdef'");
3850
3851 Lex();
3852
3853 MCSymbol *Sym = getContext().LookupSymbol(Name);
3854
3855 if (expect_defined)
3856 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3857 else
3858 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3859 TheCondState.Ignore = !TheCondState.CondMet;
3860 }
3861
3862 return false;
3863}
3864
Jim Grosbach4b905842013-09-20 23:08:21 +00003865/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003866/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003867bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003868 if (TheCondState.TheCond != AsmCond::IfCond &&
3869 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003870 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3871 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003872 TheCondState.TheCond = AsmCond::ElseIfCond;
3873
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003874 bool LastIgnoreState = false;
3875 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003876 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003877 if (LastIgnoreState || TheCondState.CondMet) {
3878 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003879 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003880 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003881 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003882 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003883 return true;
3884
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003885 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003886 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003887
Sean Callanan686ed8d2010-01-19 20:22:31 +00003888 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003889 TheCondState.CondMet = ExprValue;
3890 TheCondState.Ignore = !TheCondState.CondMet;
3891 }
3892
3893 return false;
3894}
3895
Jim Grosbach4b905842013-09-20 23:08:21 +00003896/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003897/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003898bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003899 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003900 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003901
Sean Callanan686ed8d2010-01-19 20:22:31 +00003902 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003903
3904 if (TheCondState.TheCond != AsmCond::IfCond &&
3905 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003906 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3907 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003908 TheCondState.TheCond = AsmCond::ElseCond;
3909 bool LastIgnoreState = false;
3910 if (!TheCondStack.empty())
3911 LastIgnoreState = TheCondStack.back().Ignore;
3912 if (LastIgnoreState || TheCondState.CondMet)
3913 TheCondState.Ignore = true;
3914 else
3915 TheCondState.Ignore = false;
3916
3917 return false;
3918}
3919
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003920/// parseDirectiveEnd
3921/// ::= .end
3922bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3923 if (getLexer().isNot(AsmToken::EndOfStatement))
3924 return TokError("unexpected token in '.end' directive");
3925
3926 Lex();
3927
3928 while (Lexer.isNot(AsmToken::Eof))
3929 Lex();
3930
3931 return false;
3932}
3933
Jim Grosbach4b905842013-09-20 23:08:21 +00003934/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003935/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003936bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003937 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003938 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003939
Sean Callanan686ed8d2010-01-19 20:22:31 +00003940 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003941
Jim Grosbach4b905842013-09-20 23:08:21 +00003942 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003943 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3944 ".else");
3945 if (!TheCondStack.empty()) {
3946 TheCondState = TheCondStack.back();
3947 TheCondStack.pop_back();
3948 }
3949
3950 return false;
3951}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003952
Eli Bendersky17233942013-01-15 22:59:42 +00003953void AsmParser::initializeDirectiveKindMap() {
3954 DirectiveKindMap[".set"] = DK_SET;
3955 DirectiveKindMap[".equ"] = DK_EQU;
3956 DirectiveKindMap[".equiv"] = DK_EQUIV;
3957 DirectiveKindMap[".ascii"] = DK_ASCII;
3958 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3959 DirectiveKindMap[".string"] = DK_STRING;
3960 DirectiveKindMap[".byte"] = DK_BYTE;
3961 DirectiveKindMap[".short"] = DK_SHORT;
3962 DirectiveKindMap[".value"] = DK_VALUE;
3963 DirectiveKindMap[".2byte"] = DK_2BYTE;
3964 DirectiveKindMap[".long"] = DK_LONG;
3965 DirectiveKindMap[".int"] = DK_INT;
3966 DirectiveKindMap[".4byte"] = DK_4BYTE;
3967 DirectiveKindMap[".quad"] = DK_QUAD;
3968 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00003969 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00003970 DirectiveKindMap[".single"] = DK_SINGLE;
3971 DirectiveKindMap[".float"] = DK_FLOAT;
3972 DirectiveKindMap[".double"] = DK_DOUBLE;
3973 DirectiveKindMap[".align"] = DK_ALIGN;
3974 DirectiveKindMap[".align32"] = DK_ALIGN32;
3975 DirectiveKindMap[".balign"] = DK_BALIGN;
3976 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3977 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3978 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3979 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3980 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3981 DirectiveKindMap[".org"] = DK_ORG;
3982 DirectiveKindMap[".fill"] = DK_FILL;
3983 DirectiveKindMap[".zero"] = DK_ZERO;
3984 DirectiveKindMap[".extern"] = DK_EXTERN;
3985 DirectiveKindMap[".globl"] = DK_GLOBL;
3986 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003987 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3988 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3989 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3990 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3991 DirectiveKindMap[".reference"] = DK_REFERENCE;
3992 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3993 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3994 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3995 DirectiveKindMap[".comm"] = DK_COMM;
3996 DirectiveKindMap[".common"] = DK_COMMON;
3997 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3998 DirectiveKindMap[".abort"] = DK_ABORT;
3999 DirectiveKindMap[".include"] = DK_INCLUDE;
4000 DirectiveKindMap[".incbin"] = DK_INCBIN;
4001 DirectiveKindMap[".code16"] = DK_CODE16;
4002 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4003 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004004 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004005 DirectiveKindMap[".irp"] = DK_IRP;
4006 DirectiveKindMap[".irpc"] = DK_IRPC;
4007 DirectiveKindMap[".endr"] = DK_ENDR;
4008 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4009 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4010 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4011 DirectiveKindMap[".if"] = DK_IF;
4012 DirectiveKindMap[".ifb"] = DK_IFB;
4013 DirectiveKindMap[".ifnb"] = DK_IFNB;
4014 DirectiveKindMap[".ifc"] = DK_IFC;
4015 DirectiveKindMap[".ifnc"] = DK_IFNC;
4016 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4017 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4018 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4019 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4020 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004021 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004022 DirectiveKindMap[".endif"] = DK_ENDIF;
4023 DirectiveKindMap[".skip"] = DK_SKIP;
4024 DirectiveKindMap[".space"] = DK_SPACE;
4025 DirectiveKindMap[".file"] = DK_FILE;
4026 DirectiveKindMap[".line"] = DK_LINE;
4027 DirectiveKindMap[".loc"] = DK_LOC;
4028 DirectiveKindMap[".stabs"] = DK_STABS;
4029 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4030 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4031 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4032 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4033 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4034 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4035 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4036 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4037 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4038 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4039 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4040 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4041 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4042 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4043 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4044 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4045 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4046 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4047 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4048 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4049 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004050 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004051 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4052 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4053 DirectiveKindMap[".macro"] = DK_MACRO;
4054 DirectiveKindMap[".endm"] = DK_ENDM;
4055 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4056 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004057}
4058
Jim Grosbach4b905842013-09-20 23:08:21 +00004059MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004060 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004061
Rafael Espindola34b9c512012-06-03 23:57:14 +00004062 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004063 for (;;) {
4064 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004065 if (getLexer().is(AsmToken::Eof)) {
4066 Error(DirectiveLoc, "no matching '.endr' in definition");
4067 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004068 }
4069
Rafael Espindola34b9c512012-06-03 23:57:14 +00004070 if (Lexer.is(AsmToken::Identifier) &&
4071 (getTok().getIdentifier() == ".rept")) {
4072 ++NestLevel;
4073 }
4074
4075 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004076 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004077 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004078 EndToken = getTok();
4079 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004080 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4081 TokError("unexpected token in '.endr' directive");
4082 return 0;
4083 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004084 break;
4085 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004086 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004087 }
4088
Rafael Espindola34b9c512012-06-03 23:57:14 +00004089 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004090 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004091 }
4092
4093 const char *BodyStart = StartToken.getLoc().getPointer();
4094 const char *BodyEnd = EndToken.getLoc().getPointer();
4095 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4096
Rafael Espindola34b9c512012-06-03 23:57:14 +00004097 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004098 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004099 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004100}
4101
Jim Grosbach4b905842013-09-20 23:08:21 +00004102void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004103 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004104 OS << ".endr\n";
4105
4106 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004107 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004108
Rafael Espindola34b9c512012-06-03 23:57:14 +00004109 // Create the macro instantiation object and add to the current macro
4110 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004111 MacroInstantiation *MI = new MacroInstantiation(
4112 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004113 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004114
Rafael Espindola34b9c512012-06-03 23:57:14 +00004115 // Jump to the macro instantiation and prime the lexer.
4116 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4117 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4118 Lex();
4119}
4120
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004121/// parseDirectiveRept
4122/// ::= .rep | .rept count
4123bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004124 const MCExpr *CountExpr;
4125 SMLoc CountLoc = getTok().getLoc();
4126 if (parseExpression(CountExpr))
4127 return true;
4128
Rafael Espindola34b9c512012-06-03 23:57:14 +00004129 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004130 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4131 eatToEndOfStatement();
4132 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4133 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004134
4135 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004136 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004137
4138 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004139 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004140
4141 // Eat the end of statement.
4142 Lex();
4143
4144 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004145 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004146 if (!M)
4147 return true;
4148
4149 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4150 // to hold the macro body with substitutions.
4151 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004152 raw_svector_ostream OS(Buf);
4153 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004154 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004155 return true;
4156 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004157 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004158
4159 return false;
4160}
4161
Jim Grosbach4b905842013-09-20 23:08:21 +00004162/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004163/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004164bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004165 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004166
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004167 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004168 return TokError("expected identifier in '.irp' directive");
4169
Rafael Espindola768b41c2012-06-15 14:02:34 +00004170 if (Lexer.isNot(AsmToken::Comma))
4171 return TokError("expected comma in '.irp' directive");
4172
4173 Lex();
4174
Eli Bendersky38274122013-01-14 23:22:36 +00004175 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004176 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004177 return true;
4178
4179 // Eat the end of statement.
4180 Lex();
4181
4182 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004183 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004184 if (!M)
4185 return true;
4186
4187 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4188 // to hold the macro body with substitutions.
4189 SmallString<256> Buf;
4190 raw_svector_ostream OS(Buf);
4191
Eli Bendersky38274122013-01-14 23:22:36 +00004192 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004193 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004194 return true;
4195 }
4196
Jim Grosbach4b905842013-09-20 23:08:21 +00004197 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004198
4199 return false;
4200}
4201
Jim Grosbach4b905842013-09-20 23:08:21 +00004202/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004203/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004204bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004205 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004206
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004207 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004208 return TokError("expected identifier in '.irpc' directive");
4209
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004210 if (Lexer.isNot(AsmToken::Comma))
4211 return TokError("expected comma in '.irpc' directive");
4212
4213 Lex();
4214
Eli Bendersky38274122013-01-14 23:22:36 +00004215 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004216 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004217 return true;
4218
4219 if (A.size() != 1 || A.front().size() != 1)
4220 return TokError("unexpected token in '.irpc' directive");
4221
4222 // Eat the end of statement.
4223 Lex();
4224
4225 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004226 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004227 if (!M)
4228 return true;
4229
4230 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4231 // to hold the macro body with substitutions.
4232 SmallString<256> Buf;
4233 raw_svector_ostream OS(Buf);
4234
4235 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004236 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004237 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004238 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004239
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004240 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004241 return true;
4242 }
4243
Jim Grosbach4b905842013-09-20 23:08:21 +00004244 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004245
4246 return false;
4247}
4248
Jim Grosbach4b905842013-09-20 23:08:21 +00004249bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004250 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004251 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004252
4253 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004254 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004255 assert(getLexer().is(AsmToken::EndOfStatement));
4256
Jim Grosbach4b905842013-09-20 23:08:21 +00004257 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004258 return false;
4259}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004260
Jim Grosbach4b905842013-09-20 23:08:21 +00004261bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004262 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004263 const MCExpr *Value;
4264 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004265 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004266 return true;
4267 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4268 if (!MCE)
4269 return Error(ExprLoc, "unexpected expression in _emit");
4270 uint64_t IntValue = MCE->getValue();
4271 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4272 return Error(ExprLoc, "literal value out of range for directive");
4273
Chad Rosierc7f552c2013-02-12 21:33:51 +00004274 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4275 return false;
4276}
4277
Jim Grosbach4b905842013-09-20 23:08:21 +00004278bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004279 const MCExpr *Value;
4280 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004281 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004282 return true;
4283 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4284 if (!MCE)
4285 return Error(ExprLoc, "unexpected expression in align");
4286 uint64_t IntValue = MCE->getValue();
4287 if (!isPowerOf2_64(IntValue))
4288 return Error(ExprLoc, "literal value not a power of two greater then zero");
4289
Jim Grosbach4b905842013-09-20 23:08:21 +00004290 Info.AsmRewrites->push_back(
4291 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004292 return false;
4293}
4294
Chad Rosierf43fcf52013-02-13 21:27:17 +00004295// We are comparing pointers, but the pointers are relative to a single string.
4296// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004297static int rewritesSort(const AsmRewrite *AsmRewriteA,
4298 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004299 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4300 return -1;
4301 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4302 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004303
Chad Rosierfce4fab2013-04-08 17:43:47 +00004304 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4305 // rewrite to the same location. Make sure the SizeDirective rewrite is
4306 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4307 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004308 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4309 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004310 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004311
Jim Grosbach4b905842013-09-20 23:08:21 +00004312 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4313 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004314 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004315 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004316}
4317
Jim Grosbach4b905842013-09-20 23:08:21 +00004318bool AsmParser::parseMSInlineAsm(
4319 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4320 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4321 SmallVectorImpl<std::string> &Constraints,
4322 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4323 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004324 SmallVector<void *, 4> InputDecls;
4325 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004326 SmallVector<bool, 4> InputDeclsAddressOf;
4327 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004328 SmallVector<std::string, 4> InputConstraints;
4329 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004330 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004331
Benjamin Kramer1a136112013-02-15 20:37:21 +00004332 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004333
4334 // Prime the lexer.
4335 Lex();
4336
4337 // While we have input, parse each statement.
4338 unsigned InputIdx = 0;
4339 unsigned OutputIdx = 0;
4340 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004341 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004342 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004343 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004344
Chad Rosier149e8e02012-12-12 22:45:52 +00004345 if (Info.ParseError)
4346 return true;
4347
Benjamin Kramer1a136112013-02-15 20:37:21 +00004348 if (Info.Opcode == ~0U)
4349 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004350
Benjamin Kramer1a136112013-02-15 20:37:21 +00004351 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004352
Benjamin Kramer1a136112013-02-15 20:37:21 +00004353 // Build the list of clobbers, outputs and inputs.
4354 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4355 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004356
Benjamin Kramer1a136112013-02-15 20:37:21 +00004357 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004358 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004359 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004360
Benjamin Kramer1a136112013-02-15 20:37:21 +00004361 // Register operand.
4362 if (Operand->isReg() && !Operand->needAddressOf()) {
4363 unsigned NumDefs = Desc.getNumDefs();
4364 // Clobber.
4365 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4366 ClobberRegs.push_back(Operand->getReg());
4367 continue;
4368 }
4369
4370 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004371 StringRef SymName = Operand->getSymName();
4372 if (SymName.empty())
4373 continue;
4374
Chad Rosierdba3fe52013-04-22 22:12:12 +00004375 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004376 if (!OpDecl)
4377 continue;
4378
4379 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004380 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004381 if (isOutput) {
4382 ++InputIdx;
4383 OutputDecls.push_back(OpDecl);
4384 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4385 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004386 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004387 } else {
4388 InputDecls.push_back(OpDecl);
4389 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4390 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004391 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004392 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004393 }
Reid Kleckneree088972013-12-10 18:27:32 +00004394
4395 // Consider implicit defs to be clobbers. Think of cpuid and push.
4396 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4397 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4398 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004399 }
4400
4401 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004402 NumOutputs = OutputDecls.size();
4403 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004404
4405 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004406 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4407 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4408 ClobberRegs.end());
4409 Clobbers.assign(ClobberRegs.size(), std::string());
4410 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4411 raw_string_ostream OS(Clobbers[I]);
4412 IP->printRegName(OS, ClobberRegs[I]);
4413 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004414
4415 // Merge the various outputs and inputs. Output are expected first.
4416 if (NumOutputs || NumInputs) {
4417 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004418 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004419 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004420 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004421 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004422 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004423 }
4424 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004425 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004426 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004427 }
4428 }
4429
4430 // Build the IR assembly string.
4431 std::string AsmStringIR;
4432 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004433 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4434 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004435 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004436 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4437 E = AsmStrRewrites.end();
4438 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004439 AsmRewriteKind Kind = (*I).Kind;
4440 if (Kind == AOK_Delete)
4441 continue;
4442
Chad Rosier8bce6642012-10-18 15:49:34 +00004443 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004444 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004445
Chad Rosier120eefd2013-03-19 17:32:17 +00004446 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004447 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004448 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004449 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004450
Chad Rosier37e755c2012-10-23 17:43:43 +00004451 // Skip the original expression.
4452 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004453 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004454 continue;
4455 }
4456
Chad Rosierff10ed12013-04-12 16:26:42 +00004457 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004458 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004459 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004460 default:
4461 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004462 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004463 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004464 break;
4465 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004466 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004467 break;
4468 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004469 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004470 break;
4471 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004472 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004473 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004474 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004475 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004476 default: break;
4477 case 8: OS << "byte ptr "; break;
4478 case 16: OS << "word ptr "; break;
4479 case 32: OS << "dword ptr "; break;
4480 case 64: OS << "qword ptr "; break;
4481 case 80: OS << "xword ptr "; break;
4482 case 128: OS << "xmmword ptr "; break;
4483 case 256: OS << "ymmword ptr "; break;
4484 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004485 break;
4486 case AOK_Emit:
4487 OS << ".byte";
4488 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004489 case AOK_Align: {
4490 unsigned Val = (*I).Val;
4491 OS << ".align " << Val;
4492
4493 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004494 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004495 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4496 break;
4497 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004498 case AOK_DotOperator:
4499 OS << (*I).Val;
4500 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004501 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004502
Chad Rosier8bce6642012-10-18 15:49:34 +00004503 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004504 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004505 }
4506
4507 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004508 if (AsmStart != AsmEnd)
4509 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004510
4511 AsmString = OS.str();
4512 return false;
4513}
4514
Daniel Dunbar01e36072010-07-17 02:26:10 +00004515/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004516MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4517 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004518 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004519}