blob: 451ae7ae4072d1d6925e57b7629f745f47ffeabc [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"
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000016#include "llvm/ADT/STLExtras.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");
Eli Benderskycbb25142013-01-14 19:04:57 +0000701 Out.InitToTextSection();
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;
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000839 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000840 }
841 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000842
Hans Wennborgce69d772013-10-18 20:46:28 +0000843 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
844
Daniel Dunbard20cda02009-10-16 01:34:54 +0000845 // If this is an absolute variable reference, substitute it now to preserve
846 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000847 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000848 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000849 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000850
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000851 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000852 return false;
853 }
854
855 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000856 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000857 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000858 }
Kevin Enderby0510b482010-05-17 23:08:19 +0000859 case AsmToken::Integer: {
860 SMLoc Loc = getTok().getLoc();
861 int64_t IntVal = getTok().getIntVal();
862 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000863 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000864 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000865 // Look for 'b' or 'f' following an Integer as a directional label
866 if (Lexer.getKind() == AsmToken::Identifier) {
867 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000868 // Lookup the symbol variant if used.
869 std::pair<StringRef, StringRef> Split = IDVal.split('@');
870 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
871 if (Split.first.size() != IDVal.size()) {
872 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
873 if (Variant == MCSymbolRefExpr::VK_Invalid) {
874 Variant = MCSymbolRefExpr::VK_None;
875 return TokError("invalid variant '" + Split.second + "'");
876 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000877 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000878 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000879 if (IDVal == "f" || IDVal == "b") {
880 MCSymbol *Sym =
881 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000882 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000883 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000885 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000886 Lex(); // Eat identifier.
887 }
888 }
Chris Lattner78db3622009-06-22 05:51:26 +0000889 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000890 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000891 case AsmToken::Real: {
892 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000893 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000894 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000895 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000896 Lex(); // Eat token.
897 return false;
898 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000899 case AsmToken::Dot: {
900 // This is a '.' reference, which references the current PC. Emit a
901 // temporary label to the streamer and refer to it.
902 MCSymbol *Sym = Ctx.CreateTempSymbol();
903 Out.EmitLabel(Sym);
904 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000905 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000906 Lex(); // Eat identifier.
907 return false;
908 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000909 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000910 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000911 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000912 case AsmToken::LBrac:
913 if (!PlatformParser->HasBracketExpressions())
914 return TokError("brackets expression not supported on this target");
915 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000916 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000917 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000918 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000919 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000921 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000922 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000923 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000924 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000925 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000927 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000928 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000929 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000930 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000931 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000933 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000934 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000935 }
936}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000937
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000938bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000939 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000940 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000941}
942
Daniel Dunbar55f16672010-09-17 02:47:07 +0000943const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000944AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000945 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000946 // Ask the target implementation about this expression first.
947 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
948 if (NewE)
949 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000950 // Recurse over the given expression, rebuilding it to apply the given variant
951 // if there is exactly one symbol.
952 switch (E->getKind()) {
953 case MCExpr::Target:
954 case MCExpr::Constant:
955 return 0;
956
957 case MCExpr::SymbolRef: {
958 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
959
960 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000961 TokError("invalid variant on expression '" + getTok().getIdentifier() +
962 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000963 return E;
964 }
965
966 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
967 }
968
969 case MCExpr::Unary: {
970 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000971 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000972 if (!Sub)
973 return 0;
974 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
975 }
976
977 case MCExpr::Binary: {
978 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000979 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
980 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981
982 if (!LHS && !RHS)
983 return 0;
984
Jim Grosbach4b905842013-09-20 23:08:21 +0000985 if (!LHS)
986 LHS = BE->getLHS();
987 if (!RHS)
988 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000989
990 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
991 }
992 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000993
Craig Toppera2886c22012-02-07 05:05:23 +0000994 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000995}
996
Jim Grosbach4b905842013-09-20 23:08:21 +0000997/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000998///
Jim Grosbachbd164242011-08-20 16:24:13 +0000999/// expr ::= expr &&,|| expr -> lowest.
1000/// expr ::= expr |,^,&,! expr
1001/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1002/// expr ::= expr <<,>> expr
1003/// expr ::= expr +,- expr
1004/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001005/// expr ::= primaryexpr
1006///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001007bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001008 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001009 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001010 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001011 return true;
1012
Daniel Dunbar55f16672010-09-17 02:47:07 +00001013 // As a special case, we support 'a op b @ modifier' by rewriting the
1014 // expression to include the modifier. This is inefficient, but in general we
1015 // expect users to use 'a@modifier op b'.
1016 if (Lexer.getKind() == AsmToken::At) {
1017 Lex();
1018
1019 if (Lexer.isNot(AsmToken::Identifier))
1020 return TokError("unexpected symbol modifier following '@'");
1021
1022 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001023 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001024 if (Variant == MCSymbolRefExpr::VK_Invalid)
1025 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1026
Jim Grosbach4b905842013-09-20 23:08:21 +00001027 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001028 if (!ModifiedRes) {
1029 return TokError("invalid modifier '" + getTok().getIdentifier() +
1030 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001032
Daniel Dunbar55f16672010-09-17 02:47:07 +00001033 Res = ModifiedRes;
1034 Lex();
1035 }
1036
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001037 // Try to constant fold it up front, if possible.
1038 int64_t Value;
1039 if (Res->EvaluateAsAbsolute(Value))
1040 Res = MCConstantExpr::Create(Value, getContext());
1041
1042 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001043}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001044
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001045bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001046 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001047 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001048}
1049
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001050bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001051 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001052
Daniel Dunbar75630b32009-06-30 02:10:03 +00001053 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001054 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001055 return true;
1056
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001057 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001058 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001059
1060 return false;
1061}
1062
Michael J. Spencer530ce852010-10-09 11:00:50 +00001063static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001064 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001065 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001066 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001067 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001068
Jim Grosbach4b905842013-09-20 23:08:21 +00001069 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001070 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001071 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001072 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001073 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001074 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001075 return 1;
1076
Jim Grosbach4b905842013-09-20 23:08:21 +00001077 // Low Precedence: |, &, ^
1078 //
1079 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001080 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001081 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001082 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001083 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001084 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001085 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001086 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001087 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001088 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001089
Jim Grosbach4b905842013-09-20 23:08:21 +00001090 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001091 case AsmToken::EqualEqual:
1092 Kind = MCBinaryExpr::EQ;
1093 return 3;
1094 case AsmToken::ExclaimEqual:
1095 case AsmToken::LessGreater:
1096 Kind = MCBinaryExpr::NE;
1097 return 3;
1098 case AsmToken::Less:
1099 Kind = MCBinaryExpr::LT;
1100 return 3;
1101 case AsmToken::LessEqual:
1102 Kind = MCBinaryExpr::LTE;
1103 return 3;
1104 case AsmToken::Greater:
1105 Kind = MCBinaryExpr::GT;
1106 return 3;
1107 case AsmToken::GreaterEqual:
1108 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001109 return 3;
1110
Jim Grosbach4b905842013-09-20 23:08:21 +00001111 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001112 case AsmToken::LessLess:
1113 Kind = MCBinaryExpr::Shl;
1114 return 4;
1115 case AsmToken::GreaterGreater:
1116 Kind = MCBinaryExpr::Shr;
1117 return 4;
1118
Jim Grosbach4b905842013-09-20 23:08:21 +00001119 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001120 case AsmToken::Plus:
1121 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001122 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001123 case AsmToken::Minus:
1124 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001125 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001126
Jim Grosbach4b905842013-09-20 23:08:21 +00001127 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001128 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001129 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001130 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001131 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001132 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001133 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001134 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001135 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001136 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001137 }
1138}
1139
Jim Grosbach4b905842013-09-20 23:08:21 +00001140/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001141/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001142bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001143 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001144 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001145 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001146 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001147
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001148 // If the next token is lower precedence than we are allowed to eat, return
1149 // successfully with what we ate already.
1150 if (TokPrec < Precedence)
1151 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001152
Sean Callanan686ed8d2010-01-19 20:22:31 +00001153 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001154
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001156 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001157 if (parsePrimaryExpr(RHS, EndLoc))
1158 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001159
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001160 // If BinOp binds less tightly with RHS than the operator after RHS, let
1161 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001162 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001163 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001164 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1165 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001166
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001167 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001168 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001169 }
1170}
1171
Chris Lattner36e02122009-06-21 20:54:55 +00001172/// ParseStatement:
1173/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001174/// ::= Label* Directive ...Operands... EndOfStatement
1175/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001176bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001177 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001178 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001179 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001180 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001181 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001182
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001183 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001184 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001185 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001186 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001187 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001188 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001189 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001190 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001191
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001192 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001193 if (Lexer.is(AsmToken::Integer)) {
1194 LocalLabelVal = getTok().getIntVal();
1195 if (LocalLabelVal < 0) {
1196 if (!TheCondState.Ignore)
1197 return TokError("unexpected token at start of statement");
1198 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001199 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001200 IDVal = getTok().getString();
1201 Lex(); // Consume the integer token to be used as an identifier token.
1202 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001203 if (!TheCondState.Ignore)
1204 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001205 }
1206 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001207 } else if (Lexer.is(AsmToken::Dot)) {
1208 // Treat '.' as a valid identifier in this context.
1209 Lex();
1210 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001211 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001212 if (!TheCondState.Ignore)
1213 return TokError("unexpected token at start of statement");
1214 IDVal = "";
1215 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001216
Chris Lattner926885c2010-04-17 18:14:27 +00001217 // Handle conditional assembly here before checking for skipping. We
1218 // have to do this so that .endif isn't skipped in a ".if 0" block for
1219 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001220 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001221 DirectiveKindMap.find(IDVal);
1222 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1223 ? DK_NO_DIRECTIVE
1224 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001225 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001226 default:
1227 break;
1228 case DK_IF:
1229 return parseDirectiveIf(IDLoc);
1230 case DK_IFB:
1231 return parseDirectiveIfb(IDLoc, true);
1232 case DK_IFNB:
1233 return parseDirectiveIfb(IDLoc, false);
1234 case DK_IFC:
1235 return parseDirectiveIfc(IDLoc, true);
1236 case DK_IFNC:
1237 return parseDirectiveIfc(IDLoc, false);
1238 case DK_IFDEF:
1239 return parseDirectiveIfdef(IDLoc, true);
1240 case DK_IFNDEF:
1241 case DK_IFNOTDEF:
1242 return parseDirectiveIfdef(IDLoc, false);
1243 case DK_ELSEIF:
1244 return parseDirectiveElseIf(IDLoc);
1245 case DK_ELSE:
1246 return parseDirectiveElse(IDLoc);
1247 case DK_ENDIF:
1248 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001249 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001250
Eli Bendersky88024712013-01-16 19:32:36 +00001251 // Ignore the statement if in the middle of inactive conditional
1252 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001253 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001254 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001255 return false;
1256 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001257
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001258 // FIXME: Recurse on local labels?
1259
1260 // See what kind of statement we have.
1261 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001262 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001263 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001264
Chris Lattner36e02122009-06-21 20:54:55 +00001265 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001266 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001267
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001268 // Diagnose attempt to use '.' as a label.
1269 if (IDVal == ".")
1270 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1271
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001272 // Diagnose attempt to use a variable as a label.
1273 //
1274 // FIXME: Diagnostics. Note the location of the definition as a label.
1275 // FIXME: This doesn't diagnose assignment to a symbol which has been
1276 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001277 MCSymbol *Sym;
1278 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001279 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001280 else
1281 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001282 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001283 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001284
Daniel Dunbare73b2672009-08-26 22:13:22 +00001285 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001286 if (!ParsingInlineAsm)
1287 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001288
Kevin Enderbye7739d42011-12-09 18:09:40 +00001289 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001290 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001291 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001292 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1293 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001294
Tim Northover1744d0a2013-10-25 12:49:50 +00001295 getTargetParser().onLabelParsed(Sym);
1296
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001297 // Consume any end of statement token, if present, to avoid spurious
1298 // AddBlankLine calls().
1299 if (Lexer.is(AsmToken::EndOfStatement)) {
1300 Lex();
1301 if (Lexer.is(AsmToken::Eof))
1302 return false;
1303 }
1304
Eli Friedman0f4871d2012-10-22 23:58:19 +00001305 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001306 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001307
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001308 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001309 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001310 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001311
Jim Grosbach4b905842013-09-20 23:08:21 +00001312 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001313
1314 default: // Normal instruction or directive.
1315 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001316 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001317
1318 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001319 if (areMacrosEnabled())
1320 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1321 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001322 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001323
Michael J. Spencer530ce852010-10-09 11:00:50 +00001324 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001325
Eli Bendersky17233942013-01-15 22:59:42 +00001326 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001327 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001328 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001329 //
Eli Bendersky17233942013-01-15 22:59:42 +00001330 // 1. The target-specific assembly parser. Some directives are target
1331 // specific or may potentially behave differently on certain targets.
1332 // 2. Asm parser extensions. For example, platform-specific parsers
1333 // (like the ELF parser) register themselves as extensions.
1334 // 3. The generic directive parser implemented by this class. These are
1335 // all the directives that behave in a target and platform independent
1336 // manner, or at least have a default behavior that's shared between
1337 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001338
Eli Bendersky17233942013-01-15 22:59:42 +00001339 // First query the target-specific parser. It will return 'true' if it
1340 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001341 if (!getTargetParser().ParseDirective(ID))
1342 return false;
1343
Eli Bendersky17233942013-01-15 22:59:42 +00001344 // Next, check the extention directive map to see if any extension has
1345 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001346 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1347 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001348 if (Handler.first)
1349 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1350
1351 // Finally, if no one else is interested in this directive, it must be
1352 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001353 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001354 default:
1355 break;
1356 case DK_SET:
1357 case DK_EQU:
1358 return parseDirectiveSet(IDVal, true);
1359 case DK_EQUIV:
1360 return parseDirectiveSet(IDVal, false);
1361 case DK_ASCII:
1362 return parseDirectiveAscii(IDVal, false);
1363 case DK_ASCIZ:
1364 case DK_STRING:
1365 return parseDirectiveAscii(IDVal, true);
1366 case DK_BYTE:
1367 return parseDirectiveValue(1);
1368 case DK_SHORT:
1369 case DK_VALUE:
1370 case DK_2BYTE:
1371 return parseDirectiveValue(2);
1372 case DK_LONG:
1373 case DK_INT:
1374 case DK_4BYTE:
1375 return parseDirectiveValue(4);
1376 case DK_QUAD:
1377 case DK_8BYTE:
1378 return parseDirectiveValue(8);
1379 case DK_SINGLE:
1380 case DK_FLOAT:
1381 return parseDirectiveRealValue(APFloat::IEEEsingle);
1382 case DK_DOUBLE:
1383 return parseDirectiveRealValue(APFloat::IEEEdouble);
1384 case DK_ALIGN: {
1385 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1386 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1387 }
1388 case DK_ALIGN32: {
1389 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1390 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1391 }
1392 case DK_BALIGN:
1393 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1394 case DK_BALIGNW:
1395 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1396 case DK_BALIGNL:
1397 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1398 case DK_P2ALIGN:
1399 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1400 case DK_P2ALIGNW:
1401 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1402 case DK_P2ALIGNL:
1403 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1404 case DK_ORG:
1405 return parseDirectiveOrg();
1406 case DK_FILL:
1407 return parseDirectiveFill();
1408 case DK_ZERO:
1409 return parseDirectiveZero();
1410 case DK_EXTERN:
1411 eatToEndOfStatement(); // .extern is the default, ignore it.
1412 return false;
1413 case DK_GLOBL:
1414 case DK_GLOBAL:
1415 return parseDirectiveSymbolAttribute(MCSA_Global);
1416 case DK_LAZY_REFERENCE:
1417 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1418 case DK_NO_DEAD_STRIP:
1419 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1420 case DK_SYMBOL_RESOLVER:
1421 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1422 case DK_PRIVATE_EXTERN:
1423 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1424 case DK_REFERENCE:
1425 return parseDirectiveSymbolAttribute(MCSA_Reference);
1426 case DK_WEAK_DEFINITION:
1427 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1428 case DK_WEAK_REFERENCE:
1429 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1430 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1431 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1432 case DK_COMM:
1433 case DK_COMMON:
1434 return parseDirectiveComm(/*IsLocal=*/false);
1435 case DK_LCOMM:
1436 return parseDirectiveComm(/*IsLocal=*/true);
1437 case DK_ABORT:
1438 return parseDirectiveAbort();
1439 case DK_INCLUDE:
1440 return parseDirectiveInclude();
1441 case DK_INCBIN:
1442 return parseDirectiveIncbin();
1443 case DK_CODE16:
1444 case DK_CODE16GCC:
1445 return TokError(Twine(IDVal) + " not supported yet");
1446 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001447 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001448 case DK_IRP:
1449 return parseDirectiveIrp(IDLoc);
1450 case DK_IRPC:
1451 return parseDirectiveIrpc(IDLoc);
1452 case DK_ENDR:
1453 return parseDirectiveEndr(IDLoc);
1454 case DK_BUNDLE_ALIGN_MODE:
1455 return parseDirectiveBundleAlignMode();
1456 case DK_BUNDLE_LOCK:
1457 return parseDirectiveBundleLock();
1458 case DK_BUNDLE_UNLOCK:
1459 return parseDirectiveBundleUnlock();
1460 case DK_SLEB128:
1461 return parseDirectiveLEB128(true);
1462 case DK_ULEB128:
1463 return parseDirectiveLEB128(false);
1464 case DK_SPACE:
1465 case DK_SKIP:
1466 return parseDirectiveSpace(IDVal);
1467 case DK_FILE:
1468 return parseDirectiveFile(IDLoc);
1469 case DK_LINE:
1470 return parseDirectiveLine();
1471 case DK_LOC:
1472 return parseDirectiveLoc();
1473 case DK_STABS:
1474 return parseDirectiveStabs();
1475 case DK_CFI_SECTIONS:
1476 return parseDirectiveCFISections();
1477 case DK_CFI_STARTPROC:
1478 return parseDirectiveCFIStartProc();
1479 case DK_CFI_ENDPROC:
1480 return parseDirectiveCFIEndProc();
1481 case DK_CFI_DEF_CFA:
1482 return parseDirectiveCFIDefCfa(IDLoc);
1483 case DK_CFI_DEF_CFA_OFFSET:
1484 return parseDirectiveCFIDefCfaOffset();
1485 case DK_CFI_ADJUST_CFA_OFFSET:
1486 return parseDirectiveCFIAdjustCfaOffset();
1487 case DK_CFI_DEF_CFA_REGISTER:
1488 return parseDirectiveCFIDefCfaRegister(IDLoc);
1489 case DK_CFI_OFFSET:
1490 return parseDirectiveCFIOffset(IDLoc);
1491 case DK_CFI_REL_OFFSET:
1492 return parseDirectiveCFIRelOffset(IDLoc);
1493 case DK_CFI_PERSONALITY:
1494 return parseDirectiveCFIPersonalityOrLsda(true);
1495 case DK_CFI_LSDA:
1496 return parseDirectiveCFIPersonalityOrLsda(false);
1497 case DK_CFI_REMEMBER_STATE:
1498 return parseDirectiveCFIRememberState();
1499 case DK_CFI_RESTORE_STATE:
1500 return parseDirectiveCFIRestoreState();
1501 case DK_CFI_SAME_VALUE:
1502 return parseDirectiveCFISameValue(IDLoc);
1503 case DK_CFI_RESTORE:
1504 return parseDirectiveCFIRestore(IDLoc);
1505 case DK_CFI_ESCAPE:
1506 return parseDirectiveCFIEscape();
1507 case DK_CFI_SIGNAL_FRAME:
1508 return parseDirectiveCFISignalFrame();
1509 case DK_CFI_UNDEFINED:
1510 return parseDirectiveCFIUndefined(IDLoc);
1511 case DK_CFI_REGISTER:
1512 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001513 case DK_CFI_WINDOW_SAVE:
1514 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001515 case DK_MACROS_ON:
1516 case DK_MACROS_OFF:
1517 return parseDirectiveMacrosOnOff(IDVal);
1518 case DK_MACRO:
1519 return parseDirectiveMacro(IDLoc);
1520 case DK_ENDM:
1521 case DK_ENDMACRO:
1522 return parseDirectiveEndMacro(IDVal);
1523 case DK_PURGEM:
1524 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001525 case DK_END:
1526 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001527 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001528
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001529 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001530 }
Chris Lattner36e02122009-06-21 20:54:55 +00001531
Chad Rosierc7f552c2013-02-12 21:33:51 +00001532 // __asm _emit or __asm __emit
1533 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1534 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001535 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001536
1537 // __asm align
1538 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001539 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001540
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001541 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001542
Chris Lattner7cbfa442010-05-19 23:34:33 +00001543 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001544 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001545 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001546 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001547 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001548 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001549
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001550 // Dump the parsed representation, if requested.
1551 if (getShowParsedOperands()) {
1552 SmallString<256> Str;
1553 raw_svector_ostream OS(Str);
1554 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001555 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001556 if (i != 0)
1557 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001558 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001559 }
1560 OS << "]";
1561
Jim Grosbach4b905842013-09-20 23:08:21 +00001562 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001563 }
1564
Kevin Enderby6469fc22011-11-01 22:27:22 +00001565 // If we are generating dwarf for assembly source files and the current
1566 // section is the initial text section then generate a .loc directive for
1567 // the instruction.
1568 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001569 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001570 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001571
Eli Bendersky88024712013-01-16 19:32:36 +00001572 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001573
Eli Bendersky88024712013-01-16 19:32:36 +00001574 // If we previously parsed a cpp hash file line comment then make sure the
1575 // current Dwarf File is for the CppHashFilename if not then emit the
1576 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001577 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001578 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001579 if (CppHashFilename.size() != 0) {
1580 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001581 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001582 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001583 getContext().nextGenDwarfFileNumber(), StringRef(),
1584 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001585
Jim Grosbach4b905842013-09-20 23:08:21 +00001586 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1587 // cache with the different Loc from the call above we save the last
1588 // info we queried here with SrcMgr.FindLineNumber().
1589 unsigned CppHashLocLineNo;
1590 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1591 CppHashLocLineNo = LastQueryLine;
1592 else {
1593 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1594 LastQueryLine = CppHashLocLineNo;
1595 LastQueryIDLoc = CppHashLoc;
1596 LastQueryBuffer = CppHashBuf;
1597 }
1598 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001599 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001600
Jim Grosbach4b905842013-09-20 23:08:21 +00001601 getStreamer().EmitDwarfLocDirective(
1602 getContext().getGenDwarfFileNumber(), Line, 0,
1603 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1604 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001605 }
1606
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001607 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001608 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001609 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001610 HadError = getTargetParser().MatchAndEmitInstruction(
1611 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1612 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001613 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001614
Chris Lattnera2a9d162010-09-11 16:18:25 +00001615 // Don't skip the rest of the line, the instruction parser is responsible for
1616 // that.
1617 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001618}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001619
Jim Grosbach4b905842013-09-20 23:08:21 +00001620/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001621/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001622void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001623 if (!Lexer.is(AsmToken::EndOfStatement))
1624 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001625 // Eat EOL.
1626 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001627}
1628
Jim Grosbach4b905842013-09-20 23:08:21 +00001629/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001630/// ::= # number "filename"
1631/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001632bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001633 Lex(); // Eat the hash token.
1634
1635 if (getLexer().isNot(AsmToken::Integer)) {
1636 // Consume the line since in cases it is not a well-formed line directive,
1637 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001638 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001639 return false;
1640 }
1641
1642 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001643 Lex();
1644
1645 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001646 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001647 return false;
1648 }
1649
1650 StringRef Filename = getTok().getString();
1651 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001652 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001653
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001654 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1655 CppHashLoc = L;
1656 CppHashFilename = Filename;
1657 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001658 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001659
1660 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001661 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001662 return false;
1663}
1664
Jim Grosbach4b905842013-09-20 23:08:21 +00001665/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001666/// for the Filename and LineNo if any in the diagnostic.
1667void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001668 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001669 raw_ostream &OS = errs();
1670
1671 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1672 const SMLoc &DiagLoc = Diag.getLoc();
1673 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1674 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1675
Jim Grosbach4b905842013-09-20 23:08:21 +00001676 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001677 // before printing the message.
1678 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001679 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001680 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1681 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001682 }
1683
Eric Christophera7c32732012-12-18 00:30:54 +00001684 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001685 // manager changed or buffer changed (like in a nested include) then just
1686 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001687 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001688 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001689 if (Parser->SavedDiagHandler)
1690 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1691 else
1692 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001693 return;
1694 }
1695
Eric Christophera7c32732012-12-18 00:30:54 +00001696 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001697 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1698 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001699 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700
1701 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1702 int CppHashLocLineNo =
1703 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001704 int LineNo =
1705 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001706
Jim Grosbach4b905842013-09-20 23:08:21 +00001707 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1708 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001709 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001710
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001711 if (Parser->SavedDiagHandler)
1712 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1713 else
1714 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001715}
1716
Rafael Espindola2c064482012-08-21 18:29:30 +00001717// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1718// difference being that that function accepts '@' as part of identifiers and
1719// we can't do that. AsmLexer.cpp should probably be changed to handle
1720// '@' as a special case when needed.
1721static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001722 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1723 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001724}
1725
Rafael Espindola34b9c512012-06-03 23:57:14 +00001726bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +00001727 const MCAsmMacroParameters &Parameters,
Jim Grosbach4b905842013-09-20 23:08:21 +00001728 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001729 unsigned NParameters = Parameters.size();
1730 if (NParameters != 0 && NParameters != A.size())
1731 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001732
Preston Gurd05500642012-09-19 20:36:12 +00001733 // A macro without parameters is handled differently on Darwin:
1734 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001735 while (!Body.empty()) {
1736 // Scan for the next substitution.
1737 std::size_t End = Body.size(), Pos = 0;
1738 for (; Pos != End; ++Pos) {
1739 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001740 if (!NParameters) {
1741 // This macro has no parameters, look for $0, $1, etc.
1742 if (Body[Pos] != '$' || Pos + 1 == End)
1743 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001744
Rafael Espindola1134ab232011-06-05 02:43:45 +00001745 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001746 if (Next == '$' || Next == 'n' ||
1747 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001748 break;
1749 } else {
1750 // This macro has parameters, look for \foo, \bar, etc.
1751 if (Body[Pos] == '\\' && Pos + 1 != End)
1752 break;
1753 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001754 }
1755
1756 // Add the prefix.
1757 OS << Body.slice(0, Pos);
1758
1759 // Check if we reached the end.
1760 if (Pos == End)
1761 break;
1762
Rafael Espindola1134ab232011-06-05 02:43:45 +00001763 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001764 switch (Body[Pos + 1]) {
1765 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001766 case '$':
1767 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001768 break;
1769
Jim Grosbach4b905842013-09-20 23:08:21 +00001770 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001771 case 'n':
1772 OS << A.size();
1773 break;
1774
Jim Grosbach4b905842013-09-20 23:08:21 +00001775 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001776 default: {
1777 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001779 if (Index >= A.size())
1780 break;
1781
1782 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001783 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001784 ie = A[Index].end();
1785 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001786 OS << it->getString();
1787 break;
1788 }
1789 }
1790 Pos += 2;
1791 } else {
1792 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001793 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001794 ++I;
1795
Jim Grosbach4b905842013-09-20 23:08:21 +00001796 const char *Begin = Body.data() + Pos + 1;
1797 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001798 unsigned Index = 0;
1799 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001800 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001801 break;
1802
Preston Gurd05500642012-09-19 20:36:12 +00001803 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001804 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1805 Pos += 3;
1806 else {
1807 OS << '\\' << Argument;
1808 Pos = I;
1809 }
Preston Gurd05500642012-09-19 20:36:12 +00001810 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001811 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001812 ie = A[Index].end();
1813 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001814 if (it->getKind() == AsmToken::String)
1815 OS << it->getStringContents();
1816 else
1817 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001818
Preston Gurd05500642012-09-19 20:36:12 +00001819 Pos += 1 + Argument.size();
1820 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001821 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001822 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001823 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001824 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001825
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 return false;
1827}
Daniel Dunbar43235712010-07-18 18:54:11 +00001828
Jim Grosbach4b905842013-09-20 23:08:21 +00001829MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1830 SMLoc EL, MemoryBuffer *I)
1831 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1832 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001833
Jim Grosbach4b905842013-09-20 23:08:21 +00001834static bool isOperator(AsmToken::TokenKind kind) {
1835 switch (kind) {
1836 default:
1837 return false;
1838 case AsmToken::Plus:
1839 case AsmToken::Minus:
1840 case AsmToken::Tilde:
1841 case AsmToken::Slash:
1842 case AsmToken::Star:
1843 case AsmToken::Dot:
1844 case AsmToken::Equal:
1845 case AsmToken::EqualEqual:
1846 case AsmToken::Pipe:
1847 case AsmToken::PipePipe:
1848 case AsmToken::Caret:
1849 case AsmToken::Amp:
1850 case AsmToken::AmpAmp:
1851 case AsmToken::Exclaim:
1852 case AsmToken::ExclaimEqual:
1853 case AsmToken::Percent:
1854 case AsmToken::Less:
1855 case AsmToken::LessEqual:
1856 case AsmToken::LessLess:
1857 case AsmToken::LessGreater:
1858 case AsmToken::Greater:
1859 case AsmToken::GreaterEqual:
1860 case AsmToken::GreaterGreater:
1861 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001862 }
1863}
1864
Jim Grosbach4b905842013-09-20 23:08:21 +00001865bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd05500642012-09-19 20:36:12 +00001866 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001867 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001868 unsigned AddTokens = 0;
1869
1870 // gas accepts arguments separated by whitespace, except on Darwin
1871 if (!IsDarwin)
1872 Lexer.setSkipSpace(false);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001873
1874 for (;;) {
Preston Gurd05500642012-09-19 20:36:12 +00001875 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1876 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001877 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001878 }
1879
1880 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1881 // Spaces and commas cannot be mixed to delimit parameters
1882 if (ArgumentDelimiter == AsmToken::Eof)
1883 ArgumentDelimiter = AsmToken::Comma;
1884 else if (ArgumentDelimiter != AsmToken::Comma) {
1885 Lexer.setSkipSpace(true);
1886 return TokError("expected ' ' for macro argument separator");
1887 }
1888 break;
1889 }
1890
1891 if (Lexer.is(AsmToken::Space)) {
1892 Lex(); // Eat spaces
1893
1894 // Spaces can delimit parameters, but could also be part an expression.
1895 // If the token after a space is an operator, add the token and the next
1896 // one into this argument
1897 if (ArgumentDelimiter == AsmToken::Space ||
1898 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001899 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001900 // Check to see whether the token is used as an operator,
1901 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001902 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001903 if (*NextChar == ' ')
1904 AddTokens = 2;
1905 }
1906
1907 if (!AddTokens && ParenLevel == 0) {
1908 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4b905842013-09-20 23:08:21 +00001909 !isOperator(Lexer.getKind()))
Preston Gurd05500642012-09-19 20:36:12 +00001910 ArgumentDelimiter = AsmToken::Space;
1911 break;
1912 }
1913 }
1914 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001915
Jim Grosbach4b905842013-09-20 23:08:21 +00001916 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001917 // to be able to fill in the remaining default parameter values
1918 if (Lexer.is(AsmToken::EndOfStatement))
1919 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001920
1921 // Adjust the current parentheses level.
1922 if (Lexer.is(AsmToken::LParen))
1923 ++ParenLevel;
1924 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1925 --ParenLevel;
1926
1927 // Append the token to the current argument list.
1928 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001929 if (AddTokens)
1930 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931 Lex();
1932 }
Preston Gurd05500642012-09-19 20:36:12 +00001933
1934 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001935 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001936 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001937 return false;
1938}
1939
1940// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001941bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001942 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001943 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd05500642012-09-19 20:36:12 +00001944 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00001945 // parseMacroArgument()
Preston Gurd05500642012-09-19 20:36:12 +00001946 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001947
1948 // Parse two kinds of macro invocations:
1949 // - macros defined without any parameters accept an arbitrary number of them
1950 // - macros defined with parameters accept at most that many of them
1951 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1952 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001953 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001954
Jim Grosbach4b905842013-09-20 23:08:21 +00001955 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001956 return true;
1957
Preston Gurd242ed3152012-09-19 20:29:04 +00001958 if (!MA.empty() || !NParameters)
1959 A.push_back(MA);
1960 else if (NParameters) {
1961 if (!M->Parameters[Parameter].second.empty())
1962 A.push_back(M->Parameters[Parameter].second);
1963 }
Jim Grosbach206661622012-07-30 22:44:17 +00001964
Preston Gurd242ed3152012-09-19 20:29:04 +00001965 // At the end of the statement, fill in remaining arguments that have
1966 // default values. If there aren't any, then the next argument is
1967 // required but missing
1968 if (Lexer.is(AsmToken::EndOfStatement)) {
1969 if (NParameters && Parameter < NParameters - 1) {
1970 if (M->Parameters[Parameter + 1].second.empty())
1971 return TokError("macro argument '" +
1972 Twine(M->Parameters[Parameter + 1].first) +
1973 "' is missing");
1974 else
1975 continue;
1976 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001977 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001978 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001979
1980 if (Lexer.is(AsmToken::Comma))
1981 Lex();
1982 }
1983 return TokError("Too many arguments");
1984}
1985
Jim Grosbach4b905842013-09-20 23:08:21 +00001986const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1987 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001988 return (I == MacroMap.end()) ? NULL : I->getValue();
1989}
1990
Jim Grosbach4b905842013-09-20 23:08:21 +00001991void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001992 MacroMap[Name] = new MCAsmMacro(Macro);
1993}
1994
Jim Grosbach4b905842013-09-20 23:08:21 +00001995void AsmParser::undefineMacro(StringRef Name) {
1996 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001997 if (I != MacroMap.end()) {
1998 delete I->getValue();
1999 MacroMap.erase(I);
2000 }
2001}
2002
Jim Grosbach4b905842013-09-20 23:08:21 +00002003bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002004 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2005 // this, although we should protect against infinite loops.
2006 if (ActiveMacros.size() == 20)
2007 return TokError("macros cannot be nested more than 20 levels deep");
2008
Eli Bendersky38274122013-01-14 23:22:36 +00002009 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002010 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002011 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002012
Jim Grosbach206661622012-07-30 22:44:17 +00002013 // Remove any trailing empty arguments. Do this after-the-fact as we have
2014 // to keep empty arguments in the middle of the list or positionality
2015 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002016 while (!A.empty() && A.back().empty())
2017 A.pop_back();
Jim Grosbach206661622012-07-30 22:44:17 +00002018
Rafael Espindola1134ab232011-06-05 02:43:45 +00002019 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2020 // to hold the macro body with substitutions.
2021 SmallString<256> Buf;
2022 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002023 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002024
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002025 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002026 return true;
2027
Eli Bendersky38274122013-01-14 23:22:36 +00002028 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002029 // instantiation.
2030 OS << ".endmacro\n";
2031
Rafael Espindola1134ab232011-06-05 02:43:45 +00002032 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002033 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002034
Daniel Dunbar43235712010-07-18 18:54:11 +00002035 // Create the macro instantiation object and add to the current macro
2036 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002037 MacroInstantiation *MI = new MacroInstantiation(
2038 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002039 ActiveMacros.push_back(MI);
2040
2041 // Jump to the macro instantiation and prime the lexer.
2042 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2043 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2044 Lex();
2045
2046 return false;
2047}
2048
Jim Grosbach4b905842013-09-20 23:08:21 +00002049void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002050 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002051 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002052 Lex();
2053
2054 // Pop the instantiation entry.
2055 delete ActiveMacros.back();
2056 ActiveMacros.pop_back();
2057}
2058
Jim Grosbach4b905842013-09-20 23:08:21 +00002059static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002060 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002061 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002062 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2063 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002064 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002065 case MCExpr::Target:
2066 case MCExpr::Constant:
2067 return false;
2068 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002069 const MCSymbol &S =
2070 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002071 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002072 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002073 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002074 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002075 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002076 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002077 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002078
2079 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002080}
2081
Jim Grosbach4b905842013-09-20 23:08:21 +00002082bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002083 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002084 // FIXME: Use better location, we should use proper tokens.
2085 SMLoc EqualLoc = Lexer.getLoc();
2086
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002087 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002088 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002089 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002090
Rafael Espindola72f5f172012-01-28 05:57:00 +00002091 // Note: we don't count b as used in "a = b". This is to allow
2092 // a = b
2093 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002094
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002095 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002096 return TokError("unexpected token in assignment");
2097
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002098 // Error on assignment to '.'.
2099 if (Name == ".") {
2100 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2101 "(use '.space' or '.org').)"));
2102 }
2103
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002104 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002105 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002106
Daniel Dunbar5f339242009-10-16 01:57:39 +00002107 // Validate that the LHS is allowed to be a variable (either it has not been
2108 // used as a symbol, or it is an absolute symbol).
2109 MCSymbol *Sym = getContext().LookupSymbol(Name);
2110 if (Sym) {
2111 // Diagnose assignment to a label.
2112 //
2113 // FIXME: Diagnostics. Note the location of the definition as a label.
2114 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002115 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002116 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2117 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002118 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002119 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2120 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002121 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002122 return Error(EqualLoc, "redefinition of '" + Name + "'");
2123 else if (!Sym->isVariable())
2124 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002125 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002126 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002127 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002128
2129 // Don't count these checks as uses.
2130 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002131 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002132 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002133
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002134 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002135
2136 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002137 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002138 if (NoDeadStrip)
2139 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2140
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002141 return false;
2142}
2143
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002144/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002145/// ::= identifier
2146/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002147bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002148 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002149 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2150 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002151 // handle this as a context dependent token, instead we detect adjacent tokens
2152 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002153 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2154 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002155
Hans Wennborgce69d772013-10-18 20:46:28 +00002156 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002157 Lex();
2158 if (Lexer.isNot(AsmToken::Identifier))
2159 return true;
2160
Hans Wennborgce69d772013-10-18 20:46:28 +00002161 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2162 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002163 return true;
2164
2165 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002166 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002167 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002168 Lex();
2169 return false;
2170 }
2171
Jim Grosbach4b905842013-09-20 23:08:21 +00002172 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002173 return true;
2174
Sean Callanan936b0d32010-01-19 21:44:56 +00002175 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002176
Sean Callanan686ed8d2010-01-19 20:22:31 +00002177 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002178
2179 return false;
2180}
2181
Jim Grosbach4b905842013-09-20 23:08:21 +00002182/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002183/// ::= .equ identifier ',' expression
2184/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002185/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002186bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002187 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002188
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002189 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002190 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002191
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002192 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002193 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002194 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002195
Jim Grosbach4b905842013-09-20 23:08:21 +00002196 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002197}
2198
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002199bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002200 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002201
2202 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002203 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002204 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2205 if (Str[i] != '\\') {
2206 Data += Str[i];
2207 continue;
2208 }
2209
2210 // Recognize escaped characters. Note that this escape semantics currently
2211 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2212 ++i;
2213 if (i == e)
2214 return TokError("unexpected backslash at end of string");
2215
2216 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002217 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002218 // Consume up to three octal characters.
2219 unsigned Value = Str[i] - '0';
2220
Jim Grosbach4b905842013-09-20 23:08:21 +00002221 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002222 ++i;
2223 Value = Value * 8 + (Str[i] - '0');
2224
Jim Grosbach4b905842013-09-20 23:08:21 +00002225 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002226 ++i;
2227 Value = Value * 8 + (Str[i] - '0');
2228 }
2229 }
2230
2231 if (Value > 255)
2232 return TokError("invalid octal escape sequence (out of range)");
2233
Jim Grosbach4b905842013-09-20 23:08:21 +00002234 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002235 continue;
2236 }
2237
2238 // Otherwise recognize individual escapes.
2239 switch (Str[i]) {
2240 default:
2241 // Just reject invalid escape sequences for now.
2242 return TokError("invalid escape sequence (unrecognized character)");
2243
2244 case 'b': Data += '\b'; break;
2245 case 'f': Data += '\f'; break;
2246 case 'n': Data += '\n'; break;
2247 case 'r': Data += '\r'; break;
2248 case 't': Data += '\t'; break;
2249 case '"': Data += '"'; break;
2250 case '\\': Data += '\\'; break;
2251 }
2252 }
2253
2254 return false;
2255}
2256
Jim Grosbach4b905842013-09-20 23:08:21 +00002257/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002258/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002259bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002260 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002261 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002262
Daniel Dunbara10e5192009-06-24 23:30:00 +00002263 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002264 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002265 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002266
Daniel Dunbaref668c12009-08-14 18:19:52 +00002267 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002268 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002269 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002270
Rafael Espindola64e1af82013-07-02 15:49:13 +00002271 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002272 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002273 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002274
Sean Callanan686ed8d2010-01-19 20:22:31 +00002275 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002276
2277 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002278 break;
2279
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002280 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002281 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002282 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002283 }
2284 }
2285
Sean Callanan686ed8d2010-01-19 20:22:31 +00002286 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002287 return false;
2288}
2289
Jim Grosbach4b905842013-09-20 23:08:21 +00002290/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002291/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002292bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002293 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002294 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002295
Daniel Dunbara10e5192009-06-24 23:30:00 +00002296 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002297 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002298 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002299 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002300 return true;
2301
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002302 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002303 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2304 assert(Size <= 8 && "Invalid size");
2305 uint64_t IntValue = MCE->getValue();
2306 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2307 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002308 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002309 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002310 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002311
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002312 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002313 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002314
Daniel Dunbara10e5192009-06-24 23:30:00 +00002315 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002316 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002317 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002318 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002319 }
2320 }
2321
Sean Callanan686ed8d2010-01-19 20:22:31 +00002322 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002323 return false;
2324}
2325
Jim Grosbach4b905842013-09-20 23:08:21 +00002326/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002327/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002328bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002329 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002330 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002331
2332 for (;;) {
2333 // We don't truly support arithmetic on floating point expressions, so we
2334 // have to manually parse unary prefixes.
2335 bool IsNeg = false;
2336 if (getLexer().is(AsmToken::Minus)) {
2337 Lex();
2338 IsNeg = true;
2339 } else if (getLexer().is(AsmToken::Plus))
2340 Lex();
2341
Michael J. Spencer530ce852010-10-09 11:00:50 +00002342 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002343 getLexer().isNot(AsmToken::Real) &&
2344 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002345 return TokError("unexpected token in directive");
2346
2347 // Convert to an APFloat.
2348 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002349 StringRef IDVal = getTok().getString();
2350 if (getLexer().is(AsmToken::Identifier)) {
2351 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2352 Value = APFloat::getInf(Semantics);
2353 else if (!IDVal.compare_lower("nan"))
2354 Value = APFloat::getNaN(Semantics, false, ~0);
2355 else
2356 return TokError("invalid floating point literal");
2357 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002358 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002359 return TokError("invalid floating point literal");
2360 if (IsNeg)
2361 Value.changeSign();
2362
2363 // Consume the numeric token.
2364 Lex();
2365
2366 // Emit the value as an integer.
2367 APInt AsInt = Value.bitcastToAPInt();
2368 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002369 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002370
2371 if (getLexer().is(AsmToken::EndOfStatement))
2372 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002373
Daniel Dunbar2af16532010-09-24 01:59:56 +00002374 if (getLexer().isNot(AsmToken::Comma))
2375 return TokError("unexpected token in directive");
2376 Lex();
2377 }
2378 }
2379
2380 Lex();
2381 return false;
2382}
2383
Jim Grosbach4b905842013-09-20 23:08:21 +00002384/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002385/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002386bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002387 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002388
2389 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002390 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002391 return true;
2392
Rafael Espindolab91bac62010-10-05 19:42:57 +00002393 int64_t Val = 0;
2394 if (getLexer().is(AsmToken::Comma)) {
2395 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002396 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002397 return true;
2398 }
2399
Rafael Espindola922e3f42010-09-16 15:03:59 +00002400 if (getLexer().isNot(AsmToken::EndOfStatement))
2401 return TokError("unexpected token in '.zero' directive");
2402
2403 Lex();
2404
Rafael Espindola64e1af82013-07-02 15:49:13 +00002405 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002406
2407 return false;
2408}
2409
Jim Grosbach4b905842013-09-20 23:08:21 +00002410/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002411/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002412bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002413 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002414
Daniel Dunbara10e5192009-06-24 23:30:00 +00002415 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002416 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002417 return true;
2418
Roman Divackye33098f2013-09-24 17:44:41 +00002419 int64_t FillSize = 1;
2420 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002421
Roman Divackye33098f2013-09-24 17:44:41 +00002422 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2423 if (getLexer().isNot(AsmToken::Comma))
2424 return TokError("unexpected token in '.fill' directive");
2425 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002426
Roman Divackye33098f2013-09-24 17:44:41 +00002427 if (parseAbsoluteExpression(FillSize))
2428 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002429
Roman Divackye33098f2013-09-24 17:44:41 +00002430 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2431 if (getLexer().isNot(AsmToken::Comma))
2432 return TokError("unexpected token in '.fill' directive");
2433 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002434
Roman Divackye33098f2013-09-24 17:44:41 +00002435 if (parseAbsoluteExpression(FillExpr))
2436 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002437
Roman Divackye33098f2013-09-24 17:44:41 +00002438 if (getLexer().isNot(AsmToken::EndOfStatement))
2439 return TokError("unexpected token in '.fill' directive");
2440
2441 Lex();
2442 }
2443 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002444
Daniel Dunbar8e5edd82009-08-21 15:43:35 +00002445 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2446 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara10e5192009-06-24 23:30:00 +00002447
2448 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002449 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002450
2451 return false;
2452}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002453
Jim Grosbach4b905842013-09-20 23:08:21 +00002454/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002455/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002456bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002457 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002458
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002459 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002460 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002461 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002462 return true;
2463
2464 // Parse optional fill expression.
2465 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2467 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002468 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002469 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002470
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002471 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002472 return true;
2473
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002474 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002475 return TokError("unexpected token in '.org' directive");
2476 }
2477
Sean Callanan686ed8d2010-01-19 20:22:31 +00002478 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002479
Jim Grosbachb5912772012-01-27 00:37:08 +00002480 // Only limited forms of relocatable expressions are accepted here, it
2481 // has to be relative to the current section. The streamer will return
2482 // 'true' if the expression wasn't evaluatable.
2483 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2484 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002485
2486 return false;
2487}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002488
Jim Grosbach4b905842013-09-20 23:08:21 +00002489/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002490/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002491bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002492 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002493
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002494 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002495 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002496 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002497 return true;
2498
2499 SMLoc MaxBytesLoc;
2500 bool HasFillExpr = false;
2501 int64_t FillExpr = 0;
2502 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002503 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2504 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002505 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002506 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002507
2508 // The fill expression can be omitted while specifying a maximum number of
2509 // alignment bytes, e.g:
2510 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002511 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002512 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002513 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002514 return true;
2515 }
2516
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002517 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2518 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002519 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002520 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002521
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002522 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002523 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002524 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002525
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002526 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002527 return TokError("unexpected token in directive");
2528 }
2529 }
2530
Sean Callanan686ed8d2010-01-19 20:22:31 +00002531 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002532
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002533 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002534 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002535
2536 // Compute alignment in bytes.
2537 if (IsPow2) {
2538 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002539 if (Alignment >= 32) {
2540 Error(AlignmentLoc, "invalid alignment value");
2541 Alignment = 31;
2542 }
2543
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002544 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002545 } else {
2546 // Reject alignments that aren't a power of two, for gas compatibility.
2547 if (!isPowerOf2_64(Alignment))
2548 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002549 }
2550
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002551 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002552 if (MaxBytesLoc.isValid()) {
2553 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002554 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002555 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002556 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002557 }
2558
2559 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002560 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002561 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002562 MaxBytesToFill = 0;
2563 }
2564 }
2565
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002566 // Check whether we should use optimal code alignment for this .align
2567 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002568 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002569 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2570 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002571 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002572 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002573 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002574 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2575 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002576 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002577
2578 return false;
2579}
2580
Jim Grosbach4b905842013-09-20 23:08:21 +00002581/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002582/// ::= .file [number] filename
2583/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002584bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002585 // FIXME: I'm not sure what this is.
2586 int64_t FileNumber = -1;
2587 SMLoc FileNumberLoc = getLexer().getLoc();
2588 if (getLexer().is(AsmToken::Integer)) {
2589 FileNumber = getTok().getIntVal();
2590 Lex();
2591
2592 if (FileNumber < 1)
2593 return TokError("file number less than one");
2594 }
2595
2596 if (getLexer().isNot(AsmToken::String))
2597 return TokError("unexpected token in '.file' directive");
2598
2599 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002600 // Allow the strings to have escaped octal character sequence.
2601 std::string Path = getTok().getString();
2602 if (parseEscapedString(Path))
2603 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002604 Lex();
2605
2606 StringRef Directory;
2607 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002608 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002609 if (getLexer().is(AsmToken::String)) {
2610 if (FileNumber == -1)
2611 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002612 if (parseEscapedString(FilenameData))
2613 return true;
2614 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002615 Directory = Path;
2616 Lex();
2617 } else {
2618 Filename = Path;
2619 }
2620
2621 if (getLexer().isNot(AsmToken::EndOfStatement))
2622 return TokError("unexpected token in '.file' directive");
2623
2624 if (FileNumber == -1)
2625 getStreamer().EmitFileDirective(Filename);
2626 else {
2627 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002628 Error(DirectiveLoc,
2629 "input can't have .file dwarf directives when -g is "
2630 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002631
2632 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2633 Error(FileNumberLoc, "file number already allocated");
2634 }
2635
2636 return false;
2637}
2638
Jim Grosbach4b905842013-09-20 23:08:21 +00002639/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002640/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002641bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002642 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2643 if (getLexer().isNot(AsmToken::Integer))
2644 return TokError("unexpected token in '.line' directive");
2645
2646 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002647 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002648 Lex();
2649
2650 // FIXME: Do something with the .line.
2651 }
2652
2653 if (getLexer().isNot(AsmToken::EndOfStatement))
2654 return TokError("unexpected token in '.line' directive");
2655
2656 return false;
2657}
2658
Jim Grosbach4b905842013-09-20 23:08:21 +00002659/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002660/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2661/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2662/// The first number is a file number, must have been previously assigned with
2663/// a .file directive, the second number is the line number and optionally the
2664/// third number is a column position (zero if not specified). The remaining
2665/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002666bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002667 if (getLexer().isNot(AsmToken::Integer))
2668 return TokError("unexpected token in '.loc' directive");
2669 int64_t FileNumber = getTok().getIntVal();
2670 if (FileNumber < 1)
2671 return TokError("file number less than one in '.loc' directive");
2672 if (!getContext().isValidDwarfFileNumber(FileNumber))
2673 return TokError("unassigned file number in '.loc' directive");
2674 Lex();
2675
2676 int64_t LineNumber = 0;
2677 if (getLexer().is(AsmToken::Integer)) {
2678 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002679 if (LineNumber < 0)
2680 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002681 Lex();
2682 }
2683
2684 int64_t ColumnPos = 0;
2685 if (getLexer().is(AsmToken::Integer)) {
2686 ColumnPos = getTok().getIntVal();
2687 if (ColumnPos < 0)
2688 return TokError("column position less than zero in '.loc' directive");
2689 Lex();
2690 }
2691
2692 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2693 unsigned Isa = 0;
2694 int64_t Discriminator = 0;
2695 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2696 for (;;) {
2697 if (getLexer().is(AsmToken::EndOfStatement))
2698 break;
2699
2700 StringRef Name;
2701 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002702 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002703 return TokError("unexpected token in '.loc' directive");
2704
2705 if (Name == "basic_block")
2706 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2707 else if (Name == "prologue_end")
2708 Flags |= DWARF2_FLAG_PROLOGUE_END;
2709 else if (Name == "epilogue_begin")
2710 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2711 else if (Name == "is_stmt") {
2712 Loc = getTok().getLoc();
2713 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002714 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002715 return true;
2716 // The expression must be the constant 0 or 1.
2717 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2718 int Value = MCE->getValue();
2719 if (Value == 0)
2720 Flags &= ~DWARF2_FLAG_IS_STMT;
2721 else if (Value == 1)
2722 Flags |= DWARF2_FLAG_IS_STMT;
2723 else
2724 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002725 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002726 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2727 }
Craig Topperf15655b2013-04-22 04:22:40 +00002728 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002729 Loc = getTok().getLoc();
2730 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002731 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002732 return true;
2733 // The expression must be a constant greater or equal to 0.
2734 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2735 int Value = MCE->getValue();
2736 if (Value < 0)
2737 return Error(Loc, "isa number less than zero");
2738 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002739 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002740 return Error(Loc, "isa number not a constant value");
2741 }
Craig Topperf15655b2013-04-22 04:22:40 +00002742 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002743 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002744 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002745 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002746 return Error(Loc, "unknown sub-directive in '.loc' directive");
2747 }
2748
2749 if (getLexer().is(AsmToken::EndOfStatement))
2750 break;
2751 }
2752 }
2753
2754 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2755 Isa, Discriminator, StringRef());
2756
2757 return false;
2758}
2759
Jim Grosbach4b905842013-09-20 23:08:21 +00002760/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002761/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002762bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002763 return TokError("unsupported directive '.stabs'");
2764}
2765
Jim Grosbach4b905842013-09-20 23:08:21 +00002766/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002767/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002768bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002769 StringRef Name;
2770 bool EH = false;
2771 bool Debug = false;
2772
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002773 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002774 return TokError("Expected an identifier");
2775
2776 if (Name == ".eh_frame")
2777 EH = true;
2778 else if (Name == ".debug_frame")
2779 Debug = true;
2780
2781 if (getLexer().is(AsmToken::Comma)) {
2782 Lex();
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
2793 getStreamer().EmitCFISections(EH, Debug);
2794 return false;
2795}
2796
Jim Grosbach4b905842013-09-20 23:08:21 +00002797/// parseDirectiveCFIStartProc
Eli Bendersky17233942013-01-15 22:59:42 +00002798/// ::= .cfi_startproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002799bool AsmParser::parseDirectiveCFIStartProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002800 getStreamer().EmitCFIStartProc();
2801 return false;
2802}
2803
Jim Grosbach4b905842013-09-20 23:08:21 +00002804/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002805/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002806bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002807 getStreamer().EmitCFIEndProc();
2808 return false;
2809}
2810
Jim Grosbach4b905842013-09-20 23:08:21 +00002811/// \brief parse register name or number.
2812bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002813 SMLoc DirectiveLoc) {
2814 unsigned RegNo;
2815
2816 if (getLexer().isNot(AsmToken::Integer)) {
2817 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2818 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002819 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002820 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002821 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002822
2823 return false;
2824}
2825
Jim Grosbach4b905842013-09-20 23:08:21 +00002826/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002827/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002828bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002829 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002830 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002831 return true;
2832
2833 if (getLexer().isNot(AsmToken::Comma))
2834 return TokError("unexpected token in directive");
2835 Lex();
2836
2837 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002838 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002839 return true;
2840
2841 getStreamer().EmitCFIDefCfa(Register, Offset);
2842 return false;
2843}
2844
Jim Grosbach4b905842013-09-20 23:08:21 +00002845/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002846/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002847bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002848 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002849 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002850 return true;
2851
2852 getStreamer().EmitCFIDefCfaOffset(Offset);
2853 return false;
2854}
2855
Jim Grosbach4b905842013-09-20 23:08:21 +00002856/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002857/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002858bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002859 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002860 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002861 return true;
2862
2863 if (getLexer().isNot(AsmToken::Comma))
2864 return TokError("unexpected token in directive");
2865 Lex();
2866
2867 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002868 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002869 return true;
2870
2871 getStreamer().EmitCFIRegister(Register1, Register2);
2872 return false;
2873}
2874
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002875/// parseDirectiveCFIWindowSave
2876/// ::= .cfi_window_save
2877bool AsmParser::parseDirectiveCFIWindowSave() {
2878 getStreamer().EmitCFIWindowSave();
2879 return false;
2880}
2881
Jim Grosbach4b905842013-09-20 23:08:21 +00002882/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002883/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002884bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002885 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002886 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002887 return true;
2888
2889 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2890 return false;
2891}
2892
Jim Grosbach4b905842013-09-20 23:08:21 +00002893/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002894/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002895bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002896 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002897 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002898 return true;
2899
2900 getStreamer().EmitCFIDefCfaRegister(Register);
2901 return false;
2902}
2903
Jim Grosbach4b905842013-09-20 23:08:21 +00002904/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002905/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002906bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002907 int64_t Register = 0;
2908 int64_t Offset = 0;
2909
Jim Grosbach4b905842013-09-20 23:08:21 +00002910 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002911 return true;
2912
2913 if (getLexer().isNot(AsmToken::Comma))
2914 return TokError("unexpected token in directive");
2915 Lex();
2916
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002917 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002918 return true;
2919
2920 getStreamer().EmitCFIOffset(Register, Offset);
2921 return false;
2922}
2923
Jim Grosbach4b905842013-09-20 23:08:21 +00002924/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002925/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002926bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002927 int64_t Register = 0;
2928
Jim Grosbach4b905842013-09-20 23:08:21 +00002929 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002930 return true;
2931
2932 if (getLexer().isNot(AsmToken::Comma))
2933 return TokError("unexpected token in directive");
2934 Lex();
2935
2936 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002937 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002938 return true;
2939
2940 getStreamer().EmitCFIRelOffset(Register, Offset);
2941 return false;
2942}
2943
2944static bool isValidEncoding(int64_t Encoding) {
2945 if (Encoding & ~0xff)
2946 return false;
2947
2948 if (Encoding == dwarf::DW_EH_PE_omit)
2949 return true;
2950
2951 const unsigned Format = Encoding & 0xf;
2952 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2953 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2954 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2955 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2956 return false;
2957
2958 const unsigned Application = Encoding & 0x70;
2959 if (Application != dwarf::DW_EH_PE_absptr &&
2960 Application != dwarf::DW_EH_PE_pcrel)
2961 return false;
2962
2963 return true;
2964}
2965
Jim Grosbach4b905842013-09-20 23:08:21 +00002966/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00002967/// IsPersonality true for cfi_personality, false for cfi_lsda
2968/// ::= .cfi_personality encoding, [symbol_name]
2969/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00002970bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00002971 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002972 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00002973 return true;
2974 if (Encoding == dwarf::DW_EH_PE_omit)
2975 return false;
2976
2977 if (!isValidEncoding(Encoding))
2978 return TokError("unsupported encoding.");
2979
2980 if (getLexer().isNot(AsmToken::Comma))
2981 return TokError("unexpected token in directive");
2982 Lex();
2983
2984 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002985 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002986 return TokError("expected identifier in directive");
2987
2988 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2989
2990 if (IsPersonality)
2991 getStreamer().EmitCFIPersonality(Sym, Encoding);
2992 else
2993 getStreamer().EmitCFILsda(Sym, Encoding);
2994 return false;
2995}
2996
Jim Grosbach4b905842013-09-20 23:08:21 +00002997/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00002998/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00002999bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003000 getStreamer().EmitCFIRememberState();
3001 return false;
3002}
3003
Jim Grosbach4b905842013-09-20 23:08:21 +00003004/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003005/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003006bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003007 getStreamer().EmitCFIRestoreState();
3008 return false;
3009}
3010
Jim Grosbach4b905842013-09-20 23:08:21 +00003011/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003012/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003013bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003014 int64_t Register = 0;
3015
Jim Grosbach4b905842013-09-20 23:08:21 +00003016 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003017 return true;
3018
3019 getStreamer().EmitCFISameValue(Register);
3020 return false;
3021}
3022
Jim Grosbach4b905842013-09-20 23:08:21 +00003023/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003024/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003025bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003026 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003027 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003028 return true;
3029
3030 getStreamer().EmitCFIRestore(Register);
3031 return false;
3032}
3033
Jim Grosbach4b905842013-09-20 23:08:21 +00003034/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003035/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003036bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003037 std::string Values;
3038 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003039 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003040 return true;
3041
3042 Values.push_back((uint8_t)CurrValue);
3043
3044 while (getLexer().is(AsmToken::Comma)) {
3045 Lex();
3046
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003047 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003048 return true;
3049
3050 Values.push_back((uint8_t)CurrValue);
3051 }
3052
3053 getStreamer().EmitCFIEscape(Values);
3054 return false;
3055}
3056
Jim Grosbach4b905842013-09-20 23:08:21 +00003057/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003058/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003059bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003060 if (getLexer().isNot(AsmToken::EndOfStatement))
3061 return Error(getLexer().getLoc(),
3062 "unexpected token in '.cfi_signal_frame'");
3063
3064 getStreamer().EmitCFISignalFrame();
3065 return false;
3066}
3067
Jim Grosbach4b905842013-09-20 23:08:21 +00003068/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003069/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003070bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003071 int64_t Register = 0;
3072
Jim Grosbach4b905842013-09-20 23:08:21 +00003073 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003074 return true;
3075
3076 getStreamer().EmitCFIUndefined(Register);
3077 return false;
3078}
3079
Jim Grosbach4b905842013-09-20 23:08:21 +00003080/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003081/// ::= .macros_on
3082/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003083bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003084 if (getLexer().isNot(AsmToken::EndOfStatement))
3085 return Error(getLexer().getLoc(),
3086 "unexpected token in '" + Directive + "' directive");
3087
Jim Grosbach4b905842013-09-20 23:08:21 +00003088 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003089 return false;
3090}
3091
Jim Grosbach4b905842013-09-20 23:08:21 +00003092/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003093/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003094bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003095 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003096 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003097 return TokError("expected identifier in '.macro' directive");
3098
3099 MCAsmMacroParameters Parameters;
3100 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00003101 // parseMacroArgument()
Eli Bendersky17233942013-01-15 22:59:42 +00003102 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3103 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3104 for (;;) {
3105 MCAsmMacroParameter Parameter;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003106 if (parseIdentifier(Parameter.first))
Eli Bendersky17233942013-01-15 22:59:42 +00003107 return TokError("expected identifier in '.macro' directive");
3108
3109 if (getLexer().is(AsmToken::Equal)) {
3110 Lex();
Jim Grosbach4b905842013-09-20 23:08:21 +00003111 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky17233942013-01-15 22:59:42 +00003112 return true;
3113 }
3114
3115 Parameters.push_back(Parameter);
3116
3117 if (getLexer().is(AsmToken::Comma))
3118 Lex();
3119 else if (getLexer().is(AsmToken::EndOfStatement))
3120 break;
3121 }
3122 }
3123
3124 // Eat the end of statement.
3125 Lex();
3126
3127 AsmToken EndToken, StartToken = getTok();
3128
3129 // Lex the macro definition.
3130 for (;;) {
3131 // Check whether we have reached the end of the file.
3132 if (getLexer().is(AsmToken::Eof))
3133 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3134
3135 // Otherwise, check whether we have reach the .endmacro.
3136 if (getLexer().is(AsmToken::Identifier) &&
3137 (getTok().getIdentifier() == ".endm" ||
3138 getTok().getIdentifier() == ".endmacro")) {
3139 EndToken = getTok();
3140 Lex();
3141 if (getLexer().isNot(AsmToken::EndOfStatement))
3142 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3143 "' directive");
3144 break;
3145 }
3146
3147 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003148 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003149 }
3150
Jim Grosbach4b905842013-09-20 23:08:21 +00003151 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003152 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3153 }
3154
3155 const char *BodyStart = StartToken.getLoc().getPointer();
3156 const char *BodyEnd = EndToken.getLoc().getPointer();
3157 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003158 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3159 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003160 return false;
3161}
3162
Jim Grosbach4b905842013-09-20 23:08:21 +00003163/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003164///
3165/// With the support added for named parameters there may be code out there that
3166/// is transitioning from positional parameters. In versions of gas that did
3167/// not support named parameters they would be ignored on the macro defintion.
3168/// But to support both styles of parameters this is not possible so if a macro
3169/// defintion has named parameters but does not use them and has what appears
3170/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3171/// warning that the positional parameter found in body which have no effect.
3172/// Hoping the developer will either remove the named parameters from the macro
3173/// definiton so the positional parameters get used if that was what was
3174/// intended or change the macro to use the named parameters. It is possible
3175/// this warning will trigger when the none of the named parameters are used
3176/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003177void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003178 StringRef Body,
3179 MCAsmMacroParameters Parameters) {
3180 // If this macro is not defined with named parameters the warning we are
3181 // checking for here doesn't apply.
3182 unsigned NParameters = Parameters.size();
3183 if (NParameters == 0)
3184 return;
3185
3186 bool NamedParametersFound = false;
3187 bool PositionalParametersFound = false;
3188
3189 // Look at the body of the macro for use of both the named parameters and what
3190 // are likely to be positional parameters. This is what expandMacro() is
3191 // doing when it finds the parameters in the body.
3192 while (!Body.empty()) {
3193 // Scan for the next possible parameter.
3194 std::size_t End = Body.size(), Pos = 0;
3195 for (; Pos != End; ++Pos) {
3196 // Check for a substitution or escape.
3197 // This macro is defined with parameters, look for \foo, \bar, etc.
3198 if (Body[Pos] == '\\' && Pos + 1 != End)
3199 break;
3200
3201 // This macro should have parameters, but look for $0, $1, ..., $n too.
3202 if (Body[Pos] != '$' || Pos + 1 == End)
3203 continue;
3204 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003205 if (Next == '$' || Next == 'n' ||
3206 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003207 break;
3208 }
3209
3210 // Check if we reached the end.
3211 if (Pos == End)
3212 break;
3213
3214 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003215 switch (Body[Pos + 1]) {
3216 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003217 case '$':
3218 break;
3219
Jim Grosbach4b905842013-09-20 23:08:21 +00003220 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003221 case 'n':
3222 PositionalParametersFound = true;
3223 break;
3224
Jim Grosbach4b905842013-09-20 23:08:21 +00003225 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003226 default: {
3227 PositionalParametersFound = true;
3228 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003229 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003230 }
3231 Pos += 2;
3232 } else {
3233 unsigned I = Pos + 1;
3234 while (isIdentifierChar(Body[I]) && I + 1 != End)
3235 ++I;
3236
Jim Grosbach4b905842013-09-20 23:08:21 +00003237 const char *Begin = Body.data() + Pos + 1;
3238 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003239 unsigned Index = 0;
3240 for (; Index < NParameters; ++Index)
3241 if (Parameters[Index].first == Argument)
3242 break;
3243
3244 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003245 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3246 Pos += 3;
3247 else {
3248 Pos = I;
3249 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003250 } else {
3251 NamedParametersFound = true;
3252 Pos += 1 + Argument.size();
3253 }
3254 }
3255 // Update the scan point.
3256 Body = Body.substr(Pos);
3257 }
3258
3259 if (!NamedParametersFound && PositionalParametersFound)
3260 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3261 "used in macro body, possible positional parameter "
3262 "found in body which will have no effect");
3263}
3264
Jim Grosbach4b905842013-09-20 23:08:21 +00003265/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003266/// ::= .endm
3267/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003268bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003269 if (getLexer().isNot(AsmToken::EndOfStatement))
3270 return TokError("unexpected token in '" + Directive + "' directive");
3271
3272 // If we are inside a macro instantiation, terminate the current
3273 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003274 if (isInsideMacroInstantiation()) {
3275 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003276 return false;
3277 }
3278
3279 // Otherwise, this .endmacro is a stray entry in the file; well formed
3280 // .endmacro directives are handled during the macro definition parsing.
3281 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003282 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003283}
3284
Jim Grosbach4b905842013-09-20 23:08:21 +00003285/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003286/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003287bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003288 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003289 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003290 return TokError("expected identifier in '.purgem' directive");
3291
3292 if (getLexer().isNot(AsmToken::EndOfStatement))
3293 return TokError("unexpected token in '.purgem' directive");
3294
Jim Grosbach4b905842013-09-20 23:08:21 +00003295 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003296 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3297
Jim Grosbach4b905842013-09-20 23:08:21 +00003298 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003299 return false;
3300}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003301
Jim Grosbach4b905842013-09-20 23:08:21 +00003302/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003303/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003304bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003305 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003306
3307 // Expect a single argument: an expression that evaluates to a constant
3308 // in the inclusive range 0-30.
3309 SMLoc ExprLoc = getLexer().getLoc();
3310 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003311 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003312 return true;
3313 else if (getLexer().isNot(AsmToken::EndOfStatement))
3314 return TokError("unexpected token after expression in"
3315 " '.bundle_align_mode' directive");
3316 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3317 return Error(ExprLoc,
3318 "invalid bundle alignment size (expected between 0 and 30)");
3319
3320 Lex();
3321
3322 // Because of AlignSizePow2's verified range we can safely truncate it to
3323 // unsigned.
3324 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3325 return false;
3326}
3327
Jim Grosbach4b905842013-09-20 23:08:21 +00003328/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003329/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003330bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003331 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003332 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003333
Eli Bendersky802b6282013-01-07 21:51:08 +00003334 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3335 StringRef Option;
3336 SMLoc Loc = getTok().getLoc();
3337 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003338 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003339
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003340 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003341 return Error(Loc, kInvalidOptionError);
3342
3343 if (Option != "align_to_end")
3344 return Error(Loc, kInvalidOptionError);
3345 else if (getLexer().isNot(AsmToken::EndOfStatement))
3346 return Error(Loc,
3347 "unexpected token after '.bundle_lock' directive option");
3348 AlignToEnd = true;
3349 }
3350
Eli Benderskyf483ff92012-12-20 19:05:53 +00003351 Lex();
3352
Eli Bendersky802b6282013-01-07 21:51:08 +00003353 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003354 return false;
3355}
3356
Jim Grosbach4b905842013-09-20 23:08:21 +00003357/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003358/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003359bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003360 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003361
3362 if (getLexer().isNot(AsmToken::EndOfStatement))
3363 return TokError("unexpected token in '.bundle_unlock' directive");
3364 Lex();
3365
3366 getStreamer().EmitBundleUnlock();
3367 return false;
3368}
3369
Jim Grosbach4b905842013-09-20 23:08:21 +00003370/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003371/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003372bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003373 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003374
3375 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003376 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003377 return true;
3378
3379 int64_t FillExpr = 0;
3380 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3381 if (getLexer().isNot(AsmToken::Comma))
3382 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3383 Lex();
3384
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003385 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003386 return true;
3387
3388 if (getLexer().isNot(AsmToken::EndOfStatement))
3389 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3390 }
3391
3392 Lex();
3393
3394 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003395 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3396 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003397
3398 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003399 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003400
3401 return false;
3402}
3403
Jim Grosbach4b905842013-09-20 23:08:21 +00003404/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003405/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003406bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003407 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003408 const MCExpr *Value;
3409
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003410 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003411 return true;
3412
3413 if (getLexer().isNot(AsmToken::EndOfStatement))
3414 return TokError("unexpected token in directive");
3415
3416 if (Signed)
3417 getStreamer().EmitSLEB128Value(Value);
3418 else
3419 getStreamer().EmitULEB128Value(Value);
3420
3421 return false;
3422}
3423
Jim Grosbach4b905842013-09-20 23:08:21 +00003424/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003425/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003426bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003427 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003428 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003429 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003430 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003431
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003432 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003433 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003434
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003435 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003436
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003437 // Assembler local symbols don't make any sense here. Complain loudly.
3438 if (Sym->isTemporary())
3439 return Error(Loc, "non-local symbol required in directive");
3440
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003441 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3442 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003443
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003444 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003445 break;
3446
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003447 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003448 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003449 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003450 }
3451 }
3452
Sean Callanan686ed8d2010-01-19 20:22:31 +00003453 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003454 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003455}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003456
Jim Grosbach4b905842013-09-20 23:08:21 +00003457/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003458/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003459bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003460 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003461
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003462 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003463 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003464 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003465 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003466
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003467 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003468 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003469
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003470 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003471 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003472 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003473
3474 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003475 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003476 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003477 return true;
3478
3479 int64_t Pow2Alignment = 0;
3480 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003481 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003482 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003483 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003484 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003485 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003486
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003487 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3488 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003489 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3490
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003491 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003492 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3493 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003494 if (!isPowerOf2_64(Pow2Alignment))
3495 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3496 Pow2Alignment = Log2_64(Pow2Alignment);
3497 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003498 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003499
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003500 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003501 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003502
Sean Callanan686ed8d2010-01-19 20:22:31 +00003503 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003504
Chris Lattner28ad7542009-07-09 17:25:12 +00003505 // NOTE: a size of zero for a .comm should create a undefined symbol
3506 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003507 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003508 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003509 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003510
Eric Christopherbc818852010-05-14 01:38:54 +00003511 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003512 // may internally end up wanting an alignment in bytes.
3513 // FIXME: Diagnose overflow.
3514 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003515 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003516 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003517
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003518 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003519 return Error(IDLoc, "invalid symbol redefinition");
3520
Chris Lattner28ad7542009-07-09 17:25:12 +00003521 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003522 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003523 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003524 return false;
3525 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003526
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003527 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003528 return false;
3529}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003530
Jim Grosbach4b905842013-09-20 23:08:21 +00003531/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003532/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003533bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003534 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003535 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003536
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003537 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003538 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003539 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003540
Sean Callanan686ed8d2010-01-19 20:22:31 +00003541 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003542
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003543 if (Str.empty())
3544 Error(Loc, ".abort detected. Assembly stopping.");
3545 else
3546 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003547 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003548
3549 return false;
3550}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003551
Jim Grosbach4b905842013-09-20 23:08:21 +00003552/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003553/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003554bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003555 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003556 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003557
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003558 // Allow the strings to have escaped octal character sequence.
3559 std::string Filename;
3560 if (parseEscapedString(Filename))
3561 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003562 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003563 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003564
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003565 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003566 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003567
Chris Lattner693fbb82009-07-16 06:14:39 +00003568 // Attempt to switch the lexer to the included file before consuming the end
3569 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003570 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003571 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003572 return true;
3573 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003574
3575 return false;
3576}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003577
Jim Grosbach4b905842013-09-20 23:08:21 +00003578/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003579/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003580bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003581 if (getLexer().isNot(AsmToken::String))
3582 return TokError("expected string in '.incbin' directive");
3583
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003584 // Allow the strings to have escaped octal character sequence.
3585 std::string Filename;
3586 if (parseEscapedString(Filename))
3587 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003588 SMLoc IncbinLoc = getLexer().getLoc();
3589 Lex();
3590
3591 if (getLexer().isNot(AsmToken::EndOfStatement))
3592 return TokError("unexpected token in '.incbin' directive");
3593
Kevin Enderby109f25c2011-12-14 21:47:48 +00003594 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003595 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003596 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3597 return true;
3598 }
3599
3600 return false;
3601}
3602
Jim Grosbach4b905842013-09-20 23:08:21 +00003603/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003604/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003605bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003606 TheCondStack.push_back(TheCondState);
3607 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003608 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003609 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003610 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003611 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003612 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003613 return true;
3614
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003615 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003616 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003617
Sean Callanan686ed8d2010-01-19 20:22:31 +00003618 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003619
3620 TheCondState.CondMet = ExprValue;
3621 TheCondState.Ignore = !TheCondState.CondMet;
3622 }
3623
3624 return false;
3625}
3626
Jim Grosbach4b905842013-09-20 23:08:21 +00003627/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003628/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003629bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003630 TheCondStack.push_back(TheCondState);
3631 TheCondState.TheCond = AsmCond::IfCond;
3632
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003633 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003634 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003635 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003636 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003637
3638 if (getLexer().isNot(AsmToken::EndOfStatement))
3639 return TokError("unexpected token in '.ifb' directive");
3640
3641 Lex();
3642
3643 TheCondState.CondMet = ExpectBlank == Str.empty();
3644 TheCondState.Ignore = !TheCondState.CondMet;
3645 }
3646
3647 return false;
3648}
3649
Jim Grosbach4b905842013-09-20 23:08:21 +00003650/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003651/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003652bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003653 TheCondStack.push_back(TheCondState);
3654 TheCondState.TheCond = AsmCond::IfCond;
3655
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003656 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003657 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003658 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003659 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003660
3661 if (getLexer().isNot(AsmToken::Comma))
3662 return TokError("unexpected token in '.ifc' directive");
3663
3664 Lex();
3665
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003666 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003667
3668 if (getLexer().isNot(AsmToken::EndOfStatement))
3669 return TokError("unexpected token in '.ifc' directive");
3670
3671 Lex();
3672
3673 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3674 TheCondState.Ignore = !TheCondState.CondMet;
3675 }
3676
3677 return false;
3678}
3679
Jim Grosbach4b905842013-09-20 23:08:21 +00003680/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003681/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003682bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003683 StringRef Name;
3684 TheCondStack.push_back(TheCondState);
3685 TheCondState.TheCond = AsmCond::IfCond;
3686
3687 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003688 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003689 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003690 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003691 return TokError("expected identifier after '.ifdef'");
3692
3693 Lex();
3694
3695 MCSymbol *Sym = getContext().LookupSymbol(Name);
3696
3697 if (expect_defined)
3698 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3699 else
3700 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3701 TheCondState.Ignore = !TheCondState.CondMet;
3702 }
3703
3704 return false;
3705}
3706
Jim Grosbach4b905842013-09-20 23:08:21 +00003707/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003708/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003709bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003710 if (TheCondState.TheCond != AsmCond::IfCond &&
3711 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003712 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3713 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003714 TheCondState.TheCond = AsmCond::ElseIfCond;
3715
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003716 bool LastIgnoreState = false;
3717 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003718 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003719 if (LastIgnoreState || TheCondState.CondMet) {
3720 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003721 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003722 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003723 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003724 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003725 return true;
3726
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003727 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003728 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003729
Sean Callanan686ed8d2010-01-19 20:22:31 +00003730 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003731 TheCondState.CondMet = ExprValue;
3732 TheCondState.Ignore = !TheCondState.CondMet;
3733 }
3734
3735 return false;
3736}
3737
Jim Grosbach4b905842013-09-20 23:08:21 +00003738/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003739/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003740bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003741 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003742 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003743
Sean Callanan686ed8d2010-01-19 20:22:31 +00003744 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003745
3746 if (TheCondState.TheCond != AsmCond::IfCond &&
3747 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003748 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3749 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003750 TheCondState.TheCond = AsmCond::ElseCond;
3751 bool LastIgnoreState = false;
3752 if (!TheCondStack.empty())
3753 LastIgnoreState = TheCondStack.back().Ignore;
3754 if (LastIgnoreState || TheCondState.CondMet)
3755 TheCondState.Ignore = true;
3756 else
3757 TheCondState.Ignore = false;
3758
3759 return false;
3760}
3761
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003762/// parseDirectiveEnd
3763/// ::= .end
3764bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3765 if (getLexer().isNot(AsmToken::EndOfStatement))
3766 return TokError("unexpected token in '.end' directive");
3767
3768 Lex();
3769
3770 while (Lexer.isNot(AsmToken::Eof))
3771 Lex();
3772
3773 return false;
3774}
3775
Jim Grosbach4b905842013-09-20 23:08:21 +00003776/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003777/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003778bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003779 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003780 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003781
Sean Callanan686ed8d2010-01-19 20:22:31 +00003782 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003783
Jim Grosbach4b905842013-09-20 23:08:21 +00003784 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003785 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3786 ".else");
3787 if (!TheCondStack.empty()) {
3788 TheCondState = TheCondStack.back();
3789 TheCondStack.pop_back();
3790 }
3791
3792 return false;
3793}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003794
Eli Bendersky17233942013-01-15 22:59:42 +00003795void AsmParser::initializeDirectiveKindMap() {
3796 DirectiveKindMap[".set"] = DK_SET;
3797 DirectiveKindMap[".equ"] = DK_EQU;
3798 DirectiveKindMap[".equiv"] = DK_EQUIV;
3799 DirectiveKindMap[".ascii"] = DK_ASCII;
3800 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3801 DirectiveKindMap[".string"] = DK_STRING;
3802 DirectiveKindMap[".byte"] = DK_BYTE;
3803 DirectiveKindMap[".short"] = DK_SHORT;
3804 DirectiveKindMap[".value"] = DK_VALUE;
3805 DirectiveKindMap[".2byte"] = DK_2BYTE;
3806 DirectiveKindMap[".long"] = DK_LONG;
3807 DirectiveKindMap[".int"] = DK_INT;
3808 DirectiveKindMap[".4byte"] = DK_4BYTE;
3809 DirectiveKindMap[".quad"] = DK_QUAD;
3810 DirectiveKindMap[".8byte"] = DK_8BYTE;
3811 DirectiveKindMap[".single"] = DK_SINGLE;
3812 DirectiveKindMap[".float"] = DK_FLOAT;
3813 DirectiveKindMap[".double"] = DK_DOUBLE;
3814 DirectiveKindMap[".align"] = DK_ALIGN;
3815 DirectiveKindMap[".align32"] = DK_ALIGN32;
3816 DirectiveKindMap[".balign"] = DK_BALIGN;
3817 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3818 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3819 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3820 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3821 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3822 DirectiveKindMap[".org"] = DK_ORG;
3823 DirectiveKindMap[".fill"] = DK_FILL;
3824 DirectiveKindMap[".zero"] = DK_ZERO;
3825 DirectiveKindMap[".extern"] = DK_EXTERN;
3826 DirectiveKindMap[".globl"] = DK_GLOBL;
3827 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003828 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3829 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3830 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3831 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3832 DirectiveKindMap[".reference"] = DK_REFERENCE;
3833 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3834 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3835 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3836 DirectiveKindMap[".comm"] = DK_COMM;
3837 DirectiveKindMap[".common"] = DK_COMMON;
3838 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3839 DirectiveKindMap[".abort"] = DK_ABORT;
3840 DirectiveKindMap[".include"] = DK_INCLUDE;
3841 DirectiveKindMap[".incbin"] = DK_INCBIN;
3842 DirectiveKindMap[".code16"] = DK_CODE16;
3843 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3844 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003845 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003846 DirectiveKindMap[".irp"] = DK_IRP;
3847 DirectiveKindMap[".irpc"] = DK_IRPC;
3848 DirectiveKindMap[".endr"] = DK_ENDR;
3849 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3850 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3851 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3852 DirectiveKindMap[".if"] = DK_IF;
3853 DirectiveKindMap[".ifb"] = DK_IFB;
3854 DirectiveKindMap[".ifnb"] = DK_IFNB;
3855 DirectiveKindMap[".ifc"] = DK_IFC;
3856 DirectiveKindMap[".ifnc"] = DK_IFNC;
3857 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3858 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3859 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3860 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3861 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003862 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003863 DirectiveKindMap[".endif"] = DK_ENDIF;
3864 DirectiveKindMap[".skip"] = DK_SKIP;
3865 DirectiveKindMap[".space"] = DK_SPACE;
3866 DirectiveKindMap[".file"] = DK_FILE;
3867 DirectiveKindMap[".line"] = DK_LINE;
3868 DirectiveKindMap[".loc"] = DK_LOC;
3869 DirectiveKindMap[".stabs"] = DK_STABS;
3870 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3871 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3872 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3873 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3874 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3875 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3876 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3877 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3878 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3879 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3880 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3881 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3882 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3883 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3884 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3885 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3886 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3887 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3888 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3889 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3890 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003891 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003892 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3893 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3894 DirectiveKindMap[".macro"] = DK_MACRO;
3895 DirectiveKindMap[".endm"] = DK_ENDM;
3896 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3897 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003898}
3899
Jim Grosbach4b905842013-09-20 23:08:21 +00003900MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003901 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003902
Rafael Espindola34b9c512012-06-03 23:57:14 +00003903 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003904 for (;;) {
3905 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003906 if (getLexer().is(AsmToken::Eof)) {
3907 Error(DirectiveLoc, "no matching '.endr' in definition");
3908 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003909 }
3910
Rafael Espindola34b9c512012-06-03 23:57:14 +00003911 if (Lexer.is(AsmToken::Identifier) &&
3912 (getTok().getIdentifier() == ".rept")) {
3913 ++NestLevel;
3914 }
3915
3916 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003917 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003918 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003919 EndToken = getTok();
3920 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003921 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3922 TokError("unexpected token in '.endr' directive");
3923 return 0;
3924 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003925 break;
3926 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003927 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003928 }
3929
Rafael Espindola34b9c512012-06-03 23:57:14 +00003930 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003931 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003932 }
3933
3934 const char *BodyStart = StartToken.getLoc().getPointer();
3935 const char *BodyEnd = EndToken.getLoc().getPointer();
3936 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3937
Rafael Espindola34b9c512012-06-03 23:57:14 +00003938 // We Are Anonymous.
3939 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00003940 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00003941 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3942 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003943}
3944
Jim Grosbach4b905842013-09-20 23:08:21 +00003945void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00003946 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003947 OS << ".endr\n";
3948
3949 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00003950 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003951
Rafael Espindola34b9c512012-06-03 23:57:14 +00003952 // Create the macro instantiation object and add to the current macro
3953 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00003954 MacroInstantiation *MI = new MacroInstantiation(
3955 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003956 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003957
Rafael Espindola34b9c512012-06-03 23:57:14 +00003958 // Jump to the macro instantiation and prime the lexer.
3959 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3960 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3961 Lex();
3962}
3963
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003964/// parseDirectiveRept
3965/// ::= .rep | .rept count
3966bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003967 const MCExpr *CountExpr;
3968 SMLoc CountLoc = getTok().getLoc();
3969 if (parseExpression(CountExpr))
3970 return true;
3971
Rafael Espindola34b9c512012-06-03 23:57:14 +00003972 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003973 if (!CountExpr->EvaluateAsAbsolute(Count)) {
3974 eatToEndOfStatement();
3975 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
3976 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003977
3978 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003979 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003980
3981 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003982 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003983
3984 // Eat the end of statement.
3985 Lex();
3986
3987 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003988 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003989 if (!M)
3990 return true;
3991
3992 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3993 // to hold the macro body with substitutions.
3994 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00003995 MCAsmMacroParameters Parameters;
3996 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00003997 raw_svector_ostream OS(Buf);
3998 while (Count--) {
3999 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
4000 return true;
4001 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004002 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004003
4004 return false;
4005}
4006
Jim Grosbach4b905842013-09-20 23:08:21 +00004007/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004008/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004009bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004010 MCAsmMacroParameters Parameters;
4011 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004012
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004013 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004014 return TokError("expected identifier in '.irp' directive");
4015
4016 Parameters.push_back(Parameter);
4017
4018 if (Lexer.isNot(AsmToken::Comma))
4019 return TokError("expected comma in '.irp' directive");
4020
4021 Lex();
4022
Eli Bendersky38274122013-01-14 23:22:36 +00004023 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004024 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004025 return true;
4026
4027 // Eat the end of statement.
4028 Lex();
4029
4030 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004031 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004032 if (!M)
4033 return true;
4034
4035 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4036 // to hold the macro body with substitutions.
4037 SmallString<256> Buf;
4038 raw_svector_ostream OS(Buf);
4039
Eli Bendersky38274122013-01-14 23:22:36 +00004040 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4041 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004042 Args.push_back(*i);
4043
4044 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4045 return true;
4046 }
4047
Jim Grosbach4b905842013-09-20 23:08:21 +00004048 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004049
4050 return false;
4051}
4052
Jim Grosbach4b905842013-09-20 23:08:21 +00004053/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004054/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004055bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004056 MCAsmMacroParameters Parameters;
4057 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004058
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004059 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004060 return TokError("expected identifier in '.irpc' directive");
4061
4062 Parameters.push_back(Parameter);
4063
4064 if (Lexer.isNot(AsmToken::Comma))
4065 return TokError("expected comma in '.irpc' directive");
4066
4067 Lex();
4068
Eli Bendersky38274122013-01-14 23:22:36 +00004069 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004070 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004071 return true;
4072
4073 if (A.size() != 1 || A.front().size() != 1)
4074 return TokError("unexpected token in '.irpc' directive");
4075
4076 // Eat the end of statement.
4077 Lex();
4078
4079 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004080 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004081 if (!M)
4082 return true;
4083
4084 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4085 // to hold the macro body with substitutions.
4086 SmallString<256> Buf;
4087 raw_svector_ostream OS(Buf);
4088
4089 StringRef Values = A.front().front().getString();
4090 std::size_t I, End = Values.size();
4091 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004092 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004093 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004094
Eli Bendersky38274122013-01-14 23:22:36 +00004095 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004096 Args.push_back(Arg);
4097
4098 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4099 return true;
4100 }
4101
Jim Grosbach4b905842013-09-20 23:08:21 +00004102 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004103
4104 return false;
4105}
4106
Jim Grosbach4b905842013-09-20 23:08:21 +00004107bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004108 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004109 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004110
4111 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004112 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004113 assert(getLexer().is(AsmToken::EndOfStatement));
4114
Jim Grosbach4b905842013-09-20 23:08:21 +00004115 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004116 return false;
4117}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004118
Jim Grosbach4b905842013-09-20 23:08:21 +00004119bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004120 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004121 const MCExpr *Value;
4122 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004123 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004124 return true;
4125 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4126 if (!MCE)
4127 return Error(ExprLoc, "unexpected expression in _emit");
4128 uint64_t IntValue = MCE->getValue();
4129 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4130 return Error(ExprLoc, "literal value out of range for directive");
4131
Chad Rosierc7f552c2013-02-12 21:33:51 +00004132 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4133 return false;
4134}
4135
Jim Grosbach4b905842013-09-20 23:08:21 +00004136bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004137 const MCExpr *Value;
4138 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004139 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004140 return true;
4141 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4142 if (!MCE)
4143 return Error(ExprLoc, "unexpected expression in align");
4144 uint64_t IntValue = MCE->getValue();
4145 if (!isPowerOf2_64(IntValue))
4146 return Error(ExprLoc, "literal value not a power of two greater then zero");
4147
Jim Grosbach4b905842013-09-20 23:08:21 +00004148 Info.AsmRewrites->push_back(
4149 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004150 return false;
4151}
4152
Chad Rosierf43fcf52013-02-13 21:27:17 +00004153// We are comparing pointers, but the pointers are relative to a single string.
4154// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004155static int rewritesSort(const AsmRewrite *AsmRewriteA,
4156 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004157 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4158 return -1;
4159 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4160 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004161
Chad Rosierfce4fab2013-04-08 17:43:47 +00004162 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4163 // rewrite to the same location. Make sure the SizeDirective rewrite is
4164 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4165 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004166 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4167 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004168 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004169
Jim Grosbach4b905842013-09-20 23:08:21 +00004170 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4171 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004172 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004173 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004174}
4175
Jim Grosbach4b905842013-09-20 23:08:21 +00004176bool AsmParser::parseMSInlineAsm(
4177 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4178 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4179 SmallVectorImpl<std::string> &Constraints,
4180 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4181 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004182 SmallVector<void *, 4> InputDecls;
4183 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004184 SmallVector<bool, 4> InputDeclsAddressOf;
4185 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004186 SmallVector<std::string, 4> InputConstraints;
4187 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004188 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004189
Benjamin Kramer1a136112013-02-15 20:37:21 +00004190 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004191
4192 // Prime the lexer.
4193 Lex();
4194
4195 // While we have input, parse each statement.
4196 unsigned InputIdx = 0;
4197 unsigned OutputIdx = 0;
4198 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004199 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004200 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004201 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004202
Chad Rosier149e8e02012-12-12 22:45:52 +00004203 if (Info.ParseError)
4204 return true;
4205
Benjamin Kramer1a136112013-02-15 20:37:21 +00004206 if (Info.Opcode == ~0U)
4207 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004208
Benjamin Kramer1a136112013-02-15 20:37:21 +00004209 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004210
Benjamin Kramer1a136112013-02-15 20:37:21 +00004211 // Build the list of clobbers, outputs and inputs.
4212 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4213 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004214
Benjamin Kramer1a136112013-02-15 20:37:21 +00004215 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004216 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004217 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004218
Benjamin Kramer1a136112013-02-15 20:37:21 +00004219 // Register operand.
4220 if (Operand->isReg() && !Operand->needAddressOf()) {
4221 unsigned NumDefs = Desc.getNumDefs();
4222 // Clobber.
4223 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4224 ClobberRegs.push_back(Operand->getReg());
4225 continue;
4226 }
4227
4228 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004229 StringRef SymName = Operand->getSymName();
4230 if (SymName.empty())
4231 continue;
4232
Chad Rosierdba3fe52013-04-22 22:12:12 +00004233 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004234 if (!OpDecl)
4235 continue;
4236
4237 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004238 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004239 if (isOutput) {
4240 ++InputIdx;
4241 OutputDecls.push_back(OpDecl);
4242 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4243 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004244 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004245 } else {
4246 InputDecls.push_back(OpDecl);
4247 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4248 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004249 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004250 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004251 }
Reid Kleckneree088972013-12-10 18:27:32 +00004252
4253 // Consider implicit defs to be clobbers. Think of cpuid and push.
4254 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4255 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4256 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004257 }
4258
4259 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004260 NumOutputs = OutputDecls.size();
4261 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004262
4263 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004264 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4265 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4266 ClobberRegs.end());
4267 Clobbers.assign(ClobberRegs.size(), std::string());
4268 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4269 raw_string_ostream OS(Clobbers[I]);
4270 IP->printRegName(OS, ClobberRegs[I]);
4271 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004272
4273 // Merge the various outputs and inputs. Output are expected first.
4274 if (NumOutputs || NumInputs) {
4275 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004276 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004277 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004278 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004279 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004280 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004281 }
4282 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004283 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004284 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004285 }
4286 }
4287
4288 // Build the IR assembly string.
4289 std::string AsmStringIR;
4290 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004291 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4292 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004293 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004294 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4295 E = AsmStrRewrites.end();
4296 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004297 AsmRewriteKind Kind = (*I).Kind;
4298 if (Kind == AOK_Delete)
4299 continue;
4300
Chad Rosier8bce6642012-10-18 15:49:34 +00004301 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004302 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004303
Chad Rosier120eefd2013-03-19 17:32:17 +00004304 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004305 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004306 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004307 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004308
Chad Rosier37e755c2012-10-23 17:43:43 +00004309 // Skip the original expression.
4310 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004311 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004312 continue;
4313 }
4314
Chad Rosierff10ed12013-04-12 16:26:42 +00004315 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004316 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004317 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004318 default:
4319 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004320 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004321 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004322 break;
4323 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004324 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004325 break;
4326 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004327 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004328 break;
4329 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004330 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004331 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004332 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004333 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004334 default: break;
4335 case 8: OS << "byte ptr "; break;
4336 case 16: OS << "word ptr "; break;
4337 case 32: OS << "dword ptr "; break;
4338 case 64: OS << "qword ptr "; break;
4339 case 80: OS << "xword ptr "; break;
4340 case 128: OS << "xmmword ptr "; break;
4341 case 256: OS << "ymmword ptr "; break;
4342 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004343 break;
4344 case AOK_Emit:
4345 OS << ".byte";
4346 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004347 case AOK_Align: {
4348 unsigned Val = (*I).Val;
4349 OS << ".align " << Val;
4350
4351 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004352 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004353 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4354 break;
4355 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004356 case AOK_DotOperator:
4357 OS << (*I).Val;
4358 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004359 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004360
Chad Rosier8bce6642012-10-18 15:49:34 +00004361 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004362 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004363 }
4364
4365 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004366 if (AsmStart != AsmEnd)
4367 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004368
4369 AsmString = OS.str();
4370 return false;
4371}
4372
Daniel Dunbar01e36072010-07-17 02:26:10 +00004373/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004374MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4375 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004376 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004377}