blob: d77a0fd1b6985a0c0233e7ee6385b77311feb55d [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,
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000364 DK_ERR,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000365 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000366 };
367
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000369 /// directives parsed by this class.
370 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000371
372 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000373 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
374 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000375 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000376 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
377 bool parseDirectiveFill(); // ".fill"
378 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000379 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
381 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000382 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000384
Eli Bendersky17233942013-01-15 22:59:42 +0000385 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveFile(SMLoc DirectiveLoc);
387 bool parseDirectiveLine();
388 bool parseDirectiveLoc();
389 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000390
391 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000392 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000393 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000394 bool parseDirectiveCFISections();
395 bool parseDirectiveCFIStartProc();
396 bool parseDirectiveCFIEndProc();
397 bool parseDirectiveCFIDefCfaOffset();
398 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIAdjustCfaOffset();
400 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
404 bool parseDirectiveCFIRememberState();
405 bool parseDirectiveCFIRestoreState();
406 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
407 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
408 bool parseDirectiveCFIEscape();
409 bool parseDirectiveCFISignalFrame();
410 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
412 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
414 bool parseDirectiveEndMacro(StringRef Directive);
415 bool parseDirectiveMacro(SMLoc DirectiveLoc);
416 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000417
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000419 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000420 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000422 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000424
Eli Bendersky17233942013-01-15 22:59:42 +0000425 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000427
428 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000436
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveAbort(); // ".abort"
438 bool parseDirectiveInclude(); // ".include"
439 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000440
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000442 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000443 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000444 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000446 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
448 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
449 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
450 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000451 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000452
Jim Grosbach4b905842013-09-20 23:08:21 +0000453 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000454 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000455
Rafael Espindola34b9c512012-06-03 23:57:14 +0000456 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000457 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
458 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000459 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000460 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000461 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
462 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
463 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000464
Chad Rosierc7f552c2013-02-12 21:33:51 +0000465 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000466 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000467 size_t Len);
468
469 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000470 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000471
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000472 // "end"
473 bool parseDirectiveEnd(SMLoc DirectiveLoc);
474
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +0000475 // "err"
476 bool parseDirectiveErr(SMLoc DirectiveLoc);
477
Eli Bendersky17233942013-01-15 22:59:42 +0000478 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000479};
Daniel Dunbar86033402010-07-12 17:54:38 +0000480}
481
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000482namespace llvm {
483
484extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000485extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000486extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000487
488}
489
Chris Lattnerc35681b2010-01-19 19:46:13 +0000490enum { DEFAULT_ADDRSPACE = 0 };
491
Jim Grosbach4b905842013-09-20 23:08:21 +0000492AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
493 const MCAsmInfo &_MAI)
494 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
495 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
496 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
497 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000498 // Save the old handler.
499 SavedDiagHandler = SrcMgr.getDiagHandler();
500 SavedDiagContext = SrcMgr.getDiagContext();
501 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000502 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000503 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000504
Daniel Dunbarc5011082010-07-12 18:12:02 +0000505 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000506 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
507 case MCObjectFileInfo::IsCOFF:
508 PlatformParser = createCOFFAsmParser();
509 PlatformParser->Initialize(*this);
510 break;
511 case MCObjectFileInfo::IsMachO:
512 PlatformParser = createDarwinAsmParser();
513 PlatformParser->Initialize(*this);
514 IsDarwin = true;
515 break;
516 case MCObjectFileInfo::IsELF:
517 PlatformParser = createELFAsmParser();
518 PlatformParser->Initialize(*this);
519 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000520 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000521
Eli Bendersky17233942013-01-15 22:59:42 +0000522 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000523}
524
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000525AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000526 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
527
528 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000529 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
530 ie = MacroMap.end();
531 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000532 delete it->getValue();
533
Daniel Dunbarc5011082010-07-12 18:12:02 +0000534 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000535}
536
Jim Grosbach4b905842013-09-20 23:08:21 +0000537void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000538 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000539 for (std::vector<MacroInstantiation *>::const_reverse_iterator
540 it = ActiveMacros.rbegin(),
541 ie = ActiveMacros.rend();
542 it != ie; ++it)
543 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000544 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000545}
546
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000547void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
548 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
549 printMacroInstantiations();
550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000553 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000554 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000555 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
556 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000557 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000558}
559
Chris Lattnera3a06812011-10-16 04:47:35 +0000560bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000561 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000562 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
563 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000564 return true;
565}
566
Jim Grosbach4b905842013-09-20 23:08:21 +0000567bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000568 std::string IncludedFile;
569 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000570 if (NewBuf == -1)
571 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000572
Sean Callanan7a77eae2010-01-21 00:19:58 +0000573 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000574
Sean Callanan7a77eae2010-01-21 00:19:58 +0000575 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000576
Sean Callanan7a77eae2010-01-21 00:19:58 +0000577 return false;
578}
Daniel Dunbar43235712010-07-18 18:54:11 +0000579
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000580/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000581/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000582/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000583bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000584 std::string IncludedFile;
585 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
586 if (NewBuf == -1)
587 return true;
588
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000589 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000590 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000591 return false;
592}
593
Jim Grosbach4b905842013-09-20 23:08:21 +0000594void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000595 if (InBuffer != -1) {
596 CurBuffer = InBuffer;
597 } else {
598 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
599 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000600 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
601}
602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603const AsmToken &AsmParser::Lex() {
604 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000605
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606 if (tok->is(AsmToken::Eof)) {
607 // If this is the end of an included file, pop the parent file off the
608 // include stack.
609 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
610 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000611 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000612 tok = &Lexer.Lex();
613 }
614 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000615
Sean Callanan7a77eae2010-01-21 00:19:58 +0000616 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000617 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000618
Sean Callanan7a77eae2010-01-21 00:19:58 +0000619 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000620}
621
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000622bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000623 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000624 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000625 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000626
Chris Lattner36e02122009-06-21 20:54:55 +0000627 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000628 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000629
630 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000631 AsmCond StartingCondState = TheCondState;
632
Kevin Enderby6469fc22011-11-01 22:27:22 +0000633 // If we are generating dwarf for assembly source files save the initial text
634 // section and generate a .file directive.
635 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000636 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000637 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
638 getStreamer().EmitLabel(SectionStartSym);
639 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000640 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000641 StringRef(),
642 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000643 }
644
Chris Lattner73f36112009-07-02 21:53:43 +0000645 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000646 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000647 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000648 if (!parseStatement(Info))
649 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000650
Daniel Dunbar43325c42010-09-09 22:42:56 +0000651 // We had an error, validate that one was emitted and recover by skipping to
652 // the next line.
653 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000654 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000655 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000656
657 if (TheCondState.TheCond != StartingCondState.TheCond ||
658 TheCondState.Ignore != StartingCondState.Ignore)
659 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000660
661 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000662 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000663 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000664 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000665 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000666 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000667 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000668
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000669 // Check to see that all assembler local symbols were actually defined.
670 // Targets that don't do subsections via symbols may not want this, though,
671 // so conservatively exclude them. Only do this if we're finalizing, though,
672 // as otherwise we won't necessarilly have seen everything yet.
673 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
674 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
675 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000676 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000677 i != e; ++i) {
678 MCSymbol *Sym = i->getValue();
679 // Variable symbols may not be marked as defined, so check those
680 // explicitly. If we know it's a variable, we have a definition for
681 // the purposes of this check.
682 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
683 // FIXME: We would really like to refer back to where the symbol was
684 // first referenced for a source location. We need to add something
685 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000686 printMessage(
687 getLexer().getLoc(), SourceMgr::DK_Error,
688 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000689 }
690 }
691
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000692 // Finalize the output stream if there are no errors and if the client wants
693 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000694 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000695 Out.Finish();
696
Chris Lattner73f36112009-07-02 21:53:43 +0000697 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000698}
699
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000700void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000701 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000702 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000703 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000704 }
705}
706
Jim Grosbach4b905842013-09-20 23:08:21 +0000707/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000708void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000709 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000710 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000711
Chris Lattnere5074c42009-06-22 01:29:09 +0000712 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000713 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000714 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000715}
716
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000717StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000718 const char *Start = getTok().getLoc().getPointer();
719
Jim Grosbach4b905842013-09-20 23:08:21 +0000720 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000721 Lex();
722
723 const char *End = getTok().getLoc().getPointer();
724 return StringRef(Start, End - Start);
725}
Chris Lattner78db3622009-06-22 05:51:26 +0000726
Jim Grosbach4b905842013-09-20 23:08:21 +0000727StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000728 const char *Start = getTok().getLoc().getPointer();
729
730 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000731 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000732 Lex();
733
734 const char *End = getTok().getLoc().getPointer();
735 return StringRef(Start, End - Start);
736}
737
Jim Grosbach4b905842013-09-20 23:08:21 +0000738/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000739/// NOTE: This assumes the leading '(' has already been consumed.
740///
741/// parenexpr ::= expr)
742///
Jim Grosbach4b905842013-09-20 23:08:21 +0000743bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
744 if (parseExpression(Res))
745 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000746 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000747 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000748 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000749 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000750 return false;
751}
Chris Lattner78db3622009-06-22 05:51:26 +0000752
Jim Grosbach4b905842013-09-20 23:08:21 +0000753/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000754/// NOTE: This assumes the leading '[' has already been consumed.
755///
756/// bracketexpr ::= expr]
757///
Jim Grosbach4b905842013-09-20 23:08:21 +0000758bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
759 if (parseExpression(Res))
760 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000761 if (Lexer.isNot(AsmToken::RBrac))
762 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000763 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000764 Lex();
765 return false;
766}
767
Jim Grosbach4b905842013-09-20 23:08:21 +0000768/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000769/// primaryexpr ::= (parenexpr
770/// primaryexpr ::= symbol
771/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000772/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000773/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000774bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000775 SMLoc FirstTokenLoc = getLexer().getLoc();
776 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
777 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000778 default:
779 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000780 // If we have an error assume that we've already handled it.
781 case AsmToken::Error:
782 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000783 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000784 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000785 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000786 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000787 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000788 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000789 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000790 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000791 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000792 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000793 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000794 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000795 if (FirstTokenKind == AsmToken::Dollar) {
796 if (Lexer.getMAI().getDollarIsPC()) {
797 // This is a '$' reference, which references the current PC. Emit a
798 // temporary label to the streamer and refer to it.
799 MCSymbol *Sym = Ctx.CreateTempSymbol();
800 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000801 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
802 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000803 EndLoc = FirstTokenLoc;
804 return false;
805 } else
806 return Error(FirstTokenLoc, "invalid token in expression");
807 return true;
808 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000809 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000810 // Parse symbol variant
811 std::pair<StringRef, StringRef> Split;
812 if (!MAI.useParensForSymbolVariant()) {
813 Split = Identifier.split('@');
814 } else if (Lexer.is(AsmToken::LParen)) {
815 Lexer.Lex(); // eat (
816 StringRef VName;
817 parseIdentifier(VName);
818 if (Lexer.isNot(AsmToken::RParen)) {
819 return Error(Lexer.getTok().getLoc(),
820 "unexpected token in variant, expected ')'");
821 }
822 Lexer.Lex(); // eat )
823 Split = std::make_pair(Identifier, VName);
824 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000825
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000826 EndLoc = SMLoc::getFromPointer(Identifier.end());
827
Daniel Dunbard20cda02009-10-16 01:34:54 +0000828 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000829 StringRef SymbolName = Identifier;
830 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000831
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000832 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000833 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000834 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000835 if (Variant != MCSymbolRefExpr::VK_Invalid) {
836 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000837 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000838 Variant = MCSymbolRefExpr::VK_None;
839 } else {
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000840 return Error(SMLoc::getFromPointer(Split.second.begin()),
841 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000842 }
843 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000844
Hans Wennborgce69d772013-10-18 20:46:28 +0000845 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
846
Daniel Dunbard20cda02009-10-16 01:34:54 +0000847 // If this is an absolute variable reference, substitute it now to preserve
848 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000849 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000850 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000851 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000852
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000853 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000854 return false;
855 }
856
857 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000858 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000859 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000860 }
David Woodhousef42a6662014-02-01 16:20:54 +0000861 case AsmToken::BigNum:
862 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000863 case AsmToken::Integer: {
864 SMLoc Loc = getTok().getLoc();
865 int64_t IntVal = getTok().getIntVal();
866 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000867 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000868 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000869 // Look for 'b' or 'f' following an Integer as a directional label
870 if (Lexer.getKind() == AsmToken::Identifier) {
871 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000872 // Lookup the symbol variant if used.
873 std::pair<StringRef, StringRef> Split = IDVal.split('@');
874 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
875 if (Split.first.size() != IDVal.size()) {
876 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
877 if (Variant == MCSymbolRefExpr::VK_Invalid) {
878 Variant = MCSymbolRefExpr::VK_None;
879 return TokError("invalid variant '" + Split.second + "'");
880 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000881 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000882 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000883 if (IDVal == "f" || IDVal == "b") {
884 MCSymbol *Sym =
885 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000886 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000887 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000888 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000889 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000890 Lex(); // Eat identifier.
891 }
892 }
Chris Lattner78db3622009-06-22 05:51:26 +0000893 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000894 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000895 case AsmToken::Real: {
896 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000897 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000898 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000899 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000900 Lex(); // Eat token.
901 return false;
902 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000903 case AsmToken::Dot: {
904 // This is a '.' reference, which references the current PC. Emit a
905 // temporary label to the streamer and refer to it.
906 MCSymbol *Sym = Ctx.CreateTempSymbol();
907 Out.EmitLabel(Sym);
908 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000909 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000910 Lex(); // Eat identifier.
911 return false;
912 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000913 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000914 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000915 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000916 case AsmToken::LBrac:
917 if (!PlatformParser->HasBracketExpressions())
918 return TokError("brackets expression not supported on this target");
919 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000920 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000921 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000922 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000924 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000925 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000930 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000931 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000933 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000934 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000935 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000936 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000937 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000938 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000939 }
940}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000941
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000942bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000943 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000944 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000945}
946
Daniel Dunbar55f16672010-09-17 02:47:07 +0000947const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000948AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000949 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000950 // Ask the target implementation about this expression first.
951 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
952 if (NewE)
953 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000954 // Recurse over the given expression, rebuilding it to apply the given variant
955 // if there is exactly one symbol.
956 switch (E->getKind()) {
957 case MCExpr::Target:
958 case MCExpr::Constant:
959 return 0;
960
961 case MCExpr::SymbolRef: {
962 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
963
964 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000965 TokError("invalid variant on expression '" + getTok().getIdentifier() +
966 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000967 return E;
968 }
969
970 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
971 }
972
973 case MCExpr::Unary: {
974 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000975 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000976 if (!Sub)
977 return 0;
978 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
979 }
980
981 case MCExpr::Binary: {
982 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000983 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
984 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000985
986 if (!LHS && !RHS)
987 return 0;
988
Jim Grosbach4b905842013-09-20 23:08:21 +0000989 if (!LHS)
990 LHS = BE->getLHS();
991 if (!RHS)
992 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993
994 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
995 }
996 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000997
Craig Toppera2886c22012-02-07 05:05:23 +0000998 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999}
1000
Jim Grosbach4b905842013-09-20 23:08:21 +00001001/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +00001002///
Jim Grosbachbd164242011-08-20 16:24:13 +00001003/// expr ::= expr &&,|| expr -> lowest.
1004/// expr ::= expr |,^,&,! expr
1005/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1006/// expr ::= expr <<,>> expr
1007/// expr ::= expr +,- expr
1008/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001009/// expr ::= primaryexpr
1010///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001011bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001012 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001013 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001014 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001015 return true;
1016
Daniel Dunbar55f16672010-09-17 02:47:07 +00001017 // As a special case, we support 'a op b @ modifier' by rewriting the
1018 // expression to include the modifier. This is inefficient, but in general we
1019 // expect users to use 'a@modifier op b'.
1020 if (Lexer.getKind() == AsmToken::At) {
1021 Lex();
1022
1023 if (Lexer.isNot(AsmToken::Identifier))
1024 return TokError("unexpected symbol modifier following '@'");
1025
1026 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001027 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001028 if (Variant == MCSymbolRefExpr::VK_Invalid)
1029 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1030
Jim Grosbach4b905842013-09-20 23:08:21 +00001031 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001032 if (!ModifiedRes) {
1033 return TokError("invalid modifier '" + getTok().getIdentifier() +
1034 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001035 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001036
Daniel Dunbar55f16672010-09-17 02:47:07 +00001037 Res = ModifiedRes;
1038 Lex();
1039 }
1040
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001041 // Try to constant fold it up front, if possible.
1042 int64_t Value;
1043 if (Res->EvaluateAsAbsolute(Value))
1044 Res = MCConstantExpr::Create(Value, getContext());
1045
1046 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001047}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001048
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001049bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001050 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001051 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001052}
1053
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001054bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001055 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001056
Daniel Dunbar75630b32009-06-30 02:10:03 +00001057 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001058 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001059 return true;
1060
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001061 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001062 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001063
1064 return false;
1065}
1066
Michael J. Spencer530ce852010-10-09 11:00:50 +00001067static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001068 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001069 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001070 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001071 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001072
Jim Grosbach4b905842013-09-20 23:08:21 +00001073 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001074 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001075 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001076 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001079 return 1;
1080
Jim Grosbach4b905842013-09-20 23:08:21 +00001081 // Low Precedence: |, &, ^
1082 //
1083 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001084 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001085 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001086 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001090 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001093
Jim Grosbach4b905842013-09-20 23:08:21 +00001094 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001095 case AsmToken::EqualEqual:
1096 Kind = MCBinaryExpr::EQ;
1097 return 3;
1098 case AsmToken::ExclaimEqual:
1099 case AsmToken::LessGreater:
1100 Kind = MCBinaryExpr::NE;
1101 return 3;
1102 case AsmToken::Less:
1103 Kind = MCBinaryExpr::LT;
1104 return 3;
1105 case AsmToken::LessEqual:
1106 Kind = MCBinaryExpr::LTE;
1107 return 3;
1108 case AsmToken::Greater:
1109 Kind = MCBinaryExpr::GT;
1110 return 3;
1111 case AsmToken::GreaterEqual:
1112 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001113 return 3;
1114
Jim Grosbach4b905842013-09-20 23:08:21 +00001115 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001116 case AsmToken::LessLess:
1117 Kind = MCBinaryExpr::Shl;
1118 return 4;
1119 case AsmToken::GreaterGreater:
1120 Kind = MCBinaryExpr::Shr;
1121 return 4;
1122
Jim Grosbach4b905842013-09-20 23:08:21 +00001123 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001124 case AsmToken::Plus:
1125 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001126 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001127 case AsmToken::Minus:
1128 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001129 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001130
Jim Grosbach4b905842013-09-20 23:08:21 +00001131 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001132 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001133 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001135 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001136 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001138 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001140 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001141 }
1142}
1143
Jim Grosbach4b905842013-09-20 23:08:21 +00001144/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001145/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001146bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001147 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001148 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001149 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001150 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001151
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001152 // If the next token is lower precedence than we are allowed to eat, return
1153 // successfully with what we ate already.
1154 if (TokPrec < Precedence)
1155 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001156
Sean Callanan686ed8d2010-01-19 20:22:31 +00001157 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001158
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001159 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001160 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001161 if (parsePrimaryExpr(RHS, EndLoc))
1162 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001163
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164 // If BinOp binds less tightly with RHS than the operator after RHS, let
1165 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001166 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001167 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001168 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1169 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001170
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001171 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001172 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001173 }
1174}
1175
Chris Lattner36e02122009-06-21 20:54:55 +00001176/// ParseStatement:
1177/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001178/// ::= Label* Directive ...Operands... EndOfStatement
1179/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001180bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001181 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001182 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001183 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001184 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001185 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001187 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001188 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001189 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001190 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001191 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001192 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001193 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001194 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001195
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001196 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001197 if (Lexer.is(AsmToken::Integer)) {
1198 LocalLabelVal = getTok().getIntVal();
1199 if (LocalLabelVal < 0) {
1200 if (!TheCondState.Ignore)
1201 return TokError("unexpected token at start of statement");
1202 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001203 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001204 IDVal = getTok().getString();
1205 Lex(); // Consume the integer token to be used as an identifier token.
1206 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001207 if (!TheCondState.Ignore)
1208 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001209 }
1210 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001211 } else if (Lexer.is(AsmToken::Dot)) {
1212 // Treat '.' as a valid identifier in this context.
1213 Lex();
1214 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001215 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001216 if (!TheCondState.Ignore)
1217 return TokError("unexpected token at start of statement");
1218 IDVal = "";
1219 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001220
Chris Lattner926885c2010-04-17 18:14:27 +00001221 // Handle conditional assembly here before checking for skipping. We
1222 // have to do this so that .endif isn't skipped in a ".if 0" block for
1223 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001224 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001225 DirectiveKindMap.find(IDVal);
1226 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1227 ? DK_NO_DIRECTIVE
1228 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001229 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001230 default:
1231 break;
1232 case DK_IF:
1233 return parseDirectiveIf(IDLoc);
1234 case DK_IFB:
1235 return parseDirectiveIfb(IDLoc, true);
1236 case DK_IFNB:
1237 return parseDirectiveIfb(IDLoc, false);
1238 case DK_IFC:
1239 return parseDirectiveIfc(IDLoc, true);
1240 case DK_IFNC:
1241 return parseDirectiveIfc(IDLoc, false);
1242 case DK_IFDEF:
1243 return parseDirectiveIfdef(IDLoc, true);
1244 case DK_IFNDEF:
1245 case DK_IFNOTDEF:
1246 return parseDirectiveIfdef(IDLoc, false);
1247 case DK_ELSEIF:
1248 return parseDirectiveElseIf(IDLoc);
1249 case DK_ELSE:
1250 return parseDirectiveElse(IDLoc);
1251 case DK_ENDIF:
1252 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001253 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001254
Eli Bendersky88024712013-01-16 19:32:36 +00001255 // Ignore the statement if in the middle of inactive conditional
1256 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001257 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001258 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001259 return false;
1260 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001261
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001262 // FIXME: Recurse on local labels?
1263
1264 // See what kind of statement we have.
1265 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001266 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001267 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001268
Chris Lattner36e02122009-06-21 20:54:55 +00001269 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001270 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001271
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001272 // Diagnose attempt to use '.' as a label.
1273 if (IDVal == ".")
1274 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1275
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001276 // Diagnose attempt to use a variable as a label.
1277 //
1278 // FIXME: Diagnostics. Note the location of the definition as a label.
1279 // FIXME: This doesn't diagnose assignment to a symbol which has been
1280 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001281 MCSymbol *Sym;
1282 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001283 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001284 else
1285 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001286 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001287 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001288
Daniel Dunbare73b2672009-08-26 22:13:22 +00001289 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001290 if (!ParsingInlineAsm)
1291 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001292
Kevin Enderbye7739d42011-12-09 18:09:40 +00001293 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001294 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001295 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001296 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1297 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001298
Tim Northover1744d0a2013-10-25 12:49:50 +00001299 getTargetParser().onLabelParsed(Sym);
1300
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001301 // Consume any end of statement token, if present, to avoid spurious
1302 // AddBlankLine calls().
1303 if (Lexer.is(AsmToken::EndOfStatement)) {
1304 Lex();
1305 if (Lexer.is(AsmToken::Eof))
1306 return false;
1307 }
1308
Eli Friedman0f4871d2012-10-22 23:58:19 +00001309 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001310 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001311
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001312 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001313 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001314 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001315
Jim Grosbach4b905842013-09-20 23:08:21 +00001316 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001317
1318 default: // Normal instruction or directive.
1319 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001320 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001321
1322 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001323 if (areMacrosEnabled())
1324 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1325 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001326 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001327
Michael J. Spencer530ce852010-10-09 11:00:50 +00001328 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001329
Eli Bendersky17233942013-01-15 22:59:42 +00001330 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001331 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001332 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001333 //
Eli Bendersky17233942013-01-15 22:59:42 +00001334 // 1. The target-specific assembly parser. Some directives are target
1335 // specific or may potentially behave differently on certain targets.
1336 // 2. Asm parser extensions. For example, platform-specific parsers
1337 // (like the ELF parser) register themselves as extensions.
1338 // 3. The generic directive parser implemented by this class. These are
1339 // all the directives that behave in a target and platform independent
1340 // manner, or at least have a default behavior that's shared between
1341 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001342
Eli Bendersky17233942013-01-15 22:59:42 +00001343 // First query the target-specific parser. It will return 'true' if it
1344 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001345 if (!getTargetParser().ParseDirective(ID))
1346 return false;
1347
Alp Tokercb402912014-01-24 17:20:08 +00001348 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001349 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001350 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1351 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001352 if (Handler.first)
1353 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1354
1355 // Finally, if no one else is interested in this directive, it must be
1356 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001357 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001358 default:
1359 break;
1360 case DK_SET:
1361 case DK_EQU:
1362 return parseDirectiveSet(IDVal, true);
1363 case DK_EQUIV:
1364 return parseDirectiveSet(IDVal, false);
1365 case DK_ASCII:
1366 return parseDirectiveAscii(IDVal, false);
1367 case DK_ASCIZ:
1368 case DK_STRING:
1369 return parseDirectiveAscii(IDVal, true);
1370 case DK_BYTE:
1371 return parseDirectiveValue(1);
1372 case DK_SHORT:
1373 case DK_VALUE:
1374 case DK_2BYTE:
1375 return parseDirectiveValue(2);
1376 case DK_LONG:
1377 case DK_INT:
1378 case DK_4BYTE:
1379 return parseDirectiveValue(4);
1380 case DK_QUAD:
1381 case DK_8BYTE:
1382 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001383 case DK_OCTA:
1384 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001385 case DK_SINGLE:
1386 case DK_FLOAT:
1387 return parseDirectiveRealValue(APFloat::IEEEsingle);
1388 case DK_DOUBLE:
1389 return parseDirectiveRealValue(APFloat::IEEEdouble);
1390 case DK_ALIGN: {
1391 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1392 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1393 }
1394 case DK_ALIGN32: {
1395 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1396 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1397 }
1398 case DK_BALIGN:
1399 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1400 case DK_BALIGNW:
1401 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1402 case DK_BALIGNL:
1403 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1404 case DK_P2ALIGN:
1405 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1406 case DK_P2ALIGNW:
1407 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1408 case DK_P2ALIGNL:
1409 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1410 case DK_ORG:
1411 return parseDirectiveOrg();
1412 case DK_FILL:
1413 return parseDirectiveFill();
1414 case DK_ZERO:
1415 return parseDirectiveZero();
1416 case DK_EXTERN:
1417 eatToEndOfStatement(); // .extern is the default, ignore it.
1418 return false;
1419 case DK_GLOBL:
1420 case DK_GLOBAL:
1421 return parseDirectiveSymbolAttribute(MCSA_Global);
1422 case DK_LAZY_REFERENCE:
1423 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1424 case DK_NO_DEAD_STRIP:
1425 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1426 case DK_SYMBOL_RESOLVER:
1427 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1428 case DK_PRIVATE_EXTERN:
1429 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1430 case DK_REFERENCE:
1431 return parseDirectiveSymbolAttribute(MCSA_Reference);
1432 case DK_WEAK_DEFINITION:
1433 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1434 case DK_WEAK_REFERENCE:
1435 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1436 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1437 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1438 case DK_COMM:
1439 case DK_COMMON:
1440 return parseDirectiveComm(/*IsLocal=*/false);
1441 case DK_LCOMM:
1442 return parseDirectiveComm(/*IsLocal=*/true);
1443 case DK_ABORT:
1444 return parseDirectiveAbort();
1445 case DK_INCLUDE:
1446 return parseDirectiveInclude();
1447 case DK_INCBIN:
1448 return parseDirectiveIncbin();
1449 case DK_CODE16:
1450 case DK_CODE16GCC:
1451 return TokError(Twine(IDVal) + " not supported yet");
1452 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001453 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001454 case DK_IRP:
1455 return parseDirectiveIrp(IDLoc);
1456 case DK_IRPC:
1457 return parseDirectiveIrpc(IDLoc);
1458 case DK_ENDR:
1459 return parseDirectiveEndr(IDLoc);
1460 case DK_BUNDLE_ALIGN_MODE:
1461 return parseDirectiveBundleAlignMode();
1462 case DK_BUNDLE_LOCK:
1463 return parseDirectiveBundleLock();
1464 case DK_BUNDLE_UNLOCK:
1465 return parseDirectiveBundleUnlock();
1466 case DK_SLEB128:
1467 return parseDirectiveLEB128(true);
1468 case DK_ULEB128:
1469 return parseDirectiveLEB128(false);
1470 case DK_SPACE:
1471 case DK_SKIP:
1472 return parseDirectiveSpace(IDVal);
1473 case DK_FILE:
1474 return parseDirectiveFile(IDLoc);
1475 case DK_LINE:
1476 return parseDirectiveLine();
1477 case DK_LOC:
1478 return parseDirectiveLoc();
1479 case DK_STABS:
1480 return parseDirectiveStabs();
1481 case DK_CFI_SECTIONS:
1482 return parseDirectiveCFISections();
1483 case DK_CFI_STARTPROC:
1484 return parseDirectiveCFIStartProc();
1485 case DK_CFI_ENDPROC:
1486 return parseDirectiveCFIEndProc();
1487 case DK_CFI_DEF_CFA:
1488 return parseDirectiveCFIDefCfa(IDLoc);
1489 case DK_CFI_DEF_CFA_OFFSET:
1490 return parseDirectiveCFIDefCfaOffset();
1491 case DK_CFI_ADJUST_CFA_OFFSET:
1492 return parseDirectiveCFIAdjustCfaOffset();
1493 case DK_CFI_DEF_CFA_REGISTER:
1494 return parseDirectiveCFIDefCfaRegister(IDLoc);
1495 case DK_CFI_OFFSET:
1496 return parseDirectiveCFIOffset(IDLoc);
1497 case DK_CFI_REL_OFFSET:
1498 return parseDirectiveCFIRelOffset(IDLoc);
1499 case DK_CFI_PERSONALITY:
1500 return parseDirectiveCFIPersonalityOrLsda(true);
1501 case DK_CFI_LSDA:
1502 return parseDirectiveCFIPersonalityOrLsda(false);
1503 case DK_CFI_REMEMBER_STATE:
1504 return parseDirectiveCFIRememberState();
1505 case DK_CFI_RESTORE_STATE:
1506 return parseDirectiveCFIRestoreState();
1507 case DK_CFI_SAME_VALUE:
1508 return parseDirectiveCFISameValue(IDLoc);
1509 case DK_CFI_RESTORE:
1510 return parseDirectiveCFIRestore(IDLoc);
1511 case DK_CFI_ESCAPE:
1512 return parseDirectiveCFIEscape();
1513 case DK_CFI_SIGNAL_FRAME:
1514 return parseDirectiveCFISignalFrame();
1515 case DK_CFI_UNDEFINED:
1516 return parseDirectiveCFIUndefined(IDLoc);
1517 case DK_CFI_REGISTER:
1518 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001519 case DK_CFI_WINDOW_SAVE:
1520 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001521 case DK_MACROS_ON:
1522 case DK_MACROS_OFF:
1523 return parseDirectiveMacrosOnOff(IDVal);
1524 case DK_MACRO:
1525 return parseDirectiveMacro(IDLoc);
1526 case DK_ENDM:
1527 case DK_ENDMACRO:
1528 return parseDirectiveEndMacro(IDVal);
1529 case DK_PURGEM:
1530 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001531 case DK_END:
1532 return parseDirectiveEnd(IDLoc);
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00001533 case DK_ERR:
1534 return parseDirectiveErr(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001535 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001536
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001537 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001538 }
Chris Lattner36e02122009-06-21 20:54:55 +00001539
Chad Rosierc7f552c2013-02-12 21:33:51 +00001540 // __asm _emit or __asm __emit
1541 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1542 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001543 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001544
1545 // __asm align
1546 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001547 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001548
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001549 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001550
Chris Lattner7cbfa442010-05-19 23:34:33 +00001551 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001552 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001553 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001554 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001555 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001556 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001557
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001558 // Dump the parsed representation, if requested.
1559 if (getShowParsedOperands()) {
1560 SmallString<256> Str;
1561 raw_svector_ostream OS(Str);
1562 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001563 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001564 if (i != 0)
1565 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001566 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001567 }
1568 OS << "]";
1569
Jim Grosbach4b905842013-09-20 23:08:21 +00001570 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001571 }
1572
Kevin Enderby6469fc22011-11-01 22:27:22 +00001573 // If we are generating dwarf for assembly source files and the current
1574 // section is the initial text section then generate a .loc directive for
1575 // the instruction.
1576 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001577 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001579
Eli Bendersky88024712013-01-16 19:32:36 +00001580 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001581
Eli Bendersky88024712013-01-16 19:32:36 +00001582 // If we previously parsed a cpp hash file line comment then make sure the
1583 // current Dwarf File is for the CppHashFilename if not then emit the
1584 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001585 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001586 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001587 if (CppHashFilename.size() != 0) {
1588 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001589 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001590 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001591 getContext().nextGenDwarfFileNumber(), StringRef(),
1592 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001593
Jim Grosbach4b905842013-09-20 23:08:21 +00001594 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1595 // cache with the different Loc from the call above we save the last
1596 // info we queried here with SrcMgr.FindLineNumber().
1597 unsigned CppHashLocLineNo;
1598 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1599 CppHashLocLineNo = LastQueryLine;
1600 else {
1601 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1602 LastQueryLine = CppHashLocLineNo;
1603 LastQueryIDLoc = CppHashLoc;
1604 LastQueryBuffer = CppHashBuf;
1605 }
1606 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001607 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001608
Jim Grosbach4b905842013-09-20 23:08:21 +00001609 getStreamer().EmitDwarfLocDirective(
1610 getContext().getGenDwarfFileNumber(), Line, 0,
1611 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1612 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001613 }
1614
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001615 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001616 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001617 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001618 HadError = getTargetParser().MatchAndEmitInstruction(
1619 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1620 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001621 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001622
Chris Lattnera2a9d162010-09-11 16:18:25 +00001623 // Don't skip the rest of the line, the instruction parser is responsible for
1624 // that.
1625 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001626}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001627
Jim Grosbach4b905842013-09-20 23:08:21 +00001628/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001629/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001630void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001631 if (!Lexer.is(AsmToken::EndOfStatement))
1632 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001633 // Eat EOL.
1634 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001635}
1636
Jim Grosbach4b905842013-09-20 23:08:21 +00001637/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001638/// ::= # number "filename"
1639/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001640bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001641 Lex(); // Eat the hash token.
1642
1643 if (getLexer().isNot(AsmToken::Integer)) {
1644 // Consume the line since in cases it is not a well-formed line directive,
1645 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001646 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001647 return false;
1648 }
1649
1650 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001651 Lex();
1652
1653 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001654 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001655 return false;
1656 }
1657
1658 StringRef Filename = getTok().getString();
1659 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001660 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001661
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001662 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1663 CppHashLoc = L;
1664 CppHashFilename = Filename;
1665 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001666 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001667
1668 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001669 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001670 return false;
1671}
1672
Jim Grosbach4b905842013-09-20 23:08:21 +00001673/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001674/// for the Filename and LineNo if any in the diagnostic.
1675void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001677 raw_ostream &OS = errs();
1678
1679 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1680 const SMLoc &DiagLoc = Diag.getLoc();
1681 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1682 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1683
Jim Grosbach4b905842013-09-20 23:08:21 +00001684 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001685 // before printing the message.
1686 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001687 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001688 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1689 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001690 }
1691
Eric Christophera7c32732012-12-18 00:30:54 +00001692 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693 // manager changed or buffer changed (like in a nested include) then just
1694 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001695 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001697 if (Parser->SavedDiagHandler)
1698 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1699 else
1700 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001701 return;
1702 }
1703
Eric Christophera7c32732012-12-18 00:30:54 +00001704 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001705 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1706 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001707 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708
1709 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1710 int CppHashLocLineNo =
1711 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001712 int LineNo =
1713 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001714
Jim Grosbach4b905842013-09-20 23:08:21 +00001715 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1716 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001717 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001718
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001719 if (Parser->SavedDiagHandler)
1720 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1721 else
1722 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001723}
1724
Rafael Espindola2c064482012-08-21 18:29:30 +00001725// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1726// difference being that that function accepts '@' as part of identifiers and
1727// we can't do that. AsmLexer.cpp should probably be changed to handle
1728// '@' as a special case when needed.
1729static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001730 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1731 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001732}
1733
Rafael Espindola34b9c512012-06-03 23:57:14 +00001734bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001735 ArrayRef<MCAsmMacroParameter> Parameters,
1736 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001737 unsigned NParameters = Parameters.size();
Benjamin Kramer513e7442014-02-20 13:36:32 +00001738 if ((!IsDarwin || NParameters != 0) && NParameters != A.size())
Rafael Espindola1134ab232011-06-05 02:43:45 +00001739 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001740
Preston Gurd05500642012-09-19 20:36:12 +00001741 // A macro without parameters is handled differently on Darwin:
1742 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001743 while (!Body.empty()) {
1744 // Scan for the next substitution.
1745 std::size_t End = Body.size(), Pos = 0;
1746 for (; Pos != End; ++Pos) {
1747 // Check for a substitution or escape.
Benjamin Kramer513e7442014-02-20 13:36:32 +00001748 if (IsDarwin && !NParameters) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001749 // This macro has no parameters, look for $0, $1, etc.
1750 if (Body[Pos] != '$' || Pos + 1 == End)
1751 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001752
Rafael Espindola1134ab232011-06-05 02:43:45 +00001753 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001754 if (Next == '$' || Next == 'n' ||
1755 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001756 break;
1757 } else {
1758 // This macro has parameters, look for \foo, \bar, etc.
1759 if (Body[Pos] == '\\' && Pos + 1 != End)
1760 break;
1761 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001762 }
1763
1764 // Add the prefix.
1765 OS << Body.slice(0, Pos);
1766
1767 // Check if we reached the end.
1768 if (Pos == End)
1769 break;
1770
Benjamin Kramer513e7442014-02-20 13:36:32 +00001771 if (IsDarwin && !NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001772 switch (Body[Pos + 1]) {
1773 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 case '$':
1775 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001776 break;
1777
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001779 case 'n':
1780 OS << A.size();
1781 break;
1782
Jim Grosbach4b905842013-09-20 23:08:21 +00001783 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001784 default: {
1785 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001786 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001787 if (Index >= A.size())
1788 break;
1789
1790 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001791 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001792 ie = A[Index].end();
1793 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 OS << it->getString();
1795 break;
1796 }
1797 }
1798 Pos += 2;
1799 } else {
1800 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001801 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 ++I;
1803
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 const char *Begin = Body.data() + Pos + 1;
1805 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001806 unsigned Index = 0;
1807 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001808 if (Parameters[Index].Name == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001809 break;
1810
Preston Gurd05500642012-09-19 20:36:12 +00001811 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1813 Pos += 3;
1814 else {
1815 OS << '\\' << Argument;
1816 Pos = I;
1817 }
Preston Gurd05500642012-09-19 20:36:12 +00001818 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001819 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001820 ie = A[Index].end();
1821 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001822 if (it->getKind() == AsmToken::String)
1823 OS << it->getStringContents();
1824 else
1825 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826
Preston Gurd05500642012-09-19 20:36:12 +00001827 Pos += 1 + Argument.size();
1828 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001829 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001830 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001831 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001832 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001833
Rafael Espindola1134ab232011-06-05 02:43:45 +00001834 return false;
1835}
Daniel Dunbar43235712010-07-18 18:54:11 +00001836
Jim Grosbach4b905842013-09-20 23:08:21 +00001837MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1838 SMLoc EL, MemoryBuffer *I)
1839 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1840 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001841
Jim Grosbach4b905842013-09-20 23:08:21 +00001842static bool isOperator(AsmToken::TokenKind kind) {
1843 switch (kind) {
1844 default:
1845 return false;
1846 case AsmToken::Plus:
1847 case AsmToken::Minus:
1848 case AsmToken::Tilde:
1849 case AsmToken::Slash:
1850 case AsmToken::Star:
1851 case AsmToken::Dot:
1852 case AsmToken::Equal:
1853 case AsmToken::EqualEqual:
1854 case AsmToken::Pipe:
1855 case AsmToken::PipePipe:
1856 case AsmToken::Caret:
1857 case AsmToken::Amp:
1858 case AsmToken::AmpAmp:
1859 case AsmToken::Exclaim:
1860 case AsmToken::ExclaimEqual:
1861 case AsmToken::Percent:
1862 case AsmToken::Less:
1863 case AsmToken::LessEqual:
1864 case AsmToken::LessLess:
1865 case AsmToken::LessGreater:
1866 case AsmToken::Greater:
1867 case AsmToken::GreaterEqual:
1868 case AsmToken::GreaterGreater:
1869 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001870 }
1871}
1872
David Majnemer16252452014-01-29 00:07:39 +00001873namespace {
1874class AsmLexerSkipSpaceRAII {
1875public:
1876 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1877 Lexer.setSkipSpace(SkipSpace);
1878 }
1879
1880 ~AsmLexerSkipSpaceRAII() {
1881 Lexer.setSkipSpace(true);
1882 }
1883
1884private:
1885 AsmLexer &Lexer;
1886};
1887}
1888
David Majnemer91fc4c22014-01-29 18:57:46 +00001889bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001890 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001891 unsigned AddTokens = 0;
1892
David Majnemer16252452014-01-29 00:07:39 +00001893 // Darwin doesn't use spaces to delmit arguments.
1894 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001895
1896 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001897 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001898 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001899
David Majnemer91fc4c22014-01-29 18:57:46 +00001900 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001901 break;
Preston Gurd05500642012-09-19 20:36:12 +00001902
1903 if (Lexer.is(AsmToken::Space)) {
1904 Lex(); // Eat spaces
1905
1906 // Spaces can delimit parameters, but could also be part an expression.
1907 // If the token after a space is an operator, add the token and the next
1908 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001909 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001910 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001911 // Check to see whether the token is used as an operator,
1912 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001913 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001914 if (*NextChar == ' ')
1915 AddTokens = 2;
1916 }
1917
1918 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001919 break;
1920 }
1921 }
1922 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001923
Jim Grosbach4b905842013-09-20 23:08:21 +00001924 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001925 // to be able to fill in the remaining default parameter values
1926 if (Lexer.is(AsmToken::EndOfStatement))
1927 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001928
1929 // Adjust the current parentheses level.
1930 if (Lexer.is(AsmToken::LParen))
1931 ++ParenLevel;
1932 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1933 --ParenLevel;
1934
1935 // Append the token to the current argument list.
1936 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001937 if (AddTokens)
1938 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001939 Lex();
1940 }
Preston Gurd05500642012-09-19 20:36:12 +00001941
Rafael Espindola768b41c2012-06-15 14:02:34 +00001942 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001943 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001944 return false;
1945}
1946
1947// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001948bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001949 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001950 const unsigned NParameters = M ? M->Parameters.size() : 0;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001951 bool NamedParametersFound = false;
1952 SmallVector<SMLoc, 4> FALocs;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001953
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001954 A.resize(NParameters);
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001955 FALocs.resize(NParameters);
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001956
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 // Parse two kinds of macro invocations:
1958 // - macros defined without any parameters accept an arbitrary number of them
1959 // - macros defined with parameters accept at most that many of them
1960 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1961 ++Parameter) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001962 SMLoc IDLoc = Lexer.getLoc();
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001963 MCAsmMacroParameter FA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001964
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001965 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) {
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001966 if (parseIdentifier(FA.Name)) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001967 Error(IDLoc, "invalid argument identifier for formal argument");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001968 eatToEndOfStatement();
1969 return true;
1970 }
1971
1972 if (!Lexer.is(AsmToken::Equal)) {
1973 TokError("expected '=' after formal parameter identifier");
1974 eatToEndOfStatement();
1975 return true;
1976 }
1977 Lex();
1978
1979 NamedParametersFound = true;
1980 }
1981
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001982 if (NamedParametersFound && FA.Name.empty()) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001983 Error(IDLoc, "cannot mix positional and keyword arguments");
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001984 eatToEndOfStatement();
1985 return true;
1986 }
1987
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001988 if (parseMacroArgument(FA.Value))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001989 return true;
1990
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001991 unsigned PI = Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001992 if (!FA.Name.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001993 unsigned FAI = 0;
1994 for (FAI = 0; FAI < NParameters; ++FAI)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00001995 if (M->Parameters[FAI].Name == FA.Name)
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001996 break;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001997
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00001998 if (FAI >= NParameters) {
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00001999 Error(IDLoc,
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002000 "parameter named '" + FA.Name + "' does not exist for macro '" +
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002001 M->Name + "'");
2002 return true;
2003 }
2004 PI = FAI;
2005 }
2006
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002007 if (!FA.Value.empty()) {
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002008 if (A.size() <= PI)
2009 A.resize(PI + 1);
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00002010 A[PI] = FA.Value;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002011
2012 if (FALocs.size() <= PI)
2013 FALocs.resize(PI + 1);
2014
2015 FALocs[PI] = Lexer.getLoc();
Preston Gurd242ed3152012-09-19 20:29:04 +00002016 }
Jim Grosbach206661622012-07-30 22:44:17 +00002017
Preston Gurd242ed3152012-09-19 20:29:04 +00002018 // At the end of the statement, fill in remaining arguments that have
2019 // default values. If there aren't any, then the next argument is
2020 // required but missing
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00002021 if (Lexer.is(AsmToken::EndOfStatement)) {
2022 bool Failure = false;
2023 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2024 if (A[FAI].empty()) {
2025 if (M->Parameters[FAI].Required) {
2026 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2027 "missing value for required parameter "
2028 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2029 Failure = true;
2030 }
2031
2032 if (!M->Parameters[FAI].Value.empty())
2033 A[FAI] = M->Parameters[FAI].Value;
2034 }
2035 }
2036 return Failure;
2037 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00002038
2039 if (Lexer.is(AsmToken::Comma))
2040 Lex();
2041 }
Saleem Abdulrasool6d7c0c22014-02-17 00:40:17 +00002042
2043 return TokError("too many positional arguments");
Rafael Espindola768b41c2012-06-15 14:02:34 +00002044}
2045
Jim Grosbach4b905842013-09-20 23:08:21 +00002046const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
2047 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002048 return (I == MacroMap.end()) ? NULL : I->getValue();
2049}
2050
Jim Grosbach4b905842013-09-20 23:08:21 +00002051void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002052 MacroMap[Name] = new MCAsmMacro(Macro);
2053}
2054
Jim Grosbach4b905842013-09-20 23:08:21 +00002055void AsmParser::undefineMacro(StringRef Name) {
2056 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002057 if (I != MacroMap.end()) {
2058 delete I->getValue();
2059 MacroMap.erase(I);
2060 }
2061}
2062
Jim Grosbach4b905842013-09-20 23:08:21 +00002063bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002064 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2065 // this, although we should protect against infinite loops.
2066 if (ActiveMacros.size() == 20)
2067 return TokError("macros cannot be nested more than 20 levels deep");
2068
Eli Bendersky38274122013-01-14 23:22:36 +00002069 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002070 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002071 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002072
Rafael Espindola1134ab232011-06-05 02:43:45 +00002073 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2074 // to hold the macro body with substitutions.
2075 SmallString<256> Buf;
2076 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002077 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002078
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002079 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002080 return true;
2081
Eli Bendersky38274122013-01-14 23:22:36 +00002082 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002083 // instantiation.
2084 OS << ".endmacro\n";
2085
Rafael Espindola1134ab232011-06-05 02:43:45 +00002086 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002087 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002088
Daniel Dunbar43235712010-07-18 18:54:11 +00002089 // Create the macro instantiation object and add to the current macro
2090 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002091 MacroInstantiation *MI = new MacroInstantiation(
2092 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002093 ActiveMacros.push_back(MI);
2094
2095 // Jump to the macro instantiation and prime the lexer.
2096 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2097 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2098 Lex();
2099
2100 return false;
2101}
2102
Jim Grosbach4b905842013-09-20 23:08:21 +00002103void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002104 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002105 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002106 Lex();
2107
2108 // Pop the instantiation entry.
2109 delete ActiveMacros.back();
2110 ActiveMacros.pop_back();
2111}
2112
Jim Grosbach4b905842013-09-20 23:08:21 +00002113static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002114 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002115 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002116 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2117 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002118 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002119 case MCExpr::Target:
2120 case MCExpr::Constant:
2121 return false;
2122 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002123 const MCSymbol &S =
2124 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002125 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002126 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002127 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002128 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002129 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002130 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002131 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002132
2133 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002134}
2135
Jim Grosbach4b905842013-09-20 23:08:21 +00002136bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002137 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002138 // FIXME: Use better location, we should use proper tokens.
2139 SMLoc EqualLoc = Lexer.getLoc();
2140
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002141 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002142 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002143 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002144
Rafael Espindola72f5f172012-01-28 05:57:00 +00002145 // Note: we don't count b as used in "a = b". This is to allow
2146 // a = b
2147 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002148
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002149 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002150 return TokError("unexpected token in assignment");
2151
2152 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002153 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002154
Daniel Dunbar5f339242009-10-16 01:57:39 +00002155 // Validate that the LHS is allowed to be a variable (either it has not been
2156 // used as a symbol, or it is an absolute symbol).
2157 MCSymbol *Sym = getContext().LookupSymbol(Name);
2158 if (Sym) {
2159 // Diagnose assignment to a label.
2160 //
2161 // FIXME: Diagnostics. Note the location of the definition as a label.
2162 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002163 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002164 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2165 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002166 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002167 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2168 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002169 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002170 return Error(EqualLoc, "redefinition of '" + Name + "'");
2171 else if (!Sym->isVariable())
2172 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002173 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002174 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002175 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002176
2177 // Don't count these checks as uses.
2178 Sym->setUsed(false);
Anders Waldenborg84809572014-02-17 20:48:32 +00002179 } else if (Name == ".") {
2180 if (Out.EmitValueToOffset(Value, 0)) {
2181 Error(EqualLoc, "expected absolute expression");
2182 eatToEndOfStatement();
2183 }
2184 return false;
Daniel Dunbar5f339242009-10-16 01:57:39 +00002185 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002186 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002187
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002188 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002189 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002190 if (NoDeadStrip)
2191 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2192
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002193 return false;
2194}
2195
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002196/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002197/// ::= identifier
2198/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002199bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002200 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002201 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2202 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002203 // handle this as a context dependent token, instead we detect adjacent tokens
2204 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002205 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2206 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002207
Hans Wennborgce69d772013-10-18 20:46:28 +00002208 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002209 Lex();
2210 if (Lexer.isNot(AsmToken::Identifier))
2211 return true;
2212
Hans Wennborgce69d772013-10-18 20:46:28 +00002213 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2214 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002215 return true;
2216
2217 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002218 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002219 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002220 Lex();
2221 return false;
2222 }
2223
Jim Grosbach4b905842013-09-20 23:08:21 +00002224 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002225 return true;
2226
Sean Callanan936b0d32010-01-19 21:44:56 +00002227 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002228
Sean Callanan686ed8d2010-01-19 20:22:31 +00002229 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002230
2231 return false;
2232}
2233
Jim Grosbach4b905842013-09-20 23:08:21 +00002234/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002235/// ::= .equ identifier ',' expression
2236/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002237/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002238bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002239 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002240
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002241 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002242 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002243
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002244 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002245 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002246 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002247
Jim Grosbach4b905842013-09-20 23:08:21 +00002248 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002249}
2250
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002251bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002252 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002253
2254 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002255 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002256 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2257 if (Str[i] != '\\') {
2258 Data += Str[i];
2259 continue;
2260 }
2261
2262 // Recognize escaped characters. Note that this escape semantics currently
2263 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2264 ++i;
2265 if (i == e)
2266 return TokError("unexpected backslash at end of string");
2267
2268 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002269 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002270 // Consume up to three octal characters.
2271 unsigned Value = Str[i] - '0';
2272
Jim Grosbach4b905842013-09-20 23:08:21 +00002273 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002274 ++i;
2275 Value = Value * 8 + (Str[i] - '0');
2276
Jim Grosbach4b905842013-09-20 23:08:21 +00002277 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002278 ++i;
2279 Value = Value * 8 + (Str[i] - '0');
2280 }
2281 }
2282
2283 if (Value > 255)
2284 return TokError("invalid octal escape sequence (out of range)");
2285
Jim Grosbach4b905842013-09-20 23:08:21 +00002286 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002287 continue;
2288 }
2289
2290 // Otherwise recognize individual escapes.
2291 switch (Str[i]) {
2292 default:
2293 // Just reject invalid escape sequences for now.
2294 return TokError("invalid escape sequence (unrecognized character)");
2295
2296 case 'b': Data += '\b'; break;
2297 case 'f': Data += '\f'; break;
2298 case 'n': Data += '\n'; break;
2299 case 'r': Data += '\r'; break;
2300 case 't': Data += '\t'; break;
2301 case '"': Data += '"'; break;
2302 case '\\': Data += '\\'; break;
2303 }
2304 }
2305
2306 return false;
2307}
2308
Jim Grosbach4b905842013-09-20 23:08:21 +00002309/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002310/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002311bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002312 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002313 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002314
Daniel Dunbara10e5192009-06-24 23:30:00 +00002315 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002316 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002317 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002318
Daniel Dunbaref668c12009-08-14 18:19:52 +00002319 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002320 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002321 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002322
Rafael Espindola64e1af82013-07-02 15:49:13 +00002323 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002324 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002325 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002326
Sean Callanan686ed8d2010-01-19 20:22:31 +00002327 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002328
2329 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002330 break;
2331
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002332 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002333 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002334 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002335 }
2336 }
2337
Sean Callanan686ed8d2010-01-19 20:22:31 +00002338 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002339 return false;
2340}
2341
Jim Grosbach4b905842013-09-20 23:08:21 +00002342/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002343/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002344bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002345 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002346 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002347
Daniel Dunbara10e5192009-06-24 23:30:00 +00002348 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002349 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002350 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002351 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002352 return true;
2353
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002354 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002355 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2356 assert(Size <= 8 && "Invalid size");
2357 uint64_t IntValue = MCE->getValue();
2358 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2359 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002360 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002361 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002362 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002363
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002364 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002365 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002366
Daniel Dunbara10e5192009-06-24 23:30:00 +00002367 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002368 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002369 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002370 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002371 }
2372 }
2373
Sean Callanan686ed8d2010-01-19 20:22:31 +00002374 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002375 return false;
2376}
2377
David Woodhoused6de0d92014-02-01 16:20:59 +00002378/// ParseDirectiveOctaValue
2379/// ::= .octa [ hexconstant (, hexconstant)* ]
2380bool AsmParser::parseDirectiveOctaValue() {
2381 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2382 checkForValidSection();
2383
2384 for (;;) {
2385 if (Lexer.getKind() == AsmToken::Error)
2386 return true;
2387 if (Lexer.getKind() != AsmToken::Integer &&
2388 Lexer.getKind() != AsmToken::BigNum)
2389 return TokError("unknown token in expression");
2390
2391 SMLoc ExprLoc = getLexer().getLoc();
2392 APInt IntValue = getTok().getAPIntVal();
2393 Lex();
2394
2395 uint64_t hi, lo;
2396 if (IntValue.isIntN(64)) {
2397 hi = 0;
2398 lo = IntValue.getZExtValue();
2399 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002400 // It might actually have more than 128 bits, but the top ones are zero.
2401 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002402 lo = IntValue.getLoBits(64).getZExtValue();
2403 } else
2404 return Error(ExprLoc, "literal value out of range for directive");
2405
2406 if (MAI.isLittleEndian()) {
2407 getStreamer().EmitIntValue(lo, 8);
2408 getStreamer().EmitIntValue(hi, 8);
2409 } else {
2410 getStreamer().EmitIntValue(hi, 8);
2411 getStreamer().EmitIntValue(lo, 8);
2412 }
2413
2414 if (getLexer().is(AsmToken::EndOfStatement))
2415 break;
2416
2417 // FIXME: Improve diagnostic.
2418 if (getLexer().isNot(AsmToken::Comma))
2419 return TokError("unexpected token in directive");
2420 Lex();
2421 }
2422 }
2423
2424 Lex();
2425 return false;
2426}
2427
Jim Grosbach4b905842013-09-20 23:08:21 +00002428/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002429/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002430bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002431 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002432 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002433
2434 for (;;) {
2435 // We don't truly support arithmetic on floating point expressions, so we
2436 // have to manually parse unary prefixes.
2437 bool IsNeg = false;
2438 if (getLexer().is(AsmToken::Minus)) {
2439 Lex();
2440 IsNeg = true;
2441 } else if (getLexer().is(AsmToken::Plus))
2442 Lex();
2443
Michael J. Spencer530ce852010-10-09 11:00:50 +00002444 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002445 getLexer().isNot(AsmToken::Real) &&
2446 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002447 return TokError("unexpected token in directive");
2448
2449 // Convert to an APFloat.
2450 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002451 StringRef IDVal = getTok().getString();
2452 if (getLexer().is(AsmToken::Identifier)) {
2453 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2454 Value = APFloat::getInf(Semantics);
2455 else if (!IDVal.compare_lower("nan"))
2456 Value = APFloat::getNaN(Semantics, false, ~0);
2457 else
2458 return TokError("invalid floating point literal");
2459 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002460 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002461 return TokError("invalid floating point literal");
2462 if (IsNeg)
2463 Value.changeSign();
2464
2465 // Consume the numeric token.
2466 Lex();
2467
2468 // Emit the value as an integer.
2469 APInt AsInt = Value.bitcastToAPInt();
2470 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002471 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002472
2473 if (getLexer().is(AsmToken::EndOfStatement))
2474 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002475
Daniel Dunbar2af16532010-09-24 01:59:56 +00002476 if (getLexer().isNot(AsmToken::Comma))
2477 return TokError("unexpected token in directive");
2478 Lex();
2479 }
2480 }
2481
2482 Lex();
2483 return false;
2484}
2485
Jim Grosbach4b905842013-09-20 23:08:21 +00002486/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002487/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002488bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002489 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002490
2491 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002492 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002493 return true;
2494
Rafael Espindolab91bac62010-10-05 19:42:57 +00002495 int64_t Val = 0;
2496 if (getLexer().is(AsmToken::Comma)) {
2497 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002498 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002499 return true;
2500 }
2501
Rafael Espindola922e3f42010-09-16 15:03:59 +00002502 if (getLexer().isNot(AsmToken::EndOfStatement))
2503 return TokError("unexpected token in '.zero' directive");
2504
2505 Lex();
2506
Rafael Espindola64e1af82013-07-02 15:49:13 +00002507 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002508
2509 return false;
2510}
2511
Jim Grosbach4b905842013-09-20 23:08:21 +00002512/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002513/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002514bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002515 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002516
David Majnemer522d3db2014-02-01 07:19:38 +00002517 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002518 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002519 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002520 return true;
2521
David Majnemer522d3db2014-02-01 07:19:38 +00002522 if (NumValues < 0) {
2523 Warning(RepeatLoc,
2524 "'.fill' directive with negative repeat count has no effect");
2525 NumValues = 0;
2526 }
2527
Roman Divackye33098f2013-09-24 17:44:41 +00002528 int64_t FillSize = 1;
2529 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002530
David Majnemer522d3db2014-02-01 07:19:38 +00002531 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002532 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2533 if (getLexer().isNot(AsmToken::Comma))
2534 return TokError("unexpected token in '.fill' directive");
2535 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002536
David Majnemer522d3db2014-02-01 07:19:38 +00002537 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002538 if (parseAbsoluteExpression(FillSize))
2539 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002540
Roman Divackye33098f2013-09-24 17:44:41 +00002541 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2542 if (getLexer().isNot(AsmToken::Comma))
2543 return TokError("unexpected token in '.fill' directive");
2544 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002545
David Majnemer522d3db2014-02-01 07:19:38 +00002546 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002547 if (parseAbsoluteExpression(FillExpr))
2548 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002549
Roman Divackye33098f2013-09-24 17:44:41 +00002550 if (getLexer().isNot(AsmToken::EndOfStatement))
2551 return TokError("unexpected token in '.fill' directive");
2552
2553 Lex();
2554 }
2555 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002556
David Majnemer522d3db2014-02-01 07:19:38 +00002557 if (FillSize < 0) {
2558 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2559 NumValues = 0;
2560 }
2561 if (FillSize > 8) {
2562 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2563 FillSize = 8;
2564 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002565
David Majnemer522d3db2014-02-01 07:19:38 +00002566 if (!isUInt<32>(FillExpr) && FillSize > 4)
2567 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2568
2569 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2570 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2571
2572 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2573 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2574 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2575 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002576
2577 return false;
2578}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002579
Jim Grosbach4b905842013-09-20 23:08:21 +00002580/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002581/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002582bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002583 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002584
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002585 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002586 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002587 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002588 return true;
2589
2590 // Parse optional fill expression.
2591 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002592 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2593 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002594 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002595 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002596
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002597 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002598 return true;
2599
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002600 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002601 return TokError("unexpected token in '.org' directive");
2602 }
2603
Sean Callanan686ed8d2010-01-19 20:22:31 +00002604 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002605
Jim Grosbachb5912772012-01-27 00:37:08 +00002606 // Only limited forms of relocatable expressions are accepted here, it
2607 // has to be relative to the current section. The streamer will return
2608 // 'true' if the expression wasn't evaluatable.
2609 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2610 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002611
2612 return false;
2613}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002614
Jim Grosbach4b905842013-09-20 23:08:21 +00002615/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002616/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002617bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002618 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002619
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002620 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002621 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002622 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002623 return true;
2624
2625 SMLoc MaxBytesLoc;
2626 bool HasFillExpr = false;
2627 int64_t FillExpr = 0;
2628 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002629 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2630 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002631 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002632 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002633
2634 // The fill expression can be omitted while specifying a maximum number of
2635 // alignment bytes, e.g:
2636 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002637 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002638 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002639 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002640 return true;
2641 }
2642
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002643 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2644 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002645 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002646 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002647
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002648 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002649 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002650 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002651
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002652 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002653 return TokError("unexpected token in directive");
2654 }
2655 }
2656
Sean Callanan686ed8d2010-01-19 20:22:31 +00002657 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002658
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002659 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002660 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002661
2662 // Compute alignment in bytes.
2663 if (IsPow2) {
2664 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002665 if (Alignment >= 32) {
2666 Error(AlignmentLoc, "invalid alignment value");
2667 Alignment = 31;
2668 }
2669
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002670 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002671 } else {
2672 // Reject alignments that aren't a power of two, for gas compatibility.
2673 if (!isPowerOf2_64(Alignment))
2674 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002675 }
2676
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002677 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002678 if (MaxBytesLoc.isValid()) {
2679 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002680 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002681 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002682 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002683 }
2684
2685 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002686 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002687 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002688 MaxBytesToFill = 0;
2689 }
2690 }
2691
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002692 // Check whether we should use optimal code alignment for this .align
2693 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002694 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002695 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2696 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002697 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002698 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002699 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002700 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2701 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002702 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002703
2704 return false;
2705}
2706
Jim Grosbach4b905842013-09-20 23:08:21 +00002707/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002708/// ::= .file [number] filename
2709/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002710bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002711 // FIXME: I'm not sure what this is.
2712 int64_t FileNumber = -1;
2713 SMLoc FileNumberLoc = getLexer().getLoc();
2714 if (getLexer().is(AsmToken::Integer)) {
2715 FileNumber = getTok().getIntVal();
2716 Lex();
2717
2718 if (FileNumber < 1)
2719 return TokError("file number less than one");
2720 }
2721
2722 if (getLexer().isNot(AsmToken::String))
2723 return TokError("unexpected token in '.file' directive");
2724
2725 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002726 // Allow the strings to have escaped octal character sequence.
2727 std::string Path = getTok().getString();
2728 if (parseEscapedString(Path))
2729 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002730 Lex();
2731
2732 StringRef Directory;
2733 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002734 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002735 if (getLexer().is(AsmToken::String)) {
2736 if (FileNumber == -1)
2737 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002738 if (parseEscapedString(FilenameData))
2739 return true;
2740 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002741 Directory = Path;
2742 Lex();
2743 } else {
2744 Filename = Path;
2745 }
2746
2747 if (getLexer().isNot(AsmToken::EndOfStatement))
2748 return TokError("unexpected token in '.file' directive");
2749
2750 if (FileNumber == -1)
2751 getStreamer().EmitFileDirective(Filename);
2752 else {
2753 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002754 Error(DirectiveLoc,
2755 "input can't have .file dwarf directives when -g is "
2756 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002757
2758 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2759 Error(FileNumberLoc, "file number already allocated");
2760 }
2761
2762 return false;
2763}
2764
Jim Grosbach4b905842013-09-20 23:08:21 +00002765/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002766/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002767bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002768 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2769 if (getLexer().isNot(AsmToken::Integer))
2770 return TokError("unexpected token in '.line' directive");
2771
2772 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002773 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002774 Lex();
2775
2776 // FIXME: Do something with the .line.
2777 }
2778
2779 if (getLexer().isNot(AsmToken::EndOfStatement))
2780 return TokError("unexpected token in '.line' directive");
2781
2782 return false;
2783}
2784
Jim Grosbach4b905842013-09-20 23:08:21 +00002785/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002786/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2787/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2788/// The first number is a file number, must have been previously assigned with
2789/// a .file directive, the second number is the line number and optionally the
2790/// third number is a column position (zero if not specified). The remaining
2791/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002792bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002793 if (getLexer().isNot(AsmToken::Integer))
2794 return TokError("unexpected token in '.loc' directive");
2795 int64_t FileNumber = getTok().getIntVal();
2796 if (FileNumber < 1)
2797 return TokError("file number less than one in '.loc' directive");
2798 if (!getContext().isValidDwarfFileNumber(FileNumber))
2799 return TokError("unassigned file number in '.loc' directive");
2800 Lex();
2801
2802 int64_t LineNumber = 0;
2803 if (getLexer().is(AsmToken::Integer)) {
2804 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002805 if (LineNumber < 0)
2806 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002807 Lex();
2808 }
2809
2810 int64_t ColumnPos = 0;
2811 if (getLexer().is(AsmToken::Integer)) {
2812 ColumnPos = getTok().getIntVal();
2813 if (ColumnPos < 0)
2814 return TokError("column position less than zero in '.loc' directive");
2815 Lex();
2816 }
2817
2818 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2819 unsigned Isa = 0;
2820 int64_t Discriminator = 0;
2821 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2822 for (;;) {
2823 if (getLexer().is(AsmToken::EndOfStatement))
2824 break;
2825
2826 StringRef Name;
2827 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002828 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002829 return TokError("unexpected token in '.loc' directive");
2830
2831 if (Name == "basic_block")
2832 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2833 else if (Name == "prologue_end")
2834 Flags |= DWARF2_FLAG_PROLOGUE_END;
2835 else if (Name == "epilogue_begin")
2836 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2837 else if (Name == "is_stmt") {
2838 Loc = getTok().getLoc();
2839 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002840 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002841 return true;
2842 // The expression must be the constant 0 or 1.
2843 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2844 int Value = MCE->getValue();
2845 if (Value == 0)
2846 Flags &= ~DWARF2_FLAG_IS_STMT;
2847 else if (Value == 1)
2848 Flags |= DWARF2_FLAG_IS_STMT;
2849 else
2850 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002851 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002852 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2853 }
Craig Topperf15655b2013-04-22 04:22:40 +00002854 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002855 Loc = getTok().getLoc();
2856 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002857 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002858 return true;
2859 // The expression must be a constant greater or equal to 0.
2860 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2861 int Value = MCE->getValue();
2862 if (Value < 0)
2863 return Error(Loc, "isa number less than zero");
2864 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002865 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002866 return Error(Loc, "isa number not a constant value");
2867 }
Craig Topperf15655b2013-04-22 04:22:40 +00002868 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002869 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002870 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002871 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002872 return Error(Loc, "unknown sub-directive in '.loc' directive");
2873 }
2874
2875 if (getLexer().is(AsmToken::EndOfStatement))
2876 break;
2877 }
2878 }
2879
2880 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2881 Isa, Discriminator, StringRef());
2882
2883 return false;
2884}
2885
Jim Grosbach4b905842013-09-20 23:08:21 +00002886/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002887/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002888bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002889 return TokError("unsupported directive '.stabs'");
2890}
2891
Jim Grosbach4b905842013-09-20 23:08:21 +00002892/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002893/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002894bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002895 StringRef Name;
2896 bool EH = false;
2897 bool Debug = false;
2898
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002899 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002900 return TokError("Expected an identifier");
2901
2902 if (Name == ".eh_frame")
2903 EH = true;
2904 else if (Name == ".debug_frame")
2905 Debug = true;
2906
2907 if (getLexer().is(AsmToken::Comma)) {
2908 Lex();
2909
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002910 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002911 return TokError("Expected an identifier");
2912
2913 if (Name == ".eh_frame")
2914 EH = true;
2915 else if (Name == ".debug_frame")
2916 Debug = true;
2917 }
2918
2919 getStreamer().EmitCFISections(EH, Debug);
2920 return false;
2921}
2922
Jim Grosbach4b905842013-09-20 23:08:21 +00002923/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002924/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002925bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002926 StringRef Simple;
2927 if (getLexer().isNot(AsmToken::EndOfStatement))
2928 if (parseIdentifier(Simple) || Simple != "simple")
2929 return TokError("unexpected token in .cfi_startproc directive");
2930
2931 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002932 return false;
2933}
2934
Jim Grosbach4b905842013-09-20 23:08:21 +00002935/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002936/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002937bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002938 getStreamer().EmitCFIEndProc();
2939 return false;
2940}
2941
Jim Grosbach4b905842013-09-20 23:08:21 +00002942/// \brief parse register name or number.
2943bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002944 SMLoc DirectiveLoc) {
2945 unsigned RegNo;
2946
2947 if (getLexer().isNot(AsmToken::Integer)) {
2948 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2949 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002950 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002951 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002952 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002953
2954 return false;
2955}
2956
Jim Grosbach4b905842013-09-20 23:08:21 +00002957/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002958/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002959bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002960 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002961 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002962 return true;
2963
2964 if (getLexer().isNot(AsmToken::Comma))
2965 return TokError("unexpected token in directive");
2966 Lex();
2967
2968 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002969 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002970 return true;
2971
2972 getStreamer().EmitCFIDefCfa(Register, Offset);
2973 return false;
2974}
2975
Jim Grosbach4b905842013-09-20 23:08:21 +00002976/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002977/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002978bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002979 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002980 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002981 return true;
2982
2983 getStreamer().EmitCFIDefCfaOffset(Offset);
2984 return false;
2985}
2986
Jim Grosbach4b905842013-09-20 23:08:21 +00002987/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002988/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002989bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002990 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002991 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002992 return true;
2993
2994 if (getLexer().isNot(AsmToken::Comma))
2995 return TokError("unexpected token in directive");
2996 Lex();
2997
2998 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002999 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003000 return true;
3001
3002 getStreamer().EmitCFIRegister(Register1, Register2);
3003 return false;
3004}
3005
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003006/// parseDirectiveCFIWindowSave
3007/// ::= .cfi_window_save
3008bool AsmParser::parseDirectiveCFIWindowSave() {
3009 getStreamer().EmitCFIWindowSave();
3010 return false;
3011}
3012
Jim Grosbach4b905842013-09-20 23:08:21 +00003013/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003014/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00003015bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00003016 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003017 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00003018 return true;
3019
3020 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3021 return false;
3022}
3023
Jim Grosbach4b905842013-09-20 23:08:21 +00003024/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00003025/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00003026bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003027 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003028 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003029 return true;
3030
3031 getStreamer().EmitCFIDefCfaRegister(Register);
3032 return false;
3033}
3034
Jim Grosbach4b905842013-09-20 23:08:21 +00003035/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003036/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003037bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003038 int64_t Register = 0;
3039 int64_t Offset = 0;
3040
Jim Grosbach4b905842013-09-20 23:08:21 +00003041 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return true;
3043
3044 if (getLexer().isNot(AsmToken::Comma))
3045 return TokError("unexpected token in directive");
3046 Lex();
3047
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003048 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003049 return true;
3050
3051 getStreamer().EmitCFIOffset(Register, Offset);
3052 return false;
3053}
3054
Jim Grosbach4b905842013-09-20 23:08:21 +00003055/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00003056/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00003057bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003058 int64_t Register = 0;
3059
Jim Grosbach4b905842013-09-20 23:08:21 +00003060 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003061 return true;
3062
3063 if (getLexer().isNot(AsmToken::Comma))
3064 return TokError("unexpected token in directive");
3065 Lex();
3066
3067 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003068 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003069 return true;
3070
3071 getStreamer().EmitCFIRelOffset(Register, Offset);
3072 return false;
3073}
3074
3075static bool isValidEncoding(int64_t Encoding) {
3076 if (Encoding & ~0xff)
3077 return false;
3078
3079 if (Encoding == dwarf::DW_EH_PE_omit)
3080 return true;
3081
3082 const unsigned Format = Encoding & 0xf;
3083 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3084 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3085 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3086 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3087 return false;
3088
3089 const unsigned Application = Encoding & 0x70;
3090 if (Application != dwarf::DW_EH_PE_absptr &&
3091 Application != dwarf::DW_EH_PE_pcrel)
3092 return false;
3093
3094 return true;
3095}
3096
Jim Grosbach4b905842013-09-20 23:08:21 +00003097/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003098/// IsPersonality true for cfi_personality, false for cfi_lsda
3099/// ::= .cfi_personality encoding, [symbol_name]
3100/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003101bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003102 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003103 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003104 return true;
3105 if (Encoding == dwarf::DW_EH_PE_omit)
3106 return false;
3107
3108 if (!isValidEncoding(Encoding))
3109 return TokError("unsupported encoding.");
3110
3111 if (getLexer().isNot(AsmToken::Comma))
3112 return TokError("unexpected token in directive");
3113 Lex();
3114
3115 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003116 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003117 return TokError("expected identifier in directive");
3118
3119 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3120
3121 if (IsPersonality)
3122 getStreamer().EmitCFIPersonality(Sym, Encoding);
3123 else
3124 getStreamer().EmitCFILsda(Sym, Encoding);
3125 return false;
3126}
3127
Jim Grosbach4b905842013-09-20 23:08:21 +00003128/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003129/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003130bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003131 getStreamer().EmitCFIRememberState();
3132 return false;
3133}
3134
Jim Grosbach4b905842013-09-20 23:08:21 +00003135/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003136/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003137bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003138 getStreamer().EmitCFIRestoreState();
3139 return false;
3140}
3141
Jim Grosbach4b905842013-09-20 23:08:21 +00003142/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003143/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003144bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003145 int64_t Register = 0;
3146
Jim Grosbach4b905842013-09-20 23:08:21 +00003147 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003148 return true;
3149
3150 getStreamer().EmitCFISameValue(Register);
3151 return false;
3152}
3153
Jim Grosbach4b905842013-09-20 23:08:21 +00003154/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003155/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003156bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003157 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003158 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003159 return true;
3160
3161 getStreamer().EmitCFIRestore(Register);
3162 return false;
3163}
3164
Jim Grosbach4b905842013-09-20 23:08:21 +00003165/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003166/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003167bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003168 std::string Values;
3169 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003170 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003171 return true;
3172
3173 Values.push_back((uint8_t)CurrValue);
3174
3175 while (getLexer().is(AsmToken::Comma)) {
3176 Lex();
3177
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003178 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003179 return true;
3180
3181 Values.push_back((uint8_t)CurrValue);
3182 }
3183
3184 getStreamer().EmitCFIEscape(Values);
3185 return false;
3186}
3187
Jim Grosbach4b905842013-09-20 23:08:21 +00003188/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003189/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003190bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003191 if (getLexer().isNot(AsmToken::EndOfStatement))
3192 return Error(getLexer().getLoc(),
3193 "unexpected token in '.cfi_signal_frame'");
3194
3195 getStreamer().EmitCFISignalFrame();
3196 return false;
3197}
3198
Jim Grosbach4b905842013-09-20 23:08:21 +00003199/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003200/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003201bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003202 int64_t Register = 0;
3203
Jim Grosbach4b905842013-09-20 23:08:21 +00003204 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003205 return true;
3206
3207 getStreamer().EmitCFIUndefined(Register);
3208 return false;
3209}
3210
Jim Grosbach4b905842013-09-20 23:08:21 +00003211/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003212/// ::= .macros_on
3213/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003214bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003215 if (getLexer().isNot(AsmToken::EndOfStatement))
3216 return Error(getLexer().getLoc(),
3217 "unexpected token in '" + Directive + "' directive");
3218
Jim Grosbach4b905842013-09-20 23:08:21 +00003219 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003220 return false;
3221}
3222
Jim Grosbach4b905842013-09-20 23:08:21 +00003223/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003224/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003225bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003226 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003227 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003228 return TokError("expected identifier in '.macro' directive");
3229
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003230 if (getLexer().is(AsmToken::Comma))
3231 Lex();
3232
Eli Bendersky17233942013-01-15 22:59:42 +00003233 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003234 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3235 MCAsmMacroParameter Parameter;
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003236 if (parseIdentifier(Parameter.Name))
David Majnemer91fc4c22014-01-29 18:57:46 +00003237 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003238
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003239 if (Lexer.is(AsmToken::Colon)) {
3240 Lex(); // consume ':'
3241
3242 SMLoc QualLoc;
3243 StringRef Qualifier;
3244
3245 QualLoc = Lexer.getLoc();
3246 if (parseIdentifier(Qualifier))
3247 return Error(QualLoc, "missing parameter qualifier for "
3248 "'" + Parameter.Name + "' in macro '" + Name + "'");
3249
3250 if (Qualifier == "req")
3251 Parameter.Required = true;
3252 else
3253 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier "
3254 "for '" + Parameter.Name + "' in macro '" + Name + "'");
3255 }
3256
David Majnemer91fc4c22014-01-29 18:57:46 +00003257 if (getLexer().is(AsmToken::Equal)) {
3258 Lex();
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003259
3260 SMLoc ParamLoc;
3261
3262 ParamLoc = Lexer.getLoc();
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003263 if (parseMacroArgument(Parameter.Value))
David Majnemer91fc4c22014-01-29 18:57:46 +00003264 return true;
Saleem Abdulrasoolf903a442014-02-19 03:00:29 +00003265
3266 if (Parameter.Required)
3267 Warning(ParamLoc, "pointless default value for required parameter "
3268 "'" + Parameter.Name + "' in macro '" + Name + "'");
Eli Bendersky17233942013-01-15 22:59:42 +00003269 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003270
3271 Parameters.push_back(Parameter);
3272
3273 if (getLexer().is(AsmToken::Comma))
3274 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003275 }
3276
3277 // Eat the end of statement.
3278 Lex();
3279
3280 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003281 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003282
3283 // Lex the macro definition.
3284 for (;;) {
3285 // Check whether we have reached the end of the file.
3286 if (getLexer().is(AsmToken::Eof))
3287 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3288
3289 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003290 if (getLexer().is(AsmToken::Identifier)) {
3291 if (getTok().getIdentifier() == ".endm" ||
3292 getTok().getIdentifier() == ".endmacro") {
3293 if (MacroDepth == 0) { // Outermost macro.
3294 EndToken = getTok();
3295 Lex();
3296 if (getLexer().isNot(AsmToken::EndOfStatement))
3297 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3298 "' directive");
3299 break;
3300 } else {
3301 // Otherwise we just found the end of an inner macro.
3302 --MacroDepth;
3303 }
3304 } else if (getTok().getIdentifier() == ".macro") {
3305 // We allow nested macros. Those aren't instantiated until the outermost
3306 // macro is expanded so just ignore them for now.
3307 ++MacroDepth;
3308 }
Eli Bendersky17233942013-01-15 22:59:42 +00003309 }
3310
3311 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003312 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003313 }
3314
Jim Grosbach4b905842013-09-20 23:08:21 +00003315 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003316 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3317 }
3318
3319 const char *BodyStart = StartToken.getLoc().getPointer();
3320 const char *BodyEnd = EndToken.getLoc().getPointer();
3321 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003322 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3323 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003324 return false;
3325}
3326
Jim Grosbach4b905842013-09-20 23:08:21 +00003327/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003328///
3329/// With the support added for named parameters there may be code out there that
3330/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003331/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003332/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003333/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003334/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3335/// warning that the positional parameter found in body which have no effect.
3336/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003337/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003338/// intended or change the macro to use the named parameters. It is possible
3339/// this warning will trigger when the none of the named parameters are used
3340/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003341void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003342 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003343 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003344 // If this macro is not defined with named parameters the warning we are
3345 // checking for here doesn't apply.
3346 unsigned NParameters = Parameters.size();
3347 if (NParameters == 0)
3348 return;
3349
3350 bool NamedParametersFound = false;
3351 bool PositionalParametersFound = false;
3352
3353 // Look at the body of the macro for use of both the named parameters and what
3354 // are likely to be positional parameters. This is what expandMacro() is
3355 // doing when it finds the parameters in the body.
3356 while (!Body.empty()) {
3357 // Scan for the next possible parameter.
3358 std::size_t End = Body.size(), Pos = 0;
3359 for (; Pos != End; ++Pos) {
3360 // Check for a substitution or escape.
3361 // This macro is defined with parameters, look for \foo, \bar, etc.
3362 if (Body[Pos] == '\\' && Pos + 1 != End)
3363 break;
3364
3365 // This macro should have parameters, but look for $0, $1, ..., $n too.
3366 if (Body[Pos] != '$' || Pos + 1 == End)
3367 continue;
3368 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003369 if (Next == '$' || Next == 'n' ||
3370 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003371 break;
3372 }
3373
3374 // Check if we reached the end.
3375 if (Pos == End)
3376 break;
3377
3378 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003379 switch (Body[Pos + 1]) {
3380 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003381 case '$':
3382 break;
3383
Jim Grosbach4b905842013-09-20 23:08:21 +00003384 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003385 case 'n':
3386 PositionalParametersFound = true;
3387 break;
3388
Jim Grosbach4b905842013-09-20 23:08:21 +00003389 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003390 default: {
3391 PositionalParametersFound = true;
3392 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003393 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003394 }
3395 Pos += 2;
3396 } else {
3397 unsigned I = Pos + 1;
3398 while (isIdentifierChar(Body[I]) && I + 1 != End)
3399 ++I;
3400
Jim Grosbach4b905842013-09-20 23:08:21 +00003401 const char *Begin = Body.data() + Pos + 1;
3402 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003403 unsigned Index = 0;
3404 for (; Index < NParameters; ++Index)
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00003405 if (Parameters[Index].Name == Argument)
Kevin Enderby81c944c2013-01-22 21:44:53 +00003406 break;
3407
3408 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003409 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3410 Pos += 3;
3411 else {
3412 Pos = I;
3413 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003414 } else {
3415 NamedParametersFound = true;
3416 Pos += 1 + Argument.size();
3417 }
3418 }
3419 // Update the scan point.
3420 Body = Body.substr(Pos);
3421 }
3422
3423 if (!NamedParametersFound && PositionalParametersFound)
3424 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3425 "used in macro body, possible positional parameter "
3426 "found in body which will have no effect");
3427}
3428
Jim Grosbach4b905842013-09-20 23:08:21 +00003429/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003430/// ::= .endm
3431/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003432bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003433 if (getLexer().isNot(AsmToken::EndOfStatement))
3434 return TokError("unexpected token in '" + Directive + "' directive");
3435
3436 // If we are inside a macro instantiation, terminate the current
3437 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003438 if (isInsideMacroInstantiation()) {
3439 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003440 return false;
3441 }
3442
3443 // Otherwise, this .endmacro is a stray entry in the file; well formed
3444 // .endmacro directives are handled during the macro definition parsing.
3445 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003446 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003447}
3448
Jim Grosbach4b905842013-09-20 23:08:21 +00003449/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003450/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003451bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003452 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003453 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003454 return TokError("expected identifier in '.purgem' directive");
3455
3456 if (getLexer().isNot(AsmToken::EndOfStatement))
3457 return TokError("unexpected token in '.purgem' directive");
3458
Jim Grosbach4b905842013-09-20 23:08:21 +00003459 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003460 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3461
Jim Grosbach4b905842013-09-20 23:08:21 +00003462 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003463 return false;
3464}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003465
Jim Grosbach4b905842013-09-20 23:08:21 +00003466/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003467/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003468bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003469 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003470
3471 // Expect a single argument: an expression that evaluates to a constant
3472 // in the inclusive range 0-30.
3473 SMLoc ExprLoc = getLexer().getLoc();
3474 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003475 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003476 return true;
3477 else if (getLexer().isNot(AsmToken::EndOfStatement))
3478 return TokError("unexpected token after expression in"
3479 " '.bundle_align_mode' directive");
3480 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3481 return Error(ExprLoc,
3482 "invalid bundle alignment size (expected between 0 and 30)");
3483
3484 Lex();
3485
3486 // Because of AlignSizePow2's verified range we can safely truncate it to
3487 // unsigned.
3488 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3489 return false;
3490}
3491
Jim Grosbach4b905842013-09-20 23:08:21 +00003492/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003493/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003494bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003495 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003496 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003497
Eli Bendersky802b6282013-01-07 21:51:08 +00003498 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3499 StringRef Option;
3500 SMLoc Loc = getTok().getLoc();
3501 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003502 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003503
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003504 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003505 return Error(Loc, kInvalidOptionError);
3506
3507 if (Option != "align_to_end")
3508 return Error(Loc, kInvalidOptionError);
3509 else if (getLexer().isNot(AsmToken::EndOfStatement))
3510 return Error(Loc,
3511 "unexpected token after '.bundle_lock' directive option");
3512 AlignToEnd = true;
3513 }
3514
Eli Benderskyf483ff92012-12-20 19:05:53 +00003515 Lex();
3516
Eli Bendersky802b6282013-01-07 21:51:08 +00003517 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003518 return false;
3519}
3520
Jim Grosbach4b905842013-09-20 23:08:21 +00003521/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003522/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003523bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003524 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003525
3526 if (getLexer().isNot(AsmToken::EndOfStatement))
3527 return TokError("unexpected token in '.bundle_unlock' directive");
3528 Lex();
3529
3530 getStreamer().EmitBundleUnlock();
3531 return false;
3532}
3533
Jim Grosbach4b905842013-09-20 23:08:21 +00003534/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003535/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003536bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003537 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003538
3539 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003540 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003541 return true;
3542
3543 int64_t FillExpr = 0;
3544 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3545 if (getLexer().isNot(AsmToken::Comma))
3546 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3547 Lex();
3548
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003549 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003550 return true;
3551
3552 if (getLexer().isNot(AsmToken::EndOfStatement))
3553 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3554 }
3555
3556 Lex();
3557
3558 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003559 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3560 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003561
3562 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003563 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003564
3565 return false;
3566}
3567
Jim Grosbach4b905842013-09-20 23:08:21 +00003568/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003569/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003570bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003571 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003572 const MCExpr *Value;
3573
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003574 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003575 return true;
3576
3577 if (getLexer().isNot(AsmToken::EndOfStatement))
3578 return TokError("unexpected token in directive");
3579
3580 if (Signed)
3581 getStreamer().EmitSLEB128Value(Value);
3582 else
3583 getStreamer().EmitULEB128Value(Value);
3584
3585 return false;
3586}
3587
Jim Grosbach4b905842013-09-20 23:08:21 +00003588/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003589/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003590bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003591 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003592 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003593 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003594 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003595
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003596 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003597 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003598
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003599 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003600
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003601 // Assembler local symbols don't make any sense here. Complain loudly.
3602 if (Sym->isTemporary())
3603 return Error(Loc, "non-local symbol required in directive");
3604
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003605 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3606 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003607
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003608 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003609 break;
3610
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003611 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003612 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003613 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003614 }
3615 }
3616
Sean Callanan686ed8d2010-01-19 20:22:31 +00003617 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003618 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003619}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003620
Jim Grosbach4b905842013-09-20 23:08:21 +00003621/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003622/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003623bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003624 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003625
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003626 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003627 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003628 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003629 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003630
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003631 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003632 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003633
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003634 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003635 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003636 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003637
3638 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003639 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003640 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003641 return true;
3642
3643 int64_t Pow2Alignment = 0;
3644 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003645 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003646 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003647 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003648 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003649 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003650
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003651 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3652 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003653 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3654
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003655 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003656 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3657 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003658 if (!isPowerOf2_64(Pow2Alignment))
3659 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3660 Pow2Alignment = Log2_64(Pow2Alignment);
3661 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003662 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003663
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003664 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003665 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003666
Sean Callanan686ed8d2010-01-19 20:22:31 +00003667 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003668
Chris Lattner28ad7542009-07-09 17:25:12 +00003669 // NOTE: a size of zero for a .comm should create a undefined symbol
3670 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003671 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003672 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003673 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003674
Eric Christopherbc818852010-05-14 01:38:54 +00003675 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003676 // may internally end up wanting an alignment in bytes.
3677 // FIXME: Diagnose overflow.
3678 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003679 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003680 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003681
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003682 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003683 return Error(IDLoc, "invalid symbol redefinition");
3684
Chris Lattner28ad7542009-07-09 17:25:12 +00003685 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003686 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003687 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003688 return false;
3689 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003690
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003691 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003692 return false;
3693}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003694
Jim Grosbach4b905842013-09-20 23:08:21 +00003695/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003696/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003697bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003698 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003699 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003700
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003701 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003702 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003703 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003704
Sean Callanan686ed8d2010-01-19 20:22:31 +00003705 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003706
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003707 if (Str.empty())
3708 Error(Loc, ".abort detected. Assembly stopping.");
3709 else
3710 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003711 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003712
3713 return false;
3714}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003715
Jim Grosbach4b905842013-09-20 23:08:21 +00003716/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003717/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003718bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003719 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003720 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003721
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003722 // Allow the strings to have escaped octal character sequence.
3723 std::string Filename;
3724 if (parseEscapedString(Filename))
3725 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003726 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003727 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003728
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003729 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003730 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003731
Chris Lattner693fbb82009-07-16 06:14:39 +00003732 // Attempt to switch the lexer to the included file before consuming the end
3733 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003734 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003735 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003736 return true;
3737 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003738
3739 return false;
3740}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003741
Jim Grosbach4b905842013-09-20 23:08:21 +00003742/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003743/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003744bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003745 if (getLexer().isNot(AsmToken::String))
3746 return TokError("expected string in '.incbin' directive");
3747
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003748 // Allow the strings to have escaped octal character sequence.
3749 std::string Filename;
3750 if (parseEscapedString(Filename))
3751 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003752 SMLoc IncbinLoc = getLexer().getLoc();
3753 Lex();
3754
3755 if (getLexer().isNot(AsmToken::EndOfStatement))
3756 return TokError("unexpected token in '.incbin' directive");
3757
Kevin Enderby109f25c2011-12-14 21:47:48 +00003758 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003759 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003760 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3761 return true;
3762 }
3763
3764 return false;
3765}
3766
Jim Grosbach4b905842013-09-20 23:08:21 +00003767/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003768/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003769bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003770 TheCondStack.push_back(TheCondState);
3771 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003772 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003773 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003774 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003775 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003776 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003777 return true;
3778
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003779 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003780 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003781
Sean Callanan686ed8d2010-01-19 20:22:31 +00003782 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003783
3784 TheCondState.CondMet = ExprValue;
3785 TheCondState.Ignore = !TheCondState.CondMet;
3786 }
3787
3788 return false;
3789}
3790
Jim Grosbach4b905842013-09-20 23:08:21 +00003791/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003792/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003793bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003794 TheCondStack.push_back(TheCondState);
3795 TheCondState.TheCond = AsmCond::IfCond;
3796
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003797 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003798 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003799 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003800 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003801
3802 if (getLexer().isNot(AsmToken::EndOfStatement))
3803 return TokError("unexpected token in '.ifb' directive");
3804
3805 Lex();
3806
3807 TheCondState.CondMet = ExpectBlank == Str.empty();
3808 TheCondState.Ignore = !TheCondState.CondMet;
3809 }
3810
3811 return false;
3812}
3813
Jim Grosbach4b905842013-09-20 23:08:21 +00003814/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003815/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003816bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003817 TheCondStack.push_back(TheCondState);
3818 TheCondState.TheCond = AsmCond::IfCond;
3819
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003820 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003821 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003822 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003823 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003824
3825 if (getLexer().isNot(AsmToken::Comma))
3826 return TokError("unexpected token in '.ifc' directive");
3827
3828 Lex();
3829
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003830 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003831
3832 if (getLexer().isNot(AsmToken::EndOfStatement))
3833 return TokError("unexpected token in '.ifc' directive");
3834
3835 Lex();
3836
3837 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3838 TheCondState.Ignore = !TheCondState.CondMet;
3839 }
3840
3841 return false;
3842}
3843
Jim Grosbach4b905842013-09-20 23:08:21 +00003844/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003845/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003846bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003847 StringRef Name;
3848 TheCondStack.push_back(TheCondState);
3849 TheCondState.TheCond = AsmCond::IfCond;
3850
3851 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003852 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003853 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003854 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003855 return TokError("expected identifier after '.ifdef'");
3856
3857 Lex();
3858
3859 MCSymbol *Sym = getContext().LookupSymbol(Name);
3860
3861 if (expect_defined)
3862 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3863 else
3864 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3865 TheCondState.Ignore = !TheCondState.CondMet;
3866 }
3867
3868 return false;
3869}
3870
Jim Grosbach4b905842013-09-20 23:08:21 +00003871/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003872/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003873bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003874 if (TheCondState.TheCond != AsmCond::IfCond &&
3875 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003876 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3877 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003878 TheCondState.TheCond = AsmCond::ElseIfCond;
3879
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003880 bool LastIgnoreState = false;
3881 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003882 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003883 if (LastIgnoreState || TheCondState.CondMet) {
3884 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003885 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003886 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003887 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003888 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003889 return true;
3890
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003891 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003892 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003893
Sean Callanan686ed8d2010-01-19 20:22:31 +00003894 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003895 TheCondState.CondMet = ExprValue;
3896 TheCondState.Ignore = !TheCondState.CondMet;
3897 }
3898
3899 return false;
3900}
3901
Jim Grosbach4b905842013-09-20 23:08:21 +00003902/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003903/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003904bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003905 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003906 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003907
Sean Callanan686ed8d2010-01-19 20:22:31 +00003908 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003909
3910 if (TheCondState.TheCond != AsmCond::IfCond &&
3911 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003912 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3913 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003914 TheCondState.TheCond = AsmCond::ElseCond;
3915 bool LastIgnoreState = false;
3916 if (!TheCondStack.empty())
3917 LastIgnoreState = TheCondStack.back().Ignore;
3918 if (LastIgnoreState || TheCondState.CondMet)
3919 TheCondState.Ignore = true;
3920 else
3921 TheCondState.Ignore = false;
3922
3923 return false;
3924}
3925
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003926/// parseDirectiveEnd
3927/// ::= .end
3928bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3929 if (getLexer().isNot(AsmToken::EndOfStatement))
3930 return TokError("unexpected token in '.end' directive");
3931
3932 Lex();
3933
3934 while (Lexer.isNot(AsmToken::Eof))
3935 Lex();
3936
3937 return false;
3938}
3939
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00003940/// parseDirectiveErr
3941/// ::= .err
3942bool AsmParser::parseDirectiveErr(SMLoc Loc) {
3943 if (!TheCondStack.empty())
3944 if (TheCondStack.back().Ignore)
3945 return false;
3946 return Error(Loc, ".err encountered");
3947}
3948
Jim Grosbach4b905842013-09-20 23:08:21 +00003949/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003950/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003951bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003952 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003953 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003954
Sean Callanan686ed8d2010-01-19 20:22:31 +00003955 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003956
Jim Grosbach4b905842013-09-20 23:08:21 +00003957 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003958 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3959 ".else");
3960 if (!TheCondStack.empty()) {
3961 TheCondState = TheCondStack.back();
3962 TheCondStack.pop_back();
3963 }
3964
3965 return false;
3966}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003967
Eli Bendersky17233942013-01-15 22:59:42 +00003968void AsmParser::initializeDirectiveKindMap() {
3969 DirectiveKindMap[".set"] = DK_SET;
3970 DirectiveKindMap[".equ"] = DK_EQU;
3971 DirectiveKindMap[".equiv"] = DK_EQUIV;
3972 DirectiveKindMap[".ascii"] = DK_ASCII;
3973 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3974 DirectiveKindMap[".string"] = DK_STRING;
3975 DirectiveKindMap[".byte"] = DK_BYTE;
3976 DirectiveKindMap[".short"] = DK_SHORT;
3977 DirectiveKindMap[".value"] = DK_VALUE;
3978 DirectiveKindMap[".2byte"] = DK_2BYTE;
3979 DirectiveKindMap[".long"] = DK_LONG;
3980 DirectiveKindMap[".int"] = DK_INT;
3981 DirectiveKindMap[".4byte"] = DK_4BYTE;
3982 DirectiveKindMap[".quad"] = DK_QUAD;
3983 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00003984 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00003985 DirectiveKindMap[".single"] = DK_SINGLE;
3986 DirectiveKindMap[".float"] = DK_FLOAT;
3987 DirectiveKindMap[".double"] = DK_DOUBLE;
3988 DirectiveKindMap[".align"] = DK_ALIGN;
3989 DirectiveKindMap[".align32"] = DK_ALIGN32;
3990 DirectiveKindMap[".balign"] = DK_BALIGN;
3991 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3992 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3993 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3994 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3995 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3996 DirectiveKindMap[".org"] = DK_ORG;
3997 DirectiveKindMap[".fill"] = DK_FILL;
3998 DirectiveKindMap[".zero"] = DK_ZERO;
3999 DirectiveKindMap[".extern"] = DK_EXTERN;
4000 DirectiveKindMap[".globl"] = DK_GLOBL;
4001 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00004002 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
4003 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
4004 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
4005 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
4006 DirectiveKindMap[".reference"] = DK_REFERENCE;
4007 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
4008 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
4009 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
4010 DirectiveKindMap[".comm"] = DK_COMM;
4011 DirectiveKindMap[".common"] = DK_COMMON;
4012 DirectiveKindMap[".lcomm"] = DK_LCOMM;
4013 DirectiveKindMap[".abort"] = DK_ABORT;
4014 DirectiveKindMap[".include"] = DK_INCLUDE;
4015 DirectiveKindMap[".incbin"] = DK_INCBIN;
4016 DirectiveKindMap[".code16"] = DK_CODE16;
4017 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
4018 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004019 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00004020 DirectiveKindMap[".irp"] = DK_IRP;
4021 DirectiveKindMap[".irpc"] = DK_IRPC;
4022 DirectiveKindMap[".endr"] = DK_ENDR;
4023 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
4024 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
4025 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
4026 DirectiveKindMap[".if"] = DK_IF;
4027 DirectiveKindMap[".ifb"] = DK_IFB;
4028 DirectiveKindMap[".ifnb"] = DK_IFNB;
4029 DirectiveKindMap[".ifc"] = DK_IFC;
4030 DirectiveKindMap[".ifnc"] = DK_IFNC;
4031 DirectiveKindMap[".ifdef"] = DK_IFDEF;
4032 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
4033 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
4034 DirectiveKindMap[".elseif"] = DK_ELSEIF;
4035 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00004036 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00004037 DirectiveKindMap[".endif"] = DK_ENDIF;
4038 DirectiveKindMap[".skip"] = DK_SKIP;
4039 DirectiveKindMap[".space"] = DK_SPACE;
4040 DirectiveKindMap[".file"] = DK_FILE;
4041 DirectiveKindMap[".line"] = DK_LINE;
4042 DirectiveKindMap[".loc"] = DK_LOC;
4043 DirectiveKindMap[".stabs"] = DK_STABS;
4044 DirectiveKindMap[".sleb128"] = DK_SLEB128;
4045 DirectiveKindMap[".uleb128"] = DK_ULEB128;
4046 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
4047 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
4048 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
4049 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
4050 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
4051 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
4052 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
4053 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
4054 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
4055 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
4056 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
4057 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
4058 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
4059 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
4060 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
4061 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
4062 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
4063 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
4064 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00004065 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00004066 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
4067 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
4068 DirectiveKindMap[".macro"] = DK_MACRO;
4069 DirectiveKindMap[".endm"] = DK_ENDM;
4070 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
4071 DirectiveKindMap[".purgem"] = DK_PURGEM;
Saleem Abdulrasoolb2ae2c02014-02-23 15:53:30 +00004072 DirectiveKindMap[".err"] = DK_ERR;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00004073}
4074
Jim Grosbach4b905842013-09-20 23:08:21 +00004075MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004076 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004077
Rafael Espindola34b9c512012-06-03 23:57:14 +00004078 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004079 for (;;) {
4080 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00004081 if (getLexer().is(AsmToken::Eof)) {
4082 Error(DirectiveLoc, "no matching '.endr' in definition");
4083 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004084 }
4085
Rafael Espindola34b9c512012-06-03 23:57:14 +00004086 if (Lexer.is(AsmToken::Identifier) &&
4087 (getTok().getIdentifier() == ".rept")) {
4088 ++NestLevel;
4089 }
4090
4091 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00004092 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004093 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004094 EndToken = getTok();
4095 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004096 if (Lexer.isNot(AsmToken::EndOfStatement)) {
4097 TokError("unexpected token in '.endr' directive");
4098 return 0;
4099 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004100 break;
4101 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004102 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004103 }
4104
Rafael Espindola34b9c512012-06-03 23:57:14 +00004105 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004106 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004107 }
4108
4109 const char *BodyStart = StartToken.getLoc().getPointer();
4110 const char *BodyEnd = EndToken.getLoc().getPointer();
4111 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4112
Rafael Espindola34b9c512012-06-03 23:57:14 +00004113 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004114 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004115 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004116}
4117
Jim Grosbach4b905842013-09-20 23:08:21 +00004118void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004119 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004120 OS << ".endr\n";
4121
4122 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004123 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004124
Rafael Espindola34b9c512012-06-03 23:57:14 +00004125 // Create the macro instantiation object and add to the current macro
4126 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004127 MacroInstantiation *MI = new MacroInstantiation(
4128 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004129 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004130
Rafael Espindola34b9c512012-06-03 23:57:14 +00004131 // Jump to the macro instantiation and prime the lexer.
4132 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4133 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4134 Lex();
4135}
4136
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004137/// parseDirectiveRept
4138/// ::= .rep | .rept count
4139bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004140 const MCExpr *CountExpr;
4141 SMLoc CountLoc = getTok().getLoc();
4142 if (parseExpression(CountExpr))
4143 return true;
4144
Rafael Espindola34b9c512012-06-03 23:57:14 +00004145 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004146 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4147 eatToEndOfStatement();
4148 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4149 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004150
4151 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004152 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004153
4154 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004155 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004156
4157 // Eat the end of statement.
4158 Lex();
4159
4160 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004161 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004162 if (!M)
4163 return true;
4164
4165 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4166 // to hold the macro body with substitutions.
4167 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004168 raw_svector_ostream OS(Buf);
4169 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004170 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004171 return true;
4172 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004173 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004174
4175 return false;
4176}
4177
Jim Grosbach4b905842013-09-20 23:08:21 +00004178/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004179/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004180bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004181 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004182
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004183 if (parseIdentifier(Parameter.Name))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004184 return TokError("expected identifier in '.irp' directive");
4185
Rafael Espindola768b41c2012-06-15 14:02:34 +00004186 if (Lexer.isNot(AsmToken::Comma))
4187 return TokError("expected comma in '.irp' directive");
4188
4189 Lex();
4190
Eli Bendersky38274122013-01-14 23:22:36 +00004191 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004192 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004193 return true;
4194
4195 // Eat the end of statement.
4196 Lex();
4197
4198 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004199 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004200 if (!M)
4201 return true;
4202
4203 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4204 // to hold the macro body with substitutions.
4205 SmallString<256> Buf;
4206 raw_svector_ostream OS(Buf);
4207
Eli Bendersky38274122013-01-14 23:22:36 +00004208 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004209 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004210 return true;
4211 }
4212
Jim Grosbach4b905842013-09-20 23:08:21 +00004213 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004214
4215 return false;
4216}
4217
Jim Grosbach4b905842013-09-20 23:08:21 +00004218/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004219/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004220bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004221 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004222
Saleem Abdulrasoola08585b2014-02-19 03:00:23 +00004223 if (parseIdentifier(Parameter.Name))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004224 return TokError("expected identifier in '.irpc' directive");
4225
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004226 if (Lexer.isNot(AsmToken::Comma))
4227 return TokError("expected comma in '.irpc' directive");
4228
4229 Lex();
4230
Eli Bendersky38274122013-01-14 23:22:36 +00004231 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004232 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004233 return true;
4234
4235 if (A.size() != 1 || A.front().size() != 1)
4236 return TokError("unexpected token in '.irpc' directive");
4237
4238 // Eat the end of statement.
4239 Lex();
4240
4241 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004242 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004243 if (!M)
4244 return true;
4245
4246 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4247 // to hold the macro body with substitutions.
4248 SmallString<256> Buf;
4249 raw_svector_ostream OS(Buf);
4250
4251 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004252 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004253 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004254 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004255
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004256 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004257 return true;
4258 }
4259
Jim Grosbach4b905842013-09-20 23:08:21 +00004260 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004261
4262 return false;
4263}
4264
Jim Grosbach4b905842013-09-20 23:08:21 +00004265bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004266 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004267 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004268
4269 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004270 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004271 assert(getLexer().is(AsmToken::EndOfStatement));
4272
Jim Grosbach4b905842013-09-20 23:08:21 +00004273 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004274 return false;
4275}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004276
Jim Grosbach4b905842013-09-20 23:08:21 +00004277bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004278 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004279 const MCExpr *Value;
4280 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004281 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004282 return true;
4283 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4284 if (!MCE)
4285 return Error(ExprLoc, "unexpected expression in _emit");
4286 uint64_t IntValue = MCE->getValue();
4287 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4288 return Error(ExprLoc, "literal value out of range for directive");
4289
Chad Rosierc7f552c2013-02-12 21:33:51 +00004290 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4291 return false;
4292}
4293
Jim Grosbach4b905842013-09-20 23:08:21 +00004294bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004295 const MCExpr *Value;
4296 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004297 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004298 return true;
4299 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4300 if (!MCE)
4301 return Error(ExprLoc, "unexpected expression in align");
4302 uint64_t IntValue = MCE->getValue();
4303 if (!isPowerOf2_64(IntValue))
4304 return Error(ExprLoc, "literal value not a power of two greater then zero");
4305
Jim Grosbach4b905842013-09-20 23:08:21 +00004306 Info.AsmRewrites->push_back(
4307 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004308 return false;
4309}
4310
Chad Rosierf43fcf52013-02-13 21:27:17 +00004311// We are comparing pointers, but the pointers are relative to a single string.
4312// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004313static int rewritesSort(const AsmRewrite *AsmRewriteA,
4314 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004315 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4316 return -1;
4317 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4318 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004319
Chad Rosierfce4fab2013-04-08 17:43:47 +00004320 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4321 // rewrite to the same location. Make sure the SizeDirective rewrite is
4322 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4323 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004324 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4325 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004326 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004327
Jim Grosbach4b905842013-09-20 23:08:21 +00004328 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4329 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004330 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004331 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004332}
4333
Jim Grosbach4b905842013-09-20 23:08:21 +00004334bool AsmParser::parseMSInlineAsm(
4335 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4336 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4337 SmallVectorImpl<std::string> &Constraints,
4338 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4339 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004340 SmallVector<void *, 4> InputDecls;
4341 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004342 SmallVector<bool, 4> InputDeclsAddressOf;
4343 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004344 SmallVector<std::string, 4> InputConstraints;
4345 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004346 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004347
Benjamin Kramer1a136112013-02-15 20:37:21 +00004348 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004349
4350 // Prime the lexer.
4351 Lex();
4352
4353 // While we have input, parse each statement.
4354 unsigned InputIdx = 0;
4355 unsigned OutputIdx = 0;
4356 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004357 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004358 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004359 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004360
Chad Rosier149e8e02012-12-12 22:45:52 +00004361 if (Info.ParseError)
4362 return true;
4363
Benjamin Kramer1a136112013-02-15 20:37:21 +00004364 if (Info.Opcode == ~0U)
4365 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004366
Benjamin Kramer1a136112013-02-15 20:37:21 +00004367 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004368
Benjamin Kramer1a136112013-02-15 20:37:21 +00004369 // Build the list of clobbers, outputs and inputs.
4370 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4371 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004372
Benjamin Kramer1a136112013-02-15 20:37:21 +00004373 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004374 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004375 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004376
Benjamin Kramer1a136112013-02-15 20:37:21 +00004377 // Register operand.
4378 if (Operand->isReg() && !Operand->needAddressOf()) {
4379 unsigned NumDefs = Desc.getNumDefs();
4380 // Clobber.
4381 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4382 ClobberRegs.push_back(Operand->getReg());
4383 continue;
4384 }
4385
4386 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004387 StringRef SymName = Operand->getSymName();
4388 if (SymName.empty())
4389 continue;
4390
Chad Rosierdba3fe52013-04-22 22:12:12 +00004391 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004392 if (!OpDecl)
4393 continue;
4394
4395 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004396 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004397 if (isOutput) {
4398 ++InputIdx;
4399 OutputDecls.push_back(OpDecl);
4400 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4401 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004402 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004403 } else {
4404 InputDecls.push_back(OpDecl);
4405 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4406 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004407 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004408 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004409 }
Reid Kleckneree088972013-12-10 18:27:32 +00004410
4411 // Consider implicit defs to be clobbers. Think of cpuid and push.
4412 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4413 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4414 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004415 }
4416
4417 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004418 NumOutputs = OutputDecls.size();
4419 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004420
4421 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004422 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4423 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4424 ClobberRegs.end());
4425 Clobbers.assign(ClobberRegs.size(), std::string());
4426 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4427 raw_string_ostream OS(Clobbers[I]);
4428 IP->printRegName(OS, ClobberRegs[I]);
4429 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004430
4431 // Merge the various outputs and inputs. Output are expected first.
4432 if (NumOutputs || NumInputs) {
4433 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004434 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004435 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004436 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004437 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004438 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004439 }
4440 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004441 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004442 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004443 }
4444 }
4445
4446 // Build the IR assembly string.
4447 std::string AsmStringIR;
4448 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004449 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4450 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004451 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004452 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4453 E = AsmStrRewrites.end();
4454 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004455 AsmRewriteKind Kind = (*I).Kind;
4456 if (Kind == AOK_Delete)
4457 continue;
4458
Chad Rosier8bce6642012-10-18 15:49:34 +00004459 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004460 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004461
Chad Rosier120eefd2013-03-19 17:32:17 +00004462 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004463 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004464 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004465 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004466
Chad Rosier37e755c2012-10-23 17:43:43 +00004467 // Skip the original expression.
4468 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004469 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004470 continue;
4471 }
4472
Chad Rosierff10ed12013-04-12 16:26:42 +00004473 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004474 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004475 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004476 default:
4477 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004478 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004479 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004480 break;
4481 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004482 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004483 break;
4484 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004485 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004486 break;
4487 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004488 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004489 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004490 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004491 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004492 default: break;
4493 case 8: OS << "byte ptr "; break;
4494 case 16: OS << "word ptr "; break;
4495 case 32: OS << "dword ptr "; break;
4496 case 64: OS << "qword ptr "; break;
4497 case 80: OS << "xword ptr "; break;
4498 case 128: OS << "xmmword ptr "; break;
4499 case 256: OS << "ymmword ptr "; break;
4500 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004501 break;
4502 case AOK_Emit:
4503 OS << ".byte";
4504 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004505 case AOK_Align: {
4506 unsigned Val = (*I).Val;
4507 OS << ".align " << Val;
4508
4509 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004510 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004511 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4512 break;
4513 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004514 case AOK_DotOperator:
4515 OS << (*I).Val;
4516 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004517 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004518
Chad Rosier8bce6642012-10-18 15:49:34 +00004519 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004520 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004521 }
4522
4523 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004524 if (AsmStart != AsmEnd)
4525 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004526
4527 AsmString = OS.str();
4528 return false;
4529}
4530
Daniel Dunbar01e36072010-07-17 02:26:10 +00004531/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004532MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4533 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004534 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004535}