blob: 91699983e239fecc648affd95275614fd7497ea7 [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
David Majnemer16252452014-01-29 00:07:39 +00001866namespace {
1867class AsmLexerSkipSpaceRAII {
1868public:
1869 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1870 Lexer.setSkipSpace(SkipSpace);
1871 }
1872
1873 ~AsmLexerSkipSpaceRAII() {
1874 Lexer.setSkipSpace(true);
1875 }
1876
1877private:
1878 AsmLexer &Lexer;
1879};
1880}
1881
Jim Grosbach4b905842013-09-20 23:08:21 +00001882bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd05500642012-09-19 20:36:12 +00001883 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001884 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001885 unsigned AddTokens = 0;
1886
David Majnemer16252452014-01-29 00:07:39 +00001887 // Darwin doesn't use spaces to delmit arguments.
1888 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001889
1890 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001891 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001892 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001893
1894 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1895 // Spaces and commas cannot be mixed to delimit parameters
1896 if (ArgumentDelimiter == AsmToken::Eof)
1897 ArgumentDelimiter = AsmToken::Comma;
David Majnemer16252452014-01-29 00:07:39 +00001898 else if (ArgumentDelimiter != AsmToken::Comma)
Preston Gurd05500642012-09-19 20:36:12 +00001899 return TokError("expected ' ' for macro argument separator");
Preston Gurd05500642012-09-19 20:36:12 +00001900 break;
1901 }
1902
1903 if (Lexer.is(AsmToken::Space)) {
1904 Lex(); // Eat spaces
1905
1906 // Spaces can delimit parameters, but could also be part an expression.
1907 // If the token after a space is an operator, add the token and the next
1908 // one into this argument
1909 if (ArgumentDelimiter == AsmToken::Space ||
1910 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001911 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001912 // Check to see whether the token is used as an operator,
1913 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001914 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001915 if (*NextChar == ' ')
1916 AddTokens = 2;
1917 }
1918
1919 if (!AddTokens && ParenLevel == 0) {
1920 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4b905842013-09-20 23:08:21 +00001921 !isOperator(Lexer.getKind()))
Preston Gurd05500642012-09-19 20:36:12 +00001922 ArgumentDelimiter = AsmToken::Space;
1923 break;
1924 }
1925 }
1926 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001927
Jim Grosbach4b905842013-09-20 23:08:21 +00001928 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001929 // to be able to fill in the remaining default parameter values
1930 if (Lexer.is(AsmToken::EndOfStatement))
1931 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001932
1933 // Adjust the current parentheses level.
1934 if (Lexer.is(AsmToken::LParen))
1935 ++ParenLevel;
1936 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1937 --ParenLevel;
1938
1939 // Append the token to the current argument list.
1940 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001941 if (AddTokens)
1942 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001943 Lex();
1944 }
Preston Gurd05500642012-09-19 20:36:12 +00001945
Rafael Espindola768b41c2012-06-15 14:02:34 +00001946 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001947 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001948 return false;
1949}
1950
1951// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001952bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001953 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001954 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd05500642012-09-19 20:36:12 +00001955 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00001956 // parseMacroArgument()
Preston Gurd05500642012-09-19 20:36:12 +00001957 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001958
1959 // Parse two kinds of macro invocations:
1960 // - macros defined without any parameters accept an arbitrary number of them
1961 // - macros defined with parameters accept at most that many of them
1962 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1963 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001964 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001965
Jim Grosbach4b905842013-09-20 23:08:21 +00001966 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001967 return true;
1968
Preston Gurd242ed3152012-09-19 20:29:04 +00001969 if (!MA.empty() || !NParameters)
1970 A.push_back(MA);
1971 else if (NParameters) {
1972 if (!M->Parameters[Parameter].second.empty())
1973 A.push_back(M->Parameters[Parameter].second);
1974 }
Jim Grosbach206661622012-07-30 22:44:17 +00001975
Preston Gurd242ed3152012-09-19 20:29:04 +00001976 // At the end of the statement, fill in remaining arguments that have
1977 // default values. If there aren't any, then the next argument is
1978 // required but missing
1979 if (Lexer.is(AsmToken::EndOfStatement)) {
1980 if (NParameters && Parameter < NParameters - 1) {
1981 if (M->Parameters[Parameter + 1].second.empty())
1982 return TokError("macro argument '" +
1983 Twine(M->Parameters[Parameter + 1].first) +
1984 "' is missing");
1985 else
1986 continue;
1987 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001988 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001989 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001990
1991 if (Lexer.is(AsmToken::Comma))
1992 Lex();
1993 }
1994 return TokError("Too many arguments");
1995}
1996
Jim Grosbach4b905842013-09-20 23:08:21 +00001997const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1998 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001999 return (I == MacroMap.end()) ? NULL : I->getValue();
2000}
2001
Jim Grosbach4b905842013-09-20 23:08:21 +00002002void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00002003 MacroMap[Name] = new MCAsmMacro(Macro);
2004}
2005
Jim Grosbach4b905842013-09-20 23:08:21 +00002006void AsmParser::undefineMacro(StringRef Name) {
2007 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00002008 if (I != MacroMap.end()) {
2009 delete I->getValue();
2010 MacroMap.erase(I);
2011 }
2012}
2013
Jim Grosbach4b905842013-09-20 23:08:21 +00002014bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002015 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2016 // this, although we should protect against infinite loops.
2017 if (ActiveMacros.size() == 20)
2018 return TokError("macros cannot be nested more than 20 levels deep");
2019
Eli Bendersky38274122013-01-14 23:22:36 +00002020 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002021 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002022 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002023
Jim Grosbach206661622012-07-30 22:44:17 +00002024 // Remove any trailing empty arguments. Do this after-the-fact as we have
2025 // to keep empty arguments in the middle of the list or positionality
2026 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002027 while (!A.empty() && A.back().empty())
2028 A.pop_back();
Jim Grosbach206661622012-07-30 22:44:17 +00002029
Rafael Espindola1134ab232011-06-05 02:43:45 +00002030 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2031 // to hold the macro body with substitutions.
2032 SmallString<256> Buf;
2033 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002034 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002035
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002036 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002037 return true;
2038
Eli Bendersky38274122013-01-14 23:22:36 +00002039 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002040 // instantiation.
2041 OS << ".endmacro\n";
2042
Rafael Espindola1134ab232011-06-05 02:43:45 +00002043 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002044 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002045
Daniel Dunbar43235712010-07-18 18:54:11 +00002046 // Create the macro instantiation object and add to the current macro
2047 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002048 MacroInstantiation *MI = new MacroInstantiation(
2049 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002050 ActiveMacros.push_back(MI);
2051
2052 // Jump to the macro instantiation and prime the lexer.
2053 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2054 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2055 Lex();
2056
2057 return false;
2058}
2059
Jim Grosbach4b905842013-09-20 23:08:21 +00002060void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002061 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002062 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002063 Lex();
2064
2065 // Pop the instantiation entry.
2066 delete ActiveMacros.back();
2067 ActiveMacros.pop_back();
2068}
2069
Jim Grosbach4b905842013-09-20 23:08:21 +00002070static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002071 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002072 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002073 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2074 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002075 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002076 case MCExpr::Target:
2077 case MCExpr::Constant:
2078 return false;
2079 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002080 const MCSymbol &S =
2081 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002082 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002083 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002084 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002085 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002086 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002087 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002088 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002089
2090 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002091}
2092
Jim Grosbach4b905842013-09-20 23:08:21 +00002093bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002094 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002095 // FIXME: Use better location, we should use proper tokens.
2096 SMLoc EqualLoc = Lexer.getLoc();
2097
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002098 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002099 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002100 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002101
Rafael Espindola72f5f172012-01-28 05:57:00 +00002102 // Note: we don't count b as used in "a = b". This is to allow
2103 // a = b
2104 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002105
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002106 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002107 return TokError("unexpected token in assignment");
2108
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002109 // Error on assignment to '.'.
2110 if (Name == ".") {
2111 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2112 "(use '.space' or '.org').)"));
2113 }
2114
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002115 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002116 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002117
Daniel Dunbar5f339242009-10-16 01:57:39 +00002118 // Validate that the LHS is allowed to be a variable (either it has not been
2119 // used as a symbol, or it is an absolute symbol).
2120 MCSymbol *Sym = getContext().LookupSymbol(Name);
2121 if (Sym) {
2122 // Diagnose assignment to a label.
2123 //
2124 // FIXME: Diagnostics. Note the location of the definition as a label.
2125 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002126 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002127 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2128 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002129 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002130 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2131 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002132 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002133 return Error(EqualLoc, "redefinition of '" + Name + "'");
2134 else if (!Sym->isVariable())
2135 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002136 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002137 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002138 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002139
2140 // Don't count these checks as uses.
2141 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002142 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002143 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002144
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002145 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002146
2147 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002148 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002149 if (NoDeadStrip)
2150 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2151
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002152 return false;
2153}
2154
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002155/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002156/// ::= identifier
2157/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002158bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002159 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002160 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2161 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002162 // handle this as a context dependent token, instead we detect adjacent tokens
2163 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002164 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2165 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002166
Hans Wennborgce69d772013-10-18 20:46:28 +00002167 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002168 Lex();
2169 if (Lexer.isNot(AsmToken::Identifier))
2170 return true;
2171
Hans Wennborgce69d772013-10-18 20:46:28 +00002172 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2173 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002174 return true;
2175
2176 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002177 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002178 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002179 Lex();
2180 return false;
2181 }
2182
Jim Grosbach4b905842013-09-20 23:08:21 +00002183 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002184 return true;
2185
Sean Callanan936b0d32010-01-19 21:44:56 +00002186 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002187
Sean Callanan686ed8d2010-01-19 20:22:31 +00002188 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002189
2190 return false;
2191}
2192
Jim Grosbach4b905842013-09-20 23:08:21 +00002193/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002194/// ::= .equ identifier ',' expression
2195/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002196/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002197bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002198 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002199
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002200 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002201 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002202
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002203 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002204 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002205 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002206
Jim Grosbach4b905842013-09-20 23:08:21 +00002207 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002208}
2209
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002210bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002211 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002212
2213 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002214 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002215 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2216 if (Str[i] != '\\') {
2217 Data += Str[i];
2218 continue;
2219 }
2220
2221 // Recognize escaped characters. Note that this escape semantics currently
2222 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2223 ++i;
2224 if (i == e)
2225 return TokError("unexpected backslash at end of string");
2226
2227 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002228 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002229 // Consume up to three octal characters.
2230 unsigned Value = Str[i] - '0';
2231
Jim Grosbach4b905842013-09-20 23:08:21 +00002232 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002233 ++i;
2234 Value = Value * 8 + (Str[i] - '0');
2235
Jim Grosbach4b905842013-09-20 23:08:21 +00002236 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002237 ++i;
2238 Value = Value * 8 + (Str[i] - '0');
2239 }
2240 }
2241
2242 if (Value > 255)
2243 return TokError("invalid octal escape sequence (out of range)");
2244
Jim Grosbach4b905842013-09-20 23:08:21 +00002245 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002246 continue;
2247 }
2248
2249 // Otherwise recognize individual escapes.
2250 switch (Str[i]) {
2251 default:
2252 // Just reject invalid escape sequences for now.
2253 return TokError("invalid escape sequence (unrecognized character)");
2254
2255 case 'b': Data += '\b'; break;
2256 case 'f': Data += '\f'; break;
2257 case 'n': Data += '\n'; break;
2258 case 'r': Data += '\r'; break;
2259 case 't': Data += '\t'; break;
2260 case '"': Data += '"'; break;
2261 case '\\': Data += '\\'; break;
2262 }
2263 }
2264
2265 return false;
2266}
2267
Jim Grosbach4b905842013-09-20 23:08:21 +00002268/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002269/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002270bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002271 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002272 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002273
Daniel Dunbara10e5192009-06-24 23:30:00 +00002274 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002275 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002276 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002277
Daniel Dunbaref668c12009-08-14 18:19:52 +00002278 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002279 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002280 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002281
Rafael Espindola64e1af82013-07-02 15:49:13 +00002282 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002283 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002284 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002285
Sean Callanan686ed8d2010-01-19 20:22:31 +00002286 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002287
2288 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002289 break;
2290
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002291 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002292 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002293 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002294 }
2295 }
2296
Sean Callanan686ed8d2010-01-19 20:22:31 +00002297 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002298 return false;
2299}
2300
Jim Grosbach4b905842013-09-20 23:08:21 +00002301/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002302/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002303bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002304 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002305 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002306
Daniel Dunbara10e5192009-06-24 23:30:00 +00002307 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002308 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002309 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002310 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002311 return true;
2312
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002313 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002314 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2315 assert(Size <= 8 && "Invalid size");
2316 uint64_t IntValue = MCE->getValue();
2317 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2318 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002319 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002320 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002321 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002322
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002323 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002324 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002325
Daniel Dunbara10e5192009-06-24 23:30:00 +00002326 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002327 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002328 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002329 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002330 }
2331 }
2332
Sean Callanan686ed8d2010-01-19 20:22:31 +00002333 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002334 return false;
2335}
2336
Jim Grosbach4b905842013-09-20 23:08:21 +00002337/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002338/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002339bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002340 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002341 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002342
2343 for (;;) {
2344 // We don't truly support arithmetic on floating point expressions, so we
2345 // have to manually parse unary prefixes.
2346 bool IsNeg = false;
2347 if (getLexer().is(AsmToken::Minus)) {
2348 Lex();
2349 IsNeg = true;
2350 } else if (getLexer().is(AsmToken::Plus))
2351 Lex();
2352
Michael J. Spencer530ce852010-10-09 11:00:50 +00002353 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002354 getLexer().isNot(AsmToken::Real) &&
2355 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002356 return TokError("unexpected token in directive");
2357
2358 // Convert to an APFloat.
2359 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002360 StringRef IDVal = getTok().getString();
2361 if (getLexer().is(AsmToken::Identifier)) {
2362 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2363 Value = APFloat::getInf(Semantics);
2364 else if (!IDVal.compare_lower("nan"))
2365 Value = APFloat::getNaN(Semantics, false, ~0);
2366 else
2367 return TokError("invalid floating point literal");
2368 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002369 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002370 return TokError("invalid floating point literal");
2371 if (IsNeg)
2372 Value.changeSign();
2373
2374 // Consume the numeric token.
2375 Lex();
2376
2377 // Emit the value as an integer.
2378 APInt AsInt = Value.bitcastToAPInt();
2379 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002380 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002381
2382 if (getLexer().is(AsmToken::EndOfStatement))
2383 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002384
Daniel Dunbar2af16532010-09-24 01:59:56 +00002385 if (getLexer().isNot(AsmToken::Comma))
2386 return TokError("unexpected token in directive");
2387 Lex();
2388 }
2389 }
2390
2391 Lex();
2392 return false;
2393}
2394
Jim Grosbach4b905842013-09-20 23:08:21 +00002395/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002396/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002397bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002398 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002399
2400 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002401 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002402 return true;
2403
Rafael Espindolab91bac62010-10-05 19:42:57 +00002404 int64_t Val = 0;
2405 if (getLexer().is(AsmToken::Comma)) {
2406 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002407 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002408 return true;
2409 }
2410
Rafael Espindola922e3f42010-09-16 15:03:59 +00002411 if (getLexer().isNot(AsmToken::EndOfStatement))
2412 return TokError("unexpected token in '.zero' directive");
2413
2414 Lex();
2415
Rafael Espindola64e1af82013-07-02 15:49:13 +00002416 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002417
2418 return false;
2419}
2420
Jim Grosbach4b905842013-09-20 23:08:21 +00002421/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002422/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002423bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002424 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002425
Daniel Dunbara10e5192009-06-24 23:30:00 +00002426 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002427 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002428 return true;
2429
Roman Divackye33098f2013-09-24 17:44:41 +00002430 int64_t FillSize = 1;
2431 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002432
Roman Divackye33098f2013-09-24 17:44:41 +00002433 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2434 if (getLexer().isNot(AsmToken::Comma))
2435 return TokError("unexpected token in '.fill' directive");
2436 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002437
Roman Divackye33098f2013-09-24 17:44:41 +00002438 if (parseAbsoluteExpression(FillSize))
2439 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002440
Roman Divackye33098f2013-09-24 17:44:41 +00002441 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2442 if (getLexer().isNot(AsmToken::Comma))
2443 return TokError("unexpected token in '.fill' directive");
2444 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002445
Roman Divackye33098f2013-09-24 17:44:41 +00002446 if (parseAbsoluteExpression(FillExpr))
2447 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002448
Roman Divackye33098f2013-09-24 17:44:41 +00002449 if (getLexer().isNot(AsmToken::EndOfStatement))
2450 return TokError("unexpected token in '.fill' directive");
2451
2452 Lex();
2453 }
2454 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002455
Daniel Dunbar8e5edd82009-08-21 15:43:35 +00002456 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2457 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara10e5192009-06-24 23:30:00 +00002458
2459 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002460 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002461
2462 return false;
2463}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002464
Jim Grosbach4b905842013-09-20 23:08:21 +00002465/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002466/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002467bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002468 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002469
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002470 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002471 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002472 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002473 return true;
2474
2475 // Parse optional fill expression.
2476 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002477 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2478 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002479 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002480 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002481
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002482 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002483 return true;
2484
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002485 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002486 return TokError("unexpected token in '.org' directive");
2487 }
2488
Sean Callanan686ed8d2010-01-19 20:22:31 +00002489 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002490
Jim Grosbachb5912772012-01-27 00:37:08 +00002491 // Only limited forms of relocatable expressions are accepted here, it
2492 // has to be relative to the current section. The streamer will return
2493 // 'true' if the expression wasn't evaluatable.
2494 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2495 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002496
2497 return false;
2498}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002499
Jim Grosbach4b905842013-09-20 23:08:21 +00002500/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002501/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002502bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002503 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002504
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002505 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002506 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002507 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002508 return true;
2509
2510 SMLoc MaxBytesLoc;
2511 bool HasFillExpr = false;
2512 int64_t FillExpr = 0;
2513 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002514 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2515 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002516 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002517 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002518
2519 // The fill expression can be omitted while specifying a maximum number of
2520 // alignment bytes, e.g:
2521 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002522 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002523 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002524 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002525 return true;
2526 }
2527
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002528 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2529 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002530 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002531 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002532
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002533 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002534 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002535 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002536
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002537 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002538 return TokError("unexpected token in directive");
2539 }
2540 }
2541
Sean Callanan686ed8d2010-01-19 20:22:31 +00002542 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002543
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002544 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002545 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002546
2547 // Compute alignment in bytes.
2548 if (IsPow2) {
2549 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002550 if (Alignment >= 32) {
2551 Error(AlignmentLoc, "invalid alignment value");
2552 Alignment = 31;
2553 }
2554
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002555 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002556 } else {
2557 // Reject alignments that aren't a power of two, for gas compatibility.
2558 if (!isPowerOf2_64(Alignment))
2559 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002560 }
2561
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002562 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002563 if (MaxBytesLoc.isValid()) {
2564 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002565 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002566 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002567 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002568 }
2569
2570 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002571 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002572 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002573 MaxBytesToFill = 0;
2574 }
2575 }
2576
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002577 // Check whether we should use optimal code alignment for this .align
2578 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002579 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002580 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2581 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002582 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002583 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002584 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002585 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2586 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002587 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002588
2589 return false;
2590}
2591
Jim Grosbach4b905842013-09-20 23:08:21 +00002592/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002593/// ::= .file [number] filename
2594/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002595bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002596 // FIXME: I'm not sure what this is.
2597 int64_t FileNumber = -1;
2598 SMLoc FileNumberLoc = getLexer().getLoc();
2599 if (getLexer().is(AsmToken::Integer)) {
2600 FileNumber = getTok().getIntVal();
2601 Lex();
2602
2603 if (FileNumber < 1)
2604 return TokError("file number less than one");
2605 }
2606
2607 if (getLexer().isNot(AsmToken::String))
2608 return TokError("unexpected token in '.file' directive");
2609
2610 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002611 // Allow the strings to have escaped octal character sequence.
2612 std::string Path = getTok().getString();
2613 if (parseEscapedString(Path))
2614 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002615 Lex();
2616
2617 StringRef Directory;
2618 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002619 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002620 if (getLexer().is(AsmToken::String)) {
2621 if (FileNumber == -1)
2622 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002623 if (parseEscapedString(FilenameData))
2624 return true;
2625 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002626 Directory = Path;
2627 Lex();
2628 } else {
2629 Filename = Path;
2630 }
2631
2632 if (getLexer().isNot(AsmToken::EndOfStatement))
2633 return TokError("unexpected token in '.file' directive");
2634
2635 if (FileNumber == -1)
2636 getStreamer().EmitFileDirective(Filename);
2637 else {
2638 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002639 Error(DirectiveLoc,
2640 "input can't have .file dwarf directives when -g is "
2641 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002642
2643 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2644 Error(FileNumberLoc, "file number already allocated");
2645 }
2646
2647 return false;
2648}
2649
Jim Grosbach4b905842013-09-20 23:08:21 +00002650/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002651/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002652bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002653 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2654 if (getLexer().isNot(AsmToken::Integer))
2655 return TokError("unexpected token in '.line' directive");
2656
2657 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002658 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002659 Lex();
2660
2661 // FIXME: Do something with the .line.
2662 }
2663
2664 if (getLexer().isNot(AsmToken::EndOfStatement))
2665 return TokError("unexpected token in '.line' directive");
2666
2667 return false;
2668}
2669
Jim Grosbach4b905842013-09-20 23:08:21 +00002670/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002671/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2672/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2673/// The first number is a file number, must have been previously assigned with
2674/// a .file directive, the second number is the line number and optionally the
2675/// third number is a column position (zero if not specified). The remaining
2676/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002677bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002678 if (getLexer().isNot(AsmToken::Integer))
2679 return TokError("unexpected token in '.loc' directive");
2680 int64_t FileNumber = getTok().getIntVal();
2681 if (FileNumber < 1)
2682 return TokError("file number less than one in '.loc' directive");
2683 if (!getContext().isValidDwarfFileNumber(FileNumber))
2684 return TokError("unassigned file number in '.loc' directive");
2685 Lex();
2686
2687 int64_t LineNumber = 0;
2688 if (getLexer().is(AsmToken::Integer)) {
2689 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002690 if (LineNumber < 0)
2691 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002692 Lex();
2693 }
2694
2695 int64_t ColumnPos = 0;
2696 if (getLexer().is(AsmToken::Integer)) {
2697 ColumnPos = getTok().getIntVal();
2698 if (ColumnPos < 0)
2699 return TokError("column position less than zero in '.loc' directive");
2700 Lex();
2701 }
2702
2703 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2704 unsigned Isa = 0;
2705 int64_t Discriminator = 0;
2706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2707 for (;;) {
2708 if (getLexer().is(AsmToken::EndOfStatement))
2709 break;
2710
2711 StringRef Name;
2712 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002713 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002714 return TokError("unexpected token in '.loc' directive");
2715
2716 if (Name == "basic_block")
2717 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2718 else if (Name == "prologue_end")
2719 Flags |= DWARF2_FLAG_PROLOGUE_END;
2720 else if (Name == "epilogue_begin")
2721 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2722 else if (Name == "is_stmt") {
2723 Loc = getTok().getLoc();
2724 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002725 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002726 return true;
2727 // The expression must be the constant 0 or 1.
2728 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2729 int Value = MCE->getValue();
2730 if (Value == 0)
2731 Flags &= ~DWARF2_FLAG_IS_STMT;
2732 else if (Value == 1)
2733 Flags |= DWARF2_FLAG_IS_STMT;
2734 else
2735 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002736 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002737 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2738 }
Craig Topperf15655b2013-04-22 04:22:40 +00002739 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002740 Loc = getTok().getLoc();
2741 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002742 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002743 return true;
2744 // The expression must be a constant greater or equal to 0.
2745 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2746 int Value = MCE->getValue();
2747 if (Value < 0)
2748 return Error(Loc, "isa number less than zero");
2749 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002750 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002751 return Error(Loc, "isa number not a constant value");
2752 }
Craig Topperf15655b2013-04-22 04:22:40 +00002753 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002754 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002755 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002756 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002757 return Error(Loc, "unknown sub-directive in '.loc' directive");
2758 }
2759
2760 if (getLexer().is(AsmToken::EndOfStatement))
2761 break;
2762 }
2763 }
2764
2765 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2766 Isa, Discriminator, StringRef());
2767
2768 return false;
2769}
2770
Jim Grosbach4b905842013-09-20 23:08:21 +00002771/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002772/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002773bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002774 return TokError("unsupported directive '.stabs'");
2775}
2776
Jim Grosbach4b905842013-09-20 23:08:21 +00002777/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002778/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002779bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002780 StringRef Name;
2781 bool EH = false;
2782 bool Debug = false;
2783
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002784 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002785 return TokError("Expected an identifier");
2786
2787 if (Name == ".eh_frame")
2788 EH = true;
2789 else if (Name == ".debug_frame")
2790 Debug = true;
2791
2792 if (getLexer().is(AsmToken::Comma)) {
2793 Lex();
2794
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002795 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002796 return TokError("Expected an identifier");
2797
2798 if (Name == ".eh_frame")
2799 EH = true;
2800 else if (Name == ".debug_frame")
2801 Debug = true;
2802 }
2803
2804 getStreamer().EmitCFISections(EH, Debug);
2805 return false;
2806}
2807
Jim Grosbach4b905842013-09-20 23:08:21 +00002808/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002809/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002810bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002811 StringRef Simple;
2812 if (getLexer().isNot(AsmToken::EndOfStatement))
2813 if (parseIdentifier(Simple) || Simple != "simple")
2814 return TokError("unexpected token in .cfi_startproc directive");
2815
2816 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002817 return false;
2818}
2819
Jim Grosbach4b905842013-09-20 23:08:21 +00002820/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002821/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002822bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002823 getStreamer().EmitCFIEndProc();
2824 return false;
2825}
2826
Jim Grosbach4b905842013-09-20 23:08:21 +00002827/// \brief parse register name or number.
2828bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002829 SMLoc DirectiveLoc) {
2830 unsigned RegNo;
2831
2832 if (getLexer().isNot(AsmToken::Integer)) {
2833 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2834 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002835 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002836 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002837 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002838
2839 return false;
2840}
2841
Jim Grosbach4b905842013-09-20 23:08:21 +00002842/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002843/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002844bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002845 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002846 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002847 return true;
2848
2849 if (getLexer().isNot(AsmToken::Comma))
2850 return TokError("unexpected token in directive");
2851 Lex();
2852
2853 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002854 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002855 return true;
2856
2857 getStreamer().EmitCFIDefCfa(Register, Offset);
2858 return false;
2859}
2860
Jim Grosbach4b905842013-09-20 23:08:21 +00002861/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002862/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002863bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002864 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002865 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002866 return true;
2867
2868 getStreamer().EmitCFIDefCfaOffset(Offset);
2869 return false;
2870}
2871
Jim Grosbach4b905842013-09-20 23:08:21 +00002872/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002873/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002874bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002875 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002876 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002877 return true;
2878
2879 if (getLexer().isNot(AsmToken::Comma))
2880 return TokError("unexpected token in directive");
2881 Lex();
2882
2883 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002884 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002885 return true;
2886
2887 getStreamer().EmitCFIRegister(Register1, Register2);
2888 return false;
2889}
2890
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002891/// parseDirectiveCFIWindowSave
2892/// ::= .cfi_window_save
2893bool AsmParser::parseDirectiveCFIWindowSave() {
2894 getStreamer().EmitCFIWindowSave();
2895 return false;
2896}
2897
Jim Grosbach4b905842013-09-20 23:08:21 +00002898/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002899/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002900bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002901 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002902 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002903 return true;
2904
2905 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2906 return false;
2907}
2908
Jim Grosbach4b905842013-09-20 23:08:21 +00002909/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002910/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002911bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002912 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002913 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return true;
2915
2916 getStreamer().EmitCFIDefCfaRegister(Register);
2917 return false;
2918}
2919
Jim Grosbach4b905842013-09-20 23:08:21 +00002920/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002921/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002922bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002923 int64_t Register = 0;
2924 int64_t Offset = 0;
2925
Jim Grosbach4b905842013-09-20 23:08:21 +00002926 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002927 return true;
2928
2929 if (getLexer().isNot(AsmToken::Comma))
2930 return TokError("unexpected token in directive");
2931 Lex();
2932
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002933 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002934 return true;
2935
2936 getStreamer().EmitCFIOffset(Register, Offset);
2937 return false;
2938}
2939
Jim Grosbach4b905842013-09-20 23:08:21 +00002940/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002941/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002942bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002943 int64_t Register = 0;
2944
Jim Grosbach4b905842013-09-20 23:08:21 +00002945 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002946 return true;
2947
2948 if (getLexer().isNot(AsmToken::Comma))
2949 return TokError("unexpected token in directive");
2950 Lex();
2951
2952 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002953 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002954 return true;
2955
2956 getStreamer().EmitCFIRelOffset(Register, Offset);
2957 return false;
2958}
2959
2960static bool isValidEncoding(int64_t Encoding) {
2961 if (Encoding & ~0xff)
2962 return false;
2963
2964 if (Encoding == dwarf::DW_EH_PE_omit)
2965 return true;
2966
2967 const unsigned Format = Encoding & 0xf;
2968 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2969 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2970 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2971 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2972 return false;
2973
2974 const unsigned Application = Encoding & 0x70;
2975 if (Application != dwarf::DW_EH_PE_absptr &&
2976 Application != dwarf::DW_EH_PE_pcrel)
2977 return false;
2978
2979 return true;
2980}
2981
Jim Grosbach4b905842013-09-20 23:08:21 +00002982/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00002983/// IsPersonality true for cfi_personality, false for cfi_lsda
2984/// ::= .cfi_personality encoding, [symbol_name]
2985/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00002986bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00002987 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002988 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00002989 return true;
2990 if (Encoding == dwarf::DW_EH_PE_omit)
2991 return false;
2992
2993 if (!isValidEncoding(Encoding))
2994 return TokError("unsupported encoding.");
2995
2996 if (getLexer().isNot(AsmToken::Comma))
2997 return TokError("unexpected token in directive");
2998 Lex();
2999
3000 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003001 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003002 return TokError("expected identifier in directive");
3003
3004 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3005
3006 if (IsPersonality)
3007 getStreamer().EmitCFIPersonality(Sym, Encoding);
3008 else
3009 getStreamer().EmitCFILsda(Sym, Encoding);
3010 return false;
3011}
3012
Jim Grosbach4b905842013-09-20 23:08:21 +00003013/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003014/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003015bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003016 getStreamer().EmitCFIRememberState();
3017 return false;
3018}
3019
Jim Grosbach4b905842013-09-20 23:08:21 +00003020/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003021/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003022bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003023 getStreamer().EmitCFIRestoreState();
3024 return false;
3025}
3026
Jim Grosbach4b905842013-09-20 23:08:21 +00003027/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003028/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003029bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003030 int64_t Register = 0;
3031
Jim Grosbach4b905842013-09-20 23:08:21 +00003032 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003033 return true;
3034
3035 getStreamer().EmitCFISameValue(Register);
3036 return false;
3037}
3038
Jim Grosbach4b905842013-09-20 23:08:21 +00003039/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003040/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003041bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003042 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003043 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003044 return true;
3045
3046 getStreamer().EmitCFIRestore(Register);
3047 return false;
3048}
3049
Jim Grosbach4b905842013-09-20 23:08:21 +00003050/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003051/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003052bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003053 std::string Values;
3054 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003055 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003056 return true;
3057
3058 Values.push_back((uint8_t)CurrValue);
3059
3060 while (getLexer().is(AsmToken::Comma)) {
3061 Lex();
3062
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003063 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003064 return true;
3065
3066 Values.push_back((uint8_t)CurrValue);
3067 }
3068
3069 getStreamer().EmitCFIEscape(Values);
3070 return false;
3071}
3072
Jim Grosbach4b905842013-09-20 23:08:21 +00003073/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003074/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003075bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003076 if (getLexer().isNot(AsmToken::EndOfStatement))
3077 return Error(getLexer().getLoc(),
3078 "unexpected token in '.cfi_signal_frame'");
3079
3080 getStreamer().EmitCFISignalFrame();
3081 return false;
3082}
3083
Jim Grosbach4b905842013-09-20 23:08:21 +00003084/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003085/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003086bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003087 int64_t Register = 0;
3088
Jim Grosbach4b905842013-09-20 23:08:21 +00003089 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003090 return true;
3091
3092 getStreamer().EmitCFIUndefined(Register);
3093 return false;
3094}
3095
Jim Grosbach4b905842013-09-20 23:08:21 +00003096/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003097/// ::= .macros_on
3098/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003099bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003100 if (getLexer().isNot(AsmToken::EndOfStatement))
3101 return Error(getLexer().getLoc(),
3102 "unexpected token in '" + Directive + "' directive");
3103
Jim Grosbach4b905842013-09-20 23:08:21 +00003104 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003105 return false;
3106}
3107
Jim Grosbach4b905842013-09-20 23:08:21 +00003108/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003109/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003110bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003111 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003112 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003113 return TokError("expected identifier in '.macro' directive");
3114
3115 MCAsmMacroParameters Parameters;
3116 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00003117 // parseMacroArgument()
Eli Bendersky17233942013-01-15 22:59:42 +00003118 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3119 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3120 for (;;) {
3121 MCAsmMacroParameter Parameter;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003122 if (parseIdentifier(Parameter.first))
Eli Bendersky17233942013-01-15 22:59:42 +00003123 return TokError("expected identifier in '.macro' directive");
3124
3125 if (getLexer().is(AsmToken::Equal)) {
3126 Lex();
Jim Grosbach4b905842013-09-20 23:08:21 +00003127 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky17233942013-01-15 22:59:42 +00003128 return true;
3129 }
3130
3131 Parameters.push_back(Parameter);
3132
3133 if (getLexer().is(AsmToken::Comma))
3134 Lex();
3135 else if (getLexer().is(AsmToken::EndOfStatement))
3136 break;
3137 }
3138 }
3139
3140 // Eat the end of statement.
3141 Lex();
3142
3143 AsmToken EndToken, StartToken = getTok();
3144
3145 // Lex the macro definition.
3146 for (;;) {
3147 // Check whether we have reached the end of the file.
3148 if (getLexer().is(AsmToken::Eof))
3149 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3150
3151 // Otherwise, check whether we have reach the .endmacro.
3152 if (getLexer().is(AsmToken::Identifier) &&
3153 (getTok().getIdentifier() == ".endm" ||
3154 getTok().getIdentifier() == ".endmacro")) {
3155 EndToken = getTok();
3156 Lex();
3157 if (getLexer().isNot(AsmToken::EndOfStatement))
3158 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3159 "' directive");
3160 break;
3161 }
3162
3163 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003164 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003165 }
3166
Jim Grosbach4b905842013-09-20 23:08:21 +00003167 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003168 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3169 }
3170
3171 const char *BodyStart = StartToken.getLoc().getPointer();
3172 const char *BodyEnd = EndToken.getLoc().getPointer();
3173 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003174 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3175 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003176 return false;
3177}
3178
Jim Grosbach4b905842013-09-20 23:08:21 +00003179/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003180///
3181/// With the support added for named parameters there may be code out there that
3182/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003183/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003184/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003185/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003186/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3187/// warning that the positional parameter found in body which have no effect.
3188/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003189/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003190/// intended or change the macro to use the named parameters. It is possible
3191/// this warning will trigger when the none of the named parameters are used
3192/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003193void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003194 StringRef Body,
3195 MCAsmMacroParameters Parameters) {
3196 // If this macro is not defined with named parameters the warning we are
3197 // checking for here doesn't apply.
3198 unsigned NParameters = Parameters.size();
3199 if (NParameters == 0)
3200 return;
3201
3202 bool NamedParametersFound = false;
3203 bool PositionalParametersFound = false;
3204
3205 // Look at the body of the macro for use of both the named parameters and what
3206 // are likely to be positional parameters. This is what expandMacro() is
3207 // doing when it finds the parameters in the body.
3208 while (!Body.empty()) {
3209 // Scan for the next possible parameter.
3210 std::size_t End = Body.size(), Pos = 0;
3211 for (; Pos != End; ++Pos) {
3212 // Check for a substitution or escape.
3213 // This macro is defined with parameters, look for \foo, \bar, etc.
3214 if (Body[Pos] == '\\' && Pos + 1 != End)
3215 break;
3216
3217 // This macro should have parameters, but look for $0, $1, ..., $n too.
3218 if (Body[Pos] != '$' || Pos + 1 == End)
3219 continue;
3220 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003221 if (Next == '$' || Next == 'n' ||
3222 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003223 break;
3224 }
3225
3226 // Check if we reached the end.
3227 if (Pos == End)
3228 break;
3229
3230 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003231 switch (Body[Pos + 1]) {
3232 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003233 case '$':
3234 break;
3235
Jim Grosbach4b905842013-09-20 23:08:21 +00003236 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003237 case 'n':
3238 PositionalParametersFound = true;
3239 break;
3240
Jim Grosbach4b905842013-09-20 23:08:21 +00003241 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003242 default: {
3243 PositionalParametersFound = true;
3244 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003245 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003246 }
3247 Pos += 2;
3248 } else {
3249 unsigned I = Pos + 1;
3250 while (isIdentifierChar(Body[I]) && I + 1 != End)
3251 ++I;
3252
Jim Grosbach4b905842013-09-20 23:08:21 +00003253 const char *Begin = Body.data() + Pos + 1;
3254 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003255 unsigned Index = 0;
3256 for (; Index < NParameters; ++Index)
3257 if (Parameters[Index].first == Argument)
3258 break;
3259
3260 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003261 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3262 Pos += 3;
3263 else {
3264 Pos = I;
3265 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003266 } else {
3267 NamedParametersFound = true;
3268 Pos += 1 + Argument.size();
3269 }
3270 }
3271 // Update the scan point.
3272 Body = Body.substr(Pos);
3273 }
3274
3275 if (!NamedParametersFound && PositionalParametersFound)
3276 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3277 "used in macro body, possible positional parameter "
3278 "found in body which will have no effect");
3279}
3280
Jim Grosbach4b905842013-09-20 23:08:21 +00003281/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003282/// ::= .endm
3283/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003284bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003285 if (getLexer().isNot(AsmToken::EndOfStatement))
3286 return TokError("unexpected token in '" + Directive + "' directive");
3287
3288 // If we are inside a macro instantiation, terminate the current
3289 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003290 if (isInsideMacroInstantiation()) {
3291 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003292 return false;
3293 }
3294
3295 // Otherwise, this .endmacro is a stray entry in the file; well formed
3296 // .endmacro directives are handled during the macro definition parsing.
3297 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003298 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003299}
3300
Jim Grosbach4b905842013-09-20 23:08:21 +00003301/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003302/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003303bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003304 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003305 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003306 return TokError("expected identifier in '.purgem' directive");
3307
3308 if (getLexer().isNot(AsmToken::EndOfStatement))
3309 return TokError("unexpected token in '.purgem' directive");
3310
Jim Grosbach4b905842013-09-20 23:08:21 +00003311 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003312 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3313
Jim Grosbach4b905842013-09-20 23:08:21 +00003314 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003315 return false;
3316}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003317
Jim Grosbach4b905842013-09-20 23:08:21 +00003318/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003319/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003320bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003321 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003322
3323 // Expect a single argument: an expression that evaluates to a constant
3324 // in the inclusive range 0-30.
3325 SMLoc ExprLoc = getLexer().getLoc();
3326 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003327 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003328 return true;
3329 else if (getLexer().isNot(AsmToken::EndOfStatement))
3330 return TokError("unexpected token after expression in"
3331 " '.bundle_align_mode' directive");
3332 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3333 return Error(ExprLoc,
3334 "invalid bundle alignment size (expected between 0 and 30)");
3335
3336 Lex();
3337
3338 // Because of AlignSizePow2's verified range we can safely truncate it to
3339 // unsigned.
3340 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3341 return false;
3342}
3343
Jim Grosbach4b905842013-09-20 23:08:21 +00003344/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003345/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003346bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003347 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003348 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003349
Eli Bendersky802b6282013-01-07 21:51:08 +00003350 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3351 StringRef Option;
3352 SMLoc Loc = getTok().getLoc();
3353 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003354 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003355
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003356 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003357 return Error(Loc, kInvalidOptionError);
3358
3359 if (Option != "align_to_end")
3360 return Error(Loc, kInvalidOptionError);
3361 else if (getLexer().isNot(AsmToken::EndOfStatement))
3362 return Error(Loc,
3363 "unexpected token after '.bundle_lock' directive option");
3364 AlignToEnd = true;
3365 }
3366
Eli Benderskyf483ff92012-12-20 19:05:53 +00003367 Lex();
3368
Eli Bendersky802b6282013-01-07 21:51:08 +00003369 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003370 return false;
3371}
3372
Jim Grosbach4b905842013-09-20 23:08:21 +00003373/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003374/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003375bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003376 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003377
3378 if (getLexer().isNot(AsmToken::EndOfStatement))
3379 return TokError("unexpected token in '.bundle_unlock' directive");
3380 Lex();
3381
3382 getStreamer().EmitBundleUnlock();
3383 return false;
3384}
3385
Jim Grosbach4b905842013-09-20 23:08:21 +00003386/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003387/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003388bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003389 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003390
3391 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003392 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003393 return true;
3394
3395 int64_t FillExpr = 0;
3396 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3397 if (getLexer().isNot(AsmToken::Comma))
3398 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3399 Lex();
3400
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003401 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003402 return true;
3403
3404 if (getLexer().isNot(AsmToken::EndOfStatement))
3405 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3406 }
3407
3408 Lex();
3409
3410 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003411 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3412 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003413
3414 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003415 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003416
3417 return false;
3418}
3419
Jim Grosbach4b905842013-09-20 23:08:21 +00003420/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003421/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003422bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003423 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003424 const MCExpr *Value;
3425
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003426 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003427 return true;
3428
3429 if (getLexer().isNot(AsmToken::EndOfStatement))
3430 return TokError("unexpected token in directive");
3431
3432 if (Signed)
3433 getStreamer().EmitSLEB128Value(Value);
3434 else
3435 getStreamer().EmitULEB128Value(Value);
3436
3437 return false;
3438}
3439
Jim Grosbach4b905842013-09-20 23:08:21 +00003440/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003441/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003442bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003443 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003444 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003445 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003446 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003447
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003448 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003449 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003450
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003451 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003452
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003453 // Assembler local symbols don't make any sense here. Complain loudly.
3454 if (Sym->isTemporary())
3455 return Error(Loc, "non-local symbol required in directive");
3456
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003457 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3458 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003459
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003460 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003461 break;
3462
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003463 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003464 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003465 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003466 }
3467 }
3468
Sean Callanan686ed8d2010-01-19 20:22:31 +00003469 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003470 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003471}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003472
Jim Grosbach4b905842013-09-20 23:08:21 +00003473/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003474/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003475bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003476 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003477
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003478 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003479 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003480 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003481 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003482
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003483 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003484 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003485
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003486 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003487 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003488 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003489
3490 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003491 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003492 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003493 return true;
3494
3495 int64_t Pow2Alignment = 0;
3496 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003497 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003498 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003499 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003500 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003501 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003502
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003503 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3504 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003505 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3506
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003507 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003508 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3509 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003510 if (!isPowerOf2_64(Pow2Alignment))
3511 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3512 Pow2Alignment = Log2_64(Pow2Alignment);
3513 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003514 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003515
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003516 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003517 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003518
Sean Callanan686ed8d2010-01-19 20:22:31 +00003519 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003520
Chris Lattner28ad7542009-07-09 17:25:12 +00003521 // NOTE: a size of zero for a .comm should create a undefined symbol
3522 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003523 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003524 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003525 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003526
Eric Christopherbc818852010-05-14 01:38:54 +00003527 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003528 // may internally end up wanting an alignment in bytes.
3529 // FIXME: Diagnose overflow.
3530 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003531 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003532 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003533
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003534 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003535 return Error(IDLoc, "invalid symbol redefinition");
3536
Chris Lattner28ad7542009-07-09 17:25:12 +00003537 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003538 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003539 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003540 return false;
3541 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003542
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003543 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003544 return false;
3545}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003546
Jim Grosbach4b905842013-09-20 23:08:21 +00003547/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003548/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003549bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003550 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003551 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003552
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003553 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003554 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003555 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003556
Sean Callanan686ed8d2010-01-19 20:22:31 +00003557 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003558
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003559 if (Str.empty())
3560 Error(Loc, ".abort detected. Assembly stopping.");
3561 else
3562 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003563 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003564
3565 return false;
3566}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003567
Jim Grosbach4b905842013-09-20 23:08:21 +00003568/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003569/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003570bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003571 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003572 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003573
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003574 // Allow the strings to have escaped octal character sequence.
3575 std::string Filename;
3576 if (parseEscapedString(Filename))
3577 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003578 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003579 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003580
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003581 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003582 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003583
Chris Lattner693fbb82009-07-16 06:14:39 +00003584 // Attempt to switch the lexer to the included file before consuming the end
3585 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003586 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003587 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003588 return true;
3589 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003590
3591 return false;
3592}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003593
Jim Grosbach4b905842013-09-20 23:08:21 +00003594/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003595/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003596bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003597 if (getLexer().isNot(AsmToken::String))
3598 return TokError("expected string in '.incbin' directive");
3599
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003600 // Allow the strings to have escaped octal character sequence.
3601 std::string Filename;
3602 if (parseEscapedString(Filename))
3603 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003604 SMLoc IncbinLoc = getLexer().getLoc();
3605 Lex();
3606
3607 if (getLexer().isNot(AsmToken::EndOfStatement))
3608 return TokError("unexpected token in '.incbin' directive");
3609
Kevin Enderby109f25c2011-12-14 21:47:48 +00003610 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003611 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003612 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3613 return true;
3614 }
3615
3616 return false;
3617}
3618
Jim Grosbach4b905842013-09-20 23:08:21 +00003619/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003620/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003621bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003622 TheCondStack.push_back(TheCondState);
3623 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003624 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003625 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003626 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003627 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003628 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003629 return true;
3630
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003631 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003632 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003633
Sean Callanan686ed8d2010-01-19 20:22:31 +00003634 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003635
3636 TheCondState.CondMet = ExprValue;
3637 TheCondState.Ignore = !TheCondState.CondMet;
3638 }
3639
3640 return false;
3641}
3642
Jim Grosbach4b905842013-09-20 23:08:21 +00003643/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003644/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003645bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003646 TheCondStack.push_back(TheCondState);
3647 TheCondState.TheCond = AsmCond::IfCond;
3648
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003649 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003650 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003651 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003652 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003653
3654 if (getLexer().isNot(AsmToken::EndOfStatement))
3655 return TokError("unexpected token in '.ifb' directive");
3656
3657 Lex();
3658
3659 TheCondState.CondMet = ExpectBlank == Str.empty();
3660 TheCondState.Ignore = !TheCondState.CondMet;
3661 }
3662
3663 return false;
3664}
3665
Jim Grosbach4b905842013-09-20 23:08:21 +00003666/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003667/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003668bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003669 TheCondStack.push_back(TheCondState);
3670 TheCondState.TheCond = AsmCond::IfCond;
3671
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003672 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003673 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003674 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003675 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003676
3677 if (getLexer().isNot(AsmToken::Comma))
3678 return TokError("unexpected token in '.ifc' directive");
3679
3680 Lex();
3681
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003682 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003683
3684 if (getLexer().isNot(AsmToken::EndOfStatement))
3685 return TokError("unexpected token in '.ifc' directive");
3686
3687 Lex();
3688
3689 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3690 TheCondState.Ignore = !TheCondState.CondMet;
3691 }
3692
3693 return false;
3694}
3695
Jim Grosbach4b905842013-09-20 23:08:21 +00003696/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003697/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003698bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003699 StringRef Name;
3700 TheCondStack.push_back(TheCondState);
3701 TheCondState.TheCond = AsmCond::IfCond;
3702
3703 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003704 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003705 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003706 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003707 return TokError("expected identifier after '.ifdef'");
3708
3709 Lex();
3710
3711 MCSymbol *Sym = getContext().LookupSymbol(Name);
3712
3713 if (expect_defined)
3714 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3715 else
3716 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3717 TheCondState.Ignore = !TheCondState.CondMet;
3718 }
3719
3720 return false;
3721}
3722
Jim Grosbach4b905842013-09-20 23:08:21 +00003723/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003724/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003725bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003726 if (TheCondState.TheCond != AsmCond::IfCond &&
3727 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003728 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3729 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003730 TheCondState.TheCond = AsmCond::ElseIfCond;
3731
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003732 bool LastIgnoreState = false;
3733 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003734 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003735 if (LastIgnoreState || TheCondState.CondMet) {
3736 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003737 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003738 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003739 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003740 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003741 return true;
3742
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003743 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003744 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003745
Sean Callanan686ed8d2010-01-19 20:22:31 +00003746 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003747 TheCondState.CondMet = ExprValue;
3748 TheCondState.Ignore = !TheCondState.CondMet;
3749 }
3750
3751 return false;
3752}
3753
Jim Grosbach4b905842013-09-20 23:08:21 +00003754/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003755/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003756bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003757 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003758 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003759
Sean Callanan686ed8d2010-01-19 20:22:31 +00003760 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003761
3762 if (TheCondState.TheCond != AsmCond::IfCond &&
3763 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003764 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3765 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003766 TheCondState.TheCond = AsmCond::ElseCond;
3767 bool LastIgnoreState = false;
3768 if (!TheCondStack.empty())
3769 LastIgnoreState = TheCondStack.back().Ignore;
3770 if (LastIgnoreState || TheCondState.CondMet)
3771 TheCondState.Ignore = true;
3772 else
3773 TheCondState.Ignore = false;
3774
3775 return false;
3776}
3777
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003778/// parseDirectiveEnd
3779/// ::= .end
3780bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3781 if (getLexer().isNot(AsmToken::EndOfStatement))
3782 return TokError("unexpected token in '.end' directive");
3783
3784 Lex();
3785
3786 while (Lexer.isNot(AsmToken::Eof))
3787 Lex();
3788
3789 return false;
3790}
3791
Jim Grosbach4b905842013-09-20 23:08:21 +00003792/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003793/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003794bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003795 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003796 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003797
Sean Callanan686ed8d2010-01-19 20:22:31 +00003798 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003799
Jim Grosbach4b905842013-09-20 23:08:21 +00003800 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003801 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3802 ".else");
3803 if (!TheCondStack.empty()) {
3804 TheCondState = TheCondStack.back();
3805 TheCondStack.pop_back();
3806 }
3807
3808 return false;
3809}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003810
Eli Bendersky17233942013-01-15 22:59:42 +00003811void AsmParser::initializeDirectiveKindMap() {
3812 DirectiveKindMap[".set"] = DK_SET;
3813 DirectiveKindMap[".equ"] = DK_EQU;
3814 DirectiveKindMap[".equiv"] = DK_EQUIV;
3815 DirectiveKindMap[".ascii"] = DK_ASCII;
3816 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3817 DirectiveKindMap[".string"] = DK_STRING;
3818 DirectiveKindMap[".byte"] = DK_BYTE;
3819 DirectiveKindMap[".short"] = DK_SHORT;
3820 DirectiveKindMap[".value"] = DK_VALUE;
3821 DirectiveKindMap[".2byte"] = DK_2BYTE;
3822 DirectiveKindMap[".long"] = DK_LONG;
3823 DirectiveKindMap[".int"] = DK_INT;
3824 DirectiveKindMap[".4byte"] = DK_4BYTE;
3825 DirectiveKindMap[".quad"] = DK_QUAD;
3826 DirectiveKindMap[".8byte"] = DK_8BYTE;
3827 DirectiveKindMap[".single"] = DK_SINGLE;
3828 DirectiveKindMap[".float"] = DK_FLOAT;
3829 DirectiveKindMap[".double"] = DK_DOUBLE;
3830 DirectiveKindMap[".align"] = DK_ALIGN;
3831 DirectiveKindMap[".align32"] = DK_ALIGN32;
3832 DirectiveKindMap[".balign"] = DK_BALIGN;
3833 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3834 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3835 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3836 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3837 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3838 DirectiveKindMap[".org"] = DK_ORG;
3839 DirectiveKindMap[".fill"] = DK_FILL;
3840 DirectiveKindMap[".zero"] = DK_ZERO;
3841 DirectiveKindMap[".extern"] = DK_EXTERN;
3842 DirectiveKindMap[".globl"] = DK_GLOBL;
3843 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003844 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3845 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3846 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3847 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3848 DirectiveKindMap[".reference"] = DK_REFERENCE;
3849 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3850 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3851 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3852 DirectiveKindMap[".comm"] = DK_COMM;
3853 DirectiveKindMap[".common"] = DK_COMMON;
3854 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3855 DirectiveKindMap[".abort"] = DK_ABORT;
3856 DirectiveKindMap[".include"] = DK_INCLUDE;
3857 DirectiveKindMap[".incbin"] = DK_INCBIN;
3858 DirectiveKindMap[".code16"] = DK_CODE16;
3859 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3860 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003861 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003862 DirectiveKindMap[".irp"] = DK_IRP;
3863 DirectiveKindMap[".irpc"] = DK_IRPC;
3864 DirectiveKindMap[".endr"] = DK_ENDR;
3865 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3866 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3867 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3868 DirectiveKindMap[".if"] = DK_IF;
3869 DirectiveKindMap[".ifb"] = DK_IFB;
3870 DirectiveKindMap[".ifnb"] = DK_IFNB;
3871 DirectiveKindMap[".ifc"] = DK_IFC;
3872 DirectiveKindMap[".ifnc"] = DK_IFNC;
3873 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3874 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3875 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3876 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3877 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003878 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003879 DirectiveKindMap[".endif"] = DK_ENDIF;
3880 DirectiveKindMap[".skip"] = DK_SKIP;
3881 DirectiveKindMap[".space"] = DK_SPACE;
3882 DirectiveKindMap[".file"] = DK_FILE;
3883 DirectiveKindMap[".line"] = DK_LINE;
3884 DirectiveKindMap[".loc"] = DK_LOC;
3885 DirectiveKindMap[".stabs"] = DK_STABS;
3886 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3887 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3888 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3889 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3890 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3891 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3892 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3893 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3894 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3895 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3896 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3897 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3898 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3899 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3900 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3901 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3902 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3903 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3904 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3905 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3906 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003907 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003908 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3909 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3910 DirectiveKindMap[".macro"] = DK_MACRO;
3911 DirectiveKindMap[".endm"] = DK_ENDM;
3912 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3913 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003914}
3915
Jim Grosbach4b905842013-09-20 23:08:21 +00003916MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003917 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003918
Rafael Espindola34b9c512012-06-03 23:57:14 +00003919 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003920 for (;;) {
3921 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003922 if (getLexer().is(AsmToken::Eof)) {
3923 Error(DirectiveLoc, "no matching '.endr' in definition");
3924 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003925 }
3926
Rafael Espindola34b9c512012-06-03 23:57:14 +00003927 if (Lexer.is(AsmToken::Identifier) &&
3928 (getTok().getIdentifier() == ".rept")) {
3929 ++NestLevel;
3930 }
3931
3932 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003933 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003934 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003935 EndToken = getTok();
3936 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003937 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3938 TokError("unexpected token in '.endr' directive");
3939 return 0;
3940 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003941 break;
3942 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003943 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003944 }
3945
Rafael Espindola34b9c512012-06-03 23:57:14 +00003946 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003947 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003948 }
3949
3950 const char *BodyStart = StartToken.getLoc().getPointer();
3951 const char *BodyEnd = EndToken.getLoc().getPointer();
3952 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3953
Rafael Espindola34b9c512012-06-03 23:57:14 +00003954 // We Are Anonymous.
3955 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00003956 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00003957 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3958 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003959}
3960
Jim Grosbach4b905842013-09-20 23:08:21 +00003961void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00003962 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003963 OS << ".endr\n";
3964
3965 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00003966 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003967
Rafael Espindola34b9c512012-06-03 23:57:14 +00003968 // Create the macro instantiation object and add to the current macro
3969 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00003970 MacroInstantiation *MI = new MacroInstantiation(
3971 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003972 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003973
Rafael Espindola34b9c512012-06-03 23:57:14 +00003974 // Jump to the macro instantiation and prime the lexer.
3975 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3976 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3977 Lex();
3978}
3979
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003980/// parseDirectiveRept
3981/// ::= .rep | .rept count
3982bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003983 const MCExpr *CountExpr;
3984 SMLoc CountLoc = getTok().getLoc();
3985 if (parseExpression(CountExpr))
3986 return true;
3987
Rafael Espindola34b9c512012-06-03 23:57:14 +00003988 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003989 if (!CountExpr->EvaluateAsAbsolute(Count)) {
3990 eatToEndOfStatement();
3991 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
3992 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003993
3994 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003995 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003996
3997 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003998 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003999
4000 // Eat the end of statement.
4001 Lex();
4002
4003 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004004 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004005 if (!M)
4006 return true;
4007
4008 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4009 // to hold the macro body with substitutions.
4010 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00004011 MCAsmMacroParameters Parameters;
4012 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004013 raw_svector_ostream OS(Buf);
4014 while (Count--) {
4015 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
4016 return true;
4017 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004018 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004019
4020 return false;
4021}
4022
Jim Grosbach4b905842013-09-20 23:08:21 +00004023/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004024/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004025bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004026 MCAsmMacroParameters Parameters;
4027 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004028
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004029 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004030 return TokError("expected identifier in '.irp' directive");
4031
4032 Parameters.push_back(Parameter);
4033
4034 if (Lexer.isNot(AsmToken::Comma))
4035 return TokError("expected comma in '.irp' directive");
4036
4037 Lex();
4038
Eli Bendersky38274122013-01-14 23:22:36 +00004039 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004040 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004041 return true;
4042
4043 // Eat the end of statement.
4044 Lex();
4045
4046 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004047 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004048 if (!M)
4049 return true;
4050
4051 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4052 // to hold the macro body with substitutions.
4053 SmallString<256> Buf;
4054 raw_svector_ostream OS(Buf);
4055
Eli Bendersky38274122013-01-14 23:22:36 +00004056 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4057 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004058 Args.push_back(*i);
4059
4060 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4061 return true;
4062 }
4063
Jim Grosbach4b905842013-09-20 23:08:21 +00004064 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004065
4066 return false;
4067}
4068
Jim Grosbach4b905842013-09-20 23:08:21 +00004069/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004070/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004071bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004072 MCAsmMacroParameters Parameters;
4073 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004074
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004075 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004076 return TokError("expected identifier in '.irpc' directive");
4077
4078 Parameters.push_back(Parameter);
4079
4080 if (Lexer.isNot(AsmToken::Comma))
4081 return TokError("expected comma in '.irpc' directive");
4082
4083 Lex();
4084
Eli Bendersky38274122013-01-14 23:22:36 +00004085 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004086 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004087 return true;
4088
4089 if (A.size() != 1 || A.front().size() != 1)
4090 return TokError("unexpected token in '.irpc' directive");
4091
4092 // Eat the end of statement.
4093 Lex();
4094
4095 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004096 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004097 if (!M)
4098 return true;
4099
4100 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4101 // to hold the macro body with substitutions.
4102 SmallString<256> Buf;
4103 raw_svector_ostream OS(Buf);
4104
4105 StringRef Values = A.front().front().getString();
4106 std::size_t I, End = Values.size();
4107 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004108 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004109 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004110
Eli Bendersky38274122013-01-14 23:22:36 +00004111 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004112 Args.push_back(Arg);
4113
4114 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4115 return true;
4116 }
4117
Jim Grosbach4b905842013-09-20 23:08:21 +00004118 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004119
4120 return false;
4121}
4122
Jim Grosbach4b905842013-09-20 23:08:21 +00004123bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004124 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004125 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004126
4127 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004128 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004129 assert(getLexer().is(AsmToken::EndOfStatement));
4130
Jim Grosbach4b905842013-09-20 23:08:21 +00004131 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004132 return false;
4133}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004134
Jim Grosbach4b905842013-09-20 23:08:21 +00004135bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004136 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004137 const MCExpr *Value;
4138 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004139 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004140 return true;
4141 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4142 if (!MCE)
4143 return Error(ExprLoc, "unexpected expression in _emit");
4144 uint64_t IntValue = MCE->getValue();
4145 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4146 return Error(ExprLoc, "literal value out of range for directive");
4147
Chad Rosierc7f552c2013-02-12 21:33:51 +00004148 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4149 return false;
4150}
4151
Jim Grosbach4b905842013-09-20 23:08:21 +00004152bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004153 const MCExpr *Value;
4154 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004155 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004156 return true;
4157 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4158 if (!MCE)
4159 return Error(ExprLoc, "unexpected expression in align");
4160 uint64_t IntValue = MCE->getValue();
4161 if (!isPowerOf2_64(IntValue))
4162 return Error(ExprLoc, "literal value not a power of two greater then zero");
4163
Jim Grosbach4b905842013-09-20 23:08:21 +00004164 Info.AsmRewrites->push_back(
4165 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004166 return false;
4167}
4168
Chad Rosierf43fcf52013-02-13 21:27:17 +00004169// We are comparing pointers, but the pointers are relative to a single string.
4170// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004171static int rewritesSort(const AsmRewrite *AsmRewriteA,
4172 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004173 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4174 return -1;
4175 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4176 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004177
Chad Rosierfce4fab2013-04-08 17:43:47 +00004178 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4179 // rewrite to the same location. Make sure the SizeDirective rewrite is
4180 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4181 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004182 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4183 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004184 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004185
Jim Grosbach4b905842013-09-20 23:08:21 +00004186 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4187 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004188 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004189 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004190}
4191
Jim Grosbach4b905842013-09-20 23:08:21 +00004192bool AsmParser::parseMSInlineAsm(
4193 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4194 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4195 SmallVectorImpl<std::string> &Constraints,
4196 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4197 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004198 SmallVector<void *, 4> InputDecls;
4199 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004200 SmallVector<bool, 4> InputDeclsAddressOf;
4201 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004202 SmallVector<std::string, 4> InputConstraints;
4203 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004204 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004205
Benjamin Kramer1a136112013-02-15 20:37:21 +00004206 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004207
4208 // Prime the lexer.
4209 Lex();
4210
4211 // While we have input, parse each statement.
4212 unsigned InputIdx = 0;
4213 unsigned OutputIdx = 0;
4214 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004215 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004216 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004217 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004218
Chad Rosier149e8e02012-12-12 22:45:52 +00004219 if (Info.ParseError)
4220 return true;
4221
Benjamin Kramer1a136112013-02-15 20:37:21 +00004222 if (Info.Opcode == ~0U)
4223 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004224
Benjamin Kramer1a136112013-02-15 20:37:21 +00004225 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004226
Benjamin Kramer1a136112013-02-15 20:37:21 +00004227 // Build the list of clobbers, outputs and inputs.
4228 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4229 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004230
Benjamin Kramer1a136112013-02-15 20:37:21 +00004231 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004232 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004233 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004234
Benjamin Kramer1a136112013-02-15 20:37:21 +00004235 // Register operand.
4236 if (Operand->isReg() && !Operand->needAddressOf()) {
4237 unsigned NumDefs = Desc.getNumDefs();
4238 // Clobber.
4239 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4240 ClobberRegs.push_back(Operand->getReg());
4241 continue;
4242 }
4243
4244 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004245 StringRef SymName = Operand->getSymName();
4246 if (SymName.empty())
4247 continue;
4248
Chad Rosierdba3fe52013-04-22 22:12:12 +00004249 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004250 if (!OpDecl)
4251 continue;
4252
4253 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004254 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004255 if (isOutput) {
4256 ++InputIdx;
4257 OutputDecls.push_back(OpDecl);
4258 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4259 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004260 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004261 } else {
4262 InputDecls.push_back(OpDecl);
4263 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4264 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004265 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004266 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004267 }
Reid Kleckneree088972013-12-10 18:27:32 +00004268
4269 // Consider implicit defs to be clobbers. Think of cpuid and push.
4270 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4271 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4272 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004273 }
4274
4275 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004276 NumOutputs = OutputDecls.size();
4277 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004278
4279 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004280 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4281 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4282 ClobberRegs.end());
4283 Clobbers.assign(ClobberRegs.size(), std::string());
4284 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4285 raw_string_ostream OS(Clobbers[I]);
4286 IP->printRegName(OS, ClobberRegs[I]);
4287 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004288
4289 // Merge the various outputs and inputs. Output are expected first.
4290 if (NumOutputs || NumInputs) {
4291 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004292 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004293 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004294 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004295 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004296 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004297 }
4298 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004299 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004300 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004301 }
4302 }
4303
4304 // Build the IR assembly string.
4305 std::string AsmStringIR;
4306 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004307 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4308 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004309 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004310 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4311 E = AsmStrRewrites.end();
4312 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004313 AsmRewriteKind Kind = (*I).Kind;
4314 if (Kind == AOK_Delete)
4315 continue;
4316
Chad Rosier8bce6642012-10-18 15:49:34 +00004317 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004318 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004319
Chad Rosier120eefd2013-03-19 17:32:17 +00004320 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004321 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004322 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004323 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004324
Chad Rosier37e755c2012-10-23 17:43:43 +00004325 // Skip the original expression.
4326 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004327 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004328 continue;
4329 }
4330
Chad Rosierff10ed12013-04-12 16:26:42 +00004331 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004332 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004333 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004334 default:
4335 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004336 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004337 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004338 break;
4339 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004340 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004341 break;
4342 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004343 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004344 break;
4345 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004346 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004347 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004348 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004349 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004350 default: break;
4351 case 8: OS << "byte ptr "; break;
4352 case 16: OS << "word ptr "; break;
4353 case 32: OS << "dword ptr "; break;
4354 case 64: OS << "qword ptr "; break;
4355 case 80: OS << "xword ptr "; break;
4356 case 128: OS << "xmmword ptr "; break;
4357 case 256: OS << "ymmword ptr "; break;
4358 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004359 break;
4360 case AOK_Emit:
4361 OS << ".byte";
4362 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004363 case AOK_Align: {
4364 unsigned Val = (*I).Val;
4365 OS << ".align " << Val;
4366
4367 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004368 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004369 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4370 break;
4371 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004372 case AOK_DotOperator:
4373 OS << (*I).Val;
4374 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004375 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004376
Chad Rosier8bce6642012-10-18 15:49:34 +00004377 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004378 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004379 }
4380
4381 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004382 if (AsmStart != AsmEnd)
4383 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004384
4385 AsmString = OS.str();
4386 return false;
4387}
4388
Daniel Dunbar01e36072010-07-17 02:26:10 +00004389/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004390MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4391 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004392 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004393}