blob: 03b004ecfac62ba0520a55a62c251ada94680494 [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 {
54
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
58typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
59typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
60
61struct MCAsmMacro {
62 StringRef Name;
63 StringRef Body;
64 MCAsmMacroParameters Parameters;
65
66public:
Benjamin Kramerd31aaf12014-02-09 17:13:11 +000067 MCAsmMacro(StringRef N, StringRef B, ArrayRef<MCAsmMacroParameter> P) :
Eli Benderskya313ae62013-01-16 18:56:50 +000068 Name(N), Body(B), Parameters(P) {}
Eli Benderskya313ae62013-01-16 18:56:50 +000069};
70
Daniel Dunbar43235712010-07-18 18:54:11 +000071/// \brief Helper class for storing information about an active macro
72/// instantiation.
73struct MacroInstantiation {
74 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000075 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000076
77 /// The macro instantiation with substitutions.
78 MemoryBuffer *Instantiation;
79
80 /// The location of the instantiation.
81 SMLoc InstantiationLoc;
82
Daniel Dunbar40f1d852012-12-01 01:38:48 +000083 /// The buffer where parsing should resume upon instantiation completion.
84 int ExitBuffer;
85
Daniel Dunbar43235712010-07-18 18:54:11 +000086 /// The location where parsing should resume upon instantiation completion.
87 SMLoc ExitLoc;
88
89public:
Eli Bendersky38274122013-01-14 23:22:36 +000090 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000091 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000092};
93
Eli Friedman0f4871d2012-10-22 23:58:19 +000094struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000095 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +000096 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
97
Jim Grosbach4b905842013-09-20 23:08:21 +000098 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +000099 unsigned Opcode;
100
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000102 bool ParseError;
103
Eli Friedman0f4871d2012-10-22 23:58:19 +0000104 SmallVectorImpl<AsmRewrite> *AsmRewrites;
105
Chad Rosier149e8e02012-12-12 22:45:52 +0000106 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000108 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000109
110 ~ParseStatementInfo() {
111 // Free any parsed operands.
112 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
113 delete ParsedOperands[i];
114 ParsedOperands.clear();
115 }
116};
117
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000118/// \brief The concrete assembly parser instance.
119class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000120 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
121 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000122private:
123 AsmLexer Lexer;
124 MCContext &Ctx;
125 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000126 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000127 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000128 SourceMgr::DiagHandlerTy SavedDiagHandler;
129 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000131
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000132 /// This is the current buffer index we're lexing from as managed by the
133 /// SourceMgr object.
134 int CurBuffer;
135
136 AsmCond TheCondState;
137 std::vector<AsmCond> TheCondStack;
138
Jim Grosbach4b905842013-09-20 23:08:21 +0000139 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000140 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000141 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000142 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000143
Jim Grosbach4b905842013-09-20 23:08:21 +0000144 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000145 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000146
Jim Grosbach4b905842013-09-20 23:08:21 +0000147 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000148 std::vector<MacroInstantiation*> ActiveMacros;
149
Jim Grosbach4b905842013-09-20 23:08:21 +0000150 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000151 std::deque<MCAsmMacro> MacroLikeBodies;
152
Daniel Dunbar828984f2010-07-18 18:38:02 +0000153 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000154 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000155
Daniel Dunbar43325c42010-09-09 22:42:56 +0000156 /// Flag tracking whether any errors have been encountered.
157 unsigned HadError : 1;
158
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000159 /// The values from the last parsed cpp hash file line comment if any.
160 StringRef CppHashFilename;
161 int64_t CppHashLineNumber;
162 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000163 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000164 /// When generating dwarf for assembly source files we need to calculate the
165 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000166 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000167 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
168 SMLoc LastQueryIDLoc;
169 int LastQueryBuffer;
170 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000171
Devang Patela173ee52012-01-31 18:14:05 +0000172 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
173 unsigned AssemblerDialect;
174
Jim Grosbach4b905842013-09-20 23:08:21 +0000175 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000176 bool IsDarwin;
177
Jim Grosbach4b905842013-09-20 23:08:21 +0000178 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000179 bool ParsingInlineAsm;
180
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000181public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000182 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000183 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000184 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000185
186 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
187
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000188 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000189 ExtensionDirectiveHandler Handler) {
190 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000191 }
192
193public:
194 /// @name MCAsmParser Interface
195 /// {
196
197 virtual SourceMgr &getSourceManager() { return SrcMgr; }
198 virtual MCAsmLexer &getLexer() { return Lexer; }
199 virtual MCContext &getContext() { return Ctx; }
200 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000201 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000202 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000203 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000204 else
205 return AssemblerDialect;
206 }
207 virtual void setAssemblerDialect(unsigned i) {
208 AssemblerDialect = i;
209 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000210
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000211 virtual void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000212 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000213 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000214 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000215 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000216
Craig Topper5f96ca52012-08-29 05:48:09 +0000217 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Chad Rosier49963552012-10-13 00:26:04 +0000219 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000220 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000221
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000222 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000223 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000224 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000225 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 SmallVectorImpl<std::string> &Clobbers,
227 const MCInstrInfo *MII,
228 const MCInstPrinter *IP,
229 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000230
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000231 bool parseExpression(const MCExpr *&Res);
232 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000233 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000234 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
235 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000236
Jim Grosbach4b905842013-09-20 23:08:21 +0000237 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000238 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000239 virtual bool parseIdentifier(StringRef &Res);
240 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000241
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000242 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000243 /// }
244
245private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000246
Jim Grosbach4b905842013-09-20 23:08:21 +0000247 bool parseStatement(ParseStatementInfo &Info);
248 void eatToEndOfLine();
249 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000250
Jim Grosbach4b905842013-09-20 23:08:21 +0000251 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000252 ArrayRef<MCAsmMacroParameter> Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000253 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +0000254 ArrayRef<MCAsmMacroParameter> Parameters,
255 ArrayRef<MCAsmMacroArgument> A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000256 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000257
Eli Benderskya313ae62013-01-16 18:56:50 +0000258 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000259 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000260
261 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000263
264 /// \brief Lookup a previously defined macro.
265 /// \param Name Macro name.
266 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000267 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000268
269 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000270 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000271
272 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000273 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000274
275 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000276 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000278 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000279 ///
280 /// \param M The macro.
281 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000282 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000283
284 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000285 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000286
David Majnemer91fc4c22014-01-29 18:57:46 +0000287 /// \brief Extract AsmTokens for a macro argument.
288 bool parseMacroArgument(MCAsmMacroArgument &MA);
Eli Benderskya313ae62013-01-16 18:56:50 +0000289
290 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000291 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000292
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 void printMacroInstantiations();
294 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000295 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000296 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000297 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000298 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000299
Jim Grosbach4b905842013-09-20 23:08:21 +0000300 /// \brief Enter the specified file. This returns true on failure.
301 bool enterIncludeFile(const std::string &Filename);
302
303 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000304 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000305 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000306
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000307 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000308 /// current token is not set; clients should ensure Lex() is called
309 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000310 ///
311 /// \param InBuffer If not -1, should be the known buffer id that contains the
312 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000313 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000314
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000315 /// \brief Parse up to the end of statement and a return the contents from the
316 /// current token until the end of the statement; the current token on exit
317 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000318 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000319
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000320 /// \brief Parse until the end of a statement or a comma is encountered,
321 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000322 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000323
Jim Grosbach4b905842013-09-20 23:08:21 +0000324 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000325 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000326
Jim Grosbach4b905842013-09-20 23:08:21 +0000327 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
328 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
329 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000330
Jim Grosbach4b905842013-09-20 23:08:21 +0000331 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000332
Eli Bendersky17233942013-01-15 22:59:42 +0000333 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000334 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000335 DK_NO_DIRECTIVE, // Placeholder
336 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
David Woodhoused6de0d92014-02-01 16:20:59 +0000337 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
338 DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000339 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000340 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000341 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000342 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
343 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
344 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
345 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
346 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky17233942013-01-15 22:59:42 +0000347 DK_ELSEIF, DK_ELSE, DK_ENDIF,
348 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
349 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
350 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
351 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
352 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
353 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000354 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000355 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000356 DK_SLEB128, DK_ULEB128,
357 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000358 };
359
Jim Grosbach4b905842013-09-20 23:08:21 +0000360 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000361 /// directives parsed by this class.
362 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000363
364 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000365 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
366 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000367 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
369 bool parseDirectiveFill(); // ".fill"
370 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000371 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000372 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
373 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000374 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000376
Eli Bendersky17233942013-01-15 22:59:42 +0000377 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000378 bool parseDirectiveFile(SMLoc DirectiveLoc);
379 bool parseDirectiveLine();
380 bool parseDirectiveLoc();
381 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000382
383 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000384 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000385 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveCFISections();
387 bool parseDirectiveCFIStartProc();
388 bool parseDirectiveCFIEndProc();
389 bool parseDirectiveCFIDefCfaOffset();
390 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
391 bool parseDirectiveCFIAdjustCfaOffset();
392 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
394 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
396 bool parseDirectiveCFIRememberState();
397 bool parseDirectiveCFIRestoreState();
398 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIEscape();
401 bool parseDirectiveCFISignalFrame();
402 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000403
404 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000405 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
406 bool parseDirectiveEndMacro(StringRef Directive);
407 bool parseDirectiveMacro(SMLoc DirectiveLoc);
408 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000409
Eli Benderskyf483ff92012-12-20 19:05:53 +0000410 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000411 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000412 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416
Eli Bendersky17233942013-01-15 22:59:42 +0000417 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000419
420 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000422
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000424 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000426
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveAbort(); // ".abort"
430 bool parseDirectiveInclude(); // ".include"
431 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000432
Jim Grosbach4b905842013-09-20 23:08:21 +0000433 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000434 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000436 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000438 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
440 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
441 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
442 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000443 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000444
Jim Grosbach4b905842013-09-20 23:08:21 +0000445 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000446 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000447
Rafael Espindola34b9c512012-06-03 23:57:14 +0000448 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000449 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
450 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000451 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000452 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000453 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
454 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
455 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000456
Chad Rosierc7f552c2013-02-12 21:33:51 +0000457 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000459 size_t Len);
460
461 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000462 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000463
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000464 // "end"
465 bool parseDirectiveEnd(SMLoc DirectiveLoc);
466
Eli Bendersky17233942013-01-15 22:59:42 +0000467 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000468};
Daniel Dunbar86033402010-07-12 17:54:38 +0000469}
470
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000471namespace llvm {
472
473extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000474extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000475extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000476
477}
478
Chris Lattnerc35681b2010-01-19 19:46:13 +0000479enum { DEFAULT_ADDRSPACE = 0 };
480
Jim Grosbach4b905842013-09-20 23:08:21 +0000481AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
482 const MCAsmInfo &_MAI)
483 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
484 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
485 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
486 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000487 // Save the old handler.
488 SavedDiagHandler = SrcMgr.getDiagHandler();
489 SavedDiagContext = SrcMgr.getDiagContext();
490 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000491 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000492 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000493
Daniel Dunbarc5011082010-07-12 18:12:02 +0000494 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000495 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
496 case MCObjectFileInfo::IsCOFF:
497 PlatformParser = createCOFFAsmParser();
498 PlatformParser->Initialize(*this);
499 break;
500 case MCObjectFileInfo::IsMachO:
501 PlatformParser = createDarwinAsmParser();
502 PlatformParser->Initialize(*this);
503 IsDarwin = true;
504 break;
505 case MCObjectFileInfo::IsELF:
506 PlatformParser = createELFAsmParser();
507 PlatformParser->Initialize(*this);
508 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000509 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000510
Eli Bendersky17233942013-01-15 22:59:42 +0000511 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000512}
513
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000514AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000515 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
516
517 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000518 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
519 ie = MacroMap.end();
520 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000521 delete it->getValue();
522
Daniel Dunbarc5011082010-07-12 18:12:02 +0000523 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000524}
525
Jim Grosbach4b905842013-09-20 23:08:21 +0000526void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000527 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000528 for (std::vector<MacroInstantiation *>::const_reverse_iterator
529 it = ActiveMacros.rbegin(),
530 ie = ActiveMacros.rend();
531 it != ie; ++it)
532 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000533 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000534}
535
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000536void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
537 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
538 printMacroInstantiations();
539}
540
Chris Lattnera3a06812011-10-16 04:47:35 +0000541bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000542 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000543 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000544 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
545 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000546 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000547}
548
Chris Lattnera3a06812011-10-16 04:47:35 +0000549bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000550 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000551 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
552 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000553 return true;
554}
555
Jim Grosbach4b905842013-09-20 23:08:21 +0000556bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000557 std::string IncludedFile;
558 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000559 if (NewBuf == -1)
560 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000561
Sean Callanan7a77eae2010-01-21 00:19:58 +0000562 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000563
Sean Callanan7a77eae2010-01-21 00:19:58 +0000564 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000565
Sean Callanan7a77eae2010-01-21 00:19:58 +0000566 return false;
567}
Daniel Dunbar43235712010-07-18 18:54:11 +0000568
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000569/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000570/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000571/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000572bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000573 std::string IncludedFile;
574 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
575 if (NewBuf == -1)
576 return true;
577
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000578 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000579 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000580 return false;
581}
582
Jim Grosbach4b905842013-09-20 23:08:21 +0000583void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000584 if (InBuffer != -1) {
585 CurBuffer = InBuffer;
586 } else {
587 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
588 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000589 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
590}
591
Sean Callanan7a77eae2010-01-21 00:19:58 +0000592const AsmToken &AsmParser::Lex() {
593 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000594
Sean Callanan7a77eae2010-01-21 00:19:58 +0000595 if (tok->is(AsmToken::Eof)) {
596 // If this is the end of an included file, pop the parent file off the
597 // include stack.
598 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
599 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000600 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000601 tok = &Lexer.Lex();
602 }
603 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000604
Sean Callanan7a77eae2010-01-21 00:19:58 +0000605 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000606 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000609}
610
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000611bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000612 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000613 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000614 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000615
Chris Lattner36e02122009-06-21 20:54:55 +0000616 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000617 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000618
619 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000620 AsmCond StartingCondState = TheCondState;
621
Kevin Enderby6469fc22011-11-01 22:27:22 +0000622 // If we are generating dwarf for assembly source files save the initial text
623 // section and generate a .file directive.
624 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000625 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000626 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
627 getStreamer().EmitLabel(SectionStartSym);
628 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000629 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000630 StringRef(),
631 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000632 }
633
Chris Lattner73f36112009-07-02 21:53:43 +0000634 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000635 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000636 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000637 if (!parseStatement(Info))
638 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000639
Daniel Dunbar43325c42010-09-09 22:42:56 +0000640 // We had an error, validate that one was emitted and recover by skipping to
641 // the next line.
642 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000643 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000644 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000645
646 if (TheCondState.TheCond != StartingCondState.TheCond ||
647 TheCondState.Ignore != StartingCondState.Ignore)
648 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000649
650 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000651 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000652 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000653 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000654 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000655 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000656 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000657
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000658 // Check to see that all assembler local symbols were actually defined.
659 // Targets that don't do subsections via symbols may not want this, though,
660 // so conservatively exclude them. Only do this if we're finalizing, though,
661 // as otherwise we won't necessarilly have seen everything yet.
662 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
663 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
664 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000665 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000666 i != e; ++i) {
667 MCSymbol *Sym = i->getValue();
668 // Variable symbols may not be marked as defined, so check those
669 // explicitly. If we know it's a variable, we have a definition for
670 // the purposes of this check.
671 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
672 // FIXME: We would really like to refer back to where the symbol was
673 // first referenced for a source location. We need to add something
674 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000675 printMessage(
676 getLexer().getLoc(), SourceMgr::DK_Error,
677 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000678 }
679 }
680
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000681 // Finalize the output stream if there are no errors and if the client wants
682 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000683 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000684 Out.Finish();
685
Chris Lattner73f36112009-07-02 21:53:43 +0000686 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000687}
688
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000689void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000690 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000691 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000692 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000693 }
694}
695
Jim Grosbach4b905842013-09-20 23:08:21 +0000696/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000697void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000698 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000699 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000700
Chris Lattnere5074c42009-06-22 01:29:09 +0000701 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000702 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000703 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000704}
705
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000706StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000707 const char *Start = getTok().getLoc().getPointer();
708
Jim Grosbach4b905842013-09-20 23:08:21 +0000709 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000710 Lex();
711
712 const char *End = getTok().getLoc().getPointer();
713 return StringRef(Start, End - Start);
714}
Chris Lattner78db3622009-06-22 05:51:26 +0000715
Jim Grosbach4b905842013-09-20 23:08:21 +0000716StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000717 const char *Start = getTok().getLoc().getPointer();
718
719 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000720 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000721 Lex();
722
723 const char *End = getTok().getLoc().getPointer();
724 return StringRef(Start, End - Start);
725}
726
Jim Grosbach4b905842013-09-20 23:08:21 +0000727/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000728/// NOTE: This assumes the leading '(' has already been consumed.
729///
730/// parenexpr ::= expr)
731///
Jim Grosbach4b905842013-09-20 23:08:21 +0000732bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
733 if (parseExpression(Res))
734 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000735 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000736 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000737 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000738 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000739 return false;
740}
Chris Lattner78db3622009-06-22 05:51:26 +0000741
Jim Grosbach4b905842013-09-20 23:08:21 +0000742/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000743/// NOTE: This assumes the leading '[' has already been consumed.
744///
745/// bracketexpr ::= expr]
746///
Jim Grosbach4b905842013-09-20 23:08:21 +0000747bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
748 if (parseExpression(Res))
749 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000750 if (Lexer.isNot(AsmToken::RBrac))
751 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000752 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000753 Lex();
754 return false;
755}
756
Jim Grosbach4b905842013-09-20 23:08:21 +0000757/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000758/// primaryexpr ::= (parenexpr
759/// primaryexpr ::= symbol
760/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000761/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000762/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000763bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000764 SMLoc FirstTokenLoc = getLexer().getLoc();
765 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
766 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000767 default:
768 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000769 // If we have an error assume that we've already handled it.
770 case AsmToken::Error:
771 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000772 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000773 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000774 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000775 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000776 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000777 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000778 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000779 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000780 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000781 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000782 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000783 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000784 if (FirstTokenKind == AsmToken::Dollar) {
785 if (Lexer.getMAI().getDollarIsPC()) {
786 // This is a '$' reference, which references the current PC. Emit a
787 // temporary label to the streamer and refer to it.
788 MCSymbol *Sym = Ctx.CreateTempSymbol();
789 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000790 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
791 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000792 EndLoc = FirstTokenLoc;
793 return false;
794 } else
795 return Error(FirstTokenLoc, "invalid token in expression");
796 return true;
797 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000798 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000799 // Parse symbol variant
800 std::pair<StringRef, StringRef> Split;
801 if (!MAI.useParensForSymbolVariant()) {
802 Split = Identifier.split('@');
803 } else if (Lexer.is(AsmToken::LParen)) {
804 Lexer.Lex(); // eat (
805 StringRef VName;
806 parseIdentifier(VName);
807 if (Lexer.isNot(AsmToken::RParen)) {
808 return Error(Lexer.getTok().getLoc(),
809 "unexpected token in variant, expected ')'");
810 }
811 Lexer.Lex(); // eat )
812 Split = std::make_pair(Identifier, VName);
813 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000814
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000815 EndLoc = SMLoc::getFromPointer(Identifier.end());
816
Daniel Dunbard20cda02009-10-16 01:34:54 +0000817 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000818 StringRef SymbolName = Identifier;
819 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000820
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000821 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000822 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000823 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000824 if (Variant != MCSymbolRefExpr::VK_Invalid) {
825 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000826 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000827 Variant = MCSymbolRefExpr::VK_None;
828 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000829 Variant = MCSymbolRefExpr::VK_None;
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000830 return Error(SMLoc::getFromPointer(Split.second.begin()),
831 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000832 }
833 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000834
Hans Wennborgce69d772013-10-18 20:46:28 +0000835 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
836
Daniel Dunbard20cda02009-10-16 01:34:54 +0000837 // If this is an absolute variable reference, substitute it now to preserve
838 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000839 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000840 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000841 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000842
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000843 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000844 return false;
845 }
846
847 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000848 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000849 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000850 }
David Woodhousef42a6662014-02-01 16:20:54 +0000851 case AsmToken::BigNum:
852 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000853 case AsmToken::Integer: {
854 SMLoc Loc = getTok().getLoc();
855 int64_t IntVal = getTok().getIntVal();
856 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000857 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000858 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000859 // Look for 'b' or 'f' following an Integer as a directional label
860 if (Lexer.getKind() == AsmToken::Identifier) {
861 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000862 // Lookup the symbol variant if used.
863 std::pair<StringRef, StringRef> Split = IDVal.split('@');
864 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
865 if (Split.first.size() != IDVal.size()) {
866 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
867 if (Variant == MCSymbolRefExpr::VK_Invalid) {
868 Variant = MCSymbolRefExpr::VK_None;
869 return TokError("invalid variant '" + Split.second + "'");
870 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000871 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000872 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000873 if (IDVal == "f" || IDVal == "b") {
874 MCSymbol *Sym =
875 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000876 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000877 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000878 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000879 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000880 Lex(); // Eat identifier.
881 }
882 }
Chris Lattner78db3622009-06-22 05:51:26 +0000883 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000885 case AsmToken::Real: {
886 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000887 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000888 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000889 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000890 Lex(); // Eat token.
891 return false;
892 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000893 case AsmToken::Dot: {
894 // This is a '.' reference, which references the current PC. Emit a
895 // temporary label to the streamer and refer to it.
896 MCSymbol *Sym = Ctx.CreateTempSymbol();
897 Out.EmitLabel(Sym);
898 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000899 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000900 Lex(); // Eat identifier.
901 return false;
902 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000903 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000904 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000905 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000906 case AsmToken::LBrac:
907 if (!PlatformParser->HasBracketExpressions())
908 return TokError("brackets expression not supported on this target");
909 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000910 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000911 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000912 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000913 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000914 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000915 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000916 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000917 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000918 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000921 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000922 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000923 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000924 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000925 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000927 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000928 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000929 }
930}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000931
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000932bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000933 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000934 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000935}
936
Daniel Dunbar55f16672010-09-17 02:47:07 +0000937const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000938AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000939 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000940 // Ask the target implementation about this expression first.
941 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
942 if (NewE)
943 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000944 // Recurse over the given expression, rebuilding it to apply the given variant
945 // if there is exactly one symbol.
946 switch (E->getKind()) {
947 case MCExpr::Target:
948 case MCExpr::Constant:
949 return 0;
950
951 case MCExpr::SymbolRef: {
952 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
953
954 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000955 TokError("invalid variant on expression '" + getTok().getIdentifier() +
956 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000957 return E;
958 }
959
960 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
961 }
962
963 case MCExpr::Unary: {
964 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000965 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000966 if (!Sub)
967 return 0;
968 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
969 }
970
971 case MCExpr::Binary: {
972 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000973 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
974 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975
976 if (!LHS && !RHS)
977 return 0;
978
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 if (!LHS)
980 LHS = BE->getLHS();
981 if (!RHS)
982 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000983
984 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
985 }
986 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000987
Craig Toppera2886c22012-02-07 05:05:23 +0000988 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989}
990
Jim Grosbach4b905842013-09-20 23:08:21 +0000991/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000992///
Jim Grosbachbd164242011-08-20 16:24:13 +0000993/// expr ::= expr &&,|| expr -> lowest.
994/// expr ::= expr |,^,&,! expr
995/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
996/// expr ::= expr <<,>> expr
997/// expr ::= expr +,- expr
998/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000999/// expr ::= primaryexpr
1000///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001001bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001002 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001003 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001004 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001005 return true;
1006
Daniel Dunbar55f16672010-09-17 02:47:07 +00001007 // As a special case, we support 'a op b @ modifier' by rewriting the
1008 // expression to include the modifier. This is inefficient, but in general we
1009 // expect users to use 'a@modifier op b'.
1010 if (Lexer.getKind() == AsmToken::At) {
1011 Lex();
1012
1013 if (Lexer.isNot(AsmToken::Identifier))
1014 return TokError("unexpected symbol modifier following '@'");
1015
1016 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001017 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001018 if (Variant == MCSymbolRefExpr::VK_Invalid)
1019 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1020
Jim Grosbach4b905842013-09-20 23:08:21 +00001021 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001022 if (!ModifiedRes) {
1023 return TokError("invalid modifier '" + getTok().getIdentifier() +
1024 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001026
Daniel Dunbar55f16672010-09-17 02:47:07 +00001027 Res = ModifiedRes;
1028 Lex();
1029 }
1030
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001031 // Try to constant fold it up front, if possible.
1032 int64_t Value;
1033 if (Res->EvaluateAsAbsolute(Value))
1034 Res = MCConstantExpr::Create(Value, getContext());
1035
1036 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001037}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001038
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001039bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001040 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001041 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001042}
1043
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001044bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001045 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001046
Daniel Dunbar75630b32009-06-30 02:10:03 +00001047 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001048 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001049 return true;
1050
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001051 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001052 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001053
1054 return false;
1055}
1056
Michael J. Spencer530ce852010-10-09 11:00:50 +00001057static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001058 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001059 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001060 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001061 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001062
Jim Grosbach4b905842013-09-20 23:08:21 +00001063 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001064 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001065 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001066 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001067 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001068 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001069 return 1;
1070
Jim Grosbach4b905842013-09-20 23:08:21 +00001071 // Low Precedence: |, &, ^
1072 //
1073 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001074 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001075 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001076 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001077 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001078 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001079 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001082 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001083
Jim Grosbach4b905842013-09-20 23:08:21 +00001084 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001085 case AsmToken::EqualEqual:
1086 Kind = MCBinaryExpr::EQ;
1087 return 3;
1088 case AsmToken::ExclaimEqual:
1089 case AsmToken::LessGreater:
1090 Kind = MCBinaryExpr::NE;
1091 return 3;
1092 case AsmToken::Less:
1093 Kind = MCBinaryExpr::LT;
1094 return 3;
1095 case AsmToken::LessEqual:
1096 Kind = MCBinaryExpr::LTE;
1097 return 3;
1098 case AsmToken::Greater:
1099 Kind = MCBinaryExpr::GT;
1100 return 3;
1101 case AsmToken::GreaterEqual:
1102 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001103 return 3;
1104
Jim Grosbach4b905842013-09-20 23:08:21 +00001105 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001106 case AsmToken::LessLess:
1107 Kind = MCBinaryExpr::Shl;
1108 return 4;
1109 case AsmToken::GreaterGreater:
1110 Kind = MCBinaryExpr::Shr;
1111 return 4;
1112
Jim Grosbach4b905842013-09-20 23:08:21 +00001113 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001114 case AsmToken::Plus:
1115 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001116 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001117 case AsmToken::Minus:
1118 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001119 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001120
Jim Grosbach4b905842013-09-20 23:08:21 +00001121 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001122 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001123 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001124 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001125 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001126 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001127 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001131 }
1132}
1133
Jim Grosbach4b905842013-09-20 23:08:21 +00001134/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001135/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001136bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001137 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001138 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001139 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001140 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001141
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001142 // If the next token is lower precedence than we are allowed to eat, return
1143 // successfully with what we ate already.
1144 if (TokPrec < Precedence)
1145 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001146
Sean Callanan686ed8d2010-01-19 20:22:31 +00001147 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001148
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001150 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001151 if (parsePrimaryExpr(RHS, EndLoc))
1152 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001153
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001154 // If BinOp binds less tightly with RHS than the operator after RHS, let
1155 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001156 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001157 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001158 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1159 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001161 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001162 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001163 }
1164}
1165
Chris Lattner36e02122009-06-21 20:54:55 +00001166/// ParseStatement:
1167/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001168/// ::= Label* Directive ...Operands... EndOfStatement
1169/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001170bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001171 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001172 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001173 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001174 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001175 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001176
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001177 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001178 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001179 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001180 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001181 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001182 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001183 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001184 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001185
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001186 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001187 if (Lexer.is(AsmToken::Integer)) {
1188 LocalLabelVal = getTok().getIntVal();
1189 if (LocalLabelVal < 0) {
1190 if (!TheCondState.Ignore)
1191 return TokError("unexpected token at start of statement");
1192 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001193 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001194 IDVal = getTok().getString();
1195 Lex(); // Consume the integer token to be used as an identifier token.
1196 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001197 if (!TheCondState.Ignore)
1198 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001199 }
1200 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001201 } else if (Lexer.is(AsmToken::Dot)) {
1202 // Treat '.' as a valid identifier in this context.
1203 Lex();
1204 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001205 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001206 if (!TheCondState.Ignore)
1207 return TokError("unexpected token at start of statement");
1208 IDVal = "";
1209 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001210
Chris Lattner926885c2010-04-17 18:14:27 +00001211 // Handle conditional assembly here before checking for skipping. We
1212 // have to do this so that .endif isn't skipped in a ".if 0" block for
1213 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001214 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001215 DirectiveKindMap.find(IDVal);
1216 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1217 ? DK_NO_DIRECTIVE
1218 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001219 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001220 default:
1221 break;
1222 case DK_IF:
1223 return parseDirectiveIf(IDLoc);
1224 case DK_IFB:
1225 return parseDirectiveIfb(IDLoc, true);
1226 case DK_IFNB:
1227 return parseDirectiveIfb(IDLoc, false);
1228 case DK_IFC:
1229 return parseDirectiveIfc(IDLoc, true);
1230 case DK_IFNC:
1231 return parseDirectiveIfc(IDLoc, false);
1232 case DK_IFDEF:
1233 return parseDirectiveIfdef(IDLoc, true);
1234 case DK_IFNDEF:
1235 case DK_IFNOTDEF:
1236 return parseDirectiveIfdef(IDLoc, false);
1237 case DK_ELSEIF:
1238 return parseDirectiveElseIf(IDLoc);
1239 case DK_ELSE:
1240 return parseDirectiveElse(IDLoc);
1241 case DK_ENDIF:
1242 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001243 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001244
Eli Bendersky88024712013-01-16 19:32:36 +00001245 // Ignore the statement if in the middle of inactive conditional
1246 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001247 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001248 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001249 return false;
1250 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001251
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001252 // FIXME: Recurse on local labels?
1253
1254 // See what kind of statement we have.
1255 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001256 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001257 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001258
Chris Lattner36e02122009-06-21 20:54:55 +00001259 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001260 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001261
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001262 // Diagnose attempt to use '.' as a label.
1263 if (IDVal == ".")
1264 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1265
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001266 // Diagnose attempt to use a variable as a label.
1267 //
1268 // FIXME: Diagnostics. Note the location of the definition as a label.
1269 // FIXME: This doesn't diagnose assignment to a symbol which has been
1270 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001271 MCSymbol *Sym;
1272 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001273 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001274 else
1275 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001276 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001277 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001278
Daniel Dunbare73b2672009-08-26 22:13:22 +00001279 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001280 if (!ParsingInlineAsm)
1281 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001282
Kevin Enderbye7739d42011-12-09 18:09:40 +00001283 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001284 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001285 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001286 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1287 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001288
Tim Northover1744d0a2013-10-25 12:49:50 +00001289 getTargetParser().onLabelParsed(Sym);
1290
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001291 // Consume any end of statement token, if present, to avoid spurious
1292 // AddBlankLine calls().
1293 if (Lexer.is(AsmToken::EndOfStatement)) {
1294 Lex();
1295 if (Lexer.is(AsmToken::Eof))
1296 return false;
1297 }
1298
Eli Friedman0f4871d2012-10-22 23:58:19 +00001299 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001300 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001301
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001302 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001303 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001304 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001305
Jim Grosbach4b905842013-09-20 23:08:21 +00001306 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001307
1308 default: // Normal instruction or directive.
1309 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001310 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001311
1312 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001313 if (areMacrosEnabled())
1314 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1315 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001316 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001317
Michael J. Spencer530ce852010-10-09 11:00:50 +00001318 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001319
Eli Bendersky17233942013-01-15 22:59:42 +00001320 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001321 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001322 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001323 //
Eli Bendersky17233942013-01-15 22:59:42 +00001324 // 1. The target-specific assembly parser. Some directives are target
1325 // specific or may potentially behave differently on certain targets.
1326 // 2. Asm parser extensions. For example, platform-specific parsers
1327 // (like the ELF parser) register themselves as extensions.
1328 // 3. The generic directive parser implemented by this class. These are
1329 // all the directives that behave in a target and platform independent
1330 // manner, or at least have a default behavior that's shared between
1331 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001332
Eli Bendersky17233942013-01-15 22:59:42 +00001333 // First query the target-specific parser. It will return 'true' if it
1334 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001335 if (!getTargetParser().ParseDirective(ID))
1336 return false;
1337
Alp Tokercb402912014-01-24 17:20:08 +00001338 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001339 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001340 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1341 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001342 if (Handler.first)
1343 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1344
1345 // Finally, if no one else is interested in this directive, it must be
1346 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001347 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001348 default:
1349 break;
1350 case DK_SET:
1351 case DK_EQU:
1352 return parseDirectiveSet(IDVal, true);
1353 case DK_EQUIV:
1354 return parseDirectiveSet(IDVal, false);
1355 case DK_ASCII:
1356 return parseDirectiveAscii(IDVal, false);
1357 case DK_ASCIZ:
1358 case DK_STRING:
1359 return parseDirectiveAscii(IDVal, true);
1360 case DK_BYTE:
1361 return parseDirectiveValue(1);
1362 case DK_SHORT:
1363 case DK_VALUE:
1364 case DK_2BYTE:
1365 return parseDirectiveValue(2);
1366 case DK_LONG:
1367 case DK_INT:
1368 case DK_4BYTE:
1369 return parseDirectiveValue(4);
1370 case DK_QUAD:
1371 case DK_8BYTE:
1372 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001373 case DK_OCTA:
1374 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001375 case DK_SINGLE:
1376 case DK_FLOAT:
1377 return parseDirectiveRealValue(APFloat::IEEEsingle);
1378 case DK_DOUBLE:
1379 return parseDirectiveRealValue(APFloat::IEEEdouble);
1380 case DK_ALIGN: {
1381 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1382 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1383 }
1384 case DK_ALIGN32: {
1385 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1386 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1387 }
1388 case DK_BALIGN:
1389 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1390 case DK_BALIGNW:
1391 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1392 case DK_BALIGNL:
1393 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1394 case DK_P2ALIGN:
1395 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1396 case DK_P2ALIGNW:
1397 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1398 case DK_P2ALIGNL:
1399 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1400 case DK_ORG:
1401 return parseDirectiveOrg();
1402 case DK_FILL:
1403 return parseDirectiveFill();
1404 case DK_ZERO:
1405 return parseDirectiveZero();
1406 case DK_EXTERN:
1407 eatToEndOfStatement(); // .extern is the default, ignore it.
1408 return false;
1409 case DK_GLOBL:
1410 case DK_GLOBAL:
1411 return parseDirectiveSymbolAttribute(MCSA_Global);
1412 case DK_LAZY_REFERENCE:
1413 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1414 case DK_NO_DEAD_STRIP:
1415 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1416 case DK_SYMBOL_RESOLVER:
1417 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1418 case DK_PRIVATE_EXTERN:
1419 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1420 case DK_REFERENCE:
1421 return parseDirectiveSymbolAttribute(MCSA_Reference);
1422 case DK_WEAK_DEFINITION:
1423 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1424 case DK_WEAK_REFERENCE:
1425 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1426 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1427 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1428 case DK_COMM:
1429 case DK_COMMON:
1430 return parseDirectiveComm(/*IsLocal=*/false);
1431 case DK_LCOMM:
1432 return parseDirectiveComm(/*IsLocal=*/true);
1433 case DK_ABORT:
1434 return parseDirectiveAbort();
1435 case DK_INCLUDE:
1436 return parseDirectiveInclude();
1437 case DK_INCBIN:
1438 return parseDirectiveIncbin();
1439 case DK_CODE16:
1440 case DK_CODE16GCC:
1441 return TokError(Twine(IDVal) + " not supported yet");
1442 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001443 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001444 case DK_IRP:
1445 return parseDirectiveIrp(IDLoc);
1446 case DK_IRPC:
1447 return parseDirectiveIrpc(IDLoc);
1448 case DK_ENDR:
1449 return parseDirectiveEndr(IDLoc);
1450 case DK_BUNDLE_ALIGN_MODE:
1451 return parseDirectiveBundleAlignMode();
1452 case DK_BUNDLE_LOCK:
1453 return parseDirectiveBundleLock();
1454 case DK_BUNDLE_UNLOCK:
1455 return parseDirectiveBundleUnlock();
1456 case DK_SLEB128:
1457 return parseDirectiveLEB128(true);
1458 case DK_ULEB128:
1459 return parseDirectiveLEB128(false);
1460 case DK_SPACE:
1461 case DK_SKIP:
1462 return parseDirectiveSpace(IDVal);
1463 case DK_FILE:
1464 return parseDirectiveFile(IDLoc);
1465 case DK_LINE:
1466 return parseDirectiveLine();
1467 case DK_LOC:
1468 return parseDirectiveLoc();
1469 case DK_STABS:
1470 return parseDirectiveStabs();
1471 case DK_CFI_SECTIONS:
1472 return parseDirectiveCFISections();
1473 case DK_CFI_STARTPROC:
1474 return parseDirectiveCFIStartProc();
1475 case DK_CFI_ENDPROC:
1476 return parseDirectiveCFIEndProc();
1477 case DK_CFI_DEF_CFA:
1478 return parseDirectiveCFIDefCfa(IDLoc);
1479 case DK_CFI_DEF_CFA_OFFSET:
1480 return parseDirectiveCFIDefCfaOffset();
1481 case DK_CFI_ADJUST_CFA_OFFSET:
1482 return parseDirectiveCFIAdjustCfaOffset();
1483 case DK_CFI_DEF_CFA_REGISTER:
1484 return parseDirectiveCFIDefCfaRegister(IDLoc);
1485 case DK_CFI_OFFSET:
1486 return parseDirectiveCFIOffset(IDLoc);
1487 case DK_CFI_REL_OFFSET:
1488 return parseDirectiveCFIRelOffset(IDLoc);
1489 case DK_CFI_PERSONALITY:
1490 return parseDirectiveCFIPersonalityOrLsda(true);
1491 case DK_CFI_LSDA:
1492 return parseDirectiveCFIPersonalityOrLsda(false);
1493 case DK_CFI_REMEMBER_STATE:
1494 return parseDirectiveCFIRememberState();
1495 case DK_CFI_RESTORE_STATE:
1496 return parseDirectiveCFIRestoreState();
1497 case DK_CFI_SAME_VALUE:
1498 return parseDirectiveCFISameValue(IDLoc);
1499 case DK_CFI_RESTORE:
1500 return parseDirectiveCFIRestore(IDLoc);
1501 case DK_CFI_ESCAPE:
1502 return parseDirectiveCFIEscape();
1503 case DK_CFI_SIGNAL_FRAME:
1504 return parseDirectiveCFISignalFrame();
1505 case DK_CFI_UNDEFINED:
1506 return parseDirectiveCFIUndefined(IDLoc);
1507 case DK_CFI_REGISTER:
1508 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001509 case DK_CFI_WINDOW_SAVE:
1510 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001511 case DK_MACROS_ON:
1512 case DK_MACROS_OFF:
1513 return parseDirectiveMacrosOnOff(IDVal);
1514 case DK_MACRO:
1515 return parseDirectiveMacro(IDLoc);
1516 case DK_ENDM:
1517 case DK_ENDMACRO:
1518 return parseDirectiveEndMacro(IDVal);
1519 case DK_PURGEM:
1520 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001521 case DK_END:
1522 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001523 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001524
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001525 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001526 }
Chris Lattner36e02122009-06-21 20:54:55 +00001527
Chad Rosierc7f552c2013-02-12 21:33:51 +00001528 // __asm _emit or __asm __emit
1529 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1530 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001531 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001532
1533 // __asm align
1534 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001535 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001536
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001537 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001538
Chris Lattner7cbfa442010-05-19 23:34:33 +00001539 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001540 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001541 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001542 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001543 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001544 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001545
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001546 // Dump the parsed representation, if requested.
1547 if (getShowParsedOperands()) {
1548 SmallString<256> Str;
1549 raw_svector_ostream OS(Str);
1550 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001551 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001552 if (i != 0)
1553 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001554 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001555 }
1556 OS << "]";
1557
Jim Grosbach4b905842013-09-20 23:08:21 +00001558 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001559 }
1560
Kevin Enderby6469fc22011-11-01 22:27:22 +00001561 // If we are generating dwarf for assembly source files and the current
1562 // section is the initial text section then generate a .loc directive for
1563 // the instruction.
1564 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001565 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001566 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001567
Eli Bendersky88024712013-01-16 19:32:36 +00001568 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001569
Eli Bendersky88024712013-01-16 19:32:36 +00001570 // If we previously parsed a cpp hash file line comment then make sure the
1571 // current Dwarf File is for the CppHashFilename if not then emit the
1572 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001573 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001574 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001575 if (CppHashFilename.size() != 0) {
1576 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001577 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001578 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001579 getContext().nextGenDwarfFileNumber(), StringRef(),
1580 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001581
Jim Grosbach4b905842013-09-20 23:08:21 +00001582 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1583 // cache with the different Loc from the call above we save the last
1584 // info we queried here with SrcMgr.FindLineNumber().
1585 unsigned CppHashLocLineNo;
1586 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1587 CppHashLocLineNo = LastQueryLine;
1588 else {
1589 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1590 LastQueryLine = CppHashLocLineNo;
1591 LastQueryIDLoc = CppHashLoc;
1592 LastQueryBuffer = CppHashBuf;
1593 }
1594 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001595 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001596
Jim Grosbach4b905842013-09-20 23:08:21 +00001597 getStreamer().EmitDwarfLocDirective(
1598 getContext().getGenDwarfFileNumber(), Line, 0,
1599 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1600 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001601 }
1602
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001603 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001604 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001605 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001606 HadError = getTargetParser().MatchAndEmitInstruction(
1607 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1608 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001609 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001610
Chris Lattnera2a9d162010-09-11 16:18:25 +00001611 // Don't skip the rest of the line, the instruction parser is responsible for
1612 // that.
1613 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001614}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001615
Jim Grosbach4b905842013-09-20 23:08:21 +00001616/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001617/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001618void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001619 if (!Lexer.is(AsmToken::EndOfStatement))
1620 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001621 // Eat EOL.
1622 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001623}
1624
Jim Grosbach4b905842013-09-20 23:08:21 +00001625/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001626/// ::= # number "filename"
1627/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001628bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001629 Lex(); // Eat the hash token.
1630
1631 if (getLexer().isNot(AsmToken::Integer)) {
1632 // Consume the line since in cases it is not a well-formed line directive,
1633 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001634 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001635 return false;
1636 }
1637
1638 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001639 Lex();
1640
1641 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001642 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001643 return false;
1644 }
1645
1646 StringRef Filename = getTok().getString();
1647 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001648 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001649
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001650 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1651 CppHashLoc = L;
1652 CppHashFilename = Filename;
1653 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001654 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001655
1656 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001657 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001658 return false;
1659}
1660
Jim Grosbach4b905842013-09-20 23:08:21 +00001661/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001662/// for the Filename and LineNo if any in the diagnostic.
1663void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001664 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001665 raw_ostream &OS = errs();
1666
1667 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1668 const SMLoc &DiagLoc = Diag.getLoc();
1669 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1670 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1671
Jim Grosbach4b905842013-09-20 23:08:21 +00001672 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001673 // before printing the message.
1674 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001675 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1677 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001678 }
1679
Eric Christophera7c32732012-12-18 00:30:54 +00001680 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001681 // manager changed or buffer changed (like in a nested include) then just
1682 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001683 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001684 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001685 if (Parser->SavedDiagHandler)
1686 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1687 else
1688 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001689 return;
1690 }
1691
Eric Christophera7c32732012-12-18 00:30:54 +00001692 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1694 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001695 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696
1697 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1698 int CppHashLocLineNo =
1699 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001700 int LineNo =
1701 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001702
Jim Grosbach4b905842013-09-20 23:08:21 +00001703 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1704 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001705 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001706
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001707 if (Parser->SavedDiagHandler)
1708 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1709 else
1710 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711}
1712
Rafael Espindola2c064482012-08-21 18:29:30 +00001713// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1714// difference being that that function accepts '@' as part of identifiers and
1715// we can't do that. AsmLexer.cpp should probably be changed to handle
1716// '@' as a special case when needed.
1717static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001718 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1719 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001720}
1721
Rafael Espindola34b9c512012-06-03 23:57:14 +00001722bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00001723 ArrayRef<MCAsmMacroParameter> Parameters,
1724 ArrayRef<MCAsmMacroArgument> A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001725 unsigned NParameters = Parameters.size();
1726 if (NParameters != 0 && NParameters != A.size())
1727 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001728
Preston Gurd05500642012-09-19 20:36:12 +00001729 // A macro without parameters is handled differently on Darwin:
1730 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001731 while (!Body.empty()) {
1732 // Scan for the next substitution.
1733 std::size_t End = Body.size(), Pos = 0;
1734 for (; Pos != End; ++Pos) {
1735 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001736 if (!NParameters) {
1737 // This macro has no parameters, look for $0, $1, etc.
1738 if (Body[Pos] != '$' || Pos + 1 == End)
1739 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001740
Rafael Espindola1134ab232011-06-05 02:43:45 +00001741 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001742 if (Next == '$' || Next == 'n' ||
1743 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001744 break;
1745 } else {
1746 // This macro has parameters, look for \foo, \bar, etc.
1747 if (Body[Pos] == '\\' && Pos + 1 != End)
1748 break;
1749 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001750 }
1751
1752 // Add the prefix.
1753 OS << Body.slice(0, Pos);
1754
1755 // Check if we reached the end.
1756 if (Pos == End)
1757 break;
1758
Rafael Espindola1134ab232011-06-05 02:43:45 +00001759 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001760 switch (Body[Pos + 1]) {
1761 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001762 case '$':
1763 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001764 break;
1765
Jim Grosbach4b905842013-09-20 23:08:21 +00001766 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001767 case 'n':
1768 OS << A.size();
1769 break;
1770
Jim Grosbach4b905842013-09-20 23:08:21 +00001771 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001772 default: {
1773 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001774 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001775 if (Index >= A.size())
1776 break;
1777
1778 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001779 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001780 ie = A[Index].end();
1781 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001782 OS << it->getString();
1783 break;
1784 }
1785 }
1786 Pos += 2;
1787 } else {
1788 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001789 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001790 ++I;
1791
Jim Grosbach4b905842013-09-20 23:08:21 +00001792 const char *Begin = Body.data() + Pos + 1;
1793 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 unsigned Index = 0;
1795 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001796 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001797 break;
1798
Preston Gurd05500642012-09-19 20:36:12 +00001799 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001800 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1801 Pos += 3;
1802 else {
1803 OS << '\\' << Argument;
1804 Pos = I;
1805 }
Preston Gurd05500642012-09-19 20:36:12 +00001806 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001807 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001808 ie = A[Index].end();
1809 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001810 if (it->getKind() == AsmToken::String)
1811 OS << it->getStringContents();
1812 else
1813 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001814
Preston Gurd05500642012-09-19 20:36:12 +00001815 Pos += 1 + Argument.size();
1816 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001817 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001818 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001819 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001820 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001821
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 return false;
1823}
Daniel Dunbar43235712010-07-18 18:54:11 +00001824
Jim Grosbach4b905842013-09-20 23:08:21 +00001825MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1826 SMLoc EL, MemoryBuffer *I)
1827 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1828 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001829
Jim Grosbach4b905842013-09-20 23:08:21 +00001830static bool isOperator(AsmToken::TokenKind kind) {
1831 switch (kind) {
1832 default:
1833 return false;
1834 case AsmToken::Plus:
1835 case AsmToken::Minus:
1836 case AsmToken::Tilde:
1837 case AsmToken::Slash:
1838 case AsmToken::Star:
1839 case AsmToken::Dot:
1840 case AsmToken::Equal:
1841 case AsmToken::EqualEqual:
1842 case AsmToken::Pipe:
1843 case AsmToken::PipePipe:
1844 case AsmToken::Caret:
1845 case AsmToken::Amp:
1846 case AsmToken::AmpAmp:
1847 case AsmToken::Exclaim:
1848 case AsmToken::ExclaimEqual:
1849 case AsmToken::Percent:
1850 case AsmToken::Less:
1851 case AsmToken::LessEqual:
1852 case AsmToken::LessLess:
1853 case AsmToken::LessGreater:
1854 case AsmToken::Greater:
1855 case AsmToken::GreaterEqual:
1856 case AsmToken::GreaterGreater:
1857 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001858 }
1859}
1860
David Majnemer16252452014-01-29 00:07:39 +00001861namespace {
1862class AsmLexerSkipSpaceRAII {
1863public:
1864 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1865 Lexer.setSkipSpace(SkipSpace);
1866 }
1867
1868 ~AsmLexerSkipSpaceRAII() {
1869 Lexer.setSkipSpace(true);
1870 }
1871
1872private:
1873 AsmLexer &Lexer;
1874};
1875}
1876
David Majnemer91fc4c22014-01-29 18:57:46 +00001877bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001878 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001879 unsigned AddTokens = 0;
1880
David Majnemer16252452014-01-29 00:07:39 +00001881 // Darwin doesn't use spaces to delmit arguments.
1882 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001883
1884 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001885 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001886 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001887
David Majnemer91fc4c22014-01-29 18:57:46 +00001888 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001889 break;
Preston Gurd05500642012-09-19 20:36:12 +00001890
1891 if (Lexer.is(AsmToken::Space)) {
1892 Lex(); // Eat spaces
1893
1894 // Spaces can delimit parameters, but could also be part an expression.
1895 // If the token after a space is an operator, add the token and the next
1896 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001897 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001898 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001899 // Check to see whether the token is used as an operator,
1900 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001901 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001902 if (*NextChar == ' ')
1903 AddTokens = 2;
1904 }
1905
1906 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001907 break;
1908 }
1909 }
1910 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001911
Jim Grosbach4b905842013-09-20 23:08:21 +00001912 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001913 // to be able to fill in the remaining default parameter values
1914 if (Lexer.is(AsmToken::EndOfStatement))
1915 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001916
1917 // Adjust the current parentheses level.
1918 if (Lexer.is(AsmToken::LParen))
1919 ++ParenLevel;
1920 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1921 --ParenLevel;
1922
1923 // Append the token to the current argument list.
1924 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001925 if (AddTokens)
1926 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001927 Lex();
1928 }
Preston Gurd05500642012-09-19 20:36:12 +00001929
Rafael Espindola768b41c2012-06-15 14:02:34 +00001930 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001931 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001932 return false;
1933}
1934
1935// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001936bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001937 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001938 const unsigned NParameters = M ? M->Parameters.size() : 0;
1939
1940 // Parse two kinds of macro invocations:
1941 // - macros defined without any parameters accept an arbitrary number of them
1942 // - macros defined with parameters accept at most that many of them
1943 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1944 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001945 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001946
David Majnemer91fc4c22014-01-29 18:57:46 +00001947 if (parseMacroArgument(MA))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001948 return true;
1949
David Majnemer91fc4c22014-01-29 18:57:46 +00001950 if (!MA.empty() || (!NParameters && !Lexer.is(AsmToken::EndOfStatement)))
Preston Gurd242ed3152012-09-19 20:29:04 +00001951 A.push_back(MA);
1952 else if (NParameters) {
1953 if (!M->Parameters[Parameter].second.empty())
1954 A.push_back(M->Parameters[Parameter].second);
David Majnemer91fc4c22014-01-29 18:57:46 +00001955 else
1956 A.push_back(MA);
Preston Gurd242ed3152012-09-19 20:29:04 +00001957 }
Jim Grosbach206661622012-07-30 22:44:17 +00001958
Preston Gurd242ed3152012-09-19 20:29:04 +00001959 // At the end of the statement, fill in remaining arguments that have
1960 // default values. If there aren't any, then the next argument is
1961 // required but missing
1962 if (Lexer.is(AsmToken::EndOfStatement)) {
1963 if (NParameters && Parameter < NParameters - 1) {
David Majnemer91fc4c22014-01-29 18:57:46 +00001964 continue;
Preston Gurd242ed3152012-09-19 20:29:04 +00001965 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001966 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001967 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001968
1969 if (Lexer.is(AsmToken::Comma))
1970 Lex();
1971 }
1972 return TokError("Too many arguments");
1973}
1974
Jim Grosbach4b905842013-09-20 23:08:21 +00001975const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1976 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001977 return (I == MacroMap.end()) ? NULL : I->getValue();
1978}
1979
Jim Grosbach4b905842013-09-20 23:08:21 +00001980void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001981 MacroMap[Name] = new MCAsmMacro(Macro);
1982}
1983
Jim Grosbach4b905842013-09-20 23:08:21 +00001984void AsmParser::undefineMacro(StringRef Name) {
1985 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001986 if (I != MacroMap.end()) {
1987 delete I->getValue();
1988 MacroMap.erase(I);
1989 }
1990}
1991
Jim Grosbach4b905842013-09-20 23:08:21 +00001992bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00001993 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1994 // this, although we should protect against infinite loops.
1995 if (ActiveMacros.size() == 20)
1996 return TokError("macros cannot be nested more than 20 levels deep");
1997
Eli Bendersky38274122013-01-14 23:22:36 +00001998 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00001999 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002000 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002001
Rafael Espindola1134ab232011-06-05 02:43:45 +00002002 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2003 // to hold the macro body with substitutions.
2004 SmallString<256> Buf;
2005 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002006 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002007
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002008 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002009 return true;
2010
Eli Bendersky38274122013-01-14 23:22:36 +00002011 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002012 // instantiation.
2013 OS << ".endmacro\n";
2014
Rafael Espindola1134ab232011-06-05 02:43:45 +00002015 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002016 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002017
Daniel Dunbar43235712010-07-18 18:54:11 +00002018 // Create the macro instantiation object and add to the current macro
2019 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002020 MacroInstantiation *MI = new MacroInstantiation(
2021 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002022 ActiveMacros.push_back(MI);
2023
2024 // Jump to the macro instantiation and prime the lexer.
2025 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2026 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2027 Lex();
2028
2029 return false;
2030}
2031
Jim Grosbach4b905842013-09-20 23:08:21 +00002032void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002033 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002034 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002035 Lex();
2036
2037 // Pop the instantiation entry.
2038 delete ActiveMacros.back();
2039 ActiveMacros.pop_back();
2040}
2041
Jim Grosbach4b905842013-09-20 23:08:21 +00002042static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002043 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002044 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002045 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2046 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002047 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002048 case MCExpr::Target:
2049 case MCExpr::Constant:
2050 return false;
2051 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002052 const MCSymbol &S =
2053 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002054 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002055 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002056 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002057 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002058 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002059 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002060 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002061
2062 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002063}
2064
Jim Grosbach4b905842013-09-20 23:08:21 +00002065bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002066 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002067 // FIXME: Use better location, we should use proper tokens.
2068 SMLoc EqualLoc = Lexer.getLoc();
2069
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002070 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002071 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002072 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002073
Rafael Espindola72f5f172012-01-28 05:57:00 +00002074 // Note: we don't count b as used in "a = b". This is to allow
2075 // a = b
2076 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002077
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002078 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002079 return TokError("unexpected token in assignment");
2080
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002081 // Error on assignment to '.'.
2082 if (Name == ".") {
2083 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2084 "(use '.space' or '.org').)"));
2085 }
2086
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002087 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002088 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002089
Daniel Dunbar5f339242009-10-16 01:57:39 +00002090 // Validate that the LHS is allowed to be a variable (either it has not been
2091 // used as a symbol, or it is an absolute symbol).
2092 MCSymbol *Sym = getContext().LookupSymbol(Name);
2093 if (Sym) {
2094 // Diagnose assignment to a label.
2095 //
2096 // FIXME: Diagnostics. Note the location of the definition as a label.
2097 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002098 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002099 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2100 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002101 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002102 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2103 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002104 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002105 return Error(EqualLoc, "redefinition of '" + Name + "'");
2106 else if (!Sym->isVariable())
2107 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002108 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002109 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002110 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002111
2112 // Don't count these checks as uses.
2113 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002114 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002115 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002116
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002117 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002118
2119 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002120 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002121 if (NoDeadStrip)
2122 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2123
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002124 return false;
2125}
2126
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002127/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002128/// ::= identifier
2129/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002130bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002131 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002132 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2133 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002134 // handle this as a context dependent token, instead we detect adjacent tokens
2135 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002136 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2137 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002138
Hans Wennborgce69d772013-10-18 20:46:28 +00002139 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002140 Lex();
2141 if (Lexer.isNot(AsmToken::Identifier))
2142 return true;
2143
Hans Wennborgce69d772013-10-18 20:46:28 +00002144 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2145 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002146 return true;
2147
2148 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002149 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002150 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002151 Lex();
2152 return false;
2153 }
2154
Jim Grosbach4b905842013-09-20 23:08:21 +00002155 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002156 return true;
2157
Sean Callanan936b0d32010-01-19 21:44:56 +00002158 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002159
Sean Callanan686ed8d2010-01-19 20:22:31 +00002160 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002161
2162 return false;
2163}
2164
Jim Grosbach4b905842013-09-20 23:08:21 +00002165/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002166/// ::= .equ identifier ',' expression
2167/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002168/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002169bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002170 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002171
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002172 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002173 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002174
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002175 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002176 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002177 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002178
Jim Grosbach4b905842013-09-20 23:08:21 +00002179 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002180}
2181
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002182bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002183 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002184
2185 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002186 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002187 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2188 if (Str[i] != '\\') {
2189 Data += Str[i];
2190 continue;
2191 }
2192
2193 // Recognize escaped characters. Note that this escape semantics currently
2194 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2195 ++i;
2196 if (i == e)
2197 return TokError("unexpected backslash at end of string");
2198
2199 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002200 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002201 // Consume up to three octal characters.
2202 unsigned Value = Str[i] - '0';
2203
Jim Grosbach4b905842013-09-20 23:08:21 +00002204 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002205 ++i;
2206 Value = Value * 8 + (Str[i] - '0');
2207
Jim Grosbach4b905842013-09-20 23:08:21 +00002208 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002209 ++i;
2210 Value = Value * 8 + (Str[i] - '0');
2211 }
2212 }
2213
2214 if (Value > 255)
2215 return TokError("invalid octal escape sequence (out of range)");
2216
Jim Grosbach4b905842013-09-20 23:08:21 +00002217 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002218 continue;
2219 }
2220
2221 // Otherwise recognize individual escapes.
2222 switch (Str[i]) {
2223 default:
2224 // Just reject invalid escape sequences for now.
2225 return TokError("invalid escape sequence (unrecognized character)");
2226
2227 case 'b': Data += '\b'; break;
2228 case 'f': Data += '\f'; break;
2229 case 'n': Data += '\n'; break;
2230 case 'r': Data += '\r'; break;
2231 case 't': Data += '\t'; break;
2232 case '"': Data += '"'; break;
2233 case '\\': Data += '\\'; break;
2234 }
2235 }
2236
2237 return false;
2238}
2239
Jim Grosbach4b905842013-09-20 23:08:21 +00002240/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002241/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002242bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002243 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002244 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002245
Daniel Dunbara10e5192009-06-24 23:30:00 +00002246 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002247 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002248 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002249
Daniel Dunbaref668c12009-08-14 18:19:52 +00002250 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002251 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002252 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002253
Rafael Espindola64e1af82013-07-02 15:49:13 +00002254 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002255 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002256 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002257
Sean Callanan686ed8d2010-01-19 20:22:31 +00002258 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002259
2260 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002261 break;
2262
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002263 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002264 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002265 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002266 }
2267 }
2268
Sean Callanan686ed8d2010-01-19 20:22:31 +00002269 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002270 return false;
2271}
2272
Jim Grosbach4b905842013-09-20 23:08:21 +00002273/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002274/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002275bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002276 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002277 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002278
Daniel Dunbara10e5192009-06-24 23:30:00 +00002279 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002280 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002281 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002282 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002283 return true;
2284
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002285 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002286 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2287 assert(Size <= 8 && "Invalid size");
2288 uint64_t IntValue = MCE->getValue();
2289 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2290 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002291 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002292 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002293 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002294
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002295 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002296 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002297
Daniel Dunbara10e5192009-06-24 23:30:00 +00002298 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002299 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002300 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002301 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002302 }
2303 }
2304
Sean Callanan686ed8d2010-01-19 20:22:31 +00002305 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002306 return false;
2307}
2308
David Woodhoused6de0d92014-02-01 16:20:59 +00002309/// ParseDirectiveOctaValue
2310/// ::= .octa [ hexconstant (, hexconstant)* ]
2311bool AsmParser::parseDirectiveOctaValue() {
2312 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2313 checkForValidSection();
2314
2315 for (;;) {
2316 if (Lexer.getKind() == AsmToken::Error)
2317 return true;
2318 if (Lexer.getKind() != AsmToken::Integer &&
2319 Lexer.getKind() != AsmToken::BigNum)
2320 return TokError("unknown token in expression");
2321
2322 SMLoc ExprLoc = getLexer().getLoc();
2323 APInt IntValue = getTok().getAPIntVal();
2324 Lex();
2325
2326 uint64_t hi, lo;
2327 if (IntValue.isIntN(64)) {
2328 hi = 0;
2329 lo = IntValue.getZExtValue();
2330 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002331 // It might actually have more than 128 bits, but the top ones are zero.
2332 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002333 lo = IntValue.getLoBits(64).getZExtValue();
2334 } else
2335 return Error(ExprLoc, "literal value out of range for directive");
2336
2337 if (MAI.isLittleEndian()) {
2338 getStreamer().EmitIntValue(lo, 8);
2339 getStreamer().EmitIntValue(hi, 8);
2340 } else {
2341 getStreamer().EmitIntValue(hi, 8);
2342 getStreamer().EmitIntValue(lo, 8);
2343 }
2344
2345 if (getLexer().is(AsmToken::EndOfStatement))
2346 break;
2347
2348 // FIXME: Improve diagnostic.
2349 if (getLexer().isNot(AsmToken::Comma))
2350 return TokError("unexpected token in directive");
2351 Lex();
2352 }
2353 }
2354
2355 Lex();
2356 return false;
2357}
2358
Jim Grosbach4b905842013-09-20 23:08:21 +00002359/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002360/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002361bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002362 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002363 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002364
2365 for (;;) {
2366 // We don't truly support arithmetic on floating point expressions, so we
2367 // have to manually parse unary prefixes.
2368 bool IsNeg = false;
2369 if (getLexer().is(AsmToken::Minus)) {
2370 Lex();
2371 IsNeg = true;
2372 } else if (getLexer().is(AsmToken::Plus))
2373 Lex();
2374
Michael J. Spencer530ce852010-10-09 11:00:50 +00002375 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002376 getLexer().isNot(AsmToken::Real) &&
2377 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002378 return TokError("unexpected token in directive");
2379
2380 // Convert to an APFloat.
2381 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002382 StringRef IDVal = getTok().getString();
2383 if (getLexer().is(AsmToken::Identifier)) {
2384 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2385 Value = APFloat::getInf(Semantics);
2386 else if (!IDVal.compare_lower("nan"))
2387 Value = APFloat::getNaN(Semantics, false, ~0);
2388 else
2389 return TokError("invalid floating point literal");
2390 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002391 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002392 return TokError("invalid floating point literal");
2393 if (IsNeg)
2394 Value.changeSign();
2395
2396 // Consume the numeric token.
2397 Lex();
2398
2399 // Emit the value as an integer.
2400 APInt AsInt = Value.bitcastToAPInt();
2401 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002402 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002403
2404 if (getLexer().is(AsmToken::EndOfStatement))
2405 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002406
Daniel Dunbar2af16532010-09-24 01:59:56 +00002407 if (getLexer().isNot(AsmToken::Comma))
2408 return TokError("unexpected token in directive");
2409 Lex();
2410 }
2411 }
2412
2413 Lex();
2414 return false;
2415}
2416
Jim Grosbach4b905842013-09-20 23:08:21 +00002417/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002418/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002419bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002420 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002421
2422 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002423 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002424 return true;
2425
Rafael Espindolab91bac62010-10-05 19:42:57 +00002426 int64_t Val = 0;
2427 if (getLexer().is(AsmToken::Comma)) {
2428 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002429 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002430 return true;
2431 }
2432
Rafael Espindola922e3f42010-09-16 15:03:59 +00002433 if (getLexer().isNot(AsmToken::EndOfStatement))
2434 return TokError("unexpected token in '.zero' directive");
2435
2436 Lex();
2437
Rafael Espindola64e1af82013-07-02 15:49:13 +00002438 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002439
2440 return false;
2441}
2442
Jim Grosbach4b905842013-09-20 23:08:21 +00002443/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002444/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002445bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002446 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002447
David Majnemer522d3db2014-02-01 07:19:38 +00002448 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002449 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002450 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002451 return true;
2452
David Majnemer522d3db2014-02-01 07:19:38 +00002453 if (NumValues < 0) {
2454 Warning(RepeatLoc,
2455 "'.fill' directive with negative repeat count has no effect");
2456 NumValues = 0;
2457 }
2458
Roman Divackye33098f2013-09-24 17:44:41 +00002459 int64_t FillSize = 1;
2460 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002461
David Majnemer522d3db2014-02-01 07:19:38 +00002462 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002463 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2464 if (getLexer().isNot(AsmToken::Comma))
2465 return TokError("unexpected token in '.fill' directive");
2466 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002467
David Majnemer522d3db2014-02-01 07:19:38 +00002468 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002469 if (parseAbsoluteExpression(FillSize))
2470 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002471
Roman Divackye33098f2013-09-24 17:44:41 +00002472 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2473 if (getLexer().isNot(AsmToken::Comma))
2474 return TokError("unexpected token in '.fill' directive");
2475 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002476
David Majnemer522d3db2014-02-01 07:19:38 +00002477 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002478 if (parseAbsoluteExpression(FillExpr))
2479 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002480
Roman Divackye33098f2013-09-24 17:44:41 +00002481 if (getLexer().isNot(AsmToken::EndOfStatement))
2482 return TokError("unexpected token in '.fill' directive");
2483
2484 Lex();
2485 }
2486 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002487
David Majnemer522d3db2014-02-01 07:19:38 +00002488 if (FillSize < 0) {
2489 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2490 NumValues = 0;
2491 }
2492 if (FillSize > 8) {
2493 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2494 FillSize = 8;
2495 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002496
David Majnemer522d3db2014-02-01 07:19:38 +00002497 if (!isUInt<32>(FillExpr) && FillSize > 4)
2498 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2499
2500 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2501 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2502
2503 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2504 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2505 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2506 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002507
2508 return false;
2509}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002510
Jim Grosbach4b905842013-09-20 23:08:21 +00002511/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002512/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002513bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002514 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002515
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002516 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002517 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002518 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002519 return true;
2520
2521 // Parse optional fill expression.
2522 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002523 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2524 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002525 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002526 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002527
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002528 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002529 return true;
2530
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002531 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002532 return TokError("unexpected token in '.org' directive");
2533 }
2534
Sean Callanan686ed8d2010-01-19 20:22:31 +00002535 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002536
Jim Grosbachb5912772012-01-27 00:37:08 +00002537 // Only limited forms of relocatable expressions are accepted here, it
2538 // has to be relative to the current section. The streamer will return
2539 // 'true' if the expression wasn't evaluatable.
2540 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2541 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002542
2543 return false;
2544}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002545
Jim Grosbach4b905842013-09-20 23:08:21 +00002546/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002547/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002548bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002549 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002550
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002551 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002552 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002553 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002554 return true;
2555
2556 SMLoc MaxBytesLoc;
2557 bool HasFillExpr = false;
2558 int64_t FillExpr = 0;
2559 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002560 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2561 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002562 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002563 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002564
2565 // The fill expression can be omitted while specifying a maximum number of
2566 // alignment bytes, e.g:
2567 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002568 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002569 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002570 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002571 return true;
2572 }
2573
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002574 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2575 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002576 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002577 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002578
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002579 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002580 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002581 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002582
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002583 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002584 return TokError("unexpected token in directive");
2585 }
2586 }
2587
Sean Callanan686ed8d2010-01-19 20:22:31 +00002588 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002589
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002590 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002591 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002592
2593 // Compute alignment in bytes.
2594 if (IsPow2) {
2595 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002596 if (Alignment >= 32) {
2597 Error(AlignmentLoc, "invalid alignment value");
2598 Alignment = 31;
2599 }
2600
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002601 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002602 } else {
2603 // Reject alignments that aren't a power of two, for gas compatibility.
2604 if (!isPowerOf2_64(Alignment))
2605 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002606 }
2607
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002608 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002609 if (MaxBytesLoc.isValid()) {
2610 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002611 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002612 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002613 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002614 }
2615
2616 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002617 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002618 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002619 MaxBytesToFill = 0;
2620 }
2621 }
2622
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002623 // Check whether we should use optimal code alignment for this .align
2624 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002625 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002626 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2627 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002628 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002629 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002630 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002631 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2632 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002633 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002634
2635 return false;
2636}
2637
Jim Grosbach4b905842013-09-20 23:08:21 +00002638/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002639/// ::= .file [number] filename
2640/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002641bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002642 // FIXME: I'm not sure what this is.
2643 int64_t FileNumber = -1;
2644 SMLoc FileNumberLoc = getLexer().getLoc();
2645 if (getLexer().is(AsmToken::Integer)) {
2646 FileNumber = getTok().getIntVal();
2647 Lex();
2648
2649 if (FileNumber < 1)
2650 return TokError("file number less than one");
2651 }
2652
2653 if (getLexer().isNot(AsmToken::String))
2654 return TokError("unexpected token in '.file' directive");
2655
2656 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002657 // Allow the strings to have escaped octal character sequence.
2658 std::string Path = getTok().getString();
2659 if (parseEscapedString(Path))
2660 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002661 Lex();
2662
2663 StringRef Directory;
2664 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002665 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002666 if (getLexer().is(AsmToken::String)) {
2667 if (FileNumber == -1)
2668 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002669 if (parseEscapedString(FilenameData))
2670 return true;
2671 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002672 Directory = Path;
2673 Lex();
2674 } else {
2675 Filename = Path;
2676 }
2677
2678 if (getLexer().isNot(AsmToken::EndOfStatement))
2679 return TokError("unexpected token in '.file' directive");
2680
2681 if (FileNumber == -1)
2682 getStreamer().EmitFileDirective(Filename);
2683 else {
2684 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002685 Error(DirectiveLoc,
2686 "input can't have .file dwarf directives when -g is "
2687 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002688
2689 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2690 Error(FileNumberLoc, "file number already allocated");
2691 }
2692
2693 return false;
2694}
2695
Jim Grosbach4b905842013-09-20 23:08:21 +00002696/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002697/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002698bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002699 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2700 if (getLexer().isNot(AsmToken::Integer))
2701 return TokError("unexpected token in '.line' directive");
2702
2703 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002704 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002705 Lex();
2706
2707 // FIXME: Do something with the .line.
2708 }
2709
2710 if (getLexer().isNot(AsmToken::EndOfStatement))
2711 return TokError("unexpected token in '.line' directive");
2712
2713 return false;
2714}
2715
Jim Grosbach4b905842013-09-20 23:08:21 +00002716/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002717/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2718/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2719/// The first number is a file number, must have been previously assigned with
2720/// a .file directive, the second number is the line number and optionally the
2721/// third number is a column position (zero if not specified). The remaining
2722/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002723bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002724 if (getLexer().isNot(AsmToken::Integer))
2725 return TokError("unexpected token in '.loc' directive");
2726 int64_t FileNumber = getTok().getIntVal();
2727 if (FileNumber < 1)
2728 return TokError("file number less than one in '.loc' directive");
2729 if (!getContext().isValidDwarfFileNumber(FileNumber))
2730 return TokError("unassigned file number in '.loc' directive");
2731 Lex();
2732
2733 int64_t LineNumber = 0;
2734 if (getLexer().is(AsmToken::Integer)) {
2735 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002736 if (LineNumber < 0)
2737 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002738 Lex();
2739 }
2740
2741 int64_t ColumnPos = 0;
2742 if (getLexer().is(AsmToken::Integer)) {
2743 ColumnPos = getTok().getIntVal();
2744 if (ColumnPos < 0)
2745 return TokError("column position less than zero in '.loc' directive");
2746 Lex();
2747 }
2748
2749 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2750 unsigned Isa = 0;
2751 int64_t Discriminator = 0;
2752 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2753 for (;;) {
2754 if (getLexer().is(AsmToken::EndOfStatement))
2755 break;
2756
2757 StringRef Name;
2758 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002759 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002760 return TokError("unexpected token in '.loc' directive");
2761
2762 if (Name == "basic_block")
2763 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2764 else if (Name == "prologue_end")
2765 Flags |= DWARF2_FLAG_PROLOGUE_END;
2766 else if (Name == "epilogue_begin")
2767 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2768 else if (Name == "is_stmt") {
2769 Loc = getTok().getLoc();
2770 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002771 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002772 return true;
2773 // The expression must be the constant 0 or 1.
2774 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2775 int Value = MCE->getValue();
2776 if (Value == 0)
2777 Flags &= ~DWARF2_FLAG_IS_STMT;
2778 else if (Value == 1)
2779 Flags |= DWARF2_FLAG_IS_STMT;
2780 else
2781 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002782 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002783 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2784 }
Craig Topperf15655b2013-04-22 04:22:40 +00002785 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002786 Loc = getTok().getLoc();
2787 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002788 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002789 return true;
2790 // The expression must be a constant greater or equal to 0.
2791 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2792 int Value = MCE->getValue();
2793 if (Value < 0)
2794 return Error(Loc, "isa number less than zero");
2795 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002796 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002797 return Error(Loc, "isa number not a constant value");
2798 }
Craig Topperf15655b2013-04-22 04:22:40 +00002799 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002800 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002801 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002802 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002803 return Error(Loc, "unknown sub-directive in '.loc' directive");
2804 }
2805
2806 if (getLexer().is(AsmToken::EndOfStatement))
2807 break;
2808 }
2809 }
2810
2811 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2812 Isa, Discriminator, StringRef());
2813
2814 return false;
2815}
2816
Jim Grosbach4b905842013-09-20 23:08:21 +00002817/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002818/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002819bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002820 return TokError("unsupported directive '.stabs'");
2821}
2822
Jim Grosbach4b905842013-09-20 23:08:21 +00002823/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002824/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002825bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002826 StringRef Name;
2827 bool EH = false;
2828 bool Debug = false;
2829
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002830 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002831 return TokError("Expected an identifier");
2832
2833 if (Name == ".eh_frame")
2834 EH = true;
2835 else if (Name == ".debug_frame")
2836 Debug = true;
2837
2838 if (getLexer().is(AsmToken::Comma)) {
2839 Lex();
2840
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002841 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002842 return TokError("Expected an identifier");
2843
2844 if (Name == ".eh_frame")
2845 EH = true;
2846 else if (Name == ".debug_frame")
2847 Debug = true;
2848 }
2849
2850 getStreamer().EmitCFISections(EH, Debug);
2851 return false;
2852}
2853
Jim Grosbach4b905842013-09-20 23:08:21 +00002854/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002855/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002856bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002857 StringRef Simple;
2858 if (getLexer().isNot(AsmToken::EndOfStatement))
2859 if (parseIdentifier(Simple) || Simple != "simple")
2860 return TokError("unexpected token in .cfi_startproc directive");
2861
2862 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002863 return false;
2864}
2865
Jim Grosbach4b905842013-09-20 23:08:21 +00002866/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002867/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002868bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002869 getStreamer().EmitCFIEndProc();
2870 return false;
2871}
2872
Jim Grosbach4b905842013-09-20 23:08:21 +00002873/// \brief parse register name or number.
2874bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002875 SMLoc DirectiveLoc) {
2876 unsigned RegNo;
2877
2878 if (getLexer().isNot(AsmToken::Integer)) {
2879 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2880 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002881 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002882 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002883 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002884
2885 return false;
2886}
2887
Jim Grosbach4b905842013-09-20 23:08:21 +00002888/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002889/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002890bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002891 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002892 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002893 return true;
2894
2895 if (getLexer().isNot(AsmToken::Comma))
2896 return TokError("unexpected token in directive");
2897 Lex();
2898
2899 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002900 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002901 return true;
2902
2903 getStreamer().EmitCFIDefCfa(Register, Offset);
2904 return false;
2905}
2906
Jim Grosbach4b905842013-09-20 23:08:21 +00002907/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002908/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002909bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002910 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002911 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return true;
2913
2914 getStreamer().EmitCFIDefCfaOffset(Offset);
2915 return false;
2916}
2917
Jim Grosbach4b905842013-09-20 23:08:21 +00002918/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002919/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002920bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002921 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002922 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002923 return true;
2924
2925 if (getLexer().isNot(AsmToken::Comma))
2926 return TokError("unexpected token in directive");
2927 Lex();
2928
2929 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002930 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002931 return true;
2932
2933 getStreamer().EmitCFIRegister(Register1, Register2);
2934 return false;
2935}
2936
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002937/// parseDirectiveCFIWindowSave
2938/// ::= .cfi_window_save
2939bool AsmParser::parseDirectiveCFIWindowSave() {
2940 getStreamer().EmitCFIWindowSave();
2941 return false;
2942}
2943
Jim Grosbach4b905842013-09-20 23:08:21 +00002944/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002945/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002946bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002947 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002948 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002949 return true;
2950
2951 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2952 return false;
2953}
2954
Jim Grosbach4b905842013-09-20 23:08:21 +00002955/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002956/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002957bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002958 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002959 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002960 return true;
2961
2962 getStreamer().EmitCFIDefCfaRegister(Register);
2963 return false;
2964}
2965
Jim Grosbach4b905842013-09-20 23:08:21 +00002966/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002967/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002968bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002969 int64_t Register = 0;
2970 int64_t Offset = 0;
2971
Jim Grosbach4b905842013-09-20 23:08:21 +00002972 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002973 return true;
2974
2975 if (getLexer().isNot(AsmToken::Comma))
2976 return TokError("unexpected token in directive");
2977 Lex();
2978
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002979 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002980 return true;
2981
2982 getStreamer().EmitCFIOffset(Register, Offset);
2983 return false;
2984}
2985
Jim Grosbach4b905842013-09-20 23:08:21 +00002986/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002987/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002988bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002989 int64_t Register = 0;
2990
Jim Grosbach4b905842013-09-20 23:08:21 +00002991 if (parseRegisterOrRegisterNumber(Register, 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 Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002999 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003000 return true;
3001
3002 getStreamer().EmitCFIRelOffset(Register, Offset);
3003 return false;
3004}
3005
3006static bool isValidEncoding(int64_t Encoding) {
3007 if (Encoding & ~0xff)
3008 return false;
3009
3010 if (Encoding == dwarf::DW_EH_PE_omit)
3011 return true;
3012
3013 const unsigned Format = Encoding & 0xf;
3014 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3015 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3016 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3017 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3018 return false;
3019
3020 const unsigned Application = Encoding & 0x70;
3021 if (Application != dwarf::DW_EH_PE_absptr &&
3022 Application != dwarf::DW_EH_PE_pcrel)
3023 return false;
3024
3025 return true;
3026}
3027
Jim Grosbach4b905842013-09-20 23:08:21 +00003028/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003029/// IsPersonality true for cfi_personality, false for cfi_lsda
3030/// ::= .cfi_personality encoding, [symbol_name]
3031/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003032bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003033 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003034 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003035 return true;
3036 if (Encoding == dwarf::DW_EH_PE_omit)
3037 return false;
3038
3039 if (!isValidEncoding(Encoding))
3040 return TokError("unsupported encoding.");
3041
3042 if (getLexer().isNot(AsmToken::Comma))
3043 return TokError("unexpected token in directive");
3044 Lex();
3045
3046 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003047 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003048 return TokError("expected identifier in directive");
3049
3050 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3051
3052 if (IsPersonality)
3053 getStreamer().EmitCFIPersonality(Sym, Encoding);
3054 else
3055 getStreamer().EmitCFILsda(Sym, Encoding);
3056 return false;
3057}
3058
Jim Grosbach4b905842013-09-20 23:08:21 +00003059/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003060/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003061bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003062 getStreamer().EmitCFIRememberState();
3063 return false;
3064}
3065
Jim Grosbach4b905842013-09-20 23:08:21 +00003066/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003067/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003068bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003069 getStreamer().EmitCFIRestoreState();
3070 return false;
3071}
3072
Jim Grosbach4b905842013-09-20 23:08:21 +00003073/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003074/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003075bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003076 int64_t Register = 0;
3077
Jim Grosbach4b905842013-09-20 23:08:21 +00003078 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003079 return true;
3080
3081 getStreamer().EmitCFISameValue(Register);
3082 return false;
3083}
3084
Jim Grosbach4b905842013-09-20 23:08:21 +00003085/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003086/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003087bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003088 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003089 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003090 return true;
3091
3092 getStreamer().EmitCFIRestore(Register);
3093 return false;
3094}
3095
Jim Grosbach4b905842013-09-20 23:08:21 +00003096/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003097/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003098bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003099 std::string Values;
3100 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003101 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003102 return true;
3103
3104 Values.push_back((uint8_t)CurrValue);
3105
3106 while (getLexer().is(AsmToken::Comma)) {
3107 Lex();
3108
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003109 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003110 return true;
3111
3112 Values.push_back((uint8_t)CurrValue);
3113 }
3114
3115 getStreamer().EmitCFIEscape(Values);
3116 return false;
3117}
3118
Jim Grosbach4b905842013-09-20 23:08:21 +00003119/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003120/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003121bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003122 if (getLexer().isNot(AsmToken::EndOfStatement))
3123 return Error(getLexer().getLoc(),
3124 "unexpected token in '.cfi_signal_frame'");
3125
3126 getStreamer().EmitCFISignalFrame();
3127 return false;
3128}
3129
Jim Grosbach4b905842013-09-20 23:08:21 +00003130/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003131/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003132bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003133 int64_t Register = 0;
3134
Jim Grosbach4b905842013-09-20 23:08:21 +00003135 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003136 return true;
3137
3138 getStreamer().EmitCFIUndefined(Register);
3139 return false;
3140}
3141
Jim Grosbach4b905842013-09-20 23:08:21 +00003142/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003143/// ::= .macros_on
3144/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003145bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003146 if (getLexer().isNot(AsmToken::EndOfStatement))
3147 return Error(getLexer().getLoc(),
3148 "unexpected token in '" + Directive + "' directive");
3149
Jim Grosbach4b905842013-09-20 23:08:21 +00003150 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003151 return false;
3152}
3153
Jim Grosbach4b905842013-09-20 23:08:21 +00003154/// parseDirectiveMacro
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003155/// ::= .macro name[,] [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003156bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003157 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003158 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003159 return TokError("expected identifier in '.macro' directive");
3160
Saleem Abdulrasool27304cb2014-02-16 04:56:31 +00003161 if (getLexer().is(AsmToken::Comma))
3162 Lex();
3163
Eli Bendersky17233942013-01-15 22:59:42 +00003164 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003165 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3166 MCAsmMacroParameter Parameter;
3167 if (parseIdentifier(Parameter.first))
3168 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003169
David Majnemer91fc4c22014-01-29 18:57:46 +00003170 if (getLexer().is(AsmToken::Equal)) {
3171 Lex();
3172 if (parseMacroArgument(Parameter.second))
3173 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003174 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003175
3176 Parameters.push_back(Parameter);
3177
3178 if (getLexer().is(AsmToken::Comma))
3179 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003180 }
3181
3182 // Eat the end of statement.
3183 Lex();
3184
3185 AsmToken EndToken, StartToken = getTok();
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003186 unsigned MacroDepth = 0;
Eli Bendersky17233942013-01-15 22:59:42 +00003187
3188 // Lex the macro definition.
3189 for (;;) {
3190 // Check whether we have reached the end of the file.
3191 if (getLexer().is(AsmToken::Eof))
3192 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3193
3194 // Otherwise, check whether we have reach the .endmacro.
Benjamin Kramer9d94a4e2014-02-09 16:22:00 +00003195 if (getLexer().is(AsmToken::Identifier)) {
3196 if (getTok().getIdentifier() == ".endm" ||
3197 getTok().getIdentifier() == ".endmacro") {
3198 if (MacroDepth == 0) { // Outermost macro.
3199 EndToken = getTok();
3200 Lex();
3201 if (getLexer().isNot(AsmToken::EndOfStatement))
3202 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3203 "' directive");
3204 break;
3205 } else {
3206 // Otherwise we just found the end of an inner macro.
3207 --MacroDepth;
3208 }
3209 } else if (getTok().getIdentifier() == ".macro") {
3210 // We allow nested macros. Those aren't instantiated until the outermost
3211 // macro is expanded so just ignore them for now.
3212 ++MacroDepth;
3213 }
Eli Bendersky17233942013-01-15 22:59:42 +00003214 }
3215
3216 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003217 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003218 }
3219
Jim Grosbach4b905842013-09-20 23:08:21 +00003220 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003221 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3222 }
3223
3224 const char *BodyStart = StartToken.getLoc().getPointer();
3225 const char *BodyEnd = EndToken.getLoc().getPointer();
3226 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003227 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3228 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003229 return false;
3230}
3231
Jim Grosbach4b905842013-09-20 23:08:21 +00003232/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003233///
3234/// With the support added for named parameters there may be code out there that
3235/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003236/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003237/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003238/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003239/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3240/// warning that the positional parameter found in body which have no effect.
3241/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003242/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003243/// intended or change the macro to use the named parameters. It is possible
3244/// this warning will trigger when the none of the named parameters are used
3245/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003246void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003247 StringRef Body,
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00003248 ArrayRef<MCAsmMacroParameter> Parameters) {
Kevin Enderby81c944c2013-01-22 21:44:53 +00003249 // If this macro is not defined with named parameters the warning we are
3250 // checking for here doesn't apply.
3251 unsigned NParameters = Parameters.size();
3252 if (NParameters == 0)
3253 return;
3254
3255 bool NamedParametersFound = false;
3256 bool PositionalParametersFound = false;
3257
3258 // Look at the body of the macro for use of both the named parameters and what
3259 // are likely to be positional parameters. This is what expandMacro() is
3260 // doing when it finds the parameters in the body.
3261 while (!Body.empty()) {
3262 // Scan for the next possible parameter.
3263 std::size_t End = Body.size(), Pos = 0;
3264 for (; Pos != End; ++Pos) {
3265 // Check for a substitution or escape.
3266 // This macro is defined with parameters, look for \foo, \bar, etc.
3267 if (Body[Pos] == '\\' && Pos + 1 != End)
3268 break;
3269
3270 // This macro should have parameters, but look for $0, $1, ..., $n too.
3271 if (Body[Pos] != '$' || Pos + 1 == End)
3272 continue;
3273 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003274 if (Next == '$' || Next == 'n' ||
3275 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003276 break;
3277 }
3278
3279 // Check if we reached the end.
3280 if (Pos == End)
3281 break;
3282
3283 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003284 switch (Body[Pos + 1]) {
3285 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003286 case '$':
3287 break;
3288
Jim Grosbach4b905842013-09-20 23:08:21 +00003289 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003290 case 'n':
3291 PositionalParametersFound = true;
3292 break;
3293
Jim Grosbach4b905842013-09-20 23:08:21 +00003294 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003295 default: {
3296 PositionalParametersFound = true;
3297 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003298 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003299 }
3300 Pos += 2;
3301 } else {
3302 unsigned I = Pos + 1;
3303 while (isIdentifierChar(Body[I]) && I + 1 != End)
3304 ++I;
3305
Jim Grosbach4b905842013-09-20 23:08:21 +00003306 const char *Begin = Body.data() + Pos + 1;
3307 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003308 unsigned Index = 0;
3309 for (; Index < NParameters; ++Index)
3310 if (Parameters[Index].first == Argument)
3311 break;
3312
3313 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003314 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3315 Pos += 3;
3316 else {
3317 Pos = I;
3318 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003319 } else {
3320 NamedParametersFound = true;
3321 Pos += 1 + Argument.size();
3322 }
3323 }
3324 // Update the scan point.
3325 Body = Body.substr(Pos);
3326 }
3327
3328 if (!NamedParametersFound && PositionalParametersFound)
3329 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3330 "used in macro body, possible positional parameter "
3331 "found in body which will have no effect");
3332}
3333
Jim Grosbach4b905842013-09-20 23:08:21 +00003334/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003335/// ::= .endm
3336/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003337bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003338 if (getLexer().isNot(AsmToken::EndOfStatement))
3339 return TokError("unexpected token in '" + Directive + "' directive");
3340
3341 // If we are inside a macro instantiation, terminate the current
3342 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003343 if (isInsideMacroInstantiation()) {
3344 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003345 return false;
3346 }
3347
3348 // Otherwise, this .endmacro is a stray entry in the file; well formed
3349 // .endmacro directives are handled during the macro definition parsing.
3350 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003351 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003352}
3353
Jim Grosbach4b905842013-09-20 23:08:21 +00003354/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003355/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003356bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003357 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003358 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003359 return TokError("expected identifier in '.purgem' directive");
3360
3361 if (getLexer().isNot(AsmToken::EndOfStatement))
3362 return TokError("unexpected token in '.purgem' directive");
3363
Jim Grosbach4b905842013-09-20 23:08:21 +00003364 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003365 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3366
Jim Grosbach4b905842013-09-20 23:08:21 +00003367 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003368 return false;
3369}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003370
Jim Grosbach4b905842013-09-20 23:08:21 +00003371/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003372/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003373bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003374 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003375
3376 // Expect a single argument: an expression that evaluates to a constant
3377 // in the inclusive range 0-30.
3378 SMLoc ExprLoc = getLexer().getLoc();
3379 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003380 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003381 return true;
3382 else if (getLexer().isNot(AsmToken::EndOfStatement))
3383 return TokError("unexpected token after expression in"
3384 " '.bundle_align_mode' directive");
3385 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3386 return Error(ExprLoc,
3387 "invalid bundle alignment size (expected between 0 and 30)");
3388
3389 Lex();
3390
3391 // Because of AlignSizePow2's verified range we can safely truncate it to
3392 // unsigned.
3393 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3394 return false;
3395}
3396
Jim Grosbach4b905842013-09-20 23:08:21 +00003397/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003398/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003399bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003400 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003401 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003402
Eli Bendersky802b6282013-01-07 21:51:08 +00003403 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3404 StringRef Option;
3405 SMLoc Loc = getTok().getLoc();
3406 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003407 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003408
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003409 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003410 return Error(Loc, kInvalidOptionError);
3411
3412 if (Option != "align_to_end")
3413 return Error(Loc, kInvalidOptionError);
3414 else if (getLexer().isNot(AsmToken::EndOfStatement))
3415 return Error(Loc,
3416 "unexpected token after '.bundle_lock' directive option");
3417 AlignToEnd = true;
3418 }
3419
Eli Benderskyf483ff92012-12-20 19:05:53 +00003420 Lex();
3421
Eli Bendersky802b6282013-01-07 21:51:08 +00003422 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003423 return false;
3424}
3425
Jim Grosbach4b905842013-09-20 23:08:21 +00003426/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003427/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003428bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003429 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003430
3431 if (getLexer().isNot(AsmToken::EndOfStatement))
3432 return TokError("unexpected token in '.bundle_unlock' directive");
3433 Lex();
3434
3435 getStreamer().EmitBundleUnlock();
3436 return false;
3437}
3438
Jim Grosbach4b905842013-09-20 23:08:21 +00003439/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003440/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003441bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003442 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003443
3444 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003445 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003446 return true;
3447
3448 int64_t FillExpr = 0;
3449 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3450 if (getLexer().isNot(AsmToken::Comma))
3451 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3452 Lex();
3453
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003454 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003455 return true;
3456
3457 if (getLexer().isNot(AsmToken::EndOfStatement))
3458 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3459 }
3460
3461 Lex();
3462
3463 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003464 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3465 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003466
3467 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003468 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003469
3470 return false;
3471}
3472
Jim Grosbach4b905842013-09-20 23:08:21 +00003473/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003474/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003475bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003476 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003477 const MCExpr *Value;
3478
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003479 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003480 return true;
3481
3482 if (getLexer().isNot(AsmToken::EndOfStatement))
3483 return TokError("unexpected token in directive");
3484
3485 if (Signed)
3486 getStreamer().EmitSLEB128Value(Value);
3487 else
3488 getStreamer().EmitULEB128Value(Value);
3489
3490 return false;
3491}
3492
Jim Grosbach4b905842013-09-20 23:08:21 +00003493/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003494/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003495bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003496 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003497 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003498 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003499 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003500
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003501 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003502 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003503
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003504 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003505
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003506 // Assembler local symbols don't make any sense here. Complain loudly.
3507 if (Sym->isTemporary())
3508 return Error(Loc, "non-local symbol required in directive");
3509
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003510 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3511 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003512
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003513 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003514 break;
3515
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003516 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003517 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003518 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003519 }
3520 }
3521
Sean Callanan686ed8d2010-01-19 20:22:31 +00003522 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003523 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003524}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003525
Jim Grosbach4b905842013-09-20 23:08:21 +00003526/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003527/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003528bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003529 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003530
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003531 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003532 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003533 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003534 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003535
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003536 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003537 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003538
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003539 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003540 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003541 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003542
3543 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003544 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003545 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003546 return true;
3547
3548 int64_t Pow2Alignment = 0;
3549 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003550 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003551 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003552 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003553 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003554 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003555
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003556 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3557 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003558 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3559
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003560 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003561 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3562 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003563 if (!isPowerOf2_64(Pow2Alignment))
3564 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3565 Pow2Alignment = Log2_64(Pow2Alignment);
3566 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003567 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003568
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003569 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003570 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003571
Sean Callanan686ed8d2010-01-19 20:22:31 +00003572 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003573
Chris Lattner28ad7542009-07-09 17:25:12 +00003574 // NOTE: a size of zero for a .comm should create a undefined symbol
3575 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003576 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003577 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003578 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003579
Eric Christopherbc818852010-05-14 01:38:54 +00003580 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003581 // may internally end up wanting an alignment in bytes.
3582 // FIXME: Diagnose overflow.
3583 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003584 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003585 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003586
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003587 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003588 return Error(IDLoc, "invalid symbol redefinition");
3589
Chris Lattner28ad7542009-07-09 17:25:12 +00003590 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003591 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003592 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003593 return false;
3594 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003595
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003596 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003597 return false;
3598}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003599
Jim Grosbach4b905842013-09-20 23:08:21 +00003600/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003601/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003602bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003603 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003604 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003605
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003606 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003607 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003608 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003609
Sean Callanan686ed8d2010-01-19 20:22:31 +00003610 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003611
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003612 if (Str.empty())
3613 Error(Loc, ".abort detected. Assembly stopping.");
3614 else
3615 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003616 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003617
3618 return false;
3619}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003620
Jim Grosbach4b905842013-09-20 23:08:21 +00003621/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003622/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003623bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003624 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003625 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003626
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003627 // Allow the strings to have escaped octal character sequence.
3628 std::string Filename;
3629 if (parseEscapedString(Filename))
3630 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003631 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003632 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003633
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003634 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003635 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003636
Chris Lattner693fbb82009-07-16 06:14:39 +00003637 // Attempt to switch the lexer to the included file before consuming the end
3638 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003639 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003640 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003641 return true;
3642 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003643
3644 return false;
3645}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003646
Jim Grosbach4b905842013-09-20 23:08:21 +00003647/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003648/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003649bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003650 if (getLexer().isNot(AsmToken::String))
3651 return TokError("expected string in '.incbin' directive");
3652
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003653 // Allow the strings to have escaped octal character sequence.
3654 std::string Filename;
3655 if (parseEscapedString(Filename))
3656 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003657 SMLoc IncbinLoc = getLexer().getLoc();
3658 Lex();
3659
3660 if (getLexer().isNot(AsmToken::EndOfStatement))
3661 return TokError("unexpected token in '.incbin' directive");
3662
Kevin Enderby109f25c2011-12-14 21:47:48 +00003663 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003664 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003665 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3666 return true;
3667 }
3668
3669 return false;
3670}
3671
Jim Grosbach4b905842013-09-20 23:08:21 +00003672/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003673/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003674bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003675 TheCondStack.push_back(TheCondState);
3676 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003677 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003678 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003679 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003680 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003681 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003682 return true;
3683
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003684 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003685 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003686
Sean Callanan686ed8d2010-01-19 20:22:31 +00003687 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003688
3689 TheCondState.CondMet = ExprValue;
3690 TheCondState.Ignore = !TheCondState.CondMet;
3691 }
3692
3693 return false;
3694}
3695
Jim Grosbach4b905842013-09-20 23:08:21 +00003696/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003697/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003698bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003699 TheCondStack.push_back(TheCondState);
3700 TheCondState.TheCond = AsmCond::IfCond;
3701
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003702 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003703 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003704 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003705 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003706
3707 if (getLexer().isNot(AsmToken::EndOfStatement))
3708 return TokError("unexpected token in '.ifb' directive");
3709
3710 Lex();
3711
3712 TheCondState.CondMet = ExpectBlank == Str.empty();
3713 TheCondState.Ignore = !TheCondState.CondMet;
3714 }
3715
3716 return false;
3717}
3718
Jim Grosbach4b905842013-09-20 23:08:21 +00003719/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003720/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003721bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003722 TheCondStack.push_back(TheCondState);
3723 TheCondState.TheCond = AsmCond::IfCond;
3724
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003725 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003726 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003727 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003728 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003729
3730 if (getLexer().isNot(AsmToken::Comma))
3731 return TokError("unexpected token in '.ifc' directive");
3732
3733 Lex();
3734
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003735 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003736
3737 if (getLexer().isNot(AsmToken::EndOfStatement))
3738 return TokError("unexpected token in '.ifc' directive");
3739
3740 Lex();
3741
3742 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3743 TheCondState.Ignore = !TheCondState.CondMet;
3744 }
3745
3746 return false;
3747}
3748
Jim Grosbach4b905842013-09-20 23:08:21 +00003749/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003750/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003751bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003752 StringRef Name;
3753 TheCondStack.push_back(TheCondState);
3754 TheCondState.TheCond = AsmCond::IfCond;
3755
3756 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003757 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003758 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003759 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003760 return TokError("expected identifier after '.ifdef'");
3761
3762 Lex();
3763
3764 MCSymbol *Sym = getContext().LookupSymbol(Name);
3765
3766 if (expect_defined)
3767 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3768 else
3769 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3770 TheCondState.Ignore = !TheCondState.CondMet;
3771 }
3772
3773 return false;
3774}
3775
Jim Grosbach4b905842013-09-20 23:08:21 +00003776/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003777/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003778bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003779 if (TheCondState.TheCond != AsmCond::IfCond &&
3780 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003781 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3782 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003783 TheCondState.TheCond = AsmCond::ElseIfCond;
3784
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003785 bool LastIgnoreState = false;
3786 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003787 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003788 if (LastIgnoreState || TheCondState.CondMet) {
3789 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003790 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003791 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003792 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003793 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003794 return true;
3795
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003796 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003797 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003798
Sean Callanan686ed8d2010-01-19 20:22:31 +00003799 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003800 TheCondState.CondMet = ExprValue;
3801 TheCondState.Ignore = !TheCondState.CondMet;
3802 }
3803
3804 return false;
3805}
3806
Jim Grosbach4b905842013-09-20 23:08:21 +00003807/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003808/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003809bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003810 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003811 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003812
Sean Callanan686ed8d2010-01-19 20:22:31 +00003813 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003814
3815 if (TheCondState.TheCond != AsmCond::IfCond &&
3816 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003817 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3818 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003819 TheCondState.TheCond = AsmCond::ElseCond;
3820 bool LastIgnoreState = false;
3821 if (!TheCondStack.empty())
3822 LastIgnoreState = TheCondStack.back().Ignore;
3823 if (LastIgnoreState || TheCondState.CondMet)
3824 TheCondState.Ignore = true;
3825 else
3826 TheCondState.Ignore = false;
3827
3828 return false;
3829}
3830
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003831/// parseDirectiveEnd
3832/// ::= .end
3833bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3834 if (getLexer().isNot(AsmToken::EndOfStatement))
3835 return TokError("unexpected token in '.end' directive");
3836
3837 Lex();
3838
3839 while (Lexer.isNot(AsmToken::Eof))
3840 Lex();
3841
3842 return false;
3843}
3844
Jim Grosbach4b905842013-09-20 23:08:21 +00003845/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003846/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003847bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003848 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003849 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003850
Sean Callanan686ed8d2010-01-19 20:22:31 +00003851 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003852
Jim Grosbach4b905842013-09-20 23:08:21 +00003853 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003854 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3855 ".else");
3856 if (!TheCondStack.empty()) {
3857 TheCondState = TheCondStack.back();
3858 TheCondStack.pop_back();
3859 }
3860
3861 return false;
3862}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003863
Eli Bendersky17233942013-01-15 22:59:42 +00003864void AsmParser::initializeDirectiveKindMap() {
3865 DirectiveKindMap[".set"] = DK_SET;
3866 DirectiveKindMap[".equ"] = DK_EQU;
3867 DirectiveKindMap[".equiv"] = DK_EQUIV;
3868 DirectiveKindMap[".ascii"] = DK_ASCII;
3869 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3870 DirectiveKindMap[".string"] = DK_STRING;
3871 DirectiveKindMap[".byte"] = DK_BYTE;
3872 DirectiveKindMap[".short"] = DK_SHORT;
3873 DirectiveKindMap[".value"] = DK_VALUE;
3874 DirectiveKindMap[".2byte"] = DK_2BYTE;
3875 DirectiveKindMap[".long"] = DK_LONG;
3876 DirectiveKindMap[".int"] = DK_INT;
3877 DirectiveKindMap[".4byte"] = DK_4BYTE;
3878 DirectiveKindMap[".quad"] = DK_QUAD;
3879 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00003880 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00003881 DirectiveKindMap[".single"] = DK_SINGLE;
3882 DirectiveKindMap[".float"] = DK_FLOAT;
3883 DirectiveKindMap[".double"] = DK_DOUBLE;
3884 DirectiveKindMap[".align"] = DK_ALIGN;
3885 DirectiveKindMap[".align32"] = DK_ALIGN32;
3886 DirectiveKindMap[".balign"] = DK_BALIGN;
3887 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3888 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3889 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3890 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3891 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3892 DirectiveKindMap[".org"] = DK_ORG;
3893 DirectiveKindMap[".fill"] = DK_FILL;
3894 DirectiveKindMap[".zero"] = DK_ZERO;
3895 DirectiveKindMap[".extern"] = DK_EXTERN;
3896 DirectiveKindMap[".globl"] = DK_GLOBL;
3897 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003898 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3899 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3900 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3901 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3902 DirectiveKindMap[".reference"] = DK_REFERENCE;
3903 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3904 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3905 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3906 DirectiveKindMap[".comm"] = DK_COMM;
3907 DirectiveKindMap[".common"] = DK_COMMON;
3908 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3909 DirectiveKindMap[".abort"] = DK_ABORT;
3910 DirectiveKindMap[".include"] = DK_INCLUDE;
3911 DirectiveKindMap[".incbin"] = DK_INCBIN;
3912 DirectiveKindMap[".code16"] = DK_CODE16;
3913 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3914 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003915 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003916 DirectiveKindMap[".irp"] = DK_IRP;
3917 DirectiveKindMap[".irpc"] = DK_IRPC;
3918 DirectiveKindMap[".endr"] = DK_ENDR;
3919 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3920 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3921 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3922 DirectiveKindMap[".if"] = DK_IF;
3923 DirectiveKindMap[".ifb"] = DK_IFB;
3924 DirectiveKindMap[".ifnb"] = DK_IFNB;
3925 DirectiveKindMap[".ifc"] = DK_IFC;
3926 DirectiveKindMap[".ifnc"] = DK_IFNC;
3927 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3928 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3929 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3930 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3931 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003932 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003933 DirectiveKindMap[".endif"] = DK_ENDIF;
3934 DirectiveKindMap[".skip"] = DK_SKIP;
3935 DirectiveKindMap[".space"] = DK_SPACE;
3936 DirectiveKindMap[".file"] = DK_FILE;
3937 DirectiveKindMap[".line"] = DK_LINE;
3938 DirectiveKindMap[".loc"] = DK_LOC;
3939 DirectiveKindMap[".stabs"] = DK_STABS;
3940 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3941 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3942 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3943 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3944 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3945 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3946 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3947 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3948 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3949 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3950 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3951 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3952 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3953 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3954 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3955 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3956 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3957 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3958 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3959 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3960 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003961 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003962 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3963 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3964 DirectiveKindMap[".macro"] = DK_MACRO;
3965 DirectiveKindMap[".endm"] = DK_ENDM;
3966 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3967 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003968}
3969
Jim Grosbach4b905842013-09-20 23:08:21 +00003970MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003971 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003972
Rafael Espindola34b9c512012-06-03 23:57:14 +00003973 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003974 for (;;) {
3975 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003976 if (getLexer().is(AsmToken::Eof)) {
3977 Error(DirectiveLoc, "no matching '.endr' in definition");
3978 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003979 }
3980
Rafael Espindola34b9c512012-06-03 23:57:14 +00003981 if (Lexer.is(AsmToken::Identifier) &&
3982 (getTok().getIdentifier() == ".rept")) {
3983 ++NestLevel;
3984 }
3985
3986 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003987 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003988 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003989 EndToken = getTok();
3990 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003991 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3992 TokError("unexpected token in '.endr' directive");
3993 return 0;
3994 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003995 break;
3996 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003997 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003998 }
3999
Rafael Espindola34b9c512012-06-03 23:57:14 +00004000 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004001 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004002 }
4003
4004 const char *BodyStart = StartToken.getLoc().getPointer();
4005 const char *BodyEnd = EndToken.getLoc().getPointer();
4006 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4007
Rafael Espindola34b9c512012-06-03 23:57:14 +00004008 // We Are Anonymous.
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004009 MacroLikeBodies.push_back(MCAsmMacro(StringRef(), Body, None));
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004010 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004011}
4012
Jim Grosbach4b905842013-09-20 23:08:21 +00004013void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004014 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004015 OS << ".endr\n";
4016
4017 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004018 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004019
Rafael Espindola34b9c512012-06-03 23:57:14 +00004020 // Create the macro instantiation object and add to the current macro
4021 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004022 MacroInstantiation *MI = new MacroInstantiation(
4023 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004024 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004025
Rafael Espindola34b9c512012-06-03 23:57:14 +00004026 // Jump to the macro instantiation and prime the lexer.
4027 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4028 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4029 Lex();
4030}
4031
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004032/// parseDirectiveRept
4033/// ::= .rep | .rept count
4034bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004035 const MCExpr *CountExpr;
4036 SMLoc CountLoc = getTok().getLoc();
4037 if (parseExpression(CountExpr))
4038 return true;
4039
Rafael Espindola34b9c512012-06-03 23:57:14 +00004040 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004041 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4042 eatToEndOfStatement();
4043 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4044 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004045
4046 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004047 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004048
4049 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004050 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004051
4052 // Eat the end of statement.
4053 Lex();
4054
4055 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004056 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004057 if (!M)
4058 return true;
4059
4060 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4061 // to hold the macro body with substitutions.
4062 SmallString<256> Buf;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004063 raw_svector_ostream OS(Buf);
4064 while (Count--) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004065 if (expandMacro(OS, M->Body, None, None, getTok().getLoc()))
Rafael Espindola34b9c512012-06-03 23:57:14 +00004066 return true;
4067 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004068 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004069
4070 return false;
4071}
4072
Jim Grosbach4b905842013-09-20 23:08:21 +00004073/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004074/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004075bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004076 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004077
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004078 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004079 return TokError("expected identifier in '.irp' directive");
4080
Rafael Espindola768b41c2012-06-15 14:02:34 +00004081 if (Lexer.isNot(AsmToken::Comma))
4082 return TokError("expected comma in '.irp' directive");
4083
4084 Lex();
4085
Eli Bendersky38274122013-01-14 23:22:36 +00004086 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004087 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004088 return true;
4089
4090 // Eat the end of statement.
4091 Lex();
4092
4093 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004094 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004095 if (!M)
4096 return true;
4097
4098 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4099 // to hold the macro body with substitutions.
4100 SmallString<256> Buf;
4101 raw_svector_ostream OS(Buf);
4102
Eli Bendersky38274122013-01-14 23:22:36 +00004103 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004104 if (expandMacro(OS, M->Body, Parameter, *i, getTok().getLoc()))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004105 return true;
4106 }
4107
Jim Grosbach4b905842013-09-20 23:08:21 +00004108 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004109
4110 return false;
4111}
4112
Jim Grosbach4b905842013-09-20 23:08:21 +00004113/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004114/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004115bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004116 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004117
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004118 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004119 return TokError("expected identifier in '.irpc' directive");
4120
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004121 if (Lexer.isNot(AsmToken::Comma))
4122 return TokError("expected comma in '.irpc' directive");
4123
4124 Lex();
4125
Eli Bendersky38274122013-01-14 23:22:36 +00004126 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004127 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004128 return true;
4129
4130 if (A.size() != 1 || A.front().size() != 1)
4131 return TokError("unexpected token in '.irpc' directive");
4132
4133 // Eat the end of statement.
4134 Lex();
4135
4136 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004137 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004138 if (!M)
4139 return true;
4140
4141 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4142 // to hold the macro body with substitutions.
4143 SmallString<256> Buf;
4144 raw_svector_ostream OS(Buf);
4145
4146 StringRef Values = A.front().front().getString();
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004147 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004148 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004149 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004150
Benjamin Kramerd31aaf12014-02-09 17:13:11 +00004151 if (expandMacro(OS, M->Body, Parameter, Arg, getTok().getLoc()))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004152 return true;
4153 }
4154
Jim Grosbach4b905842013-09-20 23:08:21 +00004155 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004156
4157 return false;
4158}
4159
Jim Grosbach4b905842013-09-20 23:08:21 +00004160bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004161 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004162 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004163
4164 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004165 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004166 assert(getLexer().is(AsmToken::EndOfStatement));
4167
Jim Grosbach4b905842013-09-20 23:08:21 +00004168 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004169 return false;
4170}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004171
Jim Grosbach4b905842013-09-20 23:08:21 +00004172bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004173 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004174 const MCExpr *Value;
4175 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004176 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004177 return true;
4178 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4179 if (!MCE)
4180 return Error(ExprLoc, "unexpected expression in _emit");
4181 uint64_t IntValue = MCE->getValue();
4182 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4183 return Error(ExprLoc, "literal value out of range for directive");
4184
Chad Rosierc7f552c2013-02-12 21:33:51 +00004185 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4186 return false;
4187}
4188
Jim Grosbach4b905842013-09-20 23:08:21 +00004189bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004190 const MCExpr *Value;
4191 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004192 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004193 return true;
4194 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4195 if (!MCE)
4196 return Error(ExprLoc, "unexpected expression in align");
4197 uint64_t IntValue = MCE->getValue();
4198 if (!isPowerOf2_64(IntValue))
4199 return Error(ExprLoc, "literal value not a power of two greater then zero");
4200
Jim Grosbach4b905842013-09-20 23:08:21 +00004201 Info.AsmRewrites->push_back(
4202 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004203 return false;
4204}
4205
Chad Rosierf43fcf52013-02-13 21:27:17 +00004206// We are comparing pointers, but the pointers are relative to a single string.
4207// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004208static int rewritesSort(const AsmRewrite *AsmRewriteA,
4209 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004210 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4211 return -1;
4212 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4213 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004214
Chad Rosierfce4fab2013-04-08 17:43:47 +00004215 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4216 // rewrite to the same location. Make sure the SizeDirective rewrite is
4217 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4218 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004219 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4220 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004221 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004222
Jim Grosbach4b905842013-09-20 23:08:21 +00004223 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4224 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004225 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004226 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004227}
4228
Jim Grosbach4b905842013-09-20 23:08:21 +00004229bool AsmParser::parseMSInlineAsm(
4230 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4231 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4232 SmallVectorImpl<std::string> &Constraints,
4233 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4234 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004235 SmallVector<void *, 4> InputDecls;
4236 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004237 SmallVector<bool, 4> InputDeclsAddressOf;
4238 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004239 SmallVector<std::string, 4> InputConstraints;
4240 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004241 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004242
Benjamin Kramer1a136112013-02-15 20:37:21 +00004243 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004244
4245 // Prime the lexer.
4246 Lex();
4247
4248 // While we have input, parse each statement.
4249 unsigned InputIdx = 0;
4250 unsigned OutputIdx = 0;
4251 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004252 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004253 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004254 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004255
Chad Rosier149e8e02012-12-12 22:45:52 +00004256 if (Info.ParseError)
4257 return true;
4258
Benjamin Kramer1a136112013-02-15 20:37:21 +00004259 if (Info.Opcode == ~0U)
4260 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004261
Benjamin Kramer1a136112013-02-15 20:37:21 +00004262 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004263
Benjamin Kramer1a136112013-02-15 20:37:21 +00004264 // Build the list of clobbers, outputs and inputs.
4265 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4266 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004267
Benjamin Kramer1a136112013-02-15 20:37:21 +00004268 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004269 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004270 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004271
Benjamin Kramer1a136112013-02-15 20:37:21 +00004272 // Register operand.
4273 if (Operand->isReg() && !Operand->needAddressOf()) {
4274 unsigned NumDefs = Desc.getNumDefs();
4275 // Clobber.
4276 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4277 ClobberRegs.push_back(Operand->getReg());
4278 continue;
4279 }
4280
4281 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004282 StringRef SymName = Operand->getSymName();
4283 if (SymName.empty())
4284 continue;
4285
Chad Rosierdba3fe52013-04-22 22:12:12 +00004286 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004287 if (!OpDecl)
4288 continue;
4289
4290 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004291 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004292 if (isOutput) {
4293 ++InputIdx;
4294 OutputDecls.push_back(OpDecl);
4295 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4296 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004297 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004298 } else {
4299 InputDecls.push_back(OpDecl);
4300 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4301 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004302 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004303 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004304 }
Reid Kleckneree088972013-12-10 18:27:32 +00004305
4306 // Consider implicit defs to be clobbers. Think of cpuid and push.
4307 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4308 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4309 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004310 }
4311
4312 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004313 NumOutputs = OutputDecls.size();
4314 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004315
4316 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004317 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4318 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4319 ClobberRegs.end());
4320 Clobbers.assign(ClobberRegs.size(), std::string());
4321 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4322 raw_string_ostream OS(Clobbers[I]);
4323 IP->printRegName(OS, ClobberRegs[I]);
4324 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004325
4326 // Merge the various outputs and inputs. Output are expected first.
4327 if (NumOutputs || NumInputs) {
4328 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004329 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004330 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004331 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004332 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004333 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004334 }
4335 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004336 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004337 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004338 }
4339 }
4340
4341 // Build the IR assembly string.
4342 std::string AsmStringIR;
4343 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004344 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4345 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004346 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004347 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4348 E = AsmStrRewrites.end();
4349 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004350 AsmRewriteKind Kind = (*I).Kind;
4351 if (Kind == AOK_Delete)
4352 continue;
4353
Chad Rosier8bce6642012-10-18 15:49:34 +00004354 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004355 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004356
Chad Rosier120eefd2013-03-19 17:32:17 +00004357 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004358 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004359 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004360 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004361
Chad Rosier37e755c2012-10-23 17:43:43 +00004362 // Skip the original expression.
4363 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004364 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004365 continue;
4366 }
4367
Chad Rosierff10ed12013-04-12 16:26:42 +00004368 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004369 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004370 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004371 default:
4372 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004373 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004374 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004375 break;
4376 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004377 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004378 break;
4379 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004380 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004381 break;
4382 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004383 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004384 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004385 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004386 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004387 default: break;
4388 case 8: OS << "byte ptr "; break;
4389 case 16: OS << "word ptr "; break;
4390 case 32: OS << "dword ptr "; break;
4391 case 64: OS << "qword ptr "; break;
4392 case 80: OS << "xword ptr "; break;
4393 case 128: OS << "xmmword ptr "; break;
4394 case 256: OS << "ymmword ptr "; break;
4395 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004396 break;
4397 case AOK_Emit:
4398 OS << ".byte";
4399 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004400 case AOK_Align: {
4401 unsigned Val = (*I).Val;
4402 OS << ".align " << Val;
4403
4404 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004405 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004406 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4407 break;
4408 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004409 case AOK_DotOperator:
4410 OS << (*I).Val;
4411 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004412 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004413
Chad Rosier8bce6642012-10-18 15:49:34 +00004414 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004415 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004416 }
4417
4418 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004419 if (AsmStart != AsmEnd)
4420 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004421
4422 AsmString = OS.str();
4423 return false;
4424}
4425
Daniel Dunbar01e36072010-07-17 02:26:10 +00004426/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004427MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4428 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004429 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004430}