blob: 10f4fbb04efbd4cd9a8f694b362e6e87b1575449 [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:
67 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
68 Name(N), Body(B), Parameters(P) {}
69
70 MCAsmMacro(const MCAsmMacro& Other)
71 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
72};
73
Daniel Dunbar43235712010-07-18 18:54:11 +000074/// \brief Helper class for storing information about an active macro
75/// instantiation.
76struct MacroInstantiation {
77 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000078 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000079
80 /// The macro instantiation with substitutions.
81 MemoryBuffer *Instantiation;
82
83 /// The location of the instantiation.
84 SMLoc InstantiationLoc;
85
Daniel Dunbar40f1d852012-12-01 01:38:48 +000086 /// The buffer where parsing should resume upon instantiation completion.
87 int ExitBuffer;
88
Daniel Dunbar43235712010-07-18 18:54:11 +000089 /// The location where parsing should resume upon instantiation completion.
90 SMLoc ExitLoc;
91
92public:
Eli Bendersky38274122013-01-14 23:22:36 +000093 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000094 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000095};
96
Eli Friedman0f4871d2012-10-22 23:58:19 +000097struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000098 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +000099 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
100
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000102 unsigned Opcode;
103
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000105 bool ParseError;
106
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 SmallVectorImpl<AsmRewrite> *AsmRewrites;
108
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000112
113 ~ParseStatementInfo() {
114 // Free any parsed operands.
115 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
116 delete ParsedOperands[i];
117 ParsedOperands.clear();
118 }
119};
120
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121/// \brief The concrete assembly parser instance.
122class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000123 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
124 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125private:
126 AsmLexer Lexer;
127 MCContext &Ctx;
128 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000129 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000131 SourceMgr::DiagHandlerTy SavedDiagHandler;
132 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000133 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000134
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000135 /// This is the current buffer index we're lexing from as managed by the
136 /// SourceMgr object.
137 int CurBuffer;
138
139 AsmCond TheCondState;
140 std::vector<AsmCond> TheCondStack;
141
Jim Grosbach4b905842013-09-20 23:08:21 +0000142 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000143 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000144 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000145 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000146
Jim Grosbach4b905842013-09-20 23:08:21 +0000147 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000148 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000149
Jim Grosbach4b905842013-09-20 23:08:21 +0000150 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000151 std::vector<MacroInstantiation*> ActiveMacros;
152
Jim Grosbach4b905842013-09-20 23:08:21 +0000153 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000154 std::deque<MCAsmMacro> MacroLikeBodies;
155
Daniel Dunbar828984f2010-07-18 18:38:02 +0000156 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000157 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000158
Daniel Dunbar43325c42010-09-09 22:42:56 +0000159 /// Flag tracking whether any errors have been encountered.
160 unsigned HadError : 1;
161
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000162 /// The values from the last parsed cpp hash file line comment if any.
163 StringRef CppHashFilename;
164 int64_t CppHashLineNumber;
165 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000166 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000167 /// When generating dwarf for assembly source files we need to calculate the
168 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000169 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000170 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
171 SMLoc LastQueryIDLoc;
172 int LastQueryBuffer;
173 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000174
Devang Patela173ee52012-01-31 18:14:05 +0000175 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
176 unsigned AssemblerDialect;
177
Jim Grosbach4b905842013-09-20 23:08:21 +0000178 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000179 bool IsDarwin;
180
Jim Grosbach4b905842013-09-20 23:08:21 +0000181 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000182 bool ParsingInlineAsm;
183
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000185 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000187 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188
189 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
190
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000191 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveHandler Handler) {
193 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194 }
195
196public:
197 /// @name MCAsmParser Interface
198 /// {
199
200 virtual SourceMgr &getSourceManager() { return SrcMgr; }
201 virtual MCAsmLexer &getLexer() { return Lexer; }
202 virtual MCContext &getContext() { return Ctx; }
203 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000204 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000205 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000206 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000207 else
208 return AssemblerDialect;
209 }
210 virtual void setAssemblerDialect(unsigned i) {
211 AssemblerDialect = i;
212 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000214 virtual void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000215 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000216 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000217 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000218 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper5f96ca52012-08-29 05:48:09 +0000220 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000221
Chad Rosier49963552012-10-13 00:26:04 +0000222 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000223 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Clobbers,
230 const MCInstrInfo *MII,
231 const MCInstPrinter *IP,
232 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000233
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000234 bool parseExpression(const MCExpr *&Res);
235 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000236 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000237 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
238 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000239
Jim Grosbach4b905842013-09-20 23:08:21 +0000240 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000241 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000242 virtual bool parseIdentifier(StringRef &Res);
243 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000244
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000245 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000246 /// }
247
248private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000249
Jim Grosbach4b905842013-09-20 23:08:21 +0000250 bool parseStatement(ParseStatementInfo &Info);
251 void eatToEndOfLine();
252 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000253
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Kevin Enderby81c944c2013-01-22 21:44:53 +0000255 MCAsmMacroParameters Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000256 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +0000257 const MCAsmMacroParameters &Parameters,
258 const MCAsmMacroArguments &A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000259 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000260
Eli Benderskya313ae62013-01-16 18:56:50 +0000261 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000263
264 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000265 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Lookup a previously defined macro.
268 /// \param Name Macro name.
269 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000270 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000271
272 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000273 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000274
275 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000276 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
278 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000279 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000280
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000281 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000282 ///
283 /// \param M The macro.
284 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000285 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000286
287 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000288 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000289
290 /// \brief Extract AsmTokens for a macro argument. If the argument delimiter
291 /// is initially unknown, set it to AsmToken::Eof. It will be set to the
292 /// correct delimiter by the method.
Jim Grosbach4b905842013-09-20 23:08:21 +0000293 bool parseMacroArgument(MCAsmMacroArgument &MA,
Eli Benderskya313ae62013-01-16 18:56:50 +0000294 AsmToken::TokenKind &ArgumentDelimiter);
295
296 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000297 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000298
Jim Grosbach4b905842013-09-20 23:08:21 +0000299 void printMacroInstantiations();
300 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000301 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000302 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000303 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000304 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000305
Jim Grosbach4b905842013-09-20 23:08:21 +0000306 /// \brief Enter the specified file. This returns true on failure.
307 bool enterIncludeFile(const std::string &Filename);
308
309 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000310 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000311 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000312
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000313 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000314 /// current token is not set; clients should ensure Lex() is called
315 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000316 ///
317 /// \param InBuffer If not -1, should be the known buffer id that contains the
318 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000319 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000320
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000321 /// \brief Parse up to the end of statement and a return the contents from the
322 /// current token until the end of the statement; the current token on exit
323 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000324 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000325
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000326 /// \brief Parse until the end of a statement or a comma is encountered,
327 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000328 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000329
Jim Grosbach4b905842013-09-20 23:08:21 +0000330 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000331 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000332
Jim Grosbach4b905842013-09-20 23:08:21 +0000333 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
334 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
335 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000336
Jim Grosbach4b905842013-09-20 23:08:21 +0000337 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000338
Eli Bendersky17233942013-01-15 22:59:42 +0000339 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000340 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000341 DK_NO_DIRECTIVE, // Placeholder
342 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
343 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
344 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000345 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000346 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000347 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000348 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
349 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
350 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
351 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
352 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky17233942013-01-15 22:59:42 +0000353 DK_ELSEIF, DK_ELSE, DK_ENDIF,
354 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
355 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
356 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
357 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
358 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
359 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000360 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000361 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000362 DK_SLEB128, DK_ULEB128,
363 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000364 };
365
Jim Grosbach4b905842013-09-20 23:08:21 +0000366 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000367 /// directives parsed by this class.
368 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000369
370 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000371 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
372 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
373 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
374 bool parseDirectiveFill(); // ".fill"
375 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000376 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
378 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000379 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000381
Eli Bendersky17233942013-01-15 22:59:42 +0000382 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000383 bool parseDirectiveFile(SMLoc DirectiveLoc);
384 bool parseDirectiveLine();
385 bool parseDirectiveLoc();
386 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000387
388 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000390 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000391 bool parseDirectiveCFISections();
392 bool parseDirectiveCFIStartProc();
393 bool parseDirectiveCFIEndProc();
394 bool parseDirectiveCFIDefCfaOffset();
395 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIAdjustCfaOffset();
397 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
399 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
401 bool parseDirectiveCFIRememberState();
402 bool parseDirectiveCFIRestoreState();
403 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
404 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
405 bool parseDirectiveCFIEscape();
406 bool parseDirectiveCFISignalFrame();
407 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000408
409 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000410 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
411 bool parseDirectiveEndMacro(StringRef Directive);
412 bool parseDirectiveMacro(SMLoc DirectiveLoc);
413 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000414
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000417 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000421
Eli Bendersky17233942013-01-15 22:59:42 +0000422 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000424
425 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000427
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000429 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000433
Jim Grosbach4b905842013-09-20 23:08:21 +0000434 bool parseDirectiveAbort(); // ".abort"
435 bool parseDirectiveInclude(); // ".include"
436 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000437
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000439 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000441 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000443 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000444 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
445 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
446 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
447 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000448 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000449
Jim Grosbach4b905842013-09-20 23:08:21 +0000450 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000451 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000452
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000454 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
455 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000456 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000457 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000458 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
459 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
460 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000461
Chad Rosierc7f552c2013-02-12 21:33:51 +0000462 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000463 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000464 size_t Len);
465
466 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000467 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000468
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000469 // "end"
470 bool parseDirectiveEnd(SMLoc DirectiveLoc);
471
Eli Bendersky17233942013-01-15 22:59:42 +0000472 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000473};
Daniel Dunbar86033402010-07-12 17:54:38 +0000474}
475
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000476namespace llvm {
477
478extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000479extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000480extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000481
482}
483
Chris Lattnerc35681b2010-01-19 19:46:13 +0000484enum { DEFAULT_ADDRSPACE = 0 };
485
Jim Grosbach4b905842013-09-20 23:08:21 +0000486AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
487 const MCAsmInfo &_MAI)
488 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
489 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
490 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
491 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000492 // Save the old handler.
493 SavedDiagHandler = SrcMgr.getDiagHandler();
494 SavedDiagContext = SrcMgr.getDiagContext();
495 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000496 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000497 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000498
Daniel Dunbarc5011082010-07-12 18:12:02 +0000499 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000500 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
501 case MCObjectFileInfo::IsCOFF:
502 PlatformParser = createCOFFAsmParser();
503 PlatformParser->Initialize(*this);
504 break;
505 case MCObjectFileInfo::IsMachO:
506 PlatformParser = createDarwinAsmParser();
507 PlatformParser->Initialize(*this);
508 IsDarwin = true;
509 break;
510 case MCObjectFileInfo::IsELF:
511 PlatformParser = createELFAsmParser();
512 PlatformParser->Initialize(*this);
513 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000514 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000515
Eli Bendersky17233942013-01-15 22:59:42 +0000516 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000517}
518
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000519AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000520 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
521
522 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000523 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
524 ie = MacroMap.end();
525 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000526 delete it->getValue();
527
Daniel Dunbarc5011082010-07-12 18:12:02 +0000528 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000529}
530
Jim Grosbach4b905842013-09-20 23:08:21 +0000531void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000532 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000533 for (std::vector<MacroInstantiation *>::const_reverse_iterator
534 it = ActiveMacros.rbegin(),
535 ie = ActiveMacros.rend();
536 it != ie; ++it)
537 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000538 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000539}
540
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000541void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
542 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
543 printMacroInstantiations();
544}
545
Chris Lattnera3a06812011-10-16 04:47:35 +0000546bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000547 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000548 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000549 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
550 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000551 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000552}
553
Chris Lattnera3a06812011-10-16 04:47:35 +0000554bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000555 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000556 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
557 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000558 return true;
559}
560
Jim Grosbach4b905842013-09-20 23:08:21 +0000561bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000562 std::string IncludedFile;
563 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000564 if (NewBuf == -1)
565 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000566
Sean Callanan7a77eae2010-01-21 00:19:58 +0000567 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000568
Sean Callanan7a77eae2010-01-21 00:19:58 +0000569 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000570
Sean Callanan7a77eae2010-01-21 00:19:58 +0000571 return false;
572}
Daniel Dunbar43235712010-07-18 18:54:11 +0000573
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000574/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000575/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000576/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000577bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000578 std::string IncludedFile;
579 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
580 if (NewBuf == -1)
581 return true;
582
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000583 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000584 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000585 return false;
586}
587
Jim Grosbach4b905842013-09-20 23:08:21 +0000588void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000589 if (InBuffer != -1) {
590 CurBuffer = InBuffer;
591 } else {
592 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
593 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000594 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
595}
596
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597const AsmToken &AsmParser::Lex() {
598 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000599
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600 if (tok->is(AsmToken::Eof)) {
601 // If this is the end of an included file, pop the parent file off the
602 // include stack.
603 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
604 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000605 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000606 tok = &Lexer.Lex();
607 }
608 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000609
Sean Callanan7a77eae2010-01-21 00:19:58 +0000610 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000611 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000612
Sean Callanan7a77eae2010-01-21 00:19:58 +0000613 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000614}
615
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000616bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000617 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000618 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000619 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000620
Chris Lattner36e02122009-06-21 20:54:55 +0000621 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000622 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000623
624 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000625 AsmCond StartingCondState = TheCondState;
626
Kevin Enderby6469fc22011-11-01 22:27:22 +0000627 // If we are generating dwarf for assembly source files save the initial text
628 // section and generate a .file directive.
629 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000630 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000631 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
632 getStreamer().EmitLabel(SectionStartSym);
633 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000634 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000635 StringRef(),
636 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000637 }
638
Chris Lattner73f36112009-07-02 21:53:43 +0000639 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000640 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000641 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000642 if (!parseStatement(Info))
643 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000644
Daniel Dunbar43325c42010-09-09 22:42:56 +0000645 // We had an error, validate that one was emitted and recover by skipping to
646 // the next line.
647 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000648 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000649 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000650
651 if (TheCondState.TheCond != StartingCondState.TheCond ||
652 TheCondState.Ignore != StartingCondState.Ignore)
653 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000654
655 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000656 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000657 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000658 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000659 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000660 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000661 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000662
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000663 // Check to see that all assembler local symbols were actually defined.
664 // Targets that don't do subsections via symbols may not want this, though,
665 // so conservatively exclude them. Only do this if we're finalizing, though,
666 // as otherwise we won't necessarilly have seen everything yet.
667 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
668 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
669 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000670 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000671 i != e; ++i) {
672 MCSymbol *Sym = i->getValue();
673 // Variable symbols may not be marked as defined, so check those
674 // explicitly. If we know it's a variable, we have a definition for
675 // the purposes of this check.
676 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
677 // FIXME: We would really like to refer back to where the symbol was
678 // first referenced for a source location. We need to add something
679 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000680 printMessage(
681 getLexer().getLoc(), SourceMgr::DK_Error,
682 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000683 }
684 }
685
David Peixotto308e7e42013-12-19 18:08:08 +0000686 // Callback to the target parser in case it needs to do anything.
687 if (!HadError)
688 getTargetParser().finishParse();
689
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000690 // Finalize the output stream if there are no errors and if the client wants
691 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000692 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000693 Out.Finish();
694
Chris Lattner73f36112009-07-02 21:53:43 +0000695 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000696}
697
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000698void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000699 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000700 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000701 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000702 }
703}
704
Jim Grosbach4b905842013-09-20 23:08:21 +0000705/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000706void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000707 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000708 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000709
Chris Lattnere5074c42009-06-22 01:29:09 +0000710 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000711 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000712 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000713}
714
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000715StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000716 const char *Start = getTok().getLoc().getPointer();
717
Jim Grosbach4b905842013-09-20 23:08:21 +0000718 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000719 Lex();
720
721 const char *End = getTok().getLoc().getPointer();
722 return StringRef(Start, End - Start);
723}
Chris Lattner78db3622009-06-22 05:51:26 +0000724
Jim Grosbach4b905842013-09-20 23:08:21 +0000725StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000726 const char *Start = getTok().getLoc().getPointer();
727
728 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000729 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000730 Lex();
731
732 const char *End = getTok().getLoc().getPointer();
733 return StringRef(Start, End - Start);
734}
735
Jim Grosbach4b905842013-09-20 23:08:21 +0000736/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000737/// NOTE: This assumes the leading '(' has already been consumed.
738///
739/// parenexpr ::= expr)
740///
Jim Grosbach4b905842013-09-20 23:08:21 +0000741bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
742 if (parseExpression(Res))
743 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000744 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000745 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000746 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000747 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000748 return false;
749}
Chris Lattner78db3622009-06-22 05:51:26 +0000750
Jim Grosbach4b905842013-09-20 23:08:21 +0000751/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000752/// NOTE: This assumes the leading '[' has already been consumed.
753///
754/// bracketexpr ::= expr]
755///
Jim Grosbach4b905842013-09-20 23:08:21 +0000756bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
757 if (parseExpression(Res))
758 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000759 if (Lexer.isNot(AsmToken::RBrac))
760 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000761 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000762 Lex();
763 return false;
764}
765
Jim Grosbach4b905842013-09-20 23:08:21 +0000766/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000767/// primaryexpr ::= (parenexpr
768/// primaryexpr ::= symbol
769/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000770/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000771/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000772bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000773 SMLoc FirstTokenLoc = getLexer().getLoc();
774 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
775 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000776 default:
777 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000778 // If we have an error assume that we've already handled it.
779 case AsmToken::Error:
780 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000781 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000782 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000783 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000784 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000785 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000786 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000787 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000788 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000789 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000790 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000791 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000792 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000793 if (FirstTokenKind == AsmToken::Dollar) {
794 if (Lexer.getMAI().getDollarIsPC()) {
795 // This is a '$' reference, which references the current PC. Emit a
796 // temporary label to the streamer and refer to it.
797 MCSymbol *Sym = Ctx.CreateTempSymbol();
798 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000799 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
800 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000801 EndLoc = FirstTokenLoc;
802 return false;
803 } else
804 return Error(FirstTokenLoc, "invalid token in expression");
805 return true;
806 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000807 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000808 // Parse symbol variant
809 std::pair<StringRef, StringRef> Split;
810 if (!MAI.useParensForSymbolVariant()) {
811 Split = Identifier.split('@');
812 } else if (Lexer.is(AsmToken::LParen)) {
813 Lexer.Lex(); // eat (
814 StringRef VName;
815 parseIdentifier(VName);
816 if (Lexer.isNot(AsmToken::RParen)) {
817 return Error(Lexer.getTok().getLoc(),
818 "unexpected token in variant, expected ')'");
819 }
820 Lexer.Lex(); // eat )
821 Split = std::make_pair(Identifier, VName);
822 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000823
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000824 EndLoc = SMLoc::getFromPointer(Identifier.end());
825
Daniel Dunbard20cda02009-10-16 01:34:54 +0000826 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000827 StringRef SymbolName = Identifier;
828 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000829
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000830 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000831 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000832 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000833 if (Variant != MCSymbolRefExpr::VK_Invalid) {
834 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000835 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000836 Variant = MCSymbolRefExpr::VK_None;
837 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000838 Variant = MCSymbolRefExpr::VK_None;
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000839 return Error(SMLoc::getFromPointer(Split.second.begin()),
840 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000841 }
842 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000843
Hans Wennborgce69d772013-10-18 20:46:28 +0000844 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
845
Daniel Dunbard20cda02009-10-16 01:34:54 +0000846 // If this is an absolute variable reference, substitute it now to preserve
847 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000848 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000849 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000850 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000851
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000852 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000853 return false;
854 }
855
856 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000857 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000858 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000859 }
Kevin Enderby0510b482010-05-17 23:08:19 +0000860 case AsmToken::Integer: {
861 SMLoc Loc = getTok().getLoc();
862 int64_t IntVal = getTok().getIntVal();
863 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000864 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000865 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000866 // Look for 'b' or 'f' following an Integer as a directional label
867 if (Lexer.getKind() == AsmToken::Identifier) {
868 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000869 // Lookup the symbol variant if used.
870 std::pair<StringRef, StringRef> Split = IDVal.split('@');
871 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
872 if (Split.first.size() != IDVal.size()) {
873 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
874 if (Variant == MCSymbolRefExpr::VK_Invalid) {
875 Variant = MCSymbolRefExpr::VK_None;
876 return TokError("invalid variant '" + Split.second + "'");
877 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000878 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000879 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000880 if (IDVal == "f" || IDVal == "b") {
881 MCSymbol *Sym =
882 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000883 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000884 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000885 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000886 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000887 Lex(); // Eat identifier.
888 }
889 }
Chris Lattner78db3622009-06-22 05:51:26 +0000890 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000891 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000892 case AsmToken::Real: {
893 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000894 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000895 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000896 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000897 Lex(); // Eat token.
898 return false;
899 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000900 case AsmToken::Dot: {
901 // This is a '.' reference, which references the current PC. Emit a
902 // temporary label to the streamer and refer to it.
903 MCSymbol *Sym = Ctx.CreateTempSymbol();
904 Out.EmitLabel(Sym);
905 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000906 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000907 Lex(); // Eat identifier.
908 return false;
909 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000910 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000911 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000912 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000913 case AsmToken::LBrac:
914 if (!PlatformParser->HasBracketExpressions())
915 return TokError("brackets expression not supported on this target");
916 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000917 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000918 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000919 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000920 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000921 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000922 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000923 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000924 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000925 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000927 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000928 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000930 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000931 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000934 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000936 }
937}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000938
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000939bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000940 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000941 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000942}
943
Daniel Dunbar55f16672010-09-17 02:47:07 +0000944const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000945AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000946 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000947 // Ask the target implementation about this expression first.
948 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
949 if (NewE)
950 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000951 // Recurse over the given expression, rebuilding it to apply the given variant
952 // if there is exactly one symbol.
953 switch (E->getKind()) {
954 case MCExpr::Target:
955 case MCExpr::Constant:
956 return 0;
957
958 case MCExpr::SymbolRef: {
959 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
960
961 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000962 TokError("invalid variant on expression '" + getTok().getIdentifier() +
963 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000964 return E;
965 }
966
967 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
968 }
969
970 case MCExpr::Unary: {
971 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000972 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000973 if (!Sub)
974 return 0;
975 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
976 }
977
978 case MCExpr::Binary: {
979 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000980 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
981 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000982
983 if (!LHS && !RHS)
984 return 0;
985
Jim Grosbach4b905842013-09-20 23:08:21 +0000986 if (!LHS)
987 LHS = BE->getLHS();
988 if (!RHS)
989 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990
991 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
992 }
993 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000994
Craig Toppera2886c22012-02-07 05:05:23 +0000995 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996}
997
Jim Grosbach4b905842013-09-20 23:08:21 +0000998/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000999///
Jim Grosbachbd164242011-08-20 16:24:13 +00001000/// expr ::= expr &&,|| expr -> lowest.
1001/// expr ::= expr |,^,&,! expr
1002/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1003/// expr ::= expr <<,>> expr
1004/// expr ::= expr +,- expr
1005/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001006/// expr ::= primaryexpr
1007///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001008bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001009 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001010 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001011 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001012 return true;
1013
Daniel Dunbar55f16672010-09-17 02:47:07 +00001014 // As a special case, we support 'a op b @ modifier' by rewriting the
1015 // expression to include the modifier. This is inefficient, but in general we
1016 // expect users to use 'a@modifier op b'.
1017 if (Lexer.getKind() == AsmToken::At) {
1018 Lex();
1019
1020 if (Lexer.isNot(AsmToken::Identifier))
1021 return TokError("unexpected symbol modifier following '@'");
1022
1023 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001024 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 if (Variant == MCSymbolRefExpr::VK_Invalid)
1026 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1027
Jim Grosbach4b905842013-09-20 23:08:21 +00001028 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001029 if (!ModifiedRes) {
1030 return TokError("invalid modifier '" + getTok().getIdentifier() +
1031 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001032 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001033
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034 Res = ModifiedRes;
1035 Lex();
1036 }
1037
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001038 // Try to constant fold it up front, if possible.
1039 int64_t Value;
1040 if (Res->EvaluateAsAbsolute(Value))
1041 Res = MCConstantExpr::Create(Value, getContext());
1042
1043 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001044}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001045
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001046bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001047 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001048 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001049}
1050
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001051bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001052 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001053
Daniel Dunbar75630b32009-06-30 02:10:03 +00001054 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001055 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001056 return true;
1057
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001058 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001059 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001060
1061 return false;
1062}
1063
Michael J. Spencer530ce852010-10-09 11:00:50 +00001064static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001065 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001066 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001067 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001068 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001069
Jim Grosbach4b905842013-09-20 23:08:21 +00001070 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001071 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001072 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001073 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001074 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001075 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001076 return 1;
1077
Jim Grosbach4b905842013-09-20 23:08:21 +00001078 // Low Precedence: |, &, ^
1079 //
1080 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001081 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001082 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001083 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001084 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001085 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001086 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001090
Jim Grosbach4b905842013-09-20 23:08:21 +00001091 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 case AsmToken::EqualEqual:
1093 Kind = MCBinaryExpr::EQ;
1094 return 3;
1095 case AsmToken::ExclaimEqual:
1096 case AsmToken::LessGreater:
1097 Kind = MCBinaryExpr::NE;
1098 return 3;
1099 case AsmToken::Less:
1100 Kind = MCBinaryExpr::LT;
1101 return 3;
1102 case AsmToken::LessEqual:
1103 Kind = MCBinaryExpr::LTE;
1104 return 3;
1105 case AsmToken::Greater:
1106 Kind = MCBinaryExpr::GT;
1107 return 3;
1108 case AsmToken::GreaterEqual:
1109 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001110 return 3;
1111
Jim Grosbach4b905842013-09-20 23:08:21 +00001112 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001113 case AsmToken::LessLess:
1114 Kind = MCBinaryExpr::Shl;
1115 return 4;
1116 case AsmToken::GreaterGreater:
1117 Kind = MCBinaryExpr::Shr;
1118 return 4;
1119
Jim Grosbach4b905842013-09-20 23:08:21 +00001120 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001121 case AsmToken::Plus:
1122 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001123 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001124 case AsmToken::Minus:
1125 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001126 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001127
Jim Grosbach4b905842013-09-20 23:08:21 +00001128 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001129 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001130 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001131 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001132 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001133 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001135 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001136 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001138 }
1139}
1140
Jim Grosbach4b905842013-09-20 23:08:21 +00001141/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001142/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001143bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001144 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001145 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001146 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001147 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001148
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149 // If the next token is lower precedence than we are allowed to eat, return
1150 // successfully with what we ate already.
1151 if (TokPrec < Precedence)
1152 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001153
Sean Callanan686ed8d2010-01-19 20:22:31 +00001154 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001155
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001156 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001157 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001158 if (parsePrimaryExpr(RHS, EndLoc))
1159 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001160
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001161 // If BinOp binds less tightly with RHS than the operator after RHS, let
1162 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001163 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001164 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001165 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1166 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001168 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001169 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001170 }
1171}
1172
Chris Lattner36e02122009-06-21 20:54:55 +00001173/// ParseStatement:
1174/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001175/// ::= Label* Directive ...Operands... EndOfStatement
1176/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001177bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001178 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001179 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001180 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001181 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001182 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001183
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001184 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001185 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001186 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001187 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001188 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001189 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001190 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001191 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001192
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001193 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001194 if (Lexer.is(AsmToken::Integer)) {
1195 LocalLabelVal = getTok().getIntVal();
1196 if (LocalLabelVal < 0) {
1197 if (!TheCondState.Ignore)
1198 return TokError("unexpected token at start of statement");
1199 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001200 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001201 IDVal = getTok().getString();
1202 Lex(); // Consume the integer token to be used as an identifier token.
1203 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001204 if (!TheCondState.Ignore)
1205 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001206 }
1207 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001208 } else if (Lexer.is(AsmToken::Dot)) {
1209 // Treat '.' as a valid identifier in this context.
1210 Lex();
1211 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001212 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001213 if (!TheCondState.Ignore)
1214 return TokError("unexpected token at start of statement");
1215 IDVal = "";
1216 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001217
Chris Lattner926885c2010-04-17 18:14:27 +00001218 // Handle conditional assembly here before checking for skipping. We
1219 // have to do this so that .endif isn't skipped in a ".if 0" block for
1220 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001221 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001222 DirectiveKindMap.find(IDVal);
1223 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1224 ? DK_NO_DIRECTIVE
1225 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001226 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001227 default:
1228 break;
1229 case DK_IF:
1230 return parseDirectiveIf(IDLoc);
1231 case DK_IFB:
1232 return parseDirectiveIfb(IDLoc, true);
1233 case DK_IFNB:
1234 return parseDirectiveIfb(IDLoc, false);
1235 case DK_IFC:
1236 return parseDirectiveIfc(IDLoc, true);
1237 case DK_IFNC:
1238 return parseDirectiveIfc(IDLoc, false);
1239 case DK_IFDEF:
1240 return parseDirectiveIfdef(IDLoc, true);
1241 case DK_IFNDEF:
1242 case DK_IFNOTDEF:
1243 return parseDirectiveIfdef(IDLoc, false);
1244 case DK_ELSEIF:
1245 return parseDirectiveElseIf(IDLoc);
1246 case DK_ELSE:
1247 return parseDirectiveElse(IDLoc);
1248 case DK_ENDIF:
1249 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001250 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001251
Eli Bendersky88024712013-01-16 19:32:36 +00001252 // Ignore the statement if in the middle of inactive conditional
1253 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001254 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001255 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001256 return false;
1257 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001258
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001259 // FIXME: Recurse on local labels?
1260
1261 // See what kind of statement we have.
1262 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001263 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001264 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001265
Chris Lattner36e02122009-06-21 20:54:55 +00001266 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001267 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001268
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001269 // Diagnose attempt to use '.' as a label.
1270 if (IDVal == ".")
1271 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1272
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001273 // Diagnose attempt to use a variable as a label.
1274 //
1275 // FIXME: Diagnostics. Note the location of the definition as a label.
1276 // FIXME: This doesn't diagnose assignment to a symbol which has been
1277 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001278 MCSymbol *Sym;
1279 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001280 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001281 else
1282 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001283 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001284 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001285
Daniel Dunbare73b2672009-08-26 22:13:22 +00001286 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001287 if (!ParsingInlineAsm)
1288 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001289
Kevin Enderbye7739d42011-12-09 18:09:40 +00001290 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001291 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001292 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001293 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1294 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001295
Tim Northover1744d0a2013-10-25 12:49:50 +00001296 getTargetParser().onLabelParsed(Sym);
1297
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001298 // Consume any end of statement token, if present, to avoid spurious
1299 // AddBlankLine calls().
1300 if (Lexer.is(AsmToken::EndOfStatement)) {
1301 Lex();
1302 if (Lexer.is(AsmToken::Eof))
1303 return false;
1304 }
1305
Eli Friedman0f4871d2012-10-22 23:58:19 +00001306 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001307 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001308
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001309 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001310 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001311 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001312
Jim Grosbach4b905842013-09-20 23:08:21 +00001313 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001314
1315 default: // Normal instruction or directive.
1316 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001317 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001318
1319 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001320 if (areMacrosEnabled())
1321 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1322 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001323 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001324
Michael J. Spencer530ce852010-10-09 11:00:50 +00001325 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001326
Eli Bendersky17233942013-01-15 22:59:42 +00001327 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001328 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001329 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001330 //
Eli Bendersky17233942013-01-15 22:59:42 +00001331 // 1. The target-specific assembly parser. Some directives are target
1332 // specific or may potentially behave differently on certain targets.
1333 // 2. Asm parser extensions. For example, platform-specific parsers
1334 // (like the ELF parser) register themselves as extensions.
1335 // 3. The generic directive parser implemented by this class. These are
1336 // all the directives that behave in a target and platform independent
1337 // manner, or at least have a default behavior that's shared between
1338 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001339
Eli Bendersky17233942013-01-15 22:59:42 +00001340 // First query the target-specific parser. It will return 'true' if it
1341 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001342 if (!getTargetParser().ParseDirective(ID))
1343 return false;
1344
Alp Tokercb402912014-01-24 17:20:08 +00001345 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001346 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001347 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1348 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001349 if (Handler.first)
1350 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1351
1352 // Finally, if no one else is interested in this directive, it must be
1353 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001354 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001355 default:
1356 break;
1357 case DK_SET:
1358 case DK_EQU:
1359 return parseDirectiveSet(IDVal, true);
1360 case DK_EQUIV:
1361 return parseDirectiveSet(IDVal, false);
1362 case DK_ASCII:
1363 return parseDirectiveAscii(IDVal, false);
1364 case DK_ASCIZ:
1365 case DK_STRING:
1366 return parseDirectiveAscii(IDVal, true);
1367 case DK_BYTE:
1368 return parseDirectiveValue(1);
1369 case DK_SHORT:
1370 case DK_VALUE:
1371 case DK_2BYTE:
1372 return parseDirectiveValue(2);
1373 case DK_LONG:
1374 case DK_INT:
1375 case DK_4BYTE:
1376 return parseDirectiveValue(4);
1377 case DK_QUAD:
1378 case DK_8BYTE:
1379 return parseDirectiveValue(8);
1380 case DK_SINGLE:
1381 case DK_FLOAT:
1382 return parseDirectiveRealValue(APFloat::IEEEsingle);
1383 case DK_DOUBLE:
1384 return parseDirectiveRealValue(APFloat::IEEEdouble);
1385 case DK_ALIGN: {
1386 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1387 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1388 }
1389 case DK_ALIGN32: {
1390 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1391 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1392 }
1393 case DK_BALIGN:
1394 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1395 case DK_BALIGNW:
1396 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1397 case DK_BALIGNL:
1398 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1399 case DK_P2ALIGN:
1400 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1401 case DK_P2ALIGNW:
1402 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1403 case DK_P2ALIGNL:
1404 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1405 case DK_ORG:
1406 return parseDirectiveOrg();
1407 case DK_FILL:
1408 return parseDirectiveFill();
1409 case DK_ZERO:
1410 return parseDirectiveZero();
1411 case DK_EXTERN:
1412 eatToEndOfStatement(); // .extern is the default, ignore it.
1413 return false;
1414 case DK_GLOBL:
1415 case DK_GLOBAL:
1416 return parseDirectiveSymbolAttribute(MCSA_Global);
1417 case DK_LAZY_REFERENCE:
1418 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1419 case DK_NO_DEAD_STRIP:
1420 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1421 case DK_SYMBOL_RESOLVER:
1422 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1423 case DK_PRIVATE_EXTERN:
1424 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1425 case DK_REFERENCE:
1426 return parseDirectiveSymbolAttribute(MCSA_Reference);
1427 case DK_WEAK_DEFINITION:
1428 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1429 case DK_WEAK_REFERENCE:
1430 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1431 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1432 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1433 case DK_COMM:
1434 case DK_COMMON:
1435 return parseDirectiveComm(/*IsLocal=*/false);
1436 case DK_LCOMM:
1437 return parseDirectiveComm(/*IsLocal=*/true);
1438 case DK_ABORT:
1439 return parseDirectiveAbort();
1440 case DK_INCLUDE:
1441 return parseDirectiveInclude();
1442 case DK_INCBIN:
1443 return parseDirectiveIncbin();
1444 case DK_CODE16:
1445 case DK_CODE16GCC:
1446 return TokError(Twine(IDVal) + " not supported yet");
1447 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001448 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001449 case DK_IRP:
1450 return parseDirectiveIrp(IDLoc);
1451 case DK_IRPC:
1452 return parseDirectiveIrpc(IDLoc);
1453 case DK_ENDR:
1454 return parseDirectiveEndr(IDLoc);
1455 case DK_BUNDLE_ALIGN_MODE:
1456 return parseDirectiveBundleAlignMode();
1457 case DK_BUNDLE_LOCK:
1458 return parseDirectiveBundleLock();
1459 case DK_BUNDLE_UNLOCK:
1460 return parseDirectiveBundleUnlock();
1461 case DK_SLEB128:
1462 return parseDirectiveLEB128(true);
1463 case DK_ULEB128:
1464 return parseDirectiveLEB128(false);
1465 case DK_SPACE:
1466 case DK_SKIP:
1467 return parseDirectiveSpace(IDVal);
1468 case DK_FILE:
1469 return parseDirectiveFile(IDLoc);
1470 case DK_LINE:
1471 return parseDirectiveLine();
1472 case DK_LOC:
1473 return parseDirectiveLoc();
1474 case DK_STABS:
1475 return parseDirectiveStabs();
1476 case DK_CFI_SECTIONS:
1477 return parseDirectiveCFISections();
1478 case DK_CFI_STARTPROC:
1479 return parseDirectiveCFIStartProc();
1480 case DK_CFI_ENDPROC:
1481 return parseDirectiveCFIEndProc();
1482 case DK_CFI_DEF_CFA:
1483 return parseDirectiveCFIDefCfa(IDLoc);
1484 case DK_CFI_DEF_CFA_OFFSET:
1485 return parseDirectiveCFIDefCfaOffset();
1486 case DK_CFI_ADJUST_CFA_OFFSET:
1487 return parseDirectiveCFIAdjustCfaOffset();
1488 case DK_CFI_DEF_CFA_REGISTER:
1489 return parseDirectiveCFIDefCfaRegister(IDLoc);
1490 case DK_CFI_OFFSET:
1491 return parseDirectiveCFIOffset(IDLoc);
1492 case DK_CFI_REL_OFFSET:
1493 return parseDirectiveCFIRelOffset(IDLoc);
1494 case DK_CFI_PERSONALITY:
1495 return parseDirectiveCFIPersonalityOrLsda(true);
1496 case DK_CFI_LSDA:
1497 return parseDirectiveCFIPersonalityOrLsda(false);
1498 case DK_CFI_REMEMBER_STATE:
1499 return parseDirectiveCFIRememberState();
1500 case DK_CFI_RESTORE_STATE:
1501 return parseDirectiveCFIRestoreState();
1502 case DK_CFI_SAME_VALUE:
1503 return parseDirectiveCFISameValue(IDLoc);
1504 case DK_CFI_RESTORE:
1505 return parseDirectiveCFIRestore(IDLoc);
1506 case DK_CFI_ESCAPE:
1507 return parseDirectiveCFIEscape();
1508 case DK_CFI_SIGNAL_FRAME:
1509 return parseDirectiveCFISignalFrame();
1510 case DK_CFI_UNDEFINED:
1511 return parseDirectiveCFIUndefined(IDLoc);
1512 case DK_CFI_REGISTER:
1513 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001514 case DK_CFI_WINDOW_SAVE:
1515 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001516 case DK_MACROS_ON:
1517 case DK_MACROS_OFF:
1518 return parseDirectiveMacrosOnOff(IDVal);
1519 case DK_MACRO:
1520 return parseDirectiveMacro(IDLoc);
1521 case DK_ENDM:
1522 case DK_ENDMACRO:
1523 return parseDirectiveEndMacro(IDVal);
1524 case DK_PURGEM:
1525 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001526 case DK_END:
1527 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001528 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001529
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001530 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001531 }
Chris Lattner36e02122009-06-21 20:54:55 +00001532
Chad Rosierc7f552c2013-02-12 21:33:51 +00001533 // __asm _emit or __asm __emit
1534 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1535 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001536 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001537
1538 // __asm align
1539 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001540 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001541
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001542 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001543
Chris Lattner7cbfa442010-05-19 23:34:33 +00001544 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001545 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001546 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001547 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001548 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001549 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001550
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001551 // Dump the parsed representation, if requested.
1552 if (getShowParsedOperands()) {
1553 SmallString<256> Str;
1554 raw_svector_ostream OS(Str);
1555 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001556 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001557 if (i != 0)
1558 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001559 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001560 }
1561 OS << "]";
1562
Jim Grosbach4b905842013-09-20 23:08:21 +00001563 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001564 }
1565
Kevin Enderby6469fc22011-11-01 22:27:22 +00001566 // If we are generating dwarf for assembly source files and the current
1567 // section is the initial text section then generate a .loc directive for
1568 // the instruction.
1569 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001570 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001571 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001572
Eli Bendersky88024712013-01-16 19:32:36 +00001573 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001574
Eli Bendersky88024712013-01-16 19:32:36 +00001575 // If we previously parsed a cpp hash file line comment then make sure the
1576 // current Dwarf File is for the CppHashFilename if not then emit the
1577 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001578 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001579 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001580 if (CppHashFilename.size() != 0) {
1581 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001582 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001583 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001584 getContext().nextGenDwarfFileNumber(), StringRef(),
1585 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001586
Jim Grosbach4b905842013-09-20 23:08:21 +00001587 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1588 // cache with the different Loc from the call above we save the last
1589 // info we queried here with SrcMgr.FindLineNumber().
1590 unsigned CppHashLocLineNo;
1591 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1592 CppHashLocLineNo = LastQueryLine;
1593 else {
1594 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1595 LastQueryLine = CppHashLocLineNo;
1596 LastQueryIDLoc = CppHashLoc;
1597 LastQueryBuffer = CppHashBuf;
1598 }
1599 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001600 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001601
Jim Grosbach4b905842013-09-20 23:08:21 +00001602 getStreamer().EmitDwarfLocDirective(
1603 getContext().getGenDwarfFileNumber(), Line, 0,
1604 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1605 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001606 }
1607
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001608 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001609 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001610 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001611 HadError = getTargetParser().MatchAndEmitInstruction(
1612 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1613 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001614 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001615
Chris Lattnera2a9d162010-09-11 16:18:25 +00001616 // Don't skip the rest of the line, the instruction parser is responsible for
1617 // that.
1618 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001619}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001620
Jim Grosbach4b905842013-09-20 23:08:21 +00001621/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001622/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001623void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001624 if (!Lexer.is(AsmToken::EndOfStatement))
1625 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001626 // Eat EOL.
1627 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001628}
1629
Jim Grosbach4b905842013-09-20 23:08:21 +00001630/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001631/// ::= # number "filename"
1632/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001633bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001634 Lex(); // Eat the hash token.
1635
1636 if (getLexer().isNot(AsmToken::Integer)) {
1637 // Consume the line since in cases it is not a well-formed line directive,
1638 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001639 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001640 return false;
1641 }
1642
1643 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001644 Lex();
1645
1646 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001647 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001648 return false;
1649 }
1650
1651 StringRef Filename = getTok().getString();
1652 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001653 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001654
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001655 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1656 CppHashLoc = L;
1657 CppHashFilename = Filename;
1658 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001659 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001660
1661 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001662 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001663 return false;
1664}
1665
Jim Grosbach4b905842013-09-20 23:08:21 +00001666/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001667/// for the Filename and LineNo if any in the diagnostic.
1668void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001669 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001670 raw_ostream &OS = errs();
1671
1672 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1673 const SMLoc &DiagLoc = Diag.getLoc();
1674 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1675 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1676
Jim Grosbach4b905842013-09-20 23:08:21 +00001677 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001678 // before printing the message.
1679 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001680 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001681 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1682 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001683 }
1684
Eric Christophera7c32732012-12-18 00:30:54 +00001685 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001686 // manager changed or buffer changed (like in a nested include) then just
1687 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001688 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001689 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001690 if (Parser->SavedDiagHandler)
1691 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1692 else
1693 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001694 return;
1695 }
1696
Eric Christophera7c32732012-12-18 00:30:54 +00001697 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001698 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1699 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001700 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001701
1702 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1703 int CppHashLocLineNo =
1704 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001705 int LineNo =
1706 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001707
Jim Grosbach4b905842013-09-20 23:08:21 +00001708 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1709 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001710 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001711
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001712 if (Parser->SavedDiagHandler)
1713 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1714 else
1715 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001716}
1717
Rafael Espindola2c064482012-08-21 18:29:30 +00001718// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1719// difference being that that function accepts '@' as part of identifiers and
1720// we can't do that. AsmLexer.cpp should probably be changed to handle
1721// '@' as a special case when needed.
1722static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001723 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1724 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001725}
1726
Rafael Espindola34b9c512012-06-03 23:57:14 +00001727bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +00001728 const MCAsmMacroParameters &Parameters,
Jim Grosbach4b905842013-09-20 23:08:21 +00001729 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001730 unsigned NParameters = Parameters.size();
1731 if (NParameters != 0 && NParameters != A.size())
1732 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001733
Preston Gurd05500642012-09-19 20:36:12 +00001734 // A macro without parameters is handled differently on Darwin:
1735 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001736 while (!Body.empty()) {
1737 // Scan for the next substitution.
1738 std::size_t End = Body.size(), Pos = 0;
1739 for (; Pos != End; ++Pos) {
1740 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001741 if (!NParameters) {
1742 // This macro has no parameters, look for $0, $1, etc.
1743 if (Body[Pos] != '$' || Pos + 1 == End)
1744 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001745
Rafael Espindola1134ab232011-06-05 02:43:45 +00001746 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001747 if (Next == '$' || Next == 'n' ||
1748 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001749 break;
1750 } else {
1751 // This macro has parameters, look for \foo, \bar, etc.
1752 if (Body[Pos] == '\\' && Pos + 1 != End)
1753 break;
1754 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001755 }
1756
1757 // Add the prefix.
1758 OS << Body.slice(0, Pos);
1759
1760 // Check if we reached the end.
1761 if (Pos == End)
1762 break;
1763
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001765 switch (Body[Pos + 1]) {
1766 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001767 case '$':
1768 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001769 break;
1770
Jim Grosbach4b905842013-09-20 23:08:21 +00001771 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001772 case 'n':
1773 OS << A.size();
1774 break;
1775
Jim Grosbach4b905842013-09-20 23:08:21 +00001776 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 default: {
1778 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001779 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001780 if (Index >= A.size())
1781 break;
1782
1783 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001784 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001785 ie = A[Index].end();
1786 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001787 OS << it->getString();
1788 break;
1789 }
1790 }
1791 Pos += 2;
1792 } else {
1793 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001794 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001795 ++I;
1796
Jim Grosbach4b905842013-09-20 23:08:21 +00001797 const char *Begin = Body.data() + Pos + 1;
1798 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001799 unsigned Index = 0;
1800 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001801 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802 break;
1803
Preston Gurd05500642012-09-19 20:36:12 +00001804 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001805 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1806 Pos += 3;
1807 else {
1808 OS << '\\' << Argument;
1809 Pos = I;
1810 }
Preston Gurd05500642012-09-19 20:36:12 +00001811 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001812 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001813 ie = A[Index].end();
1814 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001815 if (it->getKind() == AsmToken::String)
1816 OS << it->getStringContents();
1817 else
1818 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001819
Preston Gurd05500642012-09-19 20:36:12 +00001820 Pos += 1 + Argument.size();
1821 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001822 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001823 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001824 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001825 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001826
Rafael Espindola1134ab232011-06-05 02:43:45 +00001827 return false;
1828}
Daniel Dunbar43235712010-07-18 18:54:11 +00001829
Jim Grosbach4b905842013-09-20 23:08:21 +00001830MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1831 SMLoc EL, MemoryBuffer *I)
1832 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1833 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001834
Jim Grosbach4b905842013-09-20 23:08:21 +00001835static bool isOperator(AsmToken::TokenKind kind) {
1836 switch (kind) {
1837 default:
1838 return false;
1839 case AsmToken::Plus:
1840 case AsmToken::Minus:
1841 case AsmToken::Tilde:
1842 case AsmToken::Slash:
1843 case AsmToken::Star:
1844 case AsmToken::Dot:
1845 case AsmToken::Equal:
1846 case AsmToken::EqualEqual:
1847 case AsmToken::Pipe:
1848 case AsmToken::PipePipe:
1849 case AsmToken::Caret:
1850 case AsmToken::Amp:
1851 case AsmToken::AmpAmp:
1852 case AsmToken::Exclaim:
1853 case AsmToken::ExclaimEqual:
1854 case AsmToken::Percent:
1855 case AsmToken::Less:
1856 case AsmToken::LessEqual:
1857 case AsmToken::LessLess:
1858 case AsmToken::LessGreater:
1859 case AsmToken::Greater:
1860 case AsmToken::GreaterEqual:
1861 case AsmToken::GreaterGreater:
1862 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001863 }
1864}
1865
Jim Grosbach4b905842013-09-20 23:08:21 +00001866bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd05500642012-09-19 20:36:12 +00001867 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001868 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001869 unsigned AddTokens = 0;
1870
1871 // gas accepts arguments separated by whitespace, except on Darwin
1872 if (!IsDarwin)
1873 Lexer.setSkipSpace(false);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001874
1875 for (;;) {
Preston Gurd05500642012-09-19 20:36:12 +00001876 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1877 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001878 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001879 }
1880
1881 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1882 // Spaces and commas cannot be mixed to delimit parameters
1883 if (ArgumentDelimiter == AsmToken::Eof)
1884 ArgumentDelimiter = AsmToken::Comma;
1885 else if (ArgumentDelimiter != AsmToken::Comma) {
1886 Lexer.setSkipSpace(true);
1887 return TokError("expected ' ' for macro argument separator");
1888 }
1889 break;
1890 }
1891
1892 if (Lexer.is(AsmToken::Space)) {
1893 Lex(); // Eat spaces
1894
1895 // Spaces can delimit parameters, but could also be part an expression.
1896 // If the token after a space is an operator, add the token and the next
1897 // one into this argument
1898 if (ArgumentDelimiter == AsmToken::Space ||
1899 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001900 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001901 // Check to see whether the token is used as an operator,
1902 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001903 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001904 if (*NextChar == ' ')
1905 AddTokens = 2;
1906 }
1907
1908 if (!AddTokens && ParenLevel == 0) {
1909 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4b905842013-09-20 23:08:21 +00001910 !isOperator(Lexer.getKind()))
Preston Gurd05500642012-09-19 20:36:12 +00001911 ArgumentDelimiter = AsmToken::Space;
1912 break;
1913 }
1914 }
1915 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001916
Jim Grosbach4b905842013-09-20 23:08:21 +00001917 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001918 // to be able to fill in the remaining default parameter values
1919 if (Lexer.is(AsmToken::EndOfStatement))
1920 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001921
1922 // Adjust the current parentheses level.
1923 if (Lexer.is(AsmToken::LParen))
1924 ++ParenLevel;
1925 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1926 --ParenLevel;
1927
1928 // Append the token to the current argument list.
1929 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001930 if (AddTokens)
1931 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001932 Lex();
1933 }
Preston Gurd05500642012-09-19 20:36:12 +00001934
1935 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001936 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001937 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001938 return false;
1939}
1940
1941// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001942bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001943 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001944 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd05500642012-09-19 20:36:12 +00001945 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00001946 // parseMacroArgument()
Preston Gurd05500642012-09-19 20:36:12 +00001947 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001948
1949 // Parse two kinds of macro invocations:
1950 // - macros defined without any parameters accept an arbitrary number of them
1951 // - macros defined with parameters accept at most that many of them
1952 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1953 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001954 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001955
Jim Grosbach4b905842013-09-20 23:08:21 +00001956 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001957 return true;
1958
Preston Gurd242ed3152012-09-19 20:29:04 +00001959 if (!MA.empty() || !NParameters)
1960 A.push_back(MA);
1961 else if (NParameters) {
1962 if (!M->Parameters[Parameter].second.empty())
1963 A.push_back(M->Parameters[Parameter].second);
1964 }
Jim Grosbach206661622012-07-30 22:44:17 +00001965
Preston Gurd242ed3152012-09-19 20:29:04 +00001966 // At the end of the statement, fill in remaining arguments that have
1967 // default values. If there aren't any, then the next argument is
1968 // required but missing
1969 if (Lexer.is(AsmToken::EndOfStatement)) {
1970 if (NParameters && Parameter < NParameters - 1) {
1971 if (M->Parameters[Parameter + 1].second.empty())
1972 return TokError("macro argument '" +
1973 Twine(M->Parameters[Parameter + 1].first) +
1974 "' is missing");
1975 else
1976 continue;
1977 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001978 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001979 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001980
1981 if (Lexer.is(AsmToken::Comma))
1982 Lex();
1983 }
1984 return TokError("Too many arguments");
1985}
1986
Jim Grosbach4b905842013-09-20 23:08:21 +00001987const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1988 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001989 return (I == MacroMap.end()) ? NULL : I->getValue();
1990}
1991
Jim Grosbach4b905842013-09-20 23:08:21 +00001992void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001993 MacroMap[Name] = new MCAsmMacro(Macro);
1994}
1995
Jim Grosbach4b905842013-09-20 23:08:21 +00001996void AsmParser::undefineMacro(StringRef Name) {
1997 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001998 if (I != MacroMap.end()) {
1999 delete I->getValue();
2000 MacroMap.erase(I);
2001 }
2002}
2003
Jim Grosbach4b905842013-09-20 23:08:21 +00002004bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002005 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2006 // this, although we should protect against infinite loops.
2007 if (ActiveMacros.size() == 20)
2008 return TokError("macros cannot be nested more than 20 levels deep");
2009
Eli Bendersky38274122013-01-14 23:22:36 +00002010 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002011 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002012 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002013
Jim Grosbach206661622012-07-30 22:44:17 +00002014 // Remove any trailing empty arguments. Do this after-the-fact as we have
2015 // to keep empty arguments in the middle of the list or positionality
2016 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002017 while (!A.empty() && A.back().empty())
2018 A.pop_back();
Jim Grosbach206661622012-07-30 22:44:17 +00002019
Rafael Espindola1134ab232011-06-05 02:43:45 +00002020 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2021 // to hold the macro body with substitutions.
2022 SmallString<256> Buf;
2023 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002024 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002025
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002026 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002027 return true;
2028
Eli Bendersky38274122013-01-14 23:22:36 +00002029 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002030 // instantiation.
2031 OS << ".endmacro\n";
2032
Rafael Espindola1134ab232011-06-05 02:43:45 +00002033 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002034 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002035
Daniel Dunbar43235712010-07-18 18:54:11 +00002036 // Create the macro instantiation object and add to the current macro
2037 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002038 MacroInstantiation *MI = new MacroInstantiation(
2039 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002040 ActiveMacros.push_back(MI);
2041
2042 // Jump to the macro instantiation and prime the lexer.
2043 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2044 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2045 Lex();
2046
2047 return false;
2048}
2049
Jim Grosbach4b905842013-09-20 23:08:21 +00002050void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002051 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002052 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002053 Lex();
2054
2055 // Pop the instantiation entry.
2056 delete ActiveMacros.back();
2057 ActiveMacros.pop_back();
2058}
2059
Jim Grosbach4b905842013-09-20 23:08:21 +00002060static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002061 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002062 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002063 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2064 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002065 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002066 case MCExpr::Target:
2067 case MCExpr::Constant:
2068 return false;
2069 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002070 const MCSymbol &S =
2071 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002072 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002073 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002074 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002075 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002076 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002077 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002078 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002079
2080 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002081}
2082
Jim Grosbach4b905842013-09-20 23:08:21 +00002083bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002084 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002085 // FIXME: Use better location, we should use proper tokens.
2086 SMLoc EqualLoc = Lexer.getLoc();
2087
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002088 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002089 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002090 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002091
Rafael Espindola72f5f172012-01-28 05:57:00 +00002092 // Note: we don't count b as used in "a = b". This is to allow
2093 // a = b
2094 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002095
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002096 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002097 return TokError("unexpected token in assignment");
2098
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002099 // Error on assignment to '.'.
2100 if (Name == ".") {
2101 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2102 "(use '.space' or '.org').)"));
2103 }
2104
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002105 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002106 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002107
Daniel Dunbar5f339242009-10-16 01:57:39 +00002108 // Validate that the LHS is allowed to be a variable (either it has not been
2109 // used as a symbol, or it is an absolute symbol).
2110 MCSymbol *Sym = getContext().LookupSymbol(Name);
2111 if (Sym) {
2112 // Diagnose assignment to a label.
2113 //
2114 // FIXME: Diagnostics. Note the location of the definition as a label.
2115 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002116 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002117 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2118 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002119 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002120 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2121 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002122 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002123 return Error(EqualLoc, "redefinition of '" + Name + "'");
2124 else if (!Sym->isVariable())
2125 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002126 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002127 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002128 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002129
2130 // Don't count these checks as uses.
2131 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002132 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002133 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002134
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002135 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002136
2137 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002138 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002139 if (NoDeadStrip)
2140 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2141
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002142 return false;
2143}
2144
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002145/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002146/// ::= identifier
2147/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002148bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002149 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002150 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2151 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002152 // handle this as a context dependent token, instead we detect adjacent tokens
2153 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002154 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2155 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002156
Hans Wennborgce69d772013-10-18 20:46:28 +00002157 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002158 Lex();
2159 if (Lexer.isNot(AsmToken::Identifier))
2160 return true;
2161
Hans Wennborgce69d772013-10-18 20:46:28 +00002162 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2163 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002164 return true;
2165
2166 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002167 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002168 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002169 Lex();
2170 return false;
2171 }
2172
Jim Grosbach4b905842013-09-20 23:08:21 +00002173 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002174 return true;
2175
Sean Callanan936b0d32010-01-19 21:44:56 +00002176 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002177
Sean Callanan686ed8d2010-01-19 20:22:31 +00002178 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002179
2180 return false;
2181}
2182
Jim Grosbach4b905842013-09-20 23:08:21 +00002183/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002184/// ::= .equ identifier ',' expression
2185/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002186/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002187bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002188 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002189
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002190 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002191 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002192
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002193 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002194 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002195 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002196
Jim Grosbach4b905842013-09-20 23:08:21 +00002197 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002198}
2199
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002200bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002201 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002202
2203 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002204 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002205 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2206 if (Str[i] != '\\') {
2207 Data += Str[i];
2208 continue;
2209 }
2210
2211 // Recognize escaped characters. Note that this escape semantics currently
2212 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2213 ++i;
2214 if (i == e)
2215 return TokError("unexpected backslash at end of string");
2216
2217 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002218 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002219 // Consume up to three octal characters.
2220 unsigned Value = Str[i] - '0';
2221
Jim Grosbach4b905842013-09-20 23:08:21 +00002222 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002223 ++i;
2224 Value = Value * 8 + (Str[i] - '0');
2225
Jim Grosbach4b905842013-09-20 23:08:21 +00002226 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002227 ++i;
2228 Value = Value * 8 + (Str[i] - '0');
2229 }
2230 }
2231
2232 if (Value > 255)
2233 return TokError("invalid octal escape sequence (out of range)");
2234
Jim Grosbach4b905842013-09-20 23:08:21 +00002235 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002236 continue;
2237 }
2238
2239 // Otherwise recognize individual escapes.
2240 switch (Str[i]) {
2241 default:
2242 // Just reject invalid escape sequences for now.
2243 return TokError("invalid escape sequence (unrecognized character)");
2244
2245 case 'b': Data += '\b'; break;
2246 case 'f': Data += '\f'; break;
2247 case 'n': Data += '\n'; break;
2248 case 'r': Data += '\r'; break;
2249 case 't': Data += '\t'; break;
2250 case '"': Data += '"'; break;
2251 case '\\': Data += '\\'; break;
2252 }
2253 }
2254
2255 return false;
2256}
2257
Jim Grosbach4b905842013-09-20 23:08:21 +00002258/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002259/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002260bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002261 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002262 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002263
Daniel Dunbara10e5192009-06-24 23:30:00 +00002264 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002265 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002266 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002267
Daniel Dunbaref668c12009-08-14 18:19:52 +00002268 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002269 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002270 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002271
Rafael Espindola64e1af82013-07-02 15:49:13 +00002272 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002273 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002274 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002275
Sean Callanan686ed8d2010-01-19 20:22:31 +00002276 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002277
2278 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002279 break;
2280
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002281 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002282 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002283 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002284 }
2285 }
2286
Sean Callanan686ed8d2010-01-19 20:22:31 +00002287 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002288 return false;
2289}
2290
Jim Grosbach4b905842013-09-20 23:08:21 +00002291/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002292/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002293bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002294 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002295 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002296
Daniel Dunbara10e5192009-06-24 23:30:00 +00002297 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002298 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002299 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002300 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002301 return true;
2302
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002303 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002304 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2305 assert(Size <= 8 && "Invalid size");
2306 uint64_t IntValue = MCE->getValue();
2307 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2308 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002309 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002310 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002311 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002312
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002313 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002314 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002315
Daniel Dunbara10e5192009-06-24 23:30:00 +00002316 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002317 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002318 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002319 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002320 }
2321 }
2322
Sean Callanan686ed8d2010-01-19 20:22:31 +00002323 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002324 return false;
2325}
2326
Jim Grosbach4b905842013-09-20 23:08:21 +00002327/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002328/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002329bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002330 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002331 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002332
2333 for (;;) {
2334 // We don't truly support arithmetic on floating point expressions, so we
2335 // have to manually parse unary prefixes.
2336 bool IsNeg = false;
2337 if (getLexer().is(AsmToken::Minus)) {
2338 Lex();
2339 IsNeg = true;
2340 } else if (getLexer().is(AsmToken::Plus))
2341 Lex();
2342
Michael J. Spencer530ce852010-10-09 11:00:50 +00002343 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002344 getLexer().isNot(AsmToken::Real) &&
2345 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002346 return TokError("unexpected token in directive");
2347
2348 // Convert to an APFloat.
2349 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002350 StringRef IDVal = getTok().getString();
2351 if (getLexer().is(AsmToken::Identifier)) {
2352 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2353 Value = APFloat::getInf(Semantics);
2354 else if (!IDVal.compare_lower("nan"))
2355 Value = APFloat::getNaN(Semantics, false, ~0);
2356 else
2357 return TokError("invalid floating point literal");
2358 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002359 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002360 return TokError("invalid floating point literal");
2361 if (IsNeg)
2362 Value.changeSign();
2363
2364 // Consume the numeric token.
2365 Lex();
2366
2367 // Emit the value as an integer.
2368 APInt AsInt = Value.bitcastToAPInt();
2369 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002370 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002371
2372 if (getLexer().is(AsmToken::EndOfStatement))
2373 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002374
Daniel Dunbar2af16532010-09-24 01:59:56 +00002375 if (getLexer().isNot(AsmToken::Comma))
2376 return TokError("unexpected token in directive");
2377 Lex();
2378 }
2379 }
2380
2381 Lex();
2382 return false;
2383}
2384
Jim Grosbach4b905842013-09-20 23:08:21 +00002385/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002386/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002387bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002388 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002389
2390 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002391 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002392 return true;
2393
Rafael Espindolab91bac62010-10-05 19:42:57 +00002394 int64_t Val = 0;
2395 if (getLexer().is(AsmToken::Comma)) {
2396 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002397 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002398 return true;
2399 }
2400
Rafael Espindola922e3f42010-09-16 15:03:59 +00002401 if (getLexer().isNot(AsmToken::EndOfStatement))
2402 return TokError("unexpected token in '.zero' directive");
2403
2404 Lex();
2405
Rafael Espindola64e1af82013-07-02 15:49:13 +00002406 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002407
2408 return false;
2409}
2410
Jim Grosbach4b905842013-09-20 23:08:21 +00002411/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002412/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002413bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002414 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002415
Daniel Dunbara10e5192009-06-24 23:30:00 +00002416 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002417 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002418 return true;
2419
Roman Divackye33098f2013-09-24 17:44:41 +00002420 int64_t FillSize = 1;
2421 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002422
Roman Divackye33098f2013-09-24 17:44:41 +00002423 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2424 if (getLexer().isNot(AsmToken::Comma))
2425 return TokError("unexpected token in '.fill' directive");
2426 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002427
Roman Divackye33098f2013-09-24 17:44:41 +00002428 if (parseAbsoluteExpression(FillSize))
2429 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002430
Roman Divackye33098f2013-09-24 17:44:41 +00002431 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2432 if (getLexer().isNot(AsmToken::Comma))
2433 return TokError("unexpected token in '.fill' directive");
2434 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002435
Roman Divackye33098f2013-09-24 17:44:41 +00002436 if (parseAbsoluteExpression(FillExpr))
2437 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002438
Roman Divackye33098f2013-09-24 17:44:41 +00002439 if (getLexer().isNot(AsmToken::EndOfStatement))
2440 return TokError("unexpected token in '.fill' directive");
2441
2442 Lex();
2443 }
2444 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002445
Daniel Dunbar8e5edd82009-08-21 15:43:35 +00002446 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2447 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara10e5192009-06-24 23:30:00 +00002448
2449 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002450 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002451
2452 return false;
2453}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002454
Jim Grosbach4b905842013-09-20 23:08:21 +00002455/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002456/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002457bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002458 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002459
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002460 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002461 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002462 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002463 return true;
2464
2465 // Parse optional fill expression.
2466 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002467 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2468 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002469 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002470 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002471
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002472 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002473 return true;
2474
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002475 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002476 return TokError("unexpected token in '.org' directive");
2477 }
2478
Sean Callanan686ed8d2010-01-19 20:22:31 +00002479 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002480
Jim Grosbachb5912772012-01-27 00:37:08 +00002481 // Only limited forms of relocatable expressions are accepted here, it
2482 // has to be relative to the current section. The streamer will return
2483 // 'true' if the expression wasn't evaluatable.
2484 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2485 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002486
2487 return false;
2488}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002489
Jim Grosbach4b905842013-09-20 23:08:21 +00002490/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002491/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002492bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002493 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002494
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002495 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002496 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002497 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002498 return true;
2499
2500 SMLoc MaxBytesLoc;
2501 bool HasFillExpr = false;
2502 int64_t FillExpr = 0;
2503 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002504 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2505 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002506 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002507 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002508
2509 // The fill expression can be omitted while specifying a maximum number of
2510 // alignment bytes, e.g:
2511 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002512 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002513 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002514 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002515 return true;
2516 }
2517
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002518 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2519 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002520 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002521 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002522
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002523 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002524 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002525 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002526
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002527 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002528 return TokError("unexpected token in directive");
2529 }
2530 }
2531
Sean Callanan686ed8d2010-01-19 20:22:31 +00002532 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002533
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002534 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002535 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002536
2537 // Compute alignment in bytes.
2538 if (IsPow2) {
2539 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002540 if (Alignment >= 32) {
2541 Error(AlignmentLoc, "invalid alignment value");
2542 Alignment = 31;
2543 }
2544
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002545 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002546 } else {
2547 // Reject alignments that aren't a power of two, for gas compatibility.
2548 if (!isPowerOf2_64(Alignment))
2549 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002550 }
2551
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002552 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002553 if (MaxBytesLoc.isValid()) {
2554 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002555 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002556 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002557 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002558 }
2559
2560 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002561 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002562 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002563 MaxBytesToFill = 0;
2564 }
2565 }
2566
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002567 // Check whether we should use optimal code alignment for this .align
2568 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002569 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002570 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2571 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002572 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002573 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002574 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002575 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2576 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002577 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002578
2579 return false;
2580}
2581
Jim Grosbach4b905842013-09-20 23:08:21 +00002582/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002583/// ::= .file [number] filename
2584/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002585bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002586 // FIXME: I'm not sure what this is.
2587 int64_t FileNumber = -1;
2588 SMLoc FileNumberLoc = getLexer().getLoc();
2589 if (getLexer().is(AsmToken::Integer)) {
2590 FileNumber = getTok().getIntVal();
2591 Lex();
2592
2593 if (FileNumber < 1)
2594 return TokError("file number less than one");
2595 }
2596
2597 if (getLexer().isNot(AsmToken::String))
2598 return TokError("unexpected token in '.file' directive");
2599
2600 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002601 // Allow the strings to have escaped octal character sequence.
2602 std::string Path = getTok().getString();
2603 if (parseEscapedString(Path))
2604 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002605 Lex();
2606
2607 StringRef Directory;
2608 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002609 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002610 if (getLexer().is(AsmToken::String)) {
2611 if (FileNumber == -1)
2612 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002613 if (parseEscapedString(FilenameData))
2614 return true;
2615 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002616 Directory = Path;
2617 Lex();
2618 } else {
2619 Filename = Path;
2620 }
2621
2622 if (getLexer().isNot(AsmToken::EndOfStatement))
2623 return TokError("unexpected token in '.file' directive");
2624
2625 if (FileNumber == -1)
2626 getStreamer().EmitFileDirective(Filename);
2627 else {
2628 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002629 Error(DirectiveLoc,
2630 "input can't have .file dwarf directives when -g is "
2631 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002632
2633 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2634 Error(FileNumberLoc, "file number already allocated");
2635 }
2636
2637 return false;
2638}
2639
Jim Grosbach4b905842013-09-20 23:08:21 +00002640/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002641/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002642bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002643 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2644 if (getLexer().isNot(AsmToken::Integer))
2645 return TokError("unexpected token in '.line' directive");
2646
2647 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002648 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002649 Lex();
2650
2651 // FIXME: Do something with the .line.
2652 }
2653
2654 if (getLexer().isNot(AsmToken::EndOfStatement))
2655 return TokError("unexpected token in '.line' directive");
2656
2657 return false;
2658}
2659
Jim Grosbach4b905842013-09-20 23:08:21 +00002660/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002661/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2662/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2663/// The first number is a file number, must have been previously assigned with
2664/// a .file directive, the second number is the line number and optionally the
2665/// third number is a column position (zero if not specified). The remaining
2666/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002667bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002668 if (getLexer().isNot(AsmToken::Integer))
2669 return TokError("unexpected token in '.loc' directive");
2670 int64_t FileNumber = getTok().getIntVal();
2671 if (FileNumber < 1)
2672 return TokError("file number less than one in '.loc' directive");
2673 if (!getContext().isValidDwarfFileNumber(FileNumber))
2674 return TokError("unassigned file number in '.loc' directive");
2675 Lex();
2676
2677 int64_t LineNumber = 0;
2678 if (getLexer().is(AsmToken::Integer)) {
2679 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002680 if (LineNumber < 0)
2681 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002682 Lex();
2683 }
2684
2685 int64_t ColumnPos = 0;
2686 if (getLexer().is(AsmToken::Integer)) {
2687 ColumnPos = getTok().getIntVal();
2688 if (ColumnPos < 0)
2689 return TokError("column position less than zero in '.loc' directive");
2690 Lex();
2691 }
2692
2693 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2694 unsigned Isa = 0;
2695 int64_t Discriminator = 0;
2696 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2697 for (;;) {
2698 if (getLexer().is(AsmToken::EndOfStatement))
2699 break;
2700
2701 StringRef Name;
2702 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002703 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002704 return TokError("unexpected token in '.loc' directive");
2705
2706 if (Name == "basic_block")
2707 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2708 else if (Name == "prologue_end")
2709 Flags |= DWARF2_FLAG_PROLOGUE_END;
2710 else if (Name == "epilogue_begin")
2711 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2712 else if (Name == "is_stmt") {
2713 Loc = getTok().getLoc();
2714 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002715 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002716 return true;
2717 // The expression must be the constant 0 or 1.
2718 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2719 int Value = MCE->getValue();
2720 if (Value == 0)
2721 Flags &= ~DWARF2_FLAG_IS_STMT;
2722 else if (Value == 1)
2723 Flags |= DWARF2_FLAG_IS_STMT;
2724 else
2725 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002726 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002727 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2728 }
Craig Topperf15655b2013-04-22 04:22:40 +00002729 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002730 Loc = getTok().getLoc();
2731 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002732 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002733 return true;
2734 // The expression must be a constant greater or equal to 0.
2735 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2736 int Value = MCE->getValue();
2737 if (Value < 0)
2738 return Error(Loc, "isa number less than zero");
2739 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002740 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002741 return Error(Loc, "isa number not a constant value");
2742 }
Craig Topperf15655b2013-04-22 04:22:40 +00002743 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002744 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002745 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002746 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002747 return Error(Loc, "unknown sub-directive in '.loc' directive");
2748 }
2749
2750 if (getLexer().is(AsmToken::EndOfStatement))
2751 break;
2752 }
2753 }
2754
2755 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2756 Isa, Discriminator, StringRef());
2757
2758 return false;
2759}
2760
Jim Grosbach4b905842013-09-20 23:08:21 +00002761/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002762/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002763bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002764 return TokError("unsupported directive '.stabs'");
2765}
2766
Jim Grosbach4b905842013-09-20 23:08:21 +00002767/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002768/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002769bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002770 StringRef Name;
2771 bool EH = false;
2772 bool Debug = false;
2773
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002774 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002775 return TokError("Expected an identifier");
2776
2777 if (Name == ".eh_frame")
2778 EH = true;
2779 else if (Name == ".debug_frame")
2780 Debug = true;
2781
2782 if (getLexer().is(AsmToken::Comma)) {
2783 Lex();
2784
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002785 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002786 return TokError("Expected an identifier");
2787
2788 if (Name == ".eh_frame")
2789 EH = true;
2790 else if (Name == ".debug_frame")
2791 Debug = true;
2792 }
2793
2794 getStreamer().EmitCFISections(EH, Debug);
2795 return false;
2796}
2797
Jim Grosbach4b905842013-09-20 23:08:21 +00002798/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002799/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002800bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002801 StringRef Simple;
2802 if (getLexer().isNot(AsmToken::EndOfStatement))
2803 if (parseIdentifier(Simple) || Simple != "simple")
2804 return TokError("unexpected token in .cfi_startproc directive");
2805
2806 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002807 return false;
2808}
2809
Jim Grosbach4b905842013-09-20 23:08:21 +00002810/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002811/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002812bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002813 getStreamer().EmitCFIEndProc();
2814 return false;
2815}
2816
Jim Grosbach4b905842013-09-20 23:08:21 +00002817/// \brief parse register name or number.
2818bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002819 SMLoc DirectiveLoc) {
2820 unsigned RegNo;
2821
2822 if (getLexer().isNot(AsmToken::Integer)) {
2823 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2824 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002825 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002826 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002827 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002828
2829 return false;
2830}
2831
Jim Grosbach4b905842013-09-20 23:08:21 +00002832/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002833/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002834bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002835 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002836 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002837 return true;
2838
2839 if (getLexer().isNot(AsmToken::Comma))
2840 return TokError("unexpected token in directive");
2841 Lex();
2842
2843 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002844 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002845 return true;
2846
2847 getStreamer().EmitCFIDefCfa(Register, Offset);
2848 return false;
2849}
2850
Jim Grosbach4b905842013-09-20 23:08:21 +00002851/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002852/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002853bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002854 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002855 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002856 return true;
2857
2858 getStreamer().EmitCFIDefCfaOffset(Offset);
2859 return false;
2860}
2861
Jim Grosbach4b905842013-09-20 23:08:21 +00002862/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002863/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002864bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002865 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002866 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002867 return true;
2868
2869 if (getLexer().isNot(AsmToken::Comma))
2870 return TokError("unexpected token in directive");
2871 Lex();
2872
2873 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002874 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002875 return true;
2876
2877 getStreamer().EmitCFIRegister(Register1, Register2);
2878 return false;
2879}
2880
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002881/// parseDirectiveCFIWindowSave
2882/// ::= .cfi_window_save
2883bool AsmParser::parseDirectiveCFIWindowSave() {
2884 getStreamer().EmitCFIWindowSave();
2885 return false;
2886}
2887
Jim Grosbach4b905842013-09-20 23:08:21 +00002888/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002889/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002890bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002891 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002892 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002893 return true;
2894
2895 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2896 return false;
2897}
2898
Jim Grosbach4b905842013-09-20 23:08:21 +00002899/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002900/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002901bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002902 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002903 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002904 return true;
2905
2906 getStreamer().EmitCFIDefCfaRegister(Register);
2907 return false;
2908}
2909
Jim Grosbach4b905842013-09-20 23:08:21 +00002910/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002911/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002912bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002913 int64_t Register = 0;
2914 int64_t Offset = 0;
2915
Jim Grosbach4b905842013-09-20 23:08:21 +00002916 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002917 return true;
2918
2919 if (getLexer().isNot(AsmToken::Comma))
2920 return TokError("unexpected token in directive");
2921 Lex();
2922
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002923 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002924 return true;
2925
2926 getStreamer().EmitCFIOffset(Register, Offset);
2927 return false;
2928}
2929
Jim Grosbach4b905842013-09-20 23:08:21 +00002930/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002931/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002932bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002933 int64_t Register = 0;
2934
Jim Grosbach4b905842013-09-20 23:08:21 +00002935 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002936 return true;
2937
2938 if (getLexer().isNot(AsmToken::Comma))
2939 return TokError("unexpected token in directive");
2940 Lex();
2941
2942 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002943 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002944 return true;
2945
2946 getStreamer().EmitCFIRelOffset(Register, Offset);
2947 return false;
2948}
2949
2950static bool isValidEncoding(int64_t Encoding) {
2951 if (Encoding & ~0xff)
2952 return false;
2953
2954 if (Encoding == dwarf::DW_EH_PE_omit)
2955 return true;
2956
2957 const unsigned Format = Encoding & 0xf;
2958 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2959 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2960 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2961 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2962 return false;
2963
2964 const unsigned Application = Encoding & 0x70;
2965 if (Application != dwarf::DW_EH_PE_absptr &&
2966 Application != dwarf::DW_EH_PE_pcrel)
2967 return false;
2968
2969 return true;
2970}
2971
Jim Grosbach4b905842013-09-20 23:08:21 +00002972/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00002973/// IsPersonality true for cfi_personality, false for cfi_lsda
2974/// ::= .cfi_personality encoding, [symbol_name]
2975/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00002976bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00002977 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002978 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00002979 return true;
2980 if (Encoding == dwarf::DW_EH_PE_omit)
2981 return false;
2982
2983 if (!isValidEncoding(Encoding))
2984 return TokError("unsupported encoding.");
2985
2986 if (getLexer().isNot(AsmToken::Comma))
2987 return TokError("unexpected token in directive");
2988 Lex();
2989
2990 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002991 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002992 return TokError("expected identifier in directive");
2993
2994 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2995
2996 if (IsPersonality)
2997 getStreamer().EmitCFIPersonality(Sym, Encoding);
2998 else
2999 getStreamer().EmitCFILsda(Sym, Encoding);
3000 return false;
3001}
3002
Jim Grosbach4b905842013-09-20 23:08:21 +00003003/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003004/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003005bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003006 getStreamer().EmitCFIRememberState();
3007 return false;
3008}
3009
Jim Grosbach4b905842013-09-20 23:08:21 +00003010/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003011/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003012bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003013 getStreamer().EmitCFIRestoreState();
3014 return false;
3015}
3016
Jim Grosbach4b905842013-09-20 23:08:21 +00003017/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003018/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003019bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003020 int64_t Register = 0;
3021
Jim Grosbach4b905842013-09-20 23:08:21 +00003022 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003023 return true;
3024
3025 getStreamer().EmitCFISameValue(Register);
3026 return false;
3027}
3028
Jim Grosbach4b905842013-09-20 23:08:21 +00003029/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003030/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003031bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003032 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003033 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003034 return true;
3035
3036 getStreamer().EmitCFIRestore(Register);
3037 return false;
3038}
3039
Jim Grosbach4b905842013-09-20 23:08:21 +00003040/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003041/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003042bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003043 std::string Values;
3044 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003045 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003046 return true;
3047
3048 Values.push_back((uint8_t)CurrValue);
3049
3050 while (getLexer().is(AsmToken::Comma)) {
3051 Lex();
3052
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003053 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003054 return true;
3055
3056 Values.push_back((uint8_t)CurrValue);
3057 }
3058
3059 getStreamer().EmitCFIEscape(Values);
3060 return false;
3061}
3062
Jim Grosbach4b905842013-09-20 23:08:21 +00003063/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003064/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003065bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003066 if (getLexer().isNot(AsmToken::EndOfStatement))
3067 return Error(getLexer().getLoc(),
3068 "unexpected token in '.cfi_signal_frame'");
3069
3070 getStreamer().EmitCFISignalFrame();
3071 return false;
3072}
3073
Jim Grosbach4b905842013-09-20 23:08:21 +00003074/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003075/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003076bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003077 int64_t Register = 0;
3078
Jim Grosbach4b905842013-09-20 23:08:21 +00003079 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003080 return true;
3081
3082 getStreamer().EmitCFIUndefined(Register);
3083 return false;
3084}
3085
Jim Grosbach4b905842013-09-20 23:08:21 +00003086/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003087/// ::= .macros_on
3088/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003089bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003090 if (getLexer().isNot(AsmToken::EndOfStatement))
3091 return Error(getLexer().getLoc(),
3092 "unexpected token in '" + Directive + "' directive");
3093
Jim Grosbach4b905842013-09-20 23:08:21 +00003094 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003095 return false;
3096}
3097
Jim Grosbach4b905842013-09-20 23:08:21 +00003098/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003099/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003100bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003101 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003102 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003103 return TokError("expected identifier in '.macro' directive");
3104
3105 MCAsmMacroParameters Parameters;
3106 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00003107 // parseMacroArgument()
Eli Bendersky17233942013-01-15 22:59:42 +00003108 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3109 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3110 for (;;) {
3111 MCAsmMacroParameter Parameter;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003112 if (parseIdentifier(Parameter.first))
Eli Bendersky17233942013-01-15 22:59:42 +00003113 return TokError("expected identifier in '.macro' directive");
3114
3115 if (getLexer().is(AsmToken::Equal)) {
3116 Lex();
Jim Grosbach4b905842013-09-20 23:08:21 +00003117 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky17233942013-01-15 22:59:42 +00003118 return true;
3119 }
3120
3121 Parameters.push_back(Parameter);
3122
3123 if (getLexer().is(AsmToken::Comma))
3124 Lex();
3125 else if (getLexer().is(AsmToken::EndOfStatement))
3126 break;
3127 }
3128 }
3129
3130 // Eat the end of statement.
3131 Lex();
3132
3133 AsmToken EndToken, StartToken = getTok();
3134
3135 // Lex the macro definition.
3136 for (;;) {
3137 // Check whether we have reached the end of the file.
3138 if (getLexer().is(AsmToken::Eof))
3139 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3140
3141 // Otherwise, check whether we have reach the .endmacro.
3142 if (getLexer().is(AsmToken::Identifier) &&
3143 (getTok().getIdentifier() == ".endm" ||
3144 getTok().getIdentifier() == ".endmacro")) {
3145 EndToken = getTok();
3146 Lex();
3147 if (getLexer().isNot(AsmToken::EndOfStatement))
3148 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3149 "' directive");
3150 break;
3151 }
3152
3153 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003154 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003155 }
3156
Jim Grosbach4b905842013-09-20 23:08:21 +00003157 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003158 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3159 }
3160
3161 const char *BodyStart = StartToken.getLoc().getPointer();
3162 const char *BodyEnd = EndToken.getLoc().getPointer();
3163 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003164 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3165 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003166 return false;
3167}
3168
Jim Grosbach4b905842013-09-20 23:08:21 +00003169/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003170///
3171/// With the support added for named parameters there may be code out there that
3172/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003173/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003174/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003175/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003176/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3177/// warning that the positional parameter found in body which have no effect.
3178/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003179/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003180/// intended or change the macro to use the named parameters. It is possible
3181/// this warning will trigger when the none of the named parameters are used
3182/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003183void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003184 StringRef Body,
3185 MCAsmMacroParameters Parameters) {
3186 // If this macro is not defined with named parameters the warning we are
3187 // checking for here doesn't apply.
3188 unsigned NParameters = Parameters.size();
3189 if (NParameters == 0)
3190 return;
3191
3192 bool NamedParametersFound = false;
3193 bool PositionalParametersFound = false;
3194
3195 // Look at the body of the macro for use of both the named parameters and what
3196 // are likely to be positional parameters. This is what expandMacro() is
3197 // doing when it finds the parameters in the body.
3198 while (!Body.empty()) {
3199 // Scan for the next possible parameter.
3200 std::size_t End = Body.size(), Pos = 0;
3201 for (; Pos != End; ++Pos) {
3202 // Check for a substitution or escape.
3203 // This macro is defined with parameters, look for \foo, \bar, etc.
3204 if (Body[Pos] == '\\' && Pos + 1 != End)
3205 break;
3206
3207 // This macro should have parameters, but look for $0, $1, ..., $n too.
3208 if (Body[Pos] != '$' || Pos + 1 == End)
3209 continue;
3210 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003211 if (Next == '$' || Next == 'n' ||
3212 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003213 break;
3214 }
3215
3216 // Check if we reached the end.
3217 if (Pos == End)
3218 break;
3219
3220 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003221 switch (Body[Pos + 1]) {
3222 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003223 case '$':
3224 break;
3225
Jim Grosbach4b905842013-09-20 23:08:21 +00003226 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003227 case 'n':
3228 PositionalParametersFound = true;
3229 break;
3230
Jim Grosbach4b905842013-09-20 23:08:21 +00003231 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003232 default: {
3233 PositionalParametersFound = true;
3234 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003235 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003236 }
3237 Pos += 2;
3238 } else {
3239 unsigned I = Pos + 1;
3240 while (isIdentifierChar(Body[I]) && I + 1 != End)
3241 ++I;
3242
Jim Grosbach4b905842013-09-20 23:08:21 +00003243 const char *Begin = Body.data() + Pos + 1;
3244 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003245 unsigned Index = 0;
3246 for (; Index < NParameters; ++Index)
3247 if (Parameters[Index].first == Argument)
3248 break;
3249
3250 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003251 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3252 Pos += 3;
3253 else {
3254 Pos = I;
3255 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003256 } else {
3257 NamedParametersFound = true;
3258 Pos += 1 + Argument.size();
3259 }
3260 }
3261 // Update the scan point.
3262 Body = Body.substr(Pos);
3263 }
3264
3265 if (!NamedParametersFound && PositionalParametersFound)
3266 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3267 "used in macro body, possible positional parameter "
3268 "found in body which will have no effect");
3269}
3270
Jim Grosbach4b905842013-09-20 23:08:21 +00003271/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003272/// ::= .endm
3273/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003274bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003275 if (getLexer().isNot(AsmToken::EndOfStatement))
3276 return TokError("unexpected token in '" + Directive + "' directive");
3277
3278 // If we are inside a macro instantiation, terminate the current
3279 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003280 if (isInsideMacroInstantiation()) {
3281 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003282 return false;
3283 }
3284
3285 // Otherwise, this .endmacro is a stray entry in the file; well formed
3286 // .endmacro directives are handled during the macro definition parsing.
3287 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003288 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003289}
3290
Jim Grosbach4b905842013-09-20 23:08:21 +00003291/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003292/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003293bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003294 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003295 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003296 return TokError("expected identifier in '.purgem' directive");
3297
3298 if (getLexer().isNot(AsmToken::EndOfStatement))
3299 return TokError("unexpected token in '.purgem' directive");
3300
Jim Grosbach4b905842013-09-20 23:08:21 +00003301 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003302 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3303
Jim Grosbach4b905842013-09-20 23:08:21 +00003304 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003305 return false;
3306}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003307
Jim Grosbach4b905842013-09-20 23:08:21 +00003308/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003309/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003310bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003311 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003312
3313 // Expect a single argument: an expression that evaluates to a constant
3314 // in the inclusive range 0-30.
3315 SMLoc ExprLoc = getLexer().getLoc();
3316 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003317 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003318 return true;
3319 else if (getLexer().isNot(AsmToken::EndOfStatement))
3320 return TokError("unexpected token after expression in"
3321 " '.bundle_align_mode' directive");
3322 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3323 return Error(ExprLoc,
3324 "invalid bundle alignment size (expected between 0 and 30)");
3325
3326 Lex();
3327
3328 // Because of AlignSizePow2's verified range we can safely truncate it to
3329 // unsigned.
3330 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3331 return false;
3332}
3333
Jim Grosbach4b905842013-09-20 23:08:21 +00003334/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003335/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003336bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003337 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003338 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003339
Eli Bendersky802b6282013-01-07 21:51:08 +00003340 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3341 StringRef Option;
3342 SMLoc Loc = getTok().getLoc();
3343 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003344 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003345
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003346 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003347 return Error(Loc, kInvalidOptionError);
3348
3349 if (Option != "align_to_end")
3350 return Error(Loc, kInvalidOptionError);
3351 else if (getLexer().isNot(AsmToken::EndOfStatement))
3352 return Error(Loc,
3353 "unexpected token after '.bundle_lock' directive option");
3354 AlignToEnd = true;
3355 }
3356
Eli Benderskyf483ff92012-12-20 19:05:53 +00003357 Lex();
3358
Eli Bendersky802b6282013-01-07 21:51:08 +00003359 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003360 return false;
3361}
3362
Jim Grosbach4b905842013-09-20 23:08:21 +00003363/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003364/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003365bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003366 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003367
3368 if (getLexer().isNot(AsmToken::EndOfStatement))
3369 return TokError("unexpected token in '.bundle_unlock' directive");
3370 Lex();
3371
3372 getStreamer().EmitBundleUnlock();
3373 return false;
3374}
3375
Jim Grosbach4b905842013-09-20 23:08:21 +00003376/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003377/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003378bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003379 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003380
3381 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003382 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003383 return true;
3384
3385 int64_t FillExpr = 0;
3386 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3387 if (getLexer().isNot(AsmToken::Comma))
3388 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3389 Lex();
3390
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003391 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003392 return true;
3393
3394 if (getLexer().isNot(AsmToken::EndOfStatement))
3395 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3396 }
3397
3398 Lex();
3399
3400 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003401 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3402 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003403
3404 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003405 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003406
3407 return false;
3408}
3409
Jim Grosbach4b905842013-09-20 23:08:21 +00003410/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003411/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003412bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003413 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003414 const MCExpr *Value;
3415
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003416 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003417 return true;
3418
3419 if (getLexer().isNot(AsmToken::EndOfStatement))
3420 return TokError("unexpected token in directive");
3421
3422 if (Signed)
3423 getStreamer().EmitSLEB128Value(Value);
3424 else
3425 getStreamer().EmitULEB128Value(Value);
3426
3427 return false;
3428}
3429
Jim Grosbach4b905842013-09-20 23:08:21 +00003430/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003431/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003432bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003433 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003434 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003435 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003436 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003437
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003438 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003439 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003440
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003441 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003442
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003443 // Assembler local symbols don't make any sense here. Complain loudly.
3444 if (Sym->isTemporary())
3445 return Error(Loc, "non-local symbol required in directive");
3446
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003447 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3448 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003449
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003450 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003451 break;
3452
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003453 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003454 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003455 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003456 }
3457 }
3458
Sean Callanan686ed8d2010-01-19 20:22:31 +00003459 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003460 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003461}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003462
Jim Grosbach4b905842013-09-20 23:08:21 +00003463/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003464/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003465bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003466 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003467
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003468 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003469 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003470 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003471 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003472
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003473 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003474 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003475
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003476 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003477 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003478 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003479
3480 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003481 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003482 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003483 return true;
3484
3485 int64_t Pow2Alignment = 0;
3486 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003487 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003488 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003489 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003490 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003491 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003492
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003493 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3494 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003495 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3496
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003497 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003498 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3499 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003500 if (!isPowerOf2_64(Pow2Alignment))
3501 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3502 Pow2Alignment = Log2_64(Pow2Alignment);
3503 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003504 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003505
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003506 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003507 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003508
Sean Callanan686ed8d2010-01-19 20:22:31 +00003509 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003510
Chris Lattner28ad7542009-07-09 17:25:12 +00003511 // NOTE: a size of zero for a .comm should create a undefined symbol
3512 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003513 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003514 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003515 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003516
Eric Christopherbc818852010-05-14 01:38:54 +00003517 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003518 // may internally end up wanting an alignment in bytes.
3519 // FIXME: Diagnose overflow.
3520 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003521 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003522 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003523
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003524 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003525 return Error(IDLoc, "invalid symbol redefinition");
3526
Chris Lattner28ad7542009-07-09 17:25:12 +00003527 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003528 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003529 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003530 return false;
3531 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003532
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003533 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003534 return false;
3535}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003536
Jim Grosbach4b905842013-09-20 23:08:21 +00003537/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003538/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003539bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003540 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003541 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003542
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003543 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003544 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003545 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003546
Sean Callanan686ed8d2010-01-19 20:22:31 +00003547 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003548
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003549 if (Str.empty())
3550 Error(Loc, ".abort detected. Assembly stopping.");
3551 else
3552 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003553 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003554
3555 return false;
3556}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003557
Jim Grosbach4b905842013-09-20 23:08:21 +00003558/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003559/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003560bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003561 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003562 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003563
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003564 // Allow the strings to have escaped octal character sequence.
3565 std::string Filename;
3566 if (parseEscapedString(Filename))
3567 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003568 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003569 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003570
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003571 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003572 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003573
Chris Lattner693fbb82009-07-16 06:14:39 +00003574 // Attempt to switch the lexer to the included file before consuming the end
3575 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003576 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003577 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003578 return true;
3579 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003580
3581 return false;
3582}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003583
Jim Grosbach4b905842013-09-20 23:08:21 +00003584/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003585/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003586bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003587 if (getLexer().isNot(AsmToken::String))
3588 return TokError("expected string in '.incbin' directive");
3589
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003590 // Allow the strings to have escaped octal character sequence.
3591 std::string Filename;
3592 if (parseEscapedString(Filename))
3593 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003594 SMLoc IncbinLoc = getLexer().getLoc();
3595 Lex();
3596
3597 if (getLexer().isNot(AsmToken::EndOfStatement))
3598 return TokError("unexpected token in '.incbin' directive");
3599
Kevin Enderby109f25c2011-12-14 21:47:48 +00003600 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003601 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003602 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3603 return true;
3604 }
3605
3606 return false;
3607}
3608
Jim Grosbach4b905842013-09-20 23:08:21 +00003609/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003610/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003611bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003612 TheCondStack.push_back(TheCondState);
3613 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003614 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003615 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003616 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003617 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003618 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003619 return true;
3620
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003621 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003622 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003623
Sean Callanan686ed8d2010-01-19 20:22:31 +00003624 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003625
3626 TheCondState.CondMet = ExprValue;
3627 TheCondState.Ignore = !TheCondState.CondMet;
3628 }
3629
3630 return false;
3631}
3632
Jim Grosbach4b905842013-09-20 23:08:21 +00003633/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003634/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003635bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003636 TheCondStack.push_back(TheCondState);
3637 TheCondState.TheCond = AsmCond::IfCond;
3638
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003639 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003640 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003641 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003642 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003643
3644 if (getLexer().isNot(AsmToken::EndOfStatement))
3645 return TokError("unexpected token in '.ifb' directive");
3646
3647 Lex();
3648
3649 TheCondState.CondMet = ExpectBlank == Str.empty();
3650 TheCondState.Ignore = !TheCondState.CondMet;
3651 }
3652
3653 return false;
3654}
3655
Jim Grosbach4b905842013-09-20 23:08:21 +00003656/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003657/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003658bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003659 TheCondStack.push_back(TheCondState);
3660 TheCondState.TheCond = AsmCond::IfCond;
3661
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003662 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003663 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003664 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003665 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003666
3667 if (getLexer().isNot(AsmToken::Comma))
3668 return TokError("unexpected token in '.ifc' directive");
3669
3670 Lex();
3671
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003672 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003673
3674 if (getLexer().isNot(AsmToken::EndOfStatement))
3675 return TokError("unexpected token in '.ifc' directive");
3676
3677 Lex();
3678
3679 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3680 TheCondState.Ignore = !TheCondState.CondMet;
3681 }
3682
3683 return false;
3684}
3685
Jim Grosbach4b905842013-09-20 23:08:21 +00003686/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003687/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003688bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003689 StringRef Name;
3690 TheCondStack.push_back(TheCondState);
3691 TheCondState.TheCond = AsmCond::IfCond;
3692
3693 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003694 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003695 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003696 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003697 return TokError("expected identifier after '.ifdef'");
3698
3699 Lex();
3700
3701 MCSymbol *Sym = getContext().LookupSymbol(Name);
3702
3703 if (expect_defined)
3704 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3705 else
3706 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3707 TheCondState.Ignore = !TheCondState.CondMet;
3708 }
3709
3710 return false;
3711}
3712
Jim Grosbach4b905842013-09-20 23:08:21 +00003713/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003714/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003715bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003716 if (TheCondState.TheCond != AsmCond::IfCond &&
3717 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003718 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3719 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003720 TheCondState.TheCond = AsmCond::ElseIfCond;
3721
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003722 bool LastIgnoreState = false;
3723 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003724 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003725 if (LastIgnoreState || TheCondState.CondMet) {
3726 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003727 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003728 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003729 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003730 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003731 return true;
3732
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003733 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003734 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003735
Sean Callanan686ed8d2010-01-19 20:22:31 +00003736 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003737 TheCondState.CondMet = ExprValue;
3738 TheCondState.Ignore = !TheCondState.CondMet;
3739 }
3740
3741 return false;
3742}
3743
Jim Grosbach4b905842013-09-20 23:08:21 +00003744/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003745/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003746bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003747 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003748 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003749
Sean Callanan686ed8d2010-01-19 20:22:31 +00003750 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003751
3752 if (TheCondState.TheCond != AsmCond::IfCond &&
3753 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003754 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3755 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003756 TheCondState.TheCond = AsmCond::ElseCond;
3757 bool LastIgnoreState = false;
3758 if (!TheCondStack.empty())
3759 LastIgnoreState = TheCondStack.back().Ignore;
3760 if (LastIgnoreState || TheCondState.CondMet)
3761 TheCondState.Ignore = true;
3762 else
3763 TheCondState.Ignore = false;
3764
3765 return false;
3766}
3767
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003768/// parseDirectiveEnd
3769/// ::= .end
3770bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3771 if (getLexer().isNot(AsmToken::EndOfStatement))
3772 return TokError("unexpected token in '.end' directive");
3773
3774 Lex();
3775
3776 while (Lexer.isNot(AsmToken::Eof))
3777 Lex();
3778
3779 return false;
3780}
3781
Jim Grosbach4b905842013-09-20 23:08:21 +00003782/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003783/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003784bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003785 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003786 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003787
Sean Callanan686ed8d2010-01-19 20:22:31 +00003788 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003789
Jim Grosbach4b905842013-09-20 23:08:21 +00003790 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003791 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3792 ".else");
3793 if (!TheCondStack.empty()) {
3794 TheCondState = TheCondStack.back();
3795 TheCondStack.pop_back();
3796 }
3797
3798 return false;
3799}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003800
Eli Bendersky17233942013-01-15 22:59:42 +00003801void AsmParser::initializeDirectiveKindMap() {
3802 DirectiveKindMap[".set"] = DK_SET;
3803 DirectiveKindMap[".equ"] = DK_EQU;
3804 DirectiveKindMap[".equiv"] = DK_EQUIV;
3805 DirectiveKindMap[".ascii"] = DK_ASCII;
3806 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3807 DirectiveKindMap[".string"] = DK_STRING;
3808 DirectiveKindMap[".byte"] = DK_BYTE;
3809 DirectiveKindMap[".short"] = DK_SHORT;
3810 DirectiveKindMap[".value"] = DK_VALUE;
3811 DirectiveKindMap[".2byte"] = DK_2BYTE;
3812 DirectiveKindMap[".long"] = DK_LONG;
3813 DirectiveKindMap[".int"] = DK_INT;
3814 DirectiveKindMap[".4byte"] = DK_4BYTE;
3815 DirectiveKindMap[".quad"] = DK_QUAD;
3816 DirectiveKindMap[".8byte"] = DK_8BYTE;
3817 DirectiveKindMap[".single"] = DK_SINGLE;
3818 DirectiveKindMap[".float"] = DK_FLOAT;
3819 DirectiveKindMap[".double"] = DK_DOUBLE;
3820 DirectiveKindMap[".align"] = DK_ALIGN;
3821 DirectiveKindMap[".align32"] = DK_ALIGN32;
3822 DirectiveKindMap[".balign"] = DK_BALIGN;
3823 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3824 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3825 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3826 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3827 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3828 DirectiveKindMap[".org"] = DK_ORG;
3829 DirectiveKindMap[".fill"] = DK_FILL;
3830 DirectiveKindMap[".zero"] = DK_ZERO;
3831 DirectiveKindMap[".extern"] = DK_EXTERN;
3832 DirectiveKindMap[".globl"] = DK_GLOBL;
3833 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003834 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3835 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3836 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3837 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3838 DirectiveKindMap[".reference"] = DK_REFERENCE;
3839 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3840 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3841 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3842 DirectiveKindMap[".comm"] = DK_COMM;
3843 DirectiveKindMap[".common"] = DK_COMMON;
3844 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3845 DirectiveKindMap[".abort"] = DK_ABORT;
3846 DirectiveKindMap[".include"] = DK_INCLUDE;
3847 DirectiveKindMap[".incbin"] = DK_INCBIN;
3848 DirectiveKindMap[".code16"] = DK_CODE16;
3849 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3850 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003851 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003852 DirectiveKindMap[".irp"] = DK_IRP;
3853 DirectiveKindMap[".irpc"] = DK_IRPC;
3854 DirectiveKindMap[".endr"] = DK_ENDR;
3855 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3856 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3857 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3858 DirectiveKindMap[".if"] = DK_IF;
3859 DirectiveKindMap[".ifb"] = DK_IFB;
3860 DirectiveKindMap[".ifnb"] = DK_IFNB;
3861 DirectiveKindMap[".ifc"] = DK_IFC;
3862 DirectiveKindMap[".ifnc"] = DK_IFNC;
3863 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3864 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3865 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3866 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3867 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003868 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003869 DirectiveKindMap[".endif"] = DK_ENDIF;
3870 DirectiveKindMap[".skip"] = DK_SKIP;
3871 DirectiveKindMap[".space"] = DK_SPACE;
3872 DirectiveKindMap[".file"] = DK_FILE;
3873 DirectiveKindMap[".line"] = DK_LINE;
3874 DirectiveKindMap[".loc"] = DK_LOC;
3875 DirectiveKindMap[".stabs"] = DK_STABS;
3876 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3877 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3878 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3879 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3880 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3881 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3882 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3883 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3884 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3885 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3886 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3887 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3888 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3889 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3890 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3891 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3892 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3893 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3894 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3895 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3896 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003897 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003898 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3899 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3900 DirectiveKindMap[".macro"] = DK_MACRO;
3901 DirectiveKindMap[".endm"] = DK_ENDM;
3902 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3903 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003904}
3905
Jim Grosbach4b905842013-09-20 23:08:21 +00003906MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003907 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003908
Rafael Espindola34b9c512012-06-03 23:57:14 +00003909 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003910 for (;;) {
3911 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003912 if (getLexer().is(AsmToken::Eof)) {
3913 Error(DirectiveLoc, "no matching '.endr' in definition");
3914 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003915 }
3916
Rafael Espindola34b9c512012-06-03 23:57:14 +00003917 if (Lexer.is(AsmToken::Identifier) &&
3918 (getTok().getIdentifier() == ".rept")) {
3919 ++NestLevel;
3920 }
3921
3922 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003923 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003924 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003925 EndToken = getTok();
3926 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003927 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3928 TokError("unexpected token in '.endr' directive");
3929 return 0;
3930 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003931 break;
3932 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003933 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003934 }
3935
Rafael Espindola34b9c512012-06-03 23:57:14 +00003936 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003937 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003938 }
3939
3940 const char *BodyStart = StartToken.getLoc().getPointer();
3941 const char *BodyEnd = EndToken.getLoc().getPointer();
3942 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3943
Rafael Espindola34b9c512012-06-03 23:57:14 +00003944 // We Are Anonymous.
3945 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00003946 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00003947 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3948 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003949}
3950
Jim Grosbach4b905842013-09-20 23:08:21 +00003951void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00003952 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003953 OS << ".endr\n";
3954
3955 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00003956 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003957
Rafael Espindola34b9c512012-06-03 23:57:14 +00003958 // Create the macro instantiation object and add to the current macro
3959 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00003960 MacroInstantiation *MI = new MacroInstantiation(
3961 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003962 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003963
Rafael Espindola34b9c512012-06-03 23:57:14 +00003964 // Jump to the macro instantiation and prime the lexer.
3965 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3966 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3967 Lex();
3968}
3969
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003970/// parseDirectiveRept
3971/// ::= .rep | .rept count
3972bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003973 const MCExpr *CountExpr;
3974 SMLoc CountLoc = getTok().getLoc();
3975 if (parseExpression(CountExpr))
3976 return true;
3977
Rafael Espindola34b9c512012-06-03 23:57:14 +00003978 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003979 if (!CountExpr->EvaluateAsAbsolute(Count)) {
3980 eatToEndOfStatement();
3981 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
3982 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003983
3984 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003985 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003986
3987 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003988 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003989
3990 // Eat the end of statement.
3991 Lex();
3992
3993 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003994 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003995 if (!M)
3996 return true;
3997
3998 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3999 // to hold the macro body with substitutions.
4000 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00004001 MCAsmMacroParameters Parameters;
4002 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004003 raw_svector_ostream OS(Buf);
4004 while (Count--) {
4005 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
4006 return true;
4007 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004008 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004009
4010 return false;
4011}
4012
Jim Grosbach4b905842013-09-20 23:08:21 +00004013/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004014/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004015bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004016 MCAsmMacroParameters Parameters;
4017 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004018
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004019 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004020 return TokError("expected identifier in '.irp' directive");
4021
4022 Parameters.push_back(Parameter);
4023
4024 if (Lexer.isNot(AsmToken::Comma))
4025 return TokError("expected comma in '.irp' directive");
4026
4027 Lex();
4028
Eli Bendersky38274122013-01-14 23:22:36 +00004029 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004030 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004031 return true;
4032
4033 // Eat the end of statement.
4034 Lex();
4035
4036 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004037 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004038 if (!M)
4039 return true;
4040
4041 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4042 // to hold the macro body with substitutions.
4043 SmallString<256> Buf;
4044 raw_svector_ostream OS(Buf);
4045
Eli Bendersky38274122013-01-14 23:22:36 +00004046 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4047 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004048 Args.push_back(*i);
4049
4050 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4051 return true;
4052 }
4053
Jim Grosbach4b905842013-09-20 23:08:21 +00004054 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004055
4056 return false;
4057}
4058
Jim Grosbach4b905842013-09-20 23:08:21 +00004059/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004060/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004061bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004062 MCAsmMacroParameters Parameters;
4063 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004064
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004065 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004066 return TokError("expected identifier in '.irpc' directive");
4067
4068 Parameters.push_back(Parameter);
4069
4070 if (Lexer.isNot(AsmToken::Comma))
4071 return TokError("expected comma in '.irpc' directive");
4072
4073 Lex();
4074
Eli Bendersky38274122013-01-14 23:22:36 +00004075 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004076 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004077 return true;
4078
4079 if (A.size() != 1 || A.front().size() != 1)
4080 return TokError("unexpected token in '.irpc' directive");
4081
4082 // Eat the end of statement.
4083 Lex();
4084
4085 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004086 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004087 if (!M)
4088 return true;
4089
4090 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4091 // to hold the macro body with substitutions.
4092 SmallString<256> Buf;
4093 raw_svector_ostream OS(Buf);
4094
4095 StringRef Values = A.front().front().getString();
4096 std::size_t I, End = Values.size();
4097 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004098 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004099 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004100
Eli Bendersky38274122013-01-14 23:22:36 +00004101 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004102 Args.push_back(Arg);
4103
4104 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4105 return true;
4106 }
4107
Jim Grosbach4b905842013-09-20 23:08:21 +00004108 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004109
4110 return false;
4111}
4112
Jim Grosbach4b905842013-09-20 23:08:21 +00004113bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004114 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004115 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004116
4117 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004118 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004119 assert(getLexer().is(AsmToken::EndOfStatement));
4120
Jim Grosbach4b905842013-09-20 23:08:21 +00004121 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004122 return false;
4123}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004124
Jim Grosbach4b905842013-09-20 23:08:21 +00004125bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004126 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004127 const MCExpr *Value;
4128 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004129 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004130 return true;
4131 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4132 if (!MCE)
4133 return Error(ExprLoc, "unexpected expression in _emit");
4134 uint64_t IntValue = MCE->getValue();
4135 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4136 return Error(ExprLoc, "literal value out of range for directive");
4137
Chad Rosierc7f552c2013-02-12 21:33:51 +00004138 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4139 return false;
4140}
4141
Jim Grosbach4b905842013-09-20 23:08:21 +00004142bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004143 const MCExpr *Value;
4144 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004145 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004146 return true;
4147 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4148 if (!MCE)
4149 return Error(ExprLoc, "unexpected expression in align");
4150 uint64_t IntValue = MCE->getValue();
4151 if (!isPowerOf2_64(IntValue))
4152 return Error(ExprLoc, "literal value not a power of two greater then zero");
4153
Jim Grosbach4b905842013-09-20 23:08:21 +00004154 Info.AsmRewrites->push_back(
4155 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004156 return false;
4157}
4158
Chad Rosierf43fcf52013-02-13 21:27:17 +00004159// We are comparing pointers, but the pointers are relative to a single string.
4160// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004161static int rewritesSort(const AsmRewrite *AsmRewriteA,
4162 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004163 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4164 return -1;
4165 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4166 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004167
Chad Rosierfce4fab2013-04-08 17:43:47 +00004168 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4169 // rewrite to the same location. Make sure the SizeDirective rewrite is
4170 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4171 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004172 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4173 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004174 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004175
Jim Grosbach4b905842013-09-20 23:08:21 +00004176 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4177 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004178 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004179 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004180}
4181
Jim Grosbach4b905842013-09-20 23:08:21 +00004182bool AsmParser::parseMSInlineAsm(
4183 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4184 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4185 SmallVectorImpl<std::string> &Constraints,
4186 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4187 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004188 SmallVector<void *, 4> InputDecls;
4189 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004190 SmallVector<bool, 4> InputDeclsAddressOf;
4191 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004192 SmallVector<std::string, 4> InputConstraints;
4193 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004194 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004195
Benjamin Kramer1a136112013-02-15 20:37:21 +00004196 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004197
4198 // Prime the lexer.
4199 Lex();
4200
4201 // While we have input, parse each statement.
4202 unsigned InputIdx = 0;
4203 unsigned OutputIdx = 0;
4204 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004205 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004206 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004207 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004208
Chad Rosier149e8e02012-12-12 22:45:52 +00004209 if (Info.ParseError)
4210 return true;
4211
Benjamin Kramer1a136112013-02-15 20:37:21 +00004212 if (Info.Opcode == ~0U)
4213 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004214
Benjamin Kramer1a136112013-02-15 20:37:21 +00004215 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004216
Benjamin Kramer1a136112013-02-15 20:37:21 +00004217 // Build the list of clobbers, outputs and inputs.
4218 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4219 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004220
Benjamin Kramer1a136112013-02-15 20:37:21 +00004221 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004222 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004223 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004224
Benjamin Kramer1a136112013-02-15 20:37:21 +00004225 // Register operand.
4226 if (Operand->isReg() && !Operand->needAddressOf()) {
4227 unsigned NumDefs = Desc.getNumDefs();
4228 // Clobber.
4229 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4230 ClobberRegs.push_back(Operand->getReg());
4231 continue;
4232 }
4233
4234 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004235 StringRef SymName = Operand->getSymName();
4236 if (SymName.empty())
4237 continue;
4238
Chad Rosierdba3fe52013-04-22 22:12:12 +00004239 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004240 if (!OpDecl)
4241 continue;
4242
4243 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004244 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004245 if (isOutput) {
4246 ++InputIdx;
4247 OutputDecls.push_back(OpDecl);
4248 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4249 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004250 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004251 } else {
4252 InputDecls.push_back(OpDecl);
4253 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4254 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004255 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004256 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004257 }
Reid Kleckneree088972013-12-10 18:27:32 +00004258
4259 // Consider implicit defs to be clobbers. Think of cpuid and push.
4260 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4261 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4262 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004263 }
4264
4265 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004266 NumOutputs = OutputDecls.size();
4267 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004268
4269 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004270 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4271 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4272 ClobberRegs.end());
4273 Clobbers.assign(ClobberRegs.size(), std::string());
4274 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4275 raw_string_ostream OS(Clobbers[I]);
4276 IP->printRegName(OS, ClobberRegs[I]);
4277 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004278
4279 // Merge the various outputs and inputs. Output are expected first.
4280 if (NumOutputs || NumInputs) {
4281 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004282 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004283 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004284 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004285 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004286 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004287 }
4288 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004289 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004290 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004291 }
4292 }
4293
4294 // Build the IR assembly string.
4295 std::string AsmStringIR;
4296 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004297 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4298 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004299 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004300 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4301 E = AsmStrRewrites.end();
4302 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004303 AsmRewriteKind Kind = (*I).Kind;
4304 if (Kind == AOK_Delete)
4305 continue;
4306
Chad Rosier8bce6642012-10-18 15:49:34 +00004307 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004308 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004309
Chad Rosier120eefd2013-03-19 17:32:17 +00004310 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004311 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004312 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004313 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004314
Chad Rosier37e755c2012-10-23 17:43:43 +00004315 // Skip the original expression.
4316 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004317 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004318 continue;
4319 }
4320
Chad Rosierff10ed12013-04-12 16:26:42 +00004321 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004322 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004323 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004324 default:
4325 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004326 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004327 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004328 break;
4329 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004330 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004331 break;
4332 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004333 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004334 break;
4335 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004336 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004337 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004338 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004339 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004340 default: break;
4341 case 8: OS << "byte ptr "; break;
4342 case 16: OS << "word ptr "; break;
4343 case 32: OS << "dword ptr "; break;
4344 case 64: OS << "qword ptr "; break;
4345 case 80: OS << "xword ptr "; break;
4346 case 128: OS << "xmmword ptr "; break;
4347 case 256: OS << "ymmword ptr "; break;
4348 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004349 break;
4350 case AOK_Emit:
4351 OS << ".byte";
4352 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004353 case AOK_Align: {
4354 unsigned Val = (*I).Val;
4355 OS << ".align " << Val;
4356
4357 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004358 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004359 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4360 break;
4361 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004362 case AOK_DotOperator:
4363 OS << (*I).Val;
4364 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004365 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004366
Chad Rosier8bce6642012-10-18 15:49:34 +00004367 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004368 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004369 }
4370
4371 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004372 if (AsmStart != AsmEnd)
4373 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004374
4375 AsmString = OS.str();
4376 return false;
4377}
4378
Daniel Dunbar01e36072010-07-17 02:26:10 +00004379/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004380MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4381 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004382 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004383}