blob: 9157ac4f67cedfc5ad87aebe4289c43b84bc0c13 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/SmallString.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Chad Rosier8bce6642012-10-18 15:49:34 +000042#include <set>
43#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000044#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000045using namespace llvm;
46
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000047static cl::opt<bool>
48FatalAssemblerWarnings("fatal-assembler-warnings",
49 cl::desc("Consider warnings as error"));
50
Eric Christophera7c32732012-12-18 00:30:54 +000051MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000052
Daniel Dunbar86033402010-07-12 17:54:38 +000053namespace {
54
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
58typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
59typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
60
61struct MCAsmMacro {
62 StringRef Name;
63 StringRef Body;
64 MCAsmMacroParameters Parameters;
65
66public:
67 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
68 Name(N), Body(B), Parameters(P) {}
69
70 MCAsmMacro(const MCAsmMacro& Other)
71 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
72};
73
Daniel Dunbar43235712010-07-18 18:54:11 +000074/// \brief Helper class for storing information about an active macro
75/// instantiation.
76struct MacroInstantiation {
77 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000078 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000079
80 /// The macro instantiation with substitutions.
81 MemoryBuffer *Instantiation;
82
83 /// The location of the instantiation.
84 SMLoc InstantiationLoc;
85
Daniel Dunbar40f1d852012-12-01 01:38:48 +000086 /// The buffer where parsing should resume upon instantiation completion.
87 int ExitBuffer;
88
Daniel Dunbar43235712010-07-18 18:54:11 +000089 /// The location where parsing should resume upon instantiation completion.
90 SMLoc ExitLoc;
91
92public:
Eli Bendersky38274122013-01-14 23:22:36 +000093 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000094 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000095};
96
Eli Friedman0f4871d2012-10-22 23:58:19 +000097struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000098 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +000099 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
100
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000102 unsigned Opcode;
103
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000105 bool ParseError;
106
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 SmallVectorImpl<AsmRewrite> *AsmRewrites;
108
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000112
113 ~ParseStatementInfo() {
114 // Free any parsed operands.
115 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
116 delete ParsedOperands[i];
117 ParsedOperands.clear();
118 }
119};
120
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121/// \brief The concrete assembly parser instance.
122class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000123 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
124 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125private:
126 AsmLexer Lexer;
127 MCContext &Ctx;
128 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000129 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000131 SourceMgr::DiagHandlerTy SavedDiagHandler;
132 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000133 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000134
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000135 /// This is the current buffer index we're lexing from as managed by the
136 /// SourceMgr object.
137 int CurBuffer;
138
139 AsmCond TheCondState;
140 std::vector<AsmCond> TheCondStack;
141
Jim Grosbach4b905842013-09-20 23:08:21 +0000142 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000143 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000144 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000145 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000146
Jim Grosbach4b905842013-09-20 23:08:21 +0000147 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000148 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000149
Jim Grosbach4b905842013-09-20 23:08:21 +0000150 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000151 std::vector<MacroInstantiation*> ActiveMacros;
152
Jim Grosbach4b905842013-09-20 23:08:21 +0000153 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000154 std::deque<MCAsmMacro> MacroLikeBodies;
155
Daniel Dunbar828984f2010-07-18 18:38:02 +0000156 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000157 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000158
Daniel Dunbar43325c42010-09-09 22:42:56 +0000159 /// Flag tracking whether any errors have been encountered.
160 unsigned HadError : 1;
161
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000162 /// The values from the last parsed cpp hash file line comment if any.
163 StringRef CppHashFilename;
164 int64_t CppHashLineNumber;
165 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000166 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000167 /// When generating dwarf for assembly source files we need to calculate the
168 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000169 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000170 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
171 SMLoc LastQueryIDLoc;
172 int LastQueryBuffer;
173 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000174
Devang Patela173ee52012-01-31 18:14:05 +0000175 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
176 unsigned AssemblerDialect;
177
Jim Grosbach4b905842013-09-20 23:08:21 +0000178 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000179 bool IsDarwin;
180
Jim Grosbach4b905842013-09-20 23:08:21 +0000181 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000182 bool ParsingInlineAsm;
183
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000185 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000187 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188
189 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
190
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000191 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveHandler Handler) {
193 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194 }
195
196public:
197 /// @name MCAsmParser Interface
198 /// {
199
200 virtual SourceMgr &getSourceManager() { return SrcMgr; }
201 virtual MCAsmLexer &getLexer() { return Lexer; }
202 virtual MCContext &getContext() { return Ctx; }
203 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000204 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000205 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000206 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000207 else
208 return AssemblerDialect;
209 }
210 virtual void setAssemblerDialect(unsigned i) {
211 AssemblerDialect = i;
212 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000214 virtual void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000215 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000216 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000217 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000218 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000219
Craig Topper5f96ca52012-08-29 05:48:09 +0000220 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000221
Chad Rosier49963552012-10-13 00:26:04 +0000222 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000223 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000224
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000225 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000226 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000227 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000229 SmallVectorImpl<std::string> &Clobbers,
230 const MCInstrInfo *MII,
231 const MCInstPrinter *IP,
232 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000233
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000234 bool parseExpression(const MCExpr *&Res);
235 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000236 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000237 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
238 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000239
Jim Grosbach4b905842013-09-20 23:08:21 +0000240 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000241 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000242 virtual bool parseIdentifier(StringRef &Res);
243 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000244
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000245 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000246 /// }
247
248private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000249
Jim Grosbach4b905842013-09-20 23:08:21 +0000250 bool parseStatement(ParseStatementInfo &Info);
251 void eatToEndOfLine();
252 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000253
Jim Grosbach4b905842013-09-20 23:08:21 +0000254 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Kevin Enderby81c944c2013-01-22 21:44:53 +0000255 MCAsmMacroParameters Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000256 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +0000257 const MCAsmMacroParameters &Parameters,
258 const MCAsmMacroArguments &A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000259 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000260
Eli Benderskya313ae62013-01-16 18:56:50 +0000261 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000262 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000263
264 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000265 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000266
267 /// \brief Lookup a previously defined macro.
268 /// \param Name Macro name.
269 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000270 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000271
272 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000273 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000274
275 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000276 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000277
278 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000279 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000280
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000281 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000282 ///
283 /// \param M The macro.
284 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000285 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000286
287 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000288 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000289
David Majnemer91fc4c22014-01-29 18:57:46 +0000290 /// \brief Extract AsmTokens for a macro argument.
291 bool parseMacroArgument(MCAsmMacroArgument &MA);
Eli Benderskya313ae62013-01-16 18:56:50 +0000292
293 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000294 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000295
Jim Grosbach4b905842013-09-20 23:08:21 +0000296 void printMacroInstantiations();
297 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000298 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000299 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000300 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000301 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000302
Jim Grosbach4b905842013-09-20 23:08:21 +0000303 /// \brief Enter the specified file. This returns true on failure.
304 bool enterIncludeFile(const std::string &Filename);
305
306 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000307 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000308 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000309
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000310 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000311 /// current token is not set; clients should ensure Lex() is called
312 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000313 ///
314 /// \param InBuffer If not -1, should be the known buffer id that contains the
315 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000316 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000317
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000318 /// \brief Parse up to the end of statement and a return the contents from the
319 /// current token until the end of the statement; the current token on exit
320 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000321 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000322
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000323 /// \brief Parse until the end of a statement or a comma is encountered,
324 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000325 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000326
Jim Grosbach4b905842013-09-20 23:08:21 +0000327 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000328 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000329
Jim Grosbach4b905842013-09-20 23:08:21 +0000330 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
331 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
332 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000333
Jim Grosbach4b905842013-09-20 23:08:21 +0000334 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000335
Eli Bendersky17233942013-01-15 22:59:42 +0000336 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000337 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000338 DK_NO_DIRECTIVE, // Placeholder
339 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
340 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
341 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000342 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000343 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000344 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000345 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
346 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
347 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
348 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
349 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky17233942013-01-15 22:59:42 +0000350 DK_ELSEIF, DK_ELSE, DK_ENDIF,
351 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
352 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
353 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
354 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
355 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
356 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000357 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000358 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000359 DK_SLEB128, DK_ULEB128,
360 DK_END
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000361 };
362
Jim Grosbach4b905842013-09-20 23:08:21 +0000363 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000364 /// directives parsed by this class.
365 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000366
367 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000368 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
369 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
370 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
371 bool parseDirectiveFill(); // ".fill"
372 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000373 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000374 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
375 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000376 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000377 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000378
Eli Bendersky17233942013-01-15 22:59:42 +0000379 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000380 bool parseDirectiveFile(SMLoc DirectiveLoc);
381 bool parseDirectiveLine();
382 bool parseDirectiveLoc();
383 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000384
385 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000386 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000387 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000388 bool parseDirectiveCFISections();
389 bool parseDirectiveCFIStartProc();
390 bool parseDirectiveCFIEndProc();
391 bool parseDirectiveCFIDefCfaOffset();
392 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIAdjustCfaOffset();
394 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
398 bool parseDirectiveCFIRememberState();
399 bool parseDirectiveCFIRestoreState();
400 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIEscape();
403 bool parseDirectiveCFISignalFrame();
404 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000405
406 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000407 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
408 bool parseDirectiveEndMacro(StringRef Directive);
409 bool parseDirectiveMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000411
Eli Benderskyf483ff92012-12-20 19:05:53 +0000412 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000413 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000414 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000415 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000416 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000417 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000418
Eli Bendersky17233942013-01-15 22:59:42 +0000419 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000420 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000421
422 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000423 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000424
Jim Grosbach4b905842013-09-20 23:08:21 +0000425 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000426 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000427 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000428
Jim Grosbach4b905842013-09-20 23:08:21 +0000429 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000430
Jim Grosbach4b905842013-09-20 23:08:21 +0000431 bool parseDirectiveAbort(); // ".abort"
432 bool parseDirectiveInclude(); // ".include"
433 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000434
Jim Grosbach4b905842013-09-20 23:08:21 +0000435 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000436 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000437 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000438 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000439 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000440 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000441 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
442 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
443 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
444 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000445 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000446
Jim Grosbach4b905842013-09-20 23:08:21 +0000447 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000448 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000449
Rafael Espindola34b9c512012-06-03 23:57:14 +0000450 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000451 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
452 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000453 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000454 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
456 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
457 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000458
Chad Rosierc7f552c2013-02-12 21:33:51 +0000459 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000460 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000461 size_t Len);
462
463 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000464 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000465
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000466 // "end"
467 bool parseDirectiveEnd(SMLoc DirectiveLoc);
468
Eli Bendersky17233942013-01-15 22:59:42 +0000469 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000470};
Daniel Dunbar86033402010-07-12 17:54:38 +0000471}
472
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000473namespace llvm {
474
475extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000476extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000477extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000478
479}
480
Chris Lattnerc35681b2010-01-19 19:46:13 +0000481enum { DEFAULT_ADDRSPACE = 0 };
482
Jim Grosbach4b905842013-09-20 23:08:21 +0000483AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
484 const MCAsmInfo &_MAI)
485 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
486 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
487 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
488 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000489 // Save the old handler.
490 SavedDiagHandler = SrcMgr.getDiagHandler();
491 SavedDiagContext = SrcMgr.getDiagContext();
492 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000493 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000494 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000495
Daniel Dunbarc5011082010-07-12 18:12:02 +0000496 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000497 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
498 case MCObjectFileInfo::IsCOFF:
499 PlatformParser = createCOFFAsmParser();
500 PlatformParser->Initialize(*this);
501 break;
502 case MCObjectFileInfo::IsMachO:
503 PlatformParser = createDarwinAsmParser();
504 PlatformParser->Initialize(*this);
505 IsDarwin = true;
506 break;
507 case MCObjectFileInfo::IsELF:
508 PlatformParser = createELFAsmParser();
509 PlatformParser->Initialize(*this);
510 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000511 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000512
Eli Bendersky17233942013-01-15 22:59:42 +0000513 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000514}
515
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000516AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000517 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
518
519 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000520 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
521 ie = MacroMap.end();
522 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000523 delete it->getValue();
524
Daniel Dunbarc5011082010-07-12 18:12:02 +0000525 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000526}
527
Jim Grosbach4b905842013-09-20 23:08:21 +0000528void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000529 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000530 for (std::vector<MacroInstantiation *>::const_reverse_iterator
531 it = ActiveMacros.rbegin(),
532 ie = ActiveMacros.rend();
533 it != ie; ++it)
534 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000535 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000536}
537
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000538void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
539 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
540 printMacroInstantiations();
541}
542
Chris Lattnera3a06812011-10-16 04:47:35 +0000543bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000544 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000545 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000546 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
547 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000548 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000549}
550
Chris Lattnera3a06812011-10-16 04:47:35 +0000551bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000552 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000553 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
554 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000555 return true;
556}
557
Jim Grosbach4b905842013-09-20 23:08:21 +0000558bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000559 std::string IncludedFile;
560 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000561 if (NewBuf == -1)
562 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000563
Sean Callanan7a77eae2010-01-21 00:19:58 +0000564 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000565
Sean Callanan7a77eae2010-01-21 00:19:58 +0000566 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000567
Sean Callanan7a77eae2010-01-21 00:19:58 +0000568 return false;
569}
Daniel Dunbar43235712010-07-18 18:54:11 +0000570
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000571/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000572/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000573/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000574bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000575 std::string IncludedFile;
576 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
577 if (NewBuf == -1)
578 return true;
579
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000580 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000581 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000582 return false;
583}
584
Jim Grosbach4b905842013-09-20 23:08:21 +0000585void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000586 if (InBuffer != -1) {
587 CurBuffer = InBuffer;
588 } else {
589 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
590 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000591 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
592}
593
Sean Callanan7a77eae2010-01-21 00:19:58 +0000594const AsmToken &AsmParser::Lex() {
595 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000596
Sean Callanan7a77eae2010-01-21 00:19:58 +0000597 if (tok->is(AsmToken::Eof)) {
598 // If this is the end of an included file, pop the parent file off the
599 // include stack.
600 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
601 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000602 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603 tok = &Lexer.Lex();
604 }
605 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000606
Sean Callanan7a77eae2010-01-21 00:19:58 +0000607 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000608 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000609
Sean Callanan7a77eae2010-01-21 00:19:58 +0000610 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000611}
612
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000613bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000614 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000615 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000616 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000617
Chris Lattner36e02122009-06-21 20:54:55 +0000618 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000619 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000620
621 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000622 AsmCond StartingCondState = TheCondState;
623
Kevin Enderby6469fc22011-11-01 22:27:22 +0000624 // If we are generating dwarf for assembly source files save the initial text
625 // section and generate a .file directive.
626 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000627 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000628 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
629 getStreamer().EmitLabel(SectionStartSym);
630 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000631 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000632 StringRef(),
633 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000634 }
635
Chris Lattner73f36112009-07-02 21:53:43 +0000636 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000637 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000638 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000639 if (!parseStatement(Info))
640 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000641
Daniel Dunbar43325c42010-09-09 22:42:56 +0000642 // We had an error, validate that one was emitted and recover by skipping to
643 // the next line.
644 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000645 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000646 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000647
648 if (TheCondState.TheCond != StartingCondState.TheCond ||
649 TheCondState.Ignore != StartingCondState.Ignore)
650 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000651
652 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000653 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000654 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000655 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000656 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000657 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000658 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000659
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000660 // Check to see that all assembler local symbols were actually defined.
661 // Targets that don't do subsections via symbols may not want this, though,
662 // so conservatively exclude them. Only do this if we're finalizing, though,
663 // as otherwise we won't necessarilly have seen everything yet.
664 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
665 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
666 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000667 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000668 i != e; ++i) {
669 MCSymbol *Sym = i->getValue();
670 // Variable symbols may not be marked as defined, so check those
671 // explicitly. If we know it's a variable, we have a definition for
672 // the purposes of this check.
673 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
674 // FIXME: We would really like to refer back to where the symbol was
675 // first referenced for a source location. We need to add something
676 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000677 printMessage(
678 getLexer().getLoc(), SourceMgr::DK_Error,
679 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000680 }
681 }
682
David Peixotto308e7e42013-12-19 18:08:08 +0000683 // Callback to the target parser in case it needs to do anything.
684 if (!HadError)
685 getTargetParser().finishParse();
686
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000687 // Finalize the output stream if there are no errors and if the client wants
688 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000689 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000690 Out.Finish();
691
Chris Lattner73f36112009-07-02 21:53:43 +0000692 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000693}
694
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000695void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000696 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000697 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000698 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000699 }
700}
701
Jim Grosbach4b905842013-09-20 23:08:21 +0000702/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000703void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000704 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000705 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000706
Chris Lattnere5074c42009-06-22 01:29:09 +0000707 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000708 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000709 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000710}
711
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000712StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000713 const char *Start = getTok().getLoc().getPointer();
714
Jim Grosbach4b905842013-09-20 23:08:21 +0000715 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000716 Lex();
717
718 const char *End = getTok().getLoc().getPointer();
719 return StringRef(Start, End - Start);
720}
Chris Lattner78db3622009-06-22 05:51:26 +0000721
Jim Grosbach4b905842013-09-20 23:08:21 +0000722StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000723 const char *Start = getTok().getLoc().getPointer();
724
725 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000726 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000727 Lex();
728
729 const char *End = getTok().getLoc().getPointer();
730 return StringRef(Start, End - Start);
731}
732
Jim Grosbach4b905842013-09-20 23:08:21 +0000733/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000734/// NOTE: This assumes the leading '(' has already been consumed.
735///
736/// parenexpr ::= expr)
737///
Jim Grosbach4b905842013-09-20 23:08:21 +0000738bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
739 if (parseExpression(Res))
740 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000741 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000742 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000743 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000744 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000745 return false;
746}
Chris Lattner78db3622009-06-22 05:51:26 +0000747
Jim Grosbach4b905842013-09-20 23:08:21 +0000748/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000749/// NOTE: This assumes the leading '[' has already been consumed.
750///
751/// bracketexpr ::= expr]
752///
Jim Grosbach4b905842013-09-20 23:08:21 +0000753bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
754 if (parseExpression(Res))
755 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000756 if (Lexer.isNot(AsmToken::RBrac))
757 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000758 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000759 Lex();
760 return false;
761}
762
Jim Grosbach4b905842013-09-20 23:08:21 +0000763/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000764/// primaryexpr ::= (parenexpr
765/// primaryexpr ::= symbol
766/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000767/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000768/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000769bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000770 SMLoc FirstTokenLoc = getLexer().getLoc();
771 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
772 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000773 default:
774 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000775 // If we have an error assume that we've already handled it.
776 case AsmToken::Error:
777 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000778 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000779 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000780 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000781 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000782 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000783 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000784 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000785 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000786 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000787 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000788 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000789 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000790 if (FirstTokenKind == AsmToken::Dollar) {
791 if (Lexer.getMAI().getDollarIsPC()) {
792 // This is a '$' reference, which references the current PC. Emit a
793 // temporary label to the streamer and refer to it.
794 MCSymbol *Sym = Ctx.CreateTempSymbol();
795 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000796 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
797 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000798 EndLoc = FirstTokenLoc;
799 return false;
800 } else
801 return Error(FirstTokenLoc, "invalid token in expression");
802 return true;
803 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000804 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000805 // Parse symbol variant
806 std::pair<StringRef, StringRef> Split;
807 if (!MAI.useParensForSymbolVariant()) {
808 Split = Identifier.split('@');
809 } else if (Lexer.is(AsmToken::LParen)) {
810 Lexer.Lex(); // eat (
811 StringRef VName;
812 parseIdentifier(VName);
813 if (Lexer.isNot(AsmToken::RParen)) {
814 return Error(Lexer.getTok().getLoc(),
815 "unexpected token in variant, expected ')'");
816 }
817 Lexer.Lex(); // eat )
818 Split = std::make_pair(Identifier, VName);
819 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000820
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000821 EndLoc = SMLoc::getFromPointer(Identifier.end());
822
Daniel Dunbard20cda02009-10-16 01:34:54 +0000823 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000824 StringRef SymbolName = Identifier;
825 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000826
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000827 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000828 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000829 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000830 if (Variant != MCSymbolRefExpr::VK_Invalid) {
831 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000832 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000833 Variant = MCSymbolRefExpr::VK_None;
834 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000835 Variant = MCSymbolRefExpr::VK_None;
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000836 return Error(SMLoc::getFromPointer(Split.second.begin()),
837 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000838 }
839 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000840
Hans Wennborgce69d772013-10-18 20:46:28 +0000841 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
842
Daniel Dunbard20cda02009-10-16 01:34:54 +0000843 // If this is an absolute variable reference, substitute it now to preserve
844 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000845 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000846 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000847 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000848
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000849 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000850 return false;
851 }
852
853 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000854 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000855 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000856 }
Kevin Enderby0510b482010-05-17 23:08:19 +0000857 case AsmToken::Integer: {
858 SMLoc Loc = getTok().getLoc();
859 int64_t IntVal = getTok().getIntVal();
860 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000861 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000862 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000863 // Look for 'b' or 'f' following an Integer as a directional label
864 if (Lexer.getKind() == AsmToken::Identifier) {
865 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000866 // Lookup the symbol variant if used.
867 std::pair<StringRef, StringRef> Split = IDVal.split('@');
868 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
869 if (Split.first.size() != IDVal.size()) {
870 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
871 if (Variant == MCSymbolRefExpr::VK_Invalid) {
872 Variant = MCSymbolRefExpr::VK_None;
873 return TokError("invalid variant '" + Split.second + "'");
874 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000875 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000876 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000877 if (IDVal == "f" || IDVal == "b") {
878 MCSymbol *Sym =
879 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000880 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000881 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000882 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000883 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000884 Lex(); // Eat identifier.
885 }
886 }
Chris Lattner78db3622009-06-22 05:51:26 +0000887 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000888 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000889 case AsmToken::Real: {
890 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000891 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000892 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000893 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000894 Lex(); // Eat token.
895 return false;
896 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000897 case AsmToken::Dot: {
898 // This is a '.' reference, which references the current PC. Emit a
899 // temporary label to the streamer and refer to it.
900 MCSymbol *Sym = Ctx.CreateTempSymbol();
901 Out.EmitLabel(Sym);
902 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000903 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000904 Lex(); // Eat identifier.
905 return false;
906 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000907 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000908 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000909 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000910 case AsmToken::LBrac:
911 if (!PlatformParser->HasBracketExpressions())
912 return TokError("brackets expression not supported on this target");
913 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000914 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000915 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000916 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000917 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000918 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000919 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000921 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000922 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000923 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000924 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000925 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000926 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000927 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000928 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000929 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000930 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000931 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000932 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000933 }
934}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000935
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000936bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000937 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000938 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000939}
940
Daniel Dunbar55f16672010-09-17 02:47:07 +0000941const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000942AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000943 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000944 // Ask the target implementation about this expression first.
945 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
946 if (NewE)
947 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000948 // Recurse over the given expression, rebuilding it to apply the given variant
949 // if there is exactly one symbol.
950 switch (E->getKind()) {
951 case MCExpr::Target:
952 case MCExpr::Constant:
953 return 0;
954
955 case MCExpr::SymbolRef: {
956 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
957
958 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000959 TokError("invalid variant on expression '" + getTok().getIdentifier() +
960 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000961 return E;
962 }
963
964 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
965 }
966
967 case MCExpr::Unary: {
968 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000969 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000970 if (!Sub)
971 return 0;
972 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
973 }
974
975 case MCExpr::Binary: {
976 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000977 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
978 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000979
980 if (!LHS && !RHS)
981 return 0;
982
Jim Grosbach4b905842013-09-20 23:08:21 +0000983 if (!LHS)
984 LHS = BE->getLHS();
985 if (!RHS)
986 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000987
988 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
989 }
990 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000991
Craig Toppera2886c22012-02-07 05:05:23 +0000992 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000993}
994
Jim Grosbach4b905842013-09-20 23:08:21 +0000995/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000996///
Jim Grosbachbd164242011-08-20 16:24:13 +0000997/// expr ::= expr &&,|| expr -> lowest.
998/// expr ::= expr |,^,&,! expr
999/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1000/// expr ::= expr <<,>> expr
1001/// expr ::= expr +,- expr
1002/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001003/// expr ::= primaryexpr
1004///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001005bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001006 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001007 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001008 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001009 return true;
1010
Daniel Dunbar55f16672010-09-17 02:47:07 +00001011 // As a special case, we support 'a op b @ modifier' by rewriting the
1012 // expression to include the modifier. This is inefficient, but in general we
1013 // expect users to use 'a@modifier op b'.
1014 if (Lexer.getKind() == AsmToken::At) {
1015 Lex();
1016
1017 if (Lexer.isNot(AsmToken::Identifier))
1018 return TokError("unexpected symbol modifier following '@'");
1019
1020 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001021 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001022 if (Variant == MCSymbolRefExpr::VK_Invalid)
1023 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1024
Jim Grosbach4b905842013-09-20 23:08:21 +00001025 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001026 if (!ModifiedRes) {
1027 return TokError("invalid modifier '" + getTok().getIdentifier() +
1028 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001029 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001030
Daniel Dunbar55f16672010-09-17 02:47:07 +00001031 Res = ModifiedRes;
1032 Lex();
1033 }
1034
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001035 // Try to constant fold it up front, if possible.
1036 int64_t Value;
1037 if (Res->EvaluateAsAbsolute(Value))
1038 Res = MCConstantExpr::Create(Value, getContext());
1039
1040 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001041}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001042
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001043bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001044 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001045 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001046}
1047
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001048bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001049 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001050
Daniel Dunbar75630b32009-06-30 02:10:03 +00001051 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001052 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001053 return true;
1054
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001055 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001056 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001057
1058 return false;
1059}
1060
Michael J. Spencer530ce852010-10-09 11:00:50 +00001061static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001062 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001063 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001064 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001065 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001066
Jim Grosbach4b905842013-09-20 23:08:21 +00001067 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001068 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001069 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001070 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001071 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001072 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001073 return 1;
1074
Jim Grosbach4b905842013-09-20 23:08:21 +00001075 // Low Precedence: |, &, ^
1076 //
1077 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001078 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001079 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001080 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001081 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001082 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001083 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001084 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001085 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001086 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001087
Jim Grosbach4b905842013-09-20 23:08:21 +00001088 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 case AsmToken::EqualEqual:
1090 Kind = MCBinaryExpr::EQ;
1091 return 3;
1092 case AsmToken::ExclaimEqual:
1093 case AsmToken::LessGreater:
1094 Kind = MCBinaryExpr::NE;
1095 return 3;
1096 case AsmToken::Less:
1097 Kind = MCBinaryExpr::LT;
1098 return 3;
1099 case AsmToken::LessEqual:
1100 Kind = MCBinaryExpr::LTE;
1101 return 3;
1102 case AsmToken::Greater:
1103 Kind = MCBinaryExpr::GT;
1104 return 3;
1105 case AsmToken::GreaterEqual:
1106 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001107 return 3;
1108
Jim Grosbach4b905842013-09-20 23:08:21 +00001109 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001110 case AsmToken::LessLess:
1111 Kind = MCBinaryExpr::Shl;
1112 return 4;
1113 case AsmToken::GreaterGreater:
1114 Kind = MCBinaryExpr::Shr;
1115 return 4;
1116
Jim Grosbach4b905842013-09-20 23:08:21 +00001117 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001118 case AsmToken::Plus:
1119 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001120 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001121 case AsmToken::Minus:
1122 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001123 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001124
Jim Grosbach4b905842013-09-20 23:08:21 +00001125 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001126 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001127 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001128 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001129 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001130 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001131 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001132 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001133 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001135 }
1136}
1137
Jim Grosbach4b905842013-09-20 23:08:21 +00001138/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001139/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001140bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001141 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001142 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001143 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001144 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001145
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001146 // If the next token is lower precedence than we are allowed to eat, return
1147 // successfully with what we ate already.
1148 if (TokPrec < Precedence)
1149 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001150
Sean Callanan686ed8d2010-01-19 20:22:31 +00001151 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001152
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001153 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001154 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001155 if (parsePrimaryExpr(RHS, EndLoc))
1156 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001157
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001158 // If BinOp binds less tightly with RHS than the operator after RHS, let
1159 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001160 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001161 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001162 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1163 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001164
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001165 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001166 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167 }
1168}
1169
Chris Lattner36e02122009-06-21 20:54:55 +00001170/// ParseStatement:
1171/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001172/// ::= Label* Directive ...Operands... EndOfStatement
1173/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001174bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001175 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001176 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001177 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001178 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001179 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001180
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001181 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001182 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001183 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001184 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001185 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001186 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001187 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001188 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001189
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001190 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001191 if (Lexer.is(AsmToken::Integer)) {
1192 LocalLabelVal = getTok().getIntVal();
1193 if (LocalLabelVal < 0) {
1194 if (!TheCondState.Ignore)
1195 return TokError("unexpected token at start of statement");
1196 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001197 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001198 IDVal = getTok().getString();
1199 Lex(); // Consume the integer token to be used as an identifier token.
1200 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001201 if (!TheCondState.Ignore)
1202 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001203 }
1204 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001205 } else if (Lexer.is(AsmToken::Dot)) {
1206 // Treat '.' as a valid identifier in this context.
1207 Lex();
1208 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001209 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001210 if (!TheCondState.Ignore)
1211 return TokError("unexpected token at start of statement");
1212 IDVal = "";
1213 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001214
Chris Lattner926885c2010-04-17 18:14:27 +00001215 // Handle conditional assembly here before checking for skipping. We
1216 // have to do this so that .endif isn't skipped in a ".if 0" block for
1217 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001218 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001219 DirectiveKindMap.find(IDVal);
1220 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1221 ? DK_NO_DIRECTIVE
1222 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001223 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001224 default:
1225 break;
1226 case DK_IF:
1227 return parseDirectiveIf(IDLoc);
1228 case DK_IFB:
1229 return parseDirectiveIfb(IDLoc, true);
1230 case DK_IFNB:
1231 return parseDirectiveIfb(IDLoc, false);
1232 case DK_IFC:
1233 return parseDirectiveIfc(IDLoc, true);
1234 case DK_IFNC:
1235 return parseDirectiveIfc(IDLoc, false);
1236 case DK_IFDEF:
1237 return parseDirectiveIfdef(IDLoc, true);
1238 case DK_IFNDEF:
1239 case DK_IFNOTDEF:
1240 return parseDirectiveIfdef(IDLoc, false);
1241 case DK_ELSEIF:
1242 return parseDirectiveElseIf(IDLoc);
1243 case DK_ELSE:
1244 return parseDirectiveElse(IDLoc);
1245 case DK_ENDIF:
1246 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001247 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001248
Eli Bendersky88024712013-01-16 19:32:36 +00001249 // Ignore the statement if in the middle of inactive conditional
1250 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001251 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001252 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001253 return false;
1254 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001255
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001256 // FIXME: Recurse on local labels?
1257
1258 // See what kind of statement we have.
1259 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001260 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001261 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001262
Chris Lattner36e02122009-06-21 20:54:55 +00001263 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001264 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001265
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001266 // Diagnose attempt to use '.' as a label.
1267 if (IDVal == ".")
1268 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1269
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001270 // Diagnose attempt to use a variable as a label.
1271 //
1272 // FIXME: Diagnostics. Note the location of the definition as a label.
1273 // FIXME: This doesn't diagnose assignment to a symbol which has been
1274 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001275 MCSymbol *Sym;
1276 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001277 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001278 else
1279 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001280 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001281 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001282
Daniel Dunbare73b2672009-08-26 22:13:22 +00001283 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001284 if (!ParsingInlineAsm)
1285 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001286
Kevin Enderbye7739d42011-12-09 18:09:40 +00001287 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001288 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001289 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001290 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1291 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001292
Tim Northover1744d0a2013-10-25 12:49:50 +00001293 getTargetParser().onLabelParsed(Sym);
1294
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001295 // Consume any end of statement token, if present, to avoid spurious
1296 // AddBlankLine calls().
1297 if (Lexer.is(AsmToken::EndOfStatement)) {
1298 Lex();
1299 if (Lexer.is(AsmToken::Eof))
1300 return false;
1301 }
1302
Eli Friedman0f4871d2012-10-22 23:58:19 +00001303 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001304 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001305
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001306 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001307 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001308 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001309
Jim Grosbach4b905842013-09-20 23:08:21 +00001310 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001311
1312 default: // Normal instruction or directive.
1313 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001314 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001315
1316 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001317 if (areMacrosEnabled())
1318 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1319 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001320 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001321
Michael J. Spencer530ce852010-10-09 11:00:50 +00001322 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001323
Eli Bendersky17233942013-01-15 22:59:42 +00001324 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001325 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001326 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001327 //
Eli Bendersky17233942013-01-15 22:59:42 +00001328 // 1. The target-specific assembly parser. Some directives are target
1329 // specific or may potentially behave differently on certain targets.
1330 // 2. Asm parser extensions. For example, platform-specific parsers
1331 // (like the ELF parser) register themselves as extensions.
1332 // 3. The generic directive parser implemented by this class. These are
1333 // all the directives that behave in a target and platform independent
1334 // manner, or at least have a default behavior that's shared between
1335 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001336
Eli Bendersky17233942013-01-15 22:59:42 +00001337 // First query the target-specific parser. It will return 'true' if it
1338 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001339 if (!getTargetParser().ParseDirective(ID))
1340 return false;
1341
Alp Tokercb402912014-01-24 17:20:08 +00001342 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001343 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001344 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1345 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001346 if (Handler.first)
1347 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1348
1349 // Finally, if no one else is interested in this directive, it must be
1350 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001351 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001352 default:
1353 break;
1354 case DK_SET:
1355 case DK_EQU:
1356 return parseDirectiveSet(IDVal, true);
1357 case DK_EQUIV:
1358 return parseDirectiveSet(IDVal, false);
1359 case DK_ASCII:
1360 return parseDirectiveAscii(IDVal, false);
1361 case DK_ASCIZ:
1362 case DK_STRING:
1363 return parseDirectiveAscii(IDVal, true);
1364 case DK_BYTE:
1365 return parseDirectiveValue(1);
1366 case DK_SHORT:
1367 case DK_VALUE:
1368 case DK_2BYTE:
1369 return parseDirectiveValue(2);
1370 case DK_LONG:
1371 case DK_INT:
1372 case DK_4BYTE:
1373 return parseDirectiveValue(4);
1374 case DK_QUAD:
1375 case DK_8BYTE:
1376 return parseDirectiveValue(8);
1377 case DK_SINGLE:
1378 case DK_FLOAT:
1379 return parseDirectiveRealValue(APFloat::IEEEsingle);
1380 case DK_DOUBLE:
1381 return parseDirectiveRealValue(APFloat::IEEEdouble);
1382 case DK_ALIGN: {
1383 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1384 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1385 }
1386 case DK_ALIGN32: {
1387 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1388 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1389 }
1390 case DK_BALIGN:
1391 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1392 case DK_BALIGNW:
1393 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1394 case DK_BALIGNL:
1395 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1396 case DK_P2ALIGN:
1397 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1398 case DK_P2ALIGNW:
1399 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1400 case DK_P2ALIGNL:
1401 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1402 case DK_ORG:
1403 return parseDirectiveOrg();
1404 case DK_FILL:
1405 return parseDirectiveFill();
1406 case DK_ZERO:
1407 return parseDirectiveZero();
1408 case DK_EXTERN:
1409 eatToEndOfStatement(); // .extern is the default, ignore it.
1410 return false;
1411 case DK_GLOBL:
1412 case DK_GLOBAL:
1413 return parseDirectiveSymbolAttribute(MCSA_Global);
1414 case DK_LAZY_REFERENCE:
1415 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1416 case DK_NO_DEAD_STRIP:
1417 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1418 case DK_SYMBOL_RESOLVER:
1419 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1420 case DK_PRIVATE_EXTERN:
1421 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1422 case DK_REFERENCE:
1423 return parseDirectiveSymbolAttribute(MCSA_Reference);
1424 case DK_WEAK_DEFINITION:
1425 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1426 case DK_WEAK_REFERENCE:
1427 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1428 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1429 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1430 case DK_COMM:
1431 case DK_COMMON:
1432 return parseDirectiveComm(/*IsLocal=*/false);
1433 case DK_LCOMM:
1434 return parseDirectiveComm(/*IsLocal=*/true);
1435 case DK_ABORT:
1436 return parseDirectiveAbort();
1437 case DK_INCLUDE:
1438 return parseDirectiveInclude();
1439 case DK_INCBIN:
1440 return parseDirectiveIncbin();
1441 case DK_CODE16:
1442 case DK_CODE16GCC:
1443 return TokError(Twine(IDVal) + " not supported yet");
1444 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001445 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001446 case DK_IRP:
1447 return parseDirectiveIrp(IDLoc);
1448 case DK_IRPC:
1449 return parseDirectiveIrpc(IDLoc);
1450 case DK_ENDR:
1451 return parseDirectiveEndr(IDLoc);
1452 case DK_BUNDLE_ALIGN_MODE:
1453 return parseDirectiveBundleAlignMode();
1454 case DK_BUNDLE_LOCK:
1455 return parseDirectiveBundleLock();
1456 case DK_BUNDLE_UNLOCK:
1457 return parseDirectiveBundleUnlock();
1458 case DK_SLEB128:
1459 return parseDirectiveLEB128(true);
1460 case DK_ULEB128:
1461 return parseDirectiveLEB128(false);
1462 case DK_SPACE:
1463 case DK_SKIP:
1464 return parseDirectiveSpace(IDVal);
1465 case DK_FILE:
1466 return parseDirectiveFile(IDLoc);
1467 case DK_LINE:
1468 return parseDirectiveLine();
1469 case DK_LOC:
1470 return parseDirectiveLoc();
1471 case DK_STABS:
1472 return parseDirectiveStabs();
1473 case DK_CFI_SECTIONS:
1474 return parseDirectiveCFISections();
1475 case DK_CFI_STARTPROC:
1476 return parseDirectiveCFIStartProc();
1477 case DK_CFI_ENDPROC:
1478 return parseDirectiveCFIEndProc();
1479 case DK_CFI_DEF_CFA:
1480 return parseDirectiveCFIDefCfa(IDLoc);
1481 case DK_CFI_DEF_CFA_OFFSET:
1482 return parseDirectiveCFIDefCfaOffset();
1483 case DK_CFI_ADJUST_CFA_OFFSET:
1484 return parseDirectiveCFIAdjustCfaOffset();
1485 case DK_CFI_DEF_CFA_REGISTER:
1486 return parseDirectiveCFIDefCfaRegister(IDLoc);
1487 case DK_CFI_OFFSET:
1488 return parseDirectiveCFIOffset(IDLoc);
1489 case DK_CFI_REL_OFFSET:
1490 return parseDirectiveCFIRelOffset(IDLoc);
1491 case DK_CFI_PERSONALITY:
1492 return parseDirectiveCFIPersonalityOrLsda(true);
1493 case DK_CFI_LSDA:
1494 return parseDirectiveCFIPersonalityOrLsda(false);
1495 case DK_CFI_REMEMBER_STATE:
1496 return parseDirectiveCFIRememberState();
1497 case DK_CFI_RESTORE_STATE:
1498 return parseDirectiveCFIRestoreState();
1499 case DK_CFI_SAME_VALUE:
1500 return parseDirectiveCFISameValue(IDLoc);
1501 case DK_CFI_RESTORE:
1502 return parseDirectiveCFIRestore(IDLoc);
1503 case DK_CFI_ESCAPE:
1504 return parseDirectiveCFIEscape();
1505 case DK_CFI_SIGNAL_FRAME:
1506 return parseDirectiveCFISignalFrame();
1507 case DK_CFI_UNDEFINED:
1508 return parseDirectiveCFIUndefined(IDLoc);
1509 case DK_CFI_REGISTER:
1510 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001511 case DK_CFI_WINDOW_SAVE:
1512 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001513 case DK_MACROS_ON:
1514 case DK_MACROS_OFF:
1515 return parseDirectiveMacrosOnOff(IDVal);
1516 case DK_MACRO:
1517 return parseDirectiveMacro(IDLoc);
1518 case DK_ENDM:
1519 case DK_ENDMACRO:
1520 return parseDirectiveEndMacro(IDVal);
1521 case DK_PURGEM:
1522 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001523 case DK_END:
1524 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001525 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001526
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001527 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001528 }
Chris Lattner36e02122009-06-21 20:54:55 +00001529
Chad Rosierc7f552c2013-02-12 21:33:51 +00001530 // __asm _emit or __asm __emit
1531 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1532 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001533 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001534
1535 // __asm align
1536 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001537 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001538
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001539 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001540
Chris Lattner7cbfa442010-05-19 23:34:33 +00001541 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001542 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001543 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001544 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001545 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001546 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001547
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001548 // Dump the parsed representation, if requested.
1549 if (getShowParsedOperands()) {
1550 SmallString<256> Str;
1551 raw_svector_ostream OS(Str);
1552 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001553 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001554 if (i != 0)
1555 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001556 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001557 }
1558 OS << "]";
1559
Jim Grosbach4b905842013-09-20 23:08:21 +00001560 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001561 }
1562
Kevin Enderby6469fc22011-11-01 22:27:22 +00001563 // If we are generating dwarf for assembly source files and the current
1564 // section is the initial text section then generate a .loc directive for
1565 // the instruction.
1566 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001567 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001568 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001569
Eli Bendersky88024712013-01-16 19:32:36 +00001570 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001571
Eli Bendersky88024712013-01-16 19:32:36 +00001572 // If we previously parsed a cpp hash file line comment then make sure the
1573 // current Dwarf File is for the CppHashFilename if not then emit the
1574 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001575 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001576 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001577 if (CppHashFilename.size() != 0) {
1578 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001579 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001580 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001581 getContext().nextGenDwarfFileNumber(), StringRef(),
1582 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001583
Jim Grosbach4b905842013-09-20 23:08:21 +00001584 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1585 // cache with the different Loc from the call above we save the last
1586 // info we queried here with SrcMgr.FindLineNumber().
1587 unsigned CppHashLocLineNo;
1588 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1589 CppHashLocLineNo = LastQueryLine;
1590 else {
1591 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1592 LastQueryLine = CppHashLocLineNo;
1593 LastQueryIDLoc = CppHashLoc;
1594 LastQueryBuffer = CppHashBuf;
1595 }
1596 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001597 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001598
Jim Grosbach4b905842013-09-20 23:08:21 +00001599 getStreamer().EmitDwarfLocDirective(
1600 getContext().getGenDwarfFileNumber(), Line, 0,
1601 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1602 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001603 }
1604
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001605 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001606 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001607 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001608 HadError = getTargetParser().MatchAndEmitInstruction(
1609 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1610 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001611 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001612
Chris Lattnera2a9d162010-09-11 16:18:25 +00001613 // Don't skip the rest of the line, the instruction parser is responsible for
1614 // that.
1615 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001616}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001617
Jim Grosbach4b905842013-09-20 23:08:21 +00001618/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001619/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001620void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001621 if (!Lexer.is(AsmToken::EndOfStatement))
1622 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001623 // Eat EOL.
1624 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001625}
1626
Jim Grosbach4b905842013-09-20 23:08:21 +00001627/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001628/// ::= # number "filename"
1629/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001630bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001631 Lex(); // Eat the hash token.
1632
1633 if (getLexer().isNot(AsmToken::Integer)) {
1634 // Consume the line since in cases it is not a well-formed line directive,
1635 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001636 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001637 return false;
1638 }
1639
1640 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001641 Lex();
1642
1643 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001644 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001645 return false;
1646 }
1647
1648 StringRef Filename = getTok().getString();
1649 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001650 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001651
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001652 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1653 CppHashLoc = L;
1654 CppHashFilename = Filename;
1655 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001656 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001657
1658 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001659 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001660 return false;
1661}
1662
Jim Grosbach4b905842013-09-20 23:08:21 +00001663/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001664/// for the Filename and LineNo if any in the diagnostic.
1665void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001666 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001667 raw_ostream &OS = errs();
1668
1669 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1670 const SMLoc &DiagLoc = Diag.getLoc();
1671 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1672 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1673
Jim Grosbach4b905842013-09-20 23:08:21 +00001674 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001675 // before printing the message.
1676 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001677 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001678 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1679 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001680 }
1681
Eric Christophera7c32732012-12-18 00:30:54 +00001682 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001683 // manager changed or buffer changed (like in a nested include) then just
1684 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001685 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001686 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001687 if (Parser->SavedDiagHandler)
1688 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1689 else
1690 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001691 return;
1692 }
1693
Eric Christophera7c32732012-12-18 00:30:54 +00001694 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001695 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1696 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001697 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001698
1699 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1700 int CppHashLocLineNo =
1701 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001702 int LineNo =
1703 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001704
Jim Grosbach4b905842013-09-20 23:08:21 +00001705 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1706 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001707 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001708
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001709 if (Parser->SavedDiagHandler)
1710 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1711 else
1712 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713}
1714
Rafael Espindola2c064482012-08-21 18:29:30 +00001715// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1716// difference being that that function accepts '@' as part of identifiers and
1717// we can't do that. AsmLexer.cpp should probably be changed to handle
1718// '@' as a special case when needed.
1719static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001720 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1721 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001722}
1723
Rafael Espindola34b9c512012-06-03 23:57:14 +00001724bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +00001725 const MCAsmMacroParameters &Parameters,
Jim Grosbach4b905842013-09-20 23:08:21 +00001726 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001727 unsigned NParameters = Parameters.size();
1728 if (NParameters != 0 && NParameters != A.size())
1729 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001730
Preston Gurd05500642012-09-19 20:36:12 +00001731 // A macro without parameters is handled differently on Darwin:
1732 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001733 while (!Body.empty()) {
1734 // Scan for the next substitution.
1735 std::size_t End = Body.size(), Pos = 0;
1736 for (; Pos != End; ++Pos) {
1737 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001738 if (!NParameters) {
1739 // This macro has no parameters, look for $0, $1, etc.
1740 if (Body[Pos] != '$' || Pos + 1 == End)
1741 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001742
Rafael Espindola1134ab232011-06-05 02:43:45 +00001743 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001744 if (Next == '$' || Next == 'n' ||
1745 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001746 break;
1747 } else {
1748 // This macro has parameters, look for \foo, \bar, etc.
1749 if (Body[Pos] == '\\' && Pos + 1 != End)
1750 break;
1751 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001752 }
1753
1754 // Add the prefix.
1755 OS << Body.slice(0, Pos);
1756
1757 // Check if we reached the end.
1758 if (Pos == End)
1759 break;
1760
Rafael Espindola1134ab232011-06-05 02:43:45 +00001761 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001762 switch (Body[Pos + 1]) {
1763 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001764 case '$':
1765 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001766 break;
1767
Jim Grosbach4b905842013-09-20 23:08:21 +00001768 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001769 case 'n':
1770 OS << A.size();
1771 break;
1772
Jim Grosbach4b905842013-09-20 23:08:21 +00001773 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 default: {
1775 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001776 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001777 if (Index >= A.size())
1778 break;
1779
1780 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001781 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001782 ie = A[Index].end();
1783 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001784 OS << it->getString();
1785 break;
1786 }
1787 }
1788 Pos += 2;
1789 } else {
1790 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001791 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001792 ++I;
1793
Jim Grosbach4b905842013-09-20 23:08:21 +00001794 const char *Begin = Body.data() + Pos + 1;
1795 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001796 unsigned Index = 0;
1797 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001798 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001799 break;
1800
Preston Gurd05500642012-09-19 20:36:12 +00001801 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001802 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1803 Pos += 3;
1804 else {
1805 OS << '\\' << Argument;
1806 Pos = I;
1807 }
Preston Gurd05500642012-09-19 20:36:12 +00001808 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001809 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001810 ie = A[Index].end();
1811 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001812 if (it->getKind() == AsmToken::String)
1813 OS << it->getStringContents();
1814 else
1815 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001816
Preston Gurd05500642012-09-19 20:36:12 +00001817 Pos += 1 + Argument.size();
1818 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001819 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001820 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001821 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001822 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001823
Rafael Espindola1134ab232011-06-05 02:43:45 +00001824 return false;
1825}
Daniel Dunbar43235712010-07-18 18:54:11 +00001826
Jim Grosbach4b905842013-09-20 23:08:21 +00001827MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1828 SMLoc EL, MemoryBuffer *I)
1829 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1830 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001831
Jim Grosbach4b905842013-09-20 23:08:21 +00001832static bool isOperator(AsmToken::TokenKind kind) {
1833 switch (kind) {
1834 default:
1835 return false;
1836 case AsmToken::Plus:
1837 case AsmToken::Minus:
1838 case AsmToken::Tilde:
1839 case AsmToken::Slash:
1840 case AsmToken::Star:
1841 case AsmToken::Dot:
1842 case AsmToken::Equal:
1843 case AsmToken::EqualEqual:
1844 case AsmToken::Pipe:
1845 case AsmToken::PipePipe:
1846 case AsmToken::Caret:
1847 case AsmToken::Amp:
1848 case AsmToken::AmpAmp:
1849 case AsmToken::Exclaim:
1850 case AsmToken::ExclaimEqual:
1851 case AsmToken::Percent:
1852 case AsmToken::Less:
1853 case AsmToken::LessEqual:
1854 case AsmToken::LessLess:
1855 case AsmToken::LessGreater:
1856 case AsmToken::Greater:
1857 case AsmToken::GreaterEqual:
1858 case AsmToken::GreaterGreater:
1859 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001860 }
1861}
1862
David Majnemer16252452014-01-29 00:07:39 +00001863namespace {
1864class AsmLexerSkipSpaceRAII {
1865public:
1866 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1867 Lexer.setSkipSpace(SkipSpace);
1868 }
1869
1870 ~AsmLexerSkipSpaceRAII() {
1871 Lexer.setSkipSpace(true);
1872 }
1873
1874private:
1875 AsmLexer &Lexer;
1876};
1877}
1878
David Majnemer91fc4c22014-01-29 18:57:46 +00001879bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001880 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001881 unsigned AddTokens = 0;
1882
David Majnemer16252452014-01-29 00:07:39 +00001883 // Darwin doesn't use spaces to delmit arguments.
1884 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001885
1886 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001887 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001888 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001889
David Majnemer91fc4c22014-01-29 18:57:46 +00001890 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001891 break;
Preston Gurd05500642012-09-19 20:36:12 +00001892
1893 if (Lexer.is(AsmToken::Space)) {
1894 Lex(); // Eat spaces
1895
1896 // Spaces can delimit parameters, but could also be part an expression.
1897 // If the token after a space is an operator, add the token and the next
1898 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001899 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001900 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001901 // Check to see whether the token is used as an operator,
1902 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001903 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001904 if (*NextChar == ' ')
1905 AddTokens = 2;
1906 }
1907
1908 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001909 break;
1910 }
1911 }
1912 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001913
Jim Grosbach4b905842013-09-20 23:08:21 +00001914 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001915 // to be able to fill in the remaining default parameter values
1916 if (Lexer.is(AsmToken::EndOfStatement))
1917 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001918
1919 // Adjust the current parentheses level.
1920 if (Lexer.is(AsmToken::LParen))
1921 ++ParenLevel;
1922 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1923 --ParenLevel;
1924
1925 // Append the token to the current argument list.
1926 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001927 if (AddTokens)
1928 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001929 Lex();
1930 }
Preston Gurd05500642012-09-19 20:36:12 +00001931
Rafael Espindola768b41c2012-06-15 14:02:34 +00001932 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001933 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001934 return false;
1935}
1936
1937// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001938bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001939 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001940 const unsigned NParameters = M ? M->Parameters.size() : 0;
1941
1942 // Parse two kinds of macro invocations:
1943 // - macros defined without any parameters accept an arbitrary number of them
1944 // - macros defined with parameters accept at most that many of them
1945 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1946 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001947 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001948
David Majnemer91fc4c22014-01-29 18:57:46 +00001949 if (parseMacroArgument(MA))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001950 return true;
1951
David Majnemer91fc4c22014-01-29 18:57:46 +00001952 if (!MA.empty() || (!NParameters && !Lexer.is(AsmToken::EndOfStatement)))
Preston Gurd242ed3152012-09-19 20:29:04 +00001953 A.push_back(MA);
1954 else if (NParameters) {
1955 if (!M->Parameters[Parameter].second.empty())
1956 A.push_back(M->Parameters[Parameter].second);
David Majnemer91fc4c22014-01-29 18:57:46 +00001957 else
1958 A.push_back(MA);
Preston Gurd242ed3152012-09-19 20:29:04 +00001959 }
Jim Grosbach206661622012-07-30 22:44:17 +00001960
Preston Gurd242ed3152012-09-19 20:29:04 +00001961 // At the end of the statement, fill in remaining arguments that have
1962 // default values. If there aren't any, then the next argument is
1963 // required but missing
1964 if (Lexer.is(AsmToken::EndOfStatement)) {
1965 if (NParameters && Parameter < NParameters - 1) {
David Majnemer91fc4c22014-01-29 18:57:46 +00001966 continue;
Preston Gurd242ed3152012-09-19 20:29:04 +00001967 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001968 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001969 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001970
1971 if (Lexer.is(AsmToken::Comma))
1972 Lex();
1973 }
1974 return TokError("Too many arguments");
1975}
1976
Jim Grosbach4b905842013-09-20 23:08:21 +00001977const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1978 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001979 return (I == MacroMap.end()) ? NULL : I->getValue();
1980}
1981
Jim Grosbach4b905842013-09-20 23:08:21 +00001982void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001983 MacroMap[Name] = new MCAsmMacro(Macro);
1984}
1985
Jim Grosbach4b905842013-09-20 23:08:21 +00001986void AsmParser::undefineMacro(StringRef Name) {
1987 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001988 if (I != MacroMap.end()) {
1989 delete I->getValue();
1990 MacroMap.erase(I);
1991 }
1992}
1993
Jim Grosbach4b905842013-09-20 23:08:21 +00001994bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00001995 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1996 // this, although we should protect against infinite loops.
1997 if (ActiveMacros.size() == 20)
1998 return TokError("macros cannot be nested more than 20 levels deep");
1999
Eli Bendersky38274122013-01-14 23:22:36 +00002000 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002001 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002002 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002003
Rafael Espindola1134ab232011-06-05 02:43:45 +00002004 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2005 // to hold the macro body with substitutions.
2006 SmallString<256> Buf;
2007 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002008 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002009
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002010 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002011 return true;
2012
Eli Bendersky38274122013-01-14 23:22:36 +00002013 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002014 // instantiation.
2015 OS << ".endmacro\n";
2016
Rafael Espindola1134ab232011-06-05 02:43:45 +00002017 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002018 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002019
Daniel Dunbar43235712010-07-18 18:54:11 +00002020 // Create the macro instantiation object and add to the current macro
2021 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002022 MacroInstantiation *MI = new MacroInstantiation(
2023 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002024 ActiveMacros.push_back(MI);
2025
2026 // Jump to the macro instantiation and prime the lexer.
2027 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2028 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2029 Lex();
2030
2031 return false;
2032}
2033
Jim Grosbach4b905842013-09-20 23:08:21 +00002034void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002035 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002036 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002037 Lex();
2038
2039 // Pop the instantiation entry.
2040 delete ActiveMacros.back();
2041 ActiveMacros.pop_back();
2042}
2043
Jim Grosbach4b905842013-09-20 23:08:21 +00002044static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002045 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002046 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002047 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2048 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002049 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002050 case MCExpr::Target:
2051 case MCExpr::Constant:
2052 return false;
2053 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002054 const MCSymbol &S =
2055 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002056 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002057 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002058 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002059 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002060 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002061 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002062 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002063
2064 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002065}
2066
Jim Grosbach4b905842013-09-20 23:08:21 +00002067bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002068 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002069 // FIXME: Use better location, we should use proper tokens.
2070 SMLoc EqualLoc = Lexer.getLoc();
2071
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002072 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002073 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002074 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002075
Rafael Espindola72f5f172012-01-28 05:57:00 +00002076 // Note: we don't count b as used in "a = b". This is to allow
2077 // a = b
2078 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002079
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002080 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002081 return TokError("unexpected token in assignment");
2082
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002083 // Error on assignment to '.'.
2084 if (Name == ".") {
2085 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2086 "(use '.space' or '.org').)"));
2087 }
2088
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002089 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002090 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002091
Daniel Dunbar5f339242009-10-16 01:57:39 +00002092 // Validate that the LHS is allowed to be a variable (either it has not been
2093 // used as a symbol, or it is an absolute symbol).
2094 MCSymbol *Sym = getContext().LookupSymbol(Name);
2095 if (Sym) {
2096 // Diagnose assignment to a label.
2097 //
2098 // FIXME: Diagnostics. Note the location of the definition as a label.
2099 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002100 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002101 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2102 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002103 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002104 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2105 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002106 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002107 return Error(EqualLoc, "redefinition of '" + Name + "'");
2108 else if (!Sym->isVariable())
2109 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002110 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002111 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002112 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002113
2114 // Don't count these checks as uses.
2115 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002116 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002117 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002118
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002119 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002120
2121 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002122 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002123 if (NoDeadStrip)
2124 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2125
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002126 return false;
2127}
2128
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002129/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002130/// ::= identifier
2131/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002132bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002133 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002134 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2135 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002136 // handle this as a context dependent token, instead we detect adjacent tokens
2137 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002138 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2139 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002140
Hans Wennborgce69d772013-10-18 20:46:28 +00002141 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002142 Lex();
2143 if (Lexer.isNot(AsmToken::Identifier))
2144 return true;
2145
Hans Wennborgce69d772013-10-18 20:46:28 +00002146 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2147 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002148 return true;
2149
2150 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002151 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002152 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002153 Lex();
2154 return false;
2155 }
2156
Jim Grosbach4b905842013-09-20 23:08:21 +00002157 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002158 return true;
2159
Sean Callanan936b0d32010-01-19 21:44:56 +00002160 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002161
Sean Callanan686ed8d2010-01-19 20:22:31 +00002162 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002163
2164 return false;
2165}
2166
Jim Grosbach4b905842013-09-20 23:08:21 +00002167/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002168/// ::= .equ identifier ',' expression
2169/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002170/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002171bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002172 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002173
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002174 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002175 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002176
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002177 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002178 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002179 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002180
Jim Grosbach4b905842013-09-20 23:08:21 +00002181 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002182}
2183
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002184bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002185 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002186
2187 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002188 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002189 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2190 if (Str[i] != '\\') {
2191 Data += Str[i];
2192 continue;
2193 }
2194
2195 // Recognize escaped characters. Note that this escape semantics currently
2196 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2197 ++i;
2198 if (i == e)
2199 return TokError("unexpected backslash at end of string");
2200
2201 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002202 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002203 // Consume up to three octal characters.
2204 unsigned Value = Str[i] - '0';
2205
Jim Grosbach4b905842013-09-20 23:08:21 +00002206 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002207 ++i;
2208 Value = Value * 8 + (Str[i] - '0');
2209
Jim Grosbach4b905842013-09-20 23:08:21 +00002210 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002211 ++i;
2212 Value = Value * 8 + (Str[i] - '0');
2213 }
2214 }
2215
2216 if (Value > 255)
2217 return TokError("invalid octal escape sequence (out of range)");
2218
Jim Grosbach4b905842013-09-20 23:08:21 +00002219 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002220 continue;
2221 }
2222
2223 // Otherwise recognize individual escapes.
2224 switch (Str[i]) {
2225 default:
2226 // Just reject invalid escape sequences for now.
2227 return TokError("invalid escape sequence (unrecognized character)");
2228
2229 case 'b': Data += '\b'; break;
2230 case 'f': Data += '\f'; break;
2231 case 'n': Data += '\n'; break;
2232 case 'r': Data += '\r'; break;
2233 case 't': Data += '\t'; break;
2234 case '"': Data += '"'; break;
2235 case '\\': Data += '\\'; break;
2236 }
2237 }
2238
2239 return false;
2240}
2241
Jim Grosbach4b905842013-09-20 23:08:21 +00002242/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002243/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002244bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002245 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002246 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002247
Daniel Dunbara10e5192009-06-24 23:30:00 +00002248 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002249 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002250 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002251
Daniel Dunbaref668c12009-08-14 18:19:52 +00002252 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002253 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002254 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002255
Rafael Espindola64e1af82013-07-02 15:49:13 +00002256 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002257 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002258 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002259
Sean Callanan686ed8d2010-01-19 20:22:31 +00002260 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002261
2262 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002263 break;
2264
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002265 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002266 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002267 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002268 }
2269 }
2270
Sean Callanan686ed8d2010-01-19 20:22:31 +00002271 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002272 return false;
2273}
2274
Jim Grosbach4b905842013-09-20 23:08:21 +00002275/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002276/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002277bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002278 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002279 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002280
Daniel Dunbara10e5192009-06-24 23:30:00 +00002281 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002282 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002283 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002284 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002285 return true;
2286
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002287 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002288 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2289 assert(Size <= 8 && "Invalid size");
2290 uint64_t IntValue = MCE->getValue();
2291 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2292 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002293 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002294 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002295 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002296
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002297 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002298 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002299
Daniel Dunbara10e5192009-06-24 23:30:00 +00002300 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002301 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002302 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002303 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002304 }
2305 }
2306
Sean Callanan686ed8d2010-01-19 20:22:31 +00002307 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002308 return false;
2309}
2310
Jim Grosbach4b905842013-09-20 23:08:21 +00002311/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002312/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002313bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002314 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002315 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002316
2317 for (;;) {
2318 // We don't truly support arithmetic on floating point expressions, so we
2319 // have to manually parse unary prefixes.
2320 bool IsNeg = false;
2321 if (getLexer().is(AsmToken::Minus)) {
2322 Lex();
2323 IsNeg = true;
2324 } else if (getLexer().is(AsmToken::Plus))
2325 Lex();
2326
Michael J. Spencer530ce852010-10-09 11:00:50 +00002327 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002328 getLexer().isNot(AsmToken::Real) &&
2329 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002330 return TokError("unexpected token in directive");
2331
2332 // Convert to an APFloat.
2333 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002334 StringRef IDVal = getTok().getString();
2335 if (getLexer().is(AsmToken::Identifier)) {
2336 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2337 Value = APFloat::getInf(Semantics);
2338 else if (!IDVal.compare_lower("nan"))
2339 Value = APFloat::getNaN(Semantics, false, ~0);
2340 else
2341 return TokError("invalid floating point literal");
2342 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002343 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002344 return TokError("invalid floating point literal");
2345 if (IsNeg)
2346 Value.changeSign();
2347
2348 // Consume the numeric token.
2349 Lex();
2350
2351 // Emit the value as an integer.
2352 APInt AsInt = Value.bitcastToAPInt();
2353 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002354 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002355
2356 if (getLexer().is(AsmToken::EndOfStatement))
2357 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002358
Daniel Dunbar2af16532010-09-24 01:59:56 +00002359 if (getLexer().isNot(AsmToken::Comma))
2360 return TokError("unexpected token in directive");
2361 Lex();
2362 }
2363 }
2364
2365 Lex();
2366 return false;
2367}
2368
Jim Grosbach4b905842013-09-20 23:08:21 +00002369/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002370/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002371bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002372 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002373
2374 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002375 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002376 return true;
2377
Rafael Espindolab91bac62010-10-05 19:42:57 +00002378 int64_t Val = 0;
2379 if (getLexer().is(AsmToken::Comma)) {
2380 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002381 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002382 return true;
2383 }
2384
Rafael Espindola922e3f42010-09-16 15:03:59 +00002385 if (getLexer().isNot(AsmToken::EndOfStatement))
2386 return TokError("unexpected token in '.zero' directive");
2387
2388 Lex();
2389
Rafael Espindola64e1af82013-07-02 15:49:13 +00002390 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002391
2392 return false;
2393}
2394
Jim Grosbach4b905842013-09-20 23:08:21 +00002395/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002396/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002397bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002398 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002399
David Majnemer522d3db2014-02-01 07:19:38 +00002400 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002401 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002402 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002403 return true;
2404
David Majnemer522d3db2014-02-01 07:19:38 +00002405 if (NumValues < 0) {
2406 Warning(RepeatLoc,
2407 "'.fill' directive with negative repeat count has no effect");
2408 NumValues = 0;
2409 }
2410
Roman Divackye33098f2013-09-24 17:44:41 +00002411 int64_t FillSize = 1;
2412 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002413
David Majnemer522d3db2014-02-01 07:19:38 +00002414 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002415 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2416 if (getLexer().isNot(AsmToken::Comma))
2417 return TokError("unexpected token in '.fill' directive");
2418 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002419
David Majnemer522d3db2014-02-01 07:19:38 +00002420 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002421 if (parseAbsoluteExpression(FillSize))
2422 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002423
Roman Divackye33098f2013-09-24 17:44:41 +00002424 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2425 if (getLexer().isNot(AsmToken::Comma))
2426 return TokError("unexpected token in '.fill' directive");
2427 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002428
David Majnemer522d3db2014-02-01 07:19:38 +00002429 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002430 if (parseAbsoluteExpression(FillExpr))
2431 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002432
Roman Divackye33098f2013-09-24 17:44:41 +00002433 if (getLexer().isNot(AsmToken::EndOfStatement))
2434 return TokError("unexpected token in '.fill' directive");
2435
2436 Lex();
2437 }
2438 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002439
David Majnemer522d3db2014-02-01 07:19:38 +00002440 if (FillSize < 0) {
2441 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2442 NumValues = 0;
2443 }
2444 if (FillSize > 8) {
2445 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2446 FillSize = 8;
2447 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002448
David Majnemer522d3db2014-02-01 07:19:38 +00002449 if (!isUInt<32>(FillExpr) && FillSize > 4)
2450 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2451
2452 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2453 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2454
2455 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2456 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2457 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2458 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002459
2460 return false;
2461}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002462
Jim Grosbach4b905842013-09-20 23:08:21 +00002463/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002464/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002465bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002466 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002467
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002468 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002469 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002470 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002471 return true;
2472
2473 // Parse optional fill expression.
2474 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002475 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2476 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002477 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002478 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002479
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002480 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002481 return true;
2482
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002483 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002484 return TokError("unexpected token in '.org' directive");
2485 }
2486
Sean Callanan686ed8d2010-01-19 20:22:31 +00002487 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002488
Jim Grosbachb5912772012-01-27 00:37:08 +00002489 // Only limited forms of relocatable expressions are accepted here, it
2490 // has to be relative to the current section. The streamer will return
2491 // 'true' if the expression wasn't evaluatable.
2492 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2493 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002494
2495 return false;
2496}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002497
Jim Grosbach4b905842013-09-20 23:08:21 +00002498/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002499/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002500bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002501 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002502
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002503 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002504 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002505 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002506 return true;
2507
2508 SMLoc MaxBytesLoc;
2509 bool HasFillExpr = false;
2510 int64_t FillExpr = 0;
2511 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002512 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2513 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002514 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002515 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002516
2517 // The fill expression can be omitted while specifying a maximum number of
2518 // alignment bytes, e.g:
2519 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002520 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002521 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002522 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002523 return true;
2524 }
2525
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002526 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2527 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002528 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002529 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002530
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002531 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002532 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002533 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002534
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002535 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002536 return TokError("unexpected token in directive");
2537 }
2538 }
2539
Sean Callanan686ed8d2010-01-19 20:22:31 +00002540 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002541
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002542 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002543 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002544
2545 // Compute alignment in bytes.
2546 if (IsPow2) {
2547 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002548 if (Alignment >= 32) {
2549 Error(AlignmentLoc, "invalid alignment value");
2550 Alignment = 31;
2551 }
2552
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002553 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002554 } else {
2555 // Reject alignments that aren't a power of two, for gas compatibility.
2556 if (!isPowerOf2_64(Alignment))
2557 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002558 }
2559
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002560 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002561 if (MaxBytesLoc.isValid()) {
2562 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002563 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002564 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002565 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002566 }
2567
2568 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002569 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002570 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002571 MaxBytesToFill = 0;
2572 }
2573 }
2574
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002575 // Check whether we should use optimal code alignment for this .align
2576 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002577 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002578 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2579 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002580 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002581 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002582 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002583 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2584 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002585 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002586
2587 return false;
2588}
2589
Jim Grosbach4b905842013-09-20 23:08:21 +00002590/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002591/// ::= .file [number] filename
2592/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002593bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002594 // FIXME: I'm not sure what this is.
2595 int64_t FileNumber = -1;
2596 SMLoc FileNumberLoc = getLexer().getLoc();
2597 if (getLexer().is(AsmToken::Integer)) {
2598 FileNumber = getTok().getIntVal();
2599 Lex();
2600
2601 if (FileNumber < 1)
2602 return TokError("file number less than one");
2603 }
2604
2605 if (getLexer().isNot(AsmToken::String))
2606 return TokError("unexpected token in '.file' directive");
2607
2608 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002609 // Allow the strings to have escaped octal character sequence.
2610 std::string Path = getTok().getString();
2611 if (parseEscapedString(Path))
2612 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002613 Lex();
2614
2615 StringRef Directory;
2616 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002617 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002618 if (getLexer().is(AsmToken::String)) {
2619 if (FileNumber == -1)
2620 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002621 if (parseEscapedString(FilenameData))
2622 return true;
2623 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002624 Directory = Path;
2625 Lex();
2626 } else {
2627 Filename = Path;
2628 }
2629
2630 if (getLexer().isNot(AsmToken::EndOfStatement))
2631 return TokError("unexpected token in '.file' directive");
2632
2633 if (FileNumber == -1)
2634 getStreamer().EmitFileDirective(Filename);
2635 else {
2636 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002637 Error(DirectiveLoc,
2638 "input can't have .file dwarf directives when -g is "
2639 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002640
2641 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2642 Error(FileNumberLoc, "file number already allocated");
2643 }
2644
2645 return false;
2646}
2647
Jim Grosbach4b905842013-09-20 23:08:21 +00002648/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002649/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002650bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002651 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2652 if (getLexer().isNot(AsmToken::Integer))
2653 return TokError("unexpected token in '.line' directive");
2654
2655 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002656 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002657 Lex();
2658
2659 // FIXME: Do something with the .line.
2660 }
2661
2662 if (getLexer().isNot(AsmToken::EndOfStatement))
2663 return TokError("unexpected token in '.line' directive");
2664
2665 return false;
2666}
2667
Jim Grosbach4b905842013-09-20 23:08:21 +00002668/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002669/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2670/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2671/// The first number is a file number, must have been previously assigned with
2672/// a .file directive, the second number is the line number and optionally the
2673/// third number is a column position (zero if not specified). The remaining
2674/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002675bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002676 if (getLexer().isNot(AsmToken::Integer))
2677 return TokError("unexpected token in '.loc' directive");
2678 int64_t FileNumber = getTok().getIntVal();
2679 if (FileNumber < 1)
2680 return TokError("file number less than one in '.loc' directive");
2681 if (!getContext().isValidDwarfFileNumber(FileNumber))
2682 return TokError("unassigned file number in '.loc' directive");
2683 Lex();
2684
2685 int64_t LineNumber = 0;
2686 if (getLexer().is(AsmToken::Integer)) {
2687 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002688 if (LineNumber < 0)
2689 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002690 Lex();
2691 }
2692
2693 int64_t ColumnPos = 0;
2694 if (getLexer().is(AsmToken::Integer)) {
2695 ColumnPos = getTok().getIntVal();
2696 if (ColumnPos < 0)
2697 return TokError("column position less than zero in '.loc' directive");
2698 Lex();
2699 }
2700
2701 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2702 unsigned Isa = 0;
2703 int64_t Discriminator = 0;
2704 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2705 for (;;) {
2706 if (getLexer().is(AsmToken::EndOfStatement))
2707 break;
2708
2709 StringRef Name;
2710 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002711 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002712 return TokError("unexpected token in '.loc' directive");
2713
2714 if (Name == "basic_block")
2715 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2716 else if (Name == "prologue_end")
2717 Flags |= DWARF2_FLAG_PROLOGUE_END;
2718 else if (Name == "epilogue_begin")
2719 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2720 else if (Name == "is_stmt") {
2721 Loc = getTok().getLoc();
2722 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002723 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002724 return true;
2725 // The expression must be the constant 0 or 1.
2726 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2727 int Value = MCE->getValue();
2728 if (Value == 0)
2729 Flags &= ~DWARF2_FLAG_IS_STMT;
2730 else if (Value == 1)
2731 Flags |= DWARF2_FLAG_IS_STMT;
2732 else
2733 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002734 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002735 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2736 }
Craig Topperf15655b2013-04-22 04:22:40 +00002737 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002738 Loc = getTok().getLoc();
2739 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002740 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002741 return true;
2742 // The expression must be a constant greater or equal to 0.
2743 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2744 int Value = MCE->getValue();
2745 if (Value < 0)
2746 return Error(Loc, "isa number less than zero");
2747 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002748 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002749 return Error(Loc, "isa number not a constant value");
2750 }
Craig Topperf15655b2013-04-22 04:22:40 +00002751 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002752 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002753 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002754 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002755 return Error(Loc, "unknown sub-directive in '.loc' directive");
2756 }
2757
2758 if (getLexer().is(AsmToken::EndOfStatement))
2759 break;
2760 }
2761 }
2762
2763 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2764 Isa, Discriminator, StringRef());
2765
2766 return false;
2767}
2768
Jim Grosbach4b905842013-09-20 23:08:21 +00002769/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002770/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002771bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002772 return TokError("unsupported directive '.stabs'");
2773}
2774
Jim Grosbach4b905842013-09-20 23:08:21 +00002775/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002776/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002777bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002778 StringRef Name;
2779 bool EH = false;
2780 bool Debug = false;
2781
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002782 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002783 return TokError("Expected an identifier");
2784
2785 if (Name == ".eh_frame")
2786 EH = true;
2787 else if (Name == ".debug_frame")
2788 Debug = true;
2789
2790 if (getLexer().is(AsmToken::Comma)) {
2791 Lex();
2792
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002793 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002794 return TokError("Expected an identifier");
2795
2796 if (Name == ".eh_frame")
2797 EH = true;
2798 else if (Name == ".debug_frame")
2799 Debug = true;
2800 }
2801
2802 getStreamer().EmitCFISections(EH, Debug);
2803 return false;
2804}
2805
Jim Grosbach4b905842013-09-20 23:08:21 +00002806/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002807/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002808bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002809 StringRef Simple;
2810 if (getLexer().isNot(AsmToken::EndOfStatement))
2811 if (parseIdentifier(Simple) || Simple != "simple")
2812 return TokError("unexpected token in .cfi_startproc directive");
2813
2814 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002815 return false;
2816}
2817
Jim Grosbach4b905842013-09-20 23:08:21 +00002818/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002819/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002820bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002821 getStreamer().EmitCFIEndProc();
2822 return false;
2823}
2824
Jim Grosbach4b905842013-09-20 23:08:21 +00002825/// \brief parse register name or number.
2826bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002827 SMLoc DirectiveLoc) {
2828 unsigned RegNo;
2829
2830 if (getLexer().isNot(AsmToken::Integer)) {
2831 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2832 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002833 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002834 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002835 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002836
2837 return false;
2838}
2839
Jim Grosbach4b905842013-09-20 23:08:21 +00002840/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002841/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002842bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002843 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002844 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002845 return true;
2846
2847 if (getLexer().isNot(AsmToken::Comma))
2848 return TokError("unexpected token in directive");
2849 Lex();
2850
2851 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002852 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002853 return true;
2854
2855 getStreamer().EmitCFIDefCfa(Register, Offset);
2856 return false;
2857}
2858
Jim Grosbach4b905842013-09-20 23:08:21 +00002859/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002860/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002861bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002862 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002863 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002864 return true;
2865
2866 getStreamer().EmitCFIDefCfaOffset(Offset);
2867 return false;
2868}
2869
Jim Grosbach4b905842013-09-20 23:08:21 +00002870/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002871/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002872bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002873 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002874 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002875 return true;
2876
2877 if (getLexer().isNot(AsmToken::Comma))
2878 return TokError("unexpected token in directive");
2879 Lex();
2880
2881 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002882 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002883 return true;
2884
2885 getStreamer().EmitCFIRegister(Register1, Register2);
2886 return false;
2887}
2888
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002889/// parseDirectiveCFIWindowSave
2890/// ::= .cfi_window_save
2891bool AsmParser::parseDirectiveCFIWindowSave() {
2892 getStreamer().EmitCFIWindowSave();
2893 return false;
2894}
2895
Jim Grosbach4b905842013-09-20 23:08:21 +00002896/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002897/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002898bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002899 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002900 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002901 return true;
2902
2903 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2904 return false;
2905}
2906
Jim Grosbach4b905842013-09-20 23:08:21 +00002907/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002908/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002909bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002910 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002911 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002912 return true;
2913
2914 getStreamer().EmitCFIDefCfaRegister(Register);
2915 return false;
2916}
2917
Jim Grosbach4b905842013-09-20 23:08:21 +00002918/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002919/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002920bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002921 int64_t Register = 0;
2922 int64_t Offset = 0;
2923
Jim Grosbach4b905842013-09-20 23:08:21 +00002924 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002925 return true;
2926
2927 if (getLexer().isNot(AsmToken::Comma))
2928 return TokError("unexpected token in directive");
2929 Lex();
2930
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002931 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002932 return true;
2933
2934 getStreamer().EmitCFIOffset(Register, Offset);
2935 return false;
2936}
2937
Jim Grosbach4b905842013-09-20 23:08:21 +00002938/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002939/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002940bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002941 int64_t Register = 0;
2942
Jim Grosbach4b905842013-09-20 23:08:21 +00002943 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002944 return true;
2945
2946 if (getLexer().isNot(AsmToken::Comma))
2947 return TokError("unexpected token in directive");
2948 Lex();
2949
2950 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002951 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002952 return true;
2953
2954 getStreamer().EmitCFIRelOffset(Register, Offset);
2955 return false;
2956}
2957
2958static bool isValidEncoding(int64_t Encoding) {
2959 if (Encoding & ~0xff)
2960 return false;
2961
2962 if (Encoding == dwarf::DW_EH_PE_omit)
2963 return true;
2964
2965 const unsigned Format = Encoding & 0xf;
2966 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2967 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2968 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2969 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2970 return false;
2971
2972 const unsigned Application = Encoding & 0x70;
2973 if (Application != dwarf::DW_EH_PE_absptr &&
2974 Application != dwarf::DW_EH_PE_pcrel)
2975 return false;
2976
2977 return true;
2978}
2979
Jim Grosbach4b905842013-09-20 23:08:21 +00002980/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00002981/// IsPersonality true for cfi_personality, false for cfi_lsda
2982/// ::= .cfi_personality encoding, [symbol_name]
2983/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00002984bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00002985 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002986 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00002987 return true;
2988 if (Encoding == dwarf::DW_EH_PE_omit)
2989 return false;
2990
2991 if (!isValidEncoding(Encoding))
2992 return TokError("unsupported encoding.");
2993
2994 if (getLexer().isNot(AsmToken::Comma))
2995 return TokError("unexpected token in directive");
2996 Lex();
2997
2998 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002999 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003000 return TokError("expected identifier in directive");
3001
3002 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3003
3004 if (IsPersonality)
3005 getStreamer().EmitCFIPersonality(Sym, Encoding);
3006 else
3007 getStreamer().EmitCFILsda(Sym, Encoding);
3008 return false;
3009}
3010
Jim Grosbach4b905842013-09-20 23:08:21 +00003011/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003012/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003013bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003014 getStreamer().EmitCFIRememberState();
3015 return false;
3016}
3017
Jim Grosbach4b905842013-09-20 23:08:21 +00003018/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003019/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003020bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003021 getStreamer().EmitCFIRestoreState();
3022 return false;
3023}
3024
Jim Grosbach4b905842013-09-20 23:08:21 +00003025/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003026/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003027bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003028 int64_t Register = 0;
3029
Jim Grosbach4b905842013-09-20 23:08:21 +00003030 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003031 return true;
3032
3033 getStreamer().EmitCFISameValue(Register);
3034 return false;
3035}
3036
Jim Grosbach4b905842013-09-20 23:08:21 +00003037/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003038/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003039bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003040 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003041 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return true;
3043
3044 getStreamer().EmitCFIRestore(Register);
3045 return false;
3046}
3047
Jim Grosbach4b905842013-09-20 23:08:21 +00003048/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003049/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003050bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003051 std::string Values;
3052 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003053 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003054 return true;
3055
3056 Values.push_back((uint8_t)CurrValue);
3057
3058 while (getLexer().is(AsmToken::Comma)) {
3059 Lex();
3060
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003061 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003062 return true;
3063
3064 Values.push_back((uint8_t)CurrValue);
3065 }
3066
3067 getStreamer().EmitCFIEscape(Values);
3068 return false;
3069}
3070
Jim Grosbach4b905842013-09-20 23:08:21 +00003071/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003072/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003073bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003074 if (getLexer().isNot(AsmToken::EndOfStatement))
3075 return Error(getLexer().getLoc(),
3076 "unexpected token in '.cfi_signal_frame'");
3077
3078 getStreamer().EmitCFISignalFrame();
3079 return false;
3080}
3081
Jim Grosbach4b905842013-09-20 23:08:21 +00003082/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003083/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003084bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003085 int64_t Register = 0;
3086
Jim Grosbach4b905842013-09-20 23:08:21 +00003087 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003088 return true;
3089
3090 getStreamer().EmitCFIUndefined(Register);
3091 return false;
3092}
3093
Jim Grosbach4b905842013-09-20 23:08:21 +00003094/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003095/// ::= .macros_on
3096/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003097bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003098 if (getLexer().isNot(AsmToken::EndOfStatement))
3099 return Error(getLexer().getLoc(),
3100 "unexpected token in '" + Directive + "' directive");
3101
Jim Grosbach4b905842013-09-20 23:08:21 +00003102 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003103 return false;
3104}
3105
Jim Grosbach4b905842013-09-20 23:08:21 +00003106/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003107/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003108bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003109 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003110 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003111 return TokError("expected identifier in '.macro' directive");
3112
3113 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003114 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3115 MCAsmMacroParameter Parameter;
3116 if (parseIdentifier(Parameter.first))
3117 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003118
David Majnemer91fc4c22014-01-29 18:57:46 +00003119 if (getLexer().is(AsmToken::Equal)) {
3120 Lex();
3121 if (parseMacroArgument(Parameter.second))
3122 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003123 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003124
3125 Parameters.push_back(Parameter);
3126
3127 if (getLexer().is(AsmToken::Comma))
3128 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003129 }
3130
3131 // Eat the end of statement.
3132 Lex();
3133
3134 AsmToken EndToken, StartToken = getTok();
3135
3136 // Lex the macro definition.
3137 for (;;) {
3138 // Check whether we have reached the end of the file.
3139 if (getLexer().is(AsmToken::Eof))
3140 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3141
3142 // Otherwise, check whether we have reach the .endmacro.
3143 if (getLexer().is(AsmToken::Identifier) &&
3144 (getTok().getIdentifier() == ".endm" ||
3145 getTok().getIdentifier() == ".endmacro")) {
3146 EndToken = getTok();
3147 Lex();
3148 if (getLexer().isNot(AsmToken::EndOfStatement))
3149 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3150 "' directive");
3151 break;
3152 }
3153
3154 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003155 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003156 }
3157
Jim Grosbach4b905842013-09-20 23:08:21 +00003158 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003159 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3160 }
3161
3162 const char *BodyStart = StartToken.getLoc().getPointer();
3163 const char *BodyEnd = EndToken.getLoc().getPointer();
3164 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003165 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3166 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003167 return false;
3168}
3169
Jim Grosbach4b905842013-09-20 23:08:21 +00003170/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003171///
3172/// With the support added for named parameters there may be code out there that
3173/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003174/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003175/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003176/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003177/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3178/// warning that the positional parameter found in body which have no effect.
3179/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003180/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003181/// intended or change the macro to use the named parameters. It is possible
3182/// this warning will trigger when the none of the named parameters are used
3183/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003184void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003185 StringRef Body,
3186 MCAsmMacroParameters Parameters) {
3187 // If this macro is not defined with named parameters the warning we are
3188 // checking for here doesn't apply.
3189 unsigned NParameters = Parameters.size();
3190 if (NParameters == 0)
3191 return;
3192
3193 bool NamedParametersFound = false;
3194 bool PositionalParametersFound = false;
3195
3196 // Look at the body of the macro for use of both the named parameters and what
3197 // are likely to be positional parameters. This is what expandMacro() is
3198 // doing when it finds the parameters in the body.
3199 while (!Body.empty()) {
3200 // Scan for the next possible parameter.
3201 std::size_t End = Body.size(), Pos = 0;
3202 for (; Pos != End; ++Pos) {
3203 // Check for a substitution or escape.
3204 // This macro is defined with parameters, look for \foo, \bar, etc.
3205 if (Body[Pos] == '\\' && Pos + 1 != End)
3206 break;
3207
3208 // This macro should have parameters, but look for $0, $1, ..., $n too.
3209 if (Body[Pos] != '$' || Pos + 1 == End)
3210 continue;
3211 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003212 if (Next == '$' || Next == 'n' ||
3213 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003214 break;
3215 }
3216
3217 // Check if we reached the end.
3218 if (Pos == End)
3219 break;
3220
3221 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003222 switch (Body[Pos + 1]) {
3223 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003224 case '$':
3225 break;
3226
Jim Grosbach4b905842013-09-20 23:08:21 +00003227 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003228 case 'n':
3229 PositionalParametersFound = true;
3230 break;
3231
Jim Grosbach4b905842013-09-20 23:08:21 +00003232 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003233 default: {
3234 PositionalParametersFound = true;
3235 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003236 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003237 }
3238 Pos += 2;
3239 } else {
3240 unsigned I = Pos + 1;
3241 while (isIdentifierChar(Body[I]) && I + 1 != End)
3242 ++I;
3243
Jim Grosbach4b905842013-09-20 23:08:21 +00003244 const char *Begin = Body.data() + Pos + 1;
3245 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003246 unsigned Index = 0;
3247 for (; Index < NParameters; ++Index)
3248 if (Parameters[Index].first == Argument)
3249 break;
3250
3251 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003252 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3253 Pos += 3;
3254 else {
3255 Pos = I;
3256 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003257 } else {
3258 NamedParametersFound = true;
3259 Pos += 1 + Argument.size();
3260 }
3261 }
3262 // Update the scan point.
3263 Body = Body.substr(Pos);
3264 }
3265
3266 if (!NamedParametersFound && PositionalParametersFound)
3267 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3268 "used in macro body, possible positional parameter "
3269 "found in body which will have no effect");
3270}
3271
Jim Grosbach4b905842013-09-20 23:08:21 +00003272/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003273/// ::= .endm
3274/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003275bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003276 if (getLexer().isNot(AsmToken::EndOfStatement))
3277 return TokError("unexpected token in '" + Directive + "' directive");
3278
3279 // If we are inside a macro instantiation, terminate the current
3280 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003281 if (isInsideMacroInstantiation()) {
3282 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003283 return false;
3284 }
3285
3286 // Otherwise, this .endmacro is a stray entry in the file; well formed
3287 // .endmacro directives are handled during the macro definition parsing.
3288 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003289 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003290}
3291
Jim Grosbach4b905842013-09-20 23:08:21 +00003292/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003293/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003294bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003295 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003296 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003297 return TokError("expected identifier in '.purgem' directive");
3298
3299 if (getLexer().isNot(AsmToken::EndOfStatement))
3300 return TokError("unexpected token in '.purgem' directive");
3301
Jim Grosbach4b905842013-09-20 23:08:21 +00003302 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003303 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3304
Jim Grosbach4b905842013-09-20 23:08:21 +00003305 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003306 return false;
3307}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003308
Jim Grosbach4b905842013-09-20 23:08:21 +00003309/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003310/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003311bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003312 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003313
3314 // Expect a single argument: an expression that evaluates to a constant
3315 // in the inclusive range 0-30.
3316 SMLoc ExprLoc = getLexer().getLoc();
3317 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003318 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003319 return true;
3320 else if (getLexer().isNot(AsmToken::EndOfStatement))
3321 return TokError("unexpected token after expression in"
3322 " '.bundle_align_mode' directive");
3323 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3324 return Error(ExprLoc,
3325 "invalid bundle alignment size (expected between 0 and 30)");
3326
3327 Lex();
3328
3329 // Because of AlignSizePow2's verified range we can safely truncate it to
3330 // unsigned.
3331 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3332 return false;
3333}
3334
Jim Grosbach4b905842013-09-20 23:08:21 +00003335/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003336/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003337bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003338 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003339 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003340
Eli Bendersky802b6282013-01-07 21:51:08 +00003341 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3342 StringRef Option;
3343 SMLoc Loc = getTok().getLoc();
3344 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003345 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003346
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003347 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003348 return Error(Loc, kInvalidOptionError);
3349
3350 if (Option != "align_to_end")
3351 return Error(Loc, kInvalidOptionError);
3352 else if (getLexer().isNot(AsmToken::EndOfStatement))
3353 return Error(Loc,
3354 "unexpected token after '.bundle_lock' directive option");
3355 AlignToEnd = true;
3356 }
3357
Eli Benderskyf483ff92012-12-20 19:05:53 +00003358 Lex();
3359
Eli Bendersky802b6282013-01-07 21:51:08 +00003360 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003361 return false;
3362}
3363
Jim Grosbach4b905842013-09-20 23:08:21 +00003364/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003365/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003366bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003367 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003368
3369 if (getLexer().isNot(AsmToken::EndOfStatement))
3370 return TokError("unexpected token in '.bundle_unlock' directive");
3371 Lex();
3372
3373 getStreamer().EmitBundleUnlock();
3374 return false;
3375}
3376
Jim Grosbach4b905842013-09-20 23:08:21 +00003377/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003378/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003379bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003380 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003381
3382 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003383 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003384 return true;
3385
3386 int64_t FillExpr = 0;
3387 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3388 if (getLexer().isNot(AsmToken::Comma))
3389 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3390 Lex();
3391
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003392 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003393 return true;
3394
3395 if (getLexer().isNot(AsmToken::EndOfStatement))
3396 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3397 }
3398
3399 Lex();
3400
3401 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003402 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3403 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003404
3405 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003406 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003407
3408 return false;
3409}
3410
Jim Grosbach4b905842013-09-20 23:08:21 +00003411/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003412/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003413bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003414 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003415 const MCExpr *Value;
3416
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003417 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003418 return true;
3419
3420 if (getLexer().isNot(AsmToken::EndOfStatement))
3421 return TokError("unexpected token in directive");
3422
3423 if (Signed)
3424 getStreamer().EmitSLEB128Value(Value);
3425 else
3426 getStreamer().EmitULEB128Value(Value);
3427
3428 return false;
3429}
3430
Jim Grosbach4b905842013-09-20 23:08:21 +00003431/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003432/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003433bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003434 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003435 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003436 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003437 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003438
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003439 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003440 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003441
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003442 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003443
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003444 // Assembler local symbols don't make any sense here. Complain loudly.
3445 if (Sym->isTemporary())
3446 return Error(Loc, "non-local symbol required in directive");
3447
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003448 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3449 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003450
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003451 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003452 break;
3453
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003454 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003455 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003456 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003457 }
3458 }
3459
Sean Callanan686ed8d2010-01-19 20:22:31 +00003460 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003461 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003462}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003463
Jim Grosbach4b905842013-09-20 23:08:21 +00003464/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003465/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003466bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003467 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003468
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003469 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003470 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003471 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003472 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003473
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003474 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003475 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003476
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003477 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003478 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003479 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003480
3481 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003482 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003483 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003484 return true;
3485
3486 int64_t Pow2Alignment = 0;
3487 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003488 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003489 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003490 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003491 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003492 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003493
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003494 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3495 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003496 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3497
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003498 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003499 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3500 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003501 if (!isPowerOf2_64(Pow2Alignment))
3502 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3503 Pow2Alignment = Log2_64(Pow2Alignment);
3504 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003505 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003506
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003507 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003508 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003509
Sean Callanan686ed8d2010-01-19 20:22:31 +00003510 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003511
Chris Lattner28ad7542009-07-09 17:25:12 +00003512 // NOTE: a size of zero for a .comm should create a undefined symbol
3513 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003514 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003515 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003516 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003517
Eric Christopherbc818852010-05-14 01:38:54 +00003518 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003519 // may internally end up wanting an alignment in bytes.
3520 // FIXME: Diagnose overflow.
3521 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003522 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003523 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003524
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003525 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003526 return Error(IDLoc, "invalid symbol redefinition");
3527
Chris Lattner28ad7542009-07-09 17:25:12 +00003528 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003529 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003530 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003531 return false;
3532 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003533
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003534 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003535 return false;
3536}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003537
Jim Grosbach4b905842013-09-20 23:08:21 +00003538/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003539/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003540bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003541 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003542 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003543
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003544 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003545 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003546 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003547
Sean Callanan686ed8d2010-01-19 20:22:31 +00003548 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003549
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003550 if (Str.empty())
3551 Error(Loc, ".abort detected. Assembly stopping.");
3552 else
3553 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003554 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003555
3556 return false;
3557}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003558
Jim Grosbach4b905842013-09-20 23:08:21 +00003559/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003560/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003561bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003562 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003563 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003564
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003565 // Allow the strings to have escaped octal character sequence.
3566 std::string Filename;
3567 if (parseEscapedString(Filename))
3568 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003569 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003570 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003571
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003572 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003573 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003574
Chris Lattner693fbb82009-07-16 06:14:39 +00003575 // Attempt to switch the lexer to the included file before consuming the end
3576 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003577 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003578 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003579 return true;
3580 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003581
3582 return false;
3583}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003584
Jim Grosbach4b905842013-09-20 23:08:21 +00003585/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003586/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003587bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003588 if (getLexer().isNot(AsmToken::String))
3589 return TokError("expected string in '.incbin' directive");
3590
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003591 // Allow the strings to have escaped octal character sequence.
3592 std::string Filename;
3593 if (parseEscapedString(Filename))
3594 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003595 SMLoc IncbinLoc = getLexer().getLoc();
3596 Lex();
3597
3598 if (getLexer().isNot(AsmToken::EndOfStatement))
3599 return TokError("unexpected token in '.incbin' directive");
3600
Kevin Enderby109f25c2011-12-14 21:47:48 +00003601 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003602 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003603 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3604 return true;
3605 }
3606
3607 return false;
3608}
3609
Jim Grosbach4b905842013-09-20 23:08:21 +00003610/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003611/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003612bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003613 TheCondStack.push_back(TheCondState);
3614 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003615 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003616 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003617 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003618 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003619 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003620 return true;
3621
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003622 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003623 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003624
Sean Callanan686ed8d2010-01-19 20:22:31 +00003625 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003626
3627 TheCondState.CondMet = ExprValue;
3628 TheCondState.Ignore = !TheCondState.CondMet;
3629 }
3630
3631 return false;
3632}
3633
Jim Grosbach4b905842013-09-20 23:08:21 +00003634/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003635/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003636bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003637 TheCondStack.push_back(TheCondState);
3638 TheCondState.TheCond = AsmCond::IfCond;
3639
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003640 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003641 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003642 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003643 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003644
3645 if (getLexer().isNot(AsmToken::EndOfStatement))
3646 return TokError("unexpected token in '.ifb' directive");
3647
3648 Lex();
3649
3650 TheCondState.CondMet = ExpectBlank == Str.empty();
3651 TheCondState.Ignore = !TheCondState.CondMet;
3652 }
3653
3654 return false;
3655}
3656
Jim Grosbach4b905842013-09-20 23:08:21 +00003657/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003658/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003659bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003660 TheCondStack.push_back(TheCondState);
3661 TheCondState.TheCond = AsmCond::IfCond;
3662
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003663 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003664 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003665 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003666 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003667
3668 if (getLexer().isNot(AsmToken::Comma))
3669 return TokError("unexpected token in '.ifc' directive");
3670
3671 Lex();
3672
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003673 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003674
3675 if (getLexer().isNot(AsmToken::EndOfStatement))
3676 return TokError("unexpected token in '.ifc' directive");
3677
3678 Lex();
3679
3680 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3681 TheCondState.Ignore = !TheCondState.CondMet;
3682 }
3683
3684 return false;
3685}
3686
Jim Grosbach4b905842013-09-20 23:08:21 +00003687/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003688/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003689bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003690 StringRef Name;
3691 TheCondStack.push_back(TheCondState);
3692 TheCondState.TheCond = AsmCond::IfCond;
3693
3694 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003695 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003696 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003697 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003698 return TokError("expected identifier after '.ifdef'");
3699
3700 Lex();
3701
3702 MCSymbol *Sym = getContext().LookupSymbol(Name);
3703
3704 if (expect_defined)
3705 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3706 else
3707 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3708 TheCondState.Ignore = !TheCondState.CondMet;
3709 }
3710
3711 return false;
3712}
3713
Jim Grosbach4b905842013-09-20 23:08:21 +00003714/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003715/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003716bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003717 if (TheCondState.TheCond != AsmCond::IfCond &&
3718 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003719 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3720 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003721 TheCondState.TheCond = AsmCond::ElseIfCond;
3722
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003723 bool LastIgnoreState = false;
3724 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003725 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003726 if (LastIgnoreState || TheCondState.CondMet) {
3727 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003728 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003729 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003730 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003731 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003732 return true;
3733
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003734 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003735 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003736
Sean Callanan686ed8d2010-01-19 20:22:31 +00003737 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003738 TheCondState.CondMet = ExprValue;
3739 TheCondState.Ignore = !TheCondState.CondMet;
3740 }
3741
3742 return false;
3743}
3744
Jim Grosbach4b905842013-09-20 23:08:21 +00003745/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003746/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003747bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003748 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003749 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003750
Sean Callanan686ed8d2010-01-19 20:22:31 +00003751 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003752
3753 if (TheCondState.TheCond != AsmCond::IfCond &&
3754 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003755 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3756 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003757 TheCondState.TheCond = AsmCond::ElseCond;
3758 bool LastIgnoreState = false;
3759 if (!TheCondStack.empty())
3760 LastIgnoreState = TheCondStack.back().Ignore;
3761 if (LastIgnoreState || TheCondState.CondMet)
3762 TheCondState.Ignore = true;
3763 else
3764 TheCondState.Ignore = false;
3765
3766 return false;
3767}
3768
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003769/// parseDirectiveEnd
3770/// ::= .end
3771bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3772 if (getLexer().isNot(AsmToken::EndOfStatement))
3773 return TokError("unexpected token in '.end' directive");
3774
3775 Lex();
3776
3777 while (Lexer.isNot(AsmToken::Eof))
3778 Lex();
3779
3780 return false;
3781}
3782
Jim Grosbach4b905842013-09-20 23:08:21 +00003783/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003784/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003785bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003786 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003787 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003788
Sean Callanan686ed8d2010-01-19 20:22:31 +00003789 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003790
Jim Grosbach4b905842013-09-20 23:08:21 +00003791 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003792 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3793 ".else");
3794 if (!TheCondStack.empty()) {
3795 TheCondState = TheCondStack.back();
3796 TheCondStack.pop_back();
3797 }
3798
3799 return false;
3800}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003801
Eli Bendersky17233942013-01-15 22:59:42 +00003802void AsmParser::initializeDirectiveKindMap() {
3803 DirectiveKindMap[".set"] = DK_SET;
3804 DirectiveKindMap[".equ"] = DK_EQU;
3805 DirectiveKindMap[".equiv"] = DK_EQUIV;
3806 DirectiveKindMap[".ascii"] = DK_ASCII;
3807 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3808 DirectiveKindMap[".string"] = DK_STRING;
3809 DirectiveKindMap[".byte"] = DK_BYTE;
3810 DirectiveKindMap[".short"] = DK_SHORT;
3811 DirectiveKindMap[".value"] = DK_VALUE;
3812 DirectiveKindMap[".2byte"] = DK_2BYTE;
3813 DirectiveKindMap[".long"] = DK_LONG;
3814 DirectiveKindMap[".int"] = DK_INT;
3815 DirectiveKindMap[".4byte"] = DK_4BYTE;
3816 DirectiveKindMap[".quad"] = DK_QUAD;
3817 DirectiveKindMap[".8byte"] = DK_8BYTE;
3818 DirectiveKindMap[".single"] = DK_SINGLE;
3819 DirectiveKindMap[".float"] = DK_FLOAT;
3820 DirectiveKindMap[".double"] = DK_DOUBLE;
3821 DirectiveKindMap[".align"] = DK_ALIGN;
3822 DirectiveKindMap[".align32"] = DK_ALIGN32;
3823 DirectiveKindMap[".balign"] = DK_BALIGN;
3824 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3825 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3826 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3827 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3828 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3829 DirectiveKindMap[".org"] = DK_ORG;
3830 DirectiveKindMap[".fill"] = DK_FILL;
3831 DirectiveKindMap[".zero"] = DK_ZERO;
3832 DirectiveKindMap[".extern"] = DK_EXTERN;
3833 DirectiveKindMap[".globl"] = DK_GLOBL;
3834 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003835 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3836 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3837 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3838 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3839 DirectiveKindMap[".reference"] = DK_REFERENCE;
3840 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3841 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3842 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3843 DirectiveKindMap[".comm"] = DK_COMM;
3844 DirectiveKindMap[".common"] = DK_COMMON;
3845 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3846 DirectiveKindMap[".abort"] = DK_ABORT;
3847 DirectiveKindMap[".include"] = DK_INCLUDE;
3848 DirectiveKindMap[".incbin"] = DK_INCBIN;
3849 DirectiveKindMap[".code16"] = DK_CODE16;
3850 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3851 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003852 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003853 DirectiveKindMap[".irp"] = DK_IRP;
3854 DirectiveKindMap[".irpc"] = DK_IRPC;
3855 DirectiveKindMap[".endr"] = DK_ENDR;
3856 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3857 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3858 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3859 DirectiveKindMap[".if"] = DK_IF;
3860 DirectiveKindMap[".ifb"] = DK_IFB;
3861 DirectiveKindMap[".ifnb"] = DK_IFNB;
3862 DirectiveKindMap[".ifc"] = DK_IFC;
3863 DirectiveKindMap[".ifnc"] = DK_IFNC;
3864 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3865 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3866 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3867 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3868 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003869 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003870 DirectiveKindMap[".endif"] = DK_ENDIF;
3871 DirectiveKindMap[".skip"] = DK_SKIP;
3872 DirectiveKindMap[".space"] = DK_SPACE;
3873 DirectiveKindMap[".file"] = DK_FILE;
3874 DirectiveKindMap[".line"] = DK_LINE;
3875 DirectiveKindMap[".loc"] = DK_LOC;
3876 DirectiveKindMap[".stabs"] = DK_STABS;
3877 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3878 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3879 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3880 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3881 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3882 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3883 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3884 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3885 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3886 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3887 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3888 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3889 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3890 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3891 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3892 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3893 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3894 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3895 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3896 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3897 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003898 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003899 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3900 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3901 DirectiveKindMap[".macro"] = DK_MACRO;
3902 DirectiveKindMap[".endm"] = DK_ENDM;
3903 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3904 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003905}
3906
Jim Grosbach4b905842013-09-20 23:08:21 +00003907MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003908 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003909
Rafael Espindola34b9c512012-06-03 23:57:14 +00003910 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003911 for (;;) {
3912 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003913 if (getLexer().is(AsmToken::Eof)) {
3914 Error(DirectiveLoc, "no matching '.endr' in definition");
3915 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003916 }
3917
Rafael Espindola34b9c512012-06-03 23:57:14 +00003918 if (Lexer.is(AsmToken::Identifier) &&
3919 (getTok().getIdentifier() == ".rept")) {
3920 ++NestLevel;
3921 }
3922
3923 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003924 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003925 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003926 EndToken = getTok();
3927 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003928 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3929 TokError("unexpected token in '.endr' directive");
3930 return 0;
3931 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003932 break;
3933 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003934 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003935 }
3936
Rafael Espindola34b9c512012-06-03 23:57:14 +00003937 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003938 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003939 }
3940
3941 const char *BodyStart = StartToken.getLoc().getPointer();
3942 const char *BodyEnd = EndToken.getLoc().getPointer();
3943 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3944
Rafael Espindola34b9c512012-06-03 23:57:14 +00003945 // We Are Anonymous.
3946 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00003947 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00003948 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3949 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003950}
3951
Jim Grosbach4b905842013-09-20 23:08:21 +00003952void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00003953 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003954 OS << ".endr\n";
3955
3956 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00003957 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003958
Rafael Espindola34b9c512012-06-03 23:57:14 +00003959 // Create the macro instantiation object and add to the current macro
3960 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00003961 MacroInstantiation *MI = new MacroInstantiation(
3962 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003963 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003964
Rafael Espindola34b9c512012-06-03 23:57:14 +00003965 // Jump to the macro instantiation and prime the lexer.
3966 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3967 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3968 Lex();
3969}
3970
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003971/// parseDirectiveRept
3972/// ::= .rep | .rept count
3973bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003974 const MCExpr *CountExpr;
3975 SMLoc CountLoc = getTok().getLoc();
3976 if (parseExpression(CountExpr))
3977 return true;
3978
Rafael Espindola34b9c512012-06-03 23:57:14 +00003979 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003980 if (!CountExpr->EvaluateAsAbsolute(Count)) {
3981 eatToEndOfStatement();
3982 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
3983 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003984
3985 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00003986 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003987
3988 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003989 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00003990
3991 // Eat the end of statement.
3992 Lex();
3993
3994 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003995 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003996 if (!M)
3997 return true;
3998
3999 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4000 // to hold the macro body with substitutions.
4001 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00004002 MCAsmMacroParameters Parameters;
4003 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004004 raw_svector_ostream OS(Buf);
4005 while (Count--) {
4006 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
4007 return true;
4008 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004009 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004010
4011 return false;
4012}
4013
Jim Grosbach4b905842013-09-20 23:08:21 +00004014/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004015/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004016bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004017 MCAsmMacroParameters Parameters;
4018 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004019
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004020 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004021 return TokError("expected identifier in '.irp' directive");
4022
4023 Parameters.push_back(Parameter);
4024
4025 if (Lexer.isNot(AsmToken::Comma))
4026 return TokError("expected comma in '.irp' directive");
4027
4028 Lex();
4029
Eli Bendersky38274122013-01-14 23:22:36 +00004030 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004031 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004032 return true;
4033
4034 // Eat the end of statement.
4035 Lex();
4036
4037 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004038 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004039 if (!M)
4040 return true;
4041
4042 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4043 // to hold the macro body with substitutions.
4044 SmallString<256> Buf;
4045 raw_svector_ostream OS(Buf);
4046
Eli Bendersky38274122013-01-14 23:22:36 +00004047 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4048 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004049 Args.push_back(*i);
4050
4051 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4052 return true;
4053 }
4054
Jim Grosbach4b905842013-09-20 23:08:21 +00004055 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004056
4057 return false;
4058}
4059
Jim Grosbach4b905842013-09-20 23:08:21 +00004060/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004061/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004062bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004063 MCAsmMacroParameters Parameters;
4064 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004065
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004066 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004067 return TokError("expected identifier in '.irpc' directive");
4068
4069 Parameters.push_back(Parameter);
4070
4071 if (Lexer.isNot(AsmToken::Comma))
4072 return TokError("expected comma in '.irpc' directive");
4073
4074 Lex();
4075
Eli Bendersky38274122013-01-14 23:22:36 +00004076 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004077 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004078 return true;
4079
4080 if (A.size() != 1 || A.front().size() != 1)
4081 return TokError("unexpected token in '.irpc' directive");
4082
4083 // Eat the end of statement.
4084 Lex();
4085
4086 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004087 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004088 if (!M)
4089 return true;
4090
4091 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4092 // to hold the macro body with substitutions.
4093 SmallString<256> Buf;
4094 raw_svector_ostream OS(Buf);
4095
4096 StringRef Values = A.front().front().getString();
4097 std::size_t I, End = Values.size();
4098 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004099 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004100 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004101
Eli Bendersky38274122013-01-14 23:22:36 +00004102 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004103 Args.push_back(Arg);
4104
4105 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4106 return true;
4107 }
4108
Jim Grosbach4b905842013-09-20 23:08:21 +00004109 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004110
4111 return false;
4112}
4113
Jim Grosbach4b905842013-09-20 23:08:21 +00004114bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004115 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004116 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004117
4118 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004119 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004120 assert(getLexer().is(AsmToken::EndOfStatement));
4121
Jim Grosbach4b905842013-09-20 23:08:21 +00004122 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004123 return false;
4124}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004125
Jim Grosbach4b905842013-09-20 23:08:21 +00004126bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004127 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004128 const MCExpr *Value;
4129 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004130 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004131 return true;
4132 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4133 if (!MCE)
4134 return Error(ExprLoc, "unexpected expression in _emit");
4135 uint64_t IntValue = MCE->getValue();
4136 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4137 return Error(ExprLoc, "literal value out of range for directive");
4138
Chad Rosierc7f552c2013-02-12 21:33:51 +00004139 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4140 return false;
4141}
4142
Jim Grosbach4b905842013-09-20 23:08:21 +00004143bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004144 const MCExpr *Value;
4145 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004146 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004147 return true;
4148 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4149 if (!MCE)
4150 return Error(ExprLoc, "unexpected expression in align");
4151 uint64_t IntValue = MCE->getValue();
4152 if (!isPowerOf2_64(IntValue))
4153 return Error(ExprLoc, "literal value not a power of two greater then zero");
4154
Jim Grosbach4b905842013-09-20 23:08:21 +00004155 Info.AsmRewrites->push_back(
4156 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004157 return false;
4158}
4159
Chad Rosierf43fcf52013-02-13 21:27:17 +00004160// We are comparing pointers, but the pointers are relative to a single string.
4161// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004162static int rewritesSort(const AsmRewrite *AsmRewriteA,
4163 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004164 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4165 return -1;
4166 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4167 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004168
Chad Rosierfce4fab2013-04-08 17:43:47 +00004169 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4170 // rewrite to the same location. Make sure the SizeDirective rewrite is
4171 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4172 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004173 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4174 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004175 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004176
Jim Grosbach4b905842013-09-20 23:08:21 +00004177 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4178 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004179 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004180 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004181}
4182
Jim Grosbach4b905842013-09-20 23:08:21 +00004183bool AsmParser::parseMSInlineAsm(
4184 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4185 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4186 SmallVectorImpl<std::string> &Constraints,
4187 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4188 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004189 SmallVector<void *, 4> InputDecls;
4190 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004191 SmallVector<bool, 4> InputDeclsAddressOf;
4192 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004193 SmallVector<std::string, 4> InputConstraints;
4194 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004195 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004196
Benjamin Kramer1a136112013-02-15 20:37:21 +00004197 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004198
4199 // Prime the lexer.
4200 Lex();
4201
4202 // While we have input, parse each statement.
4203 unsigned InputIdx = 0;
4204 unsigned OutputIdx = 0;
4205 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004206 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004207 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004208 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004209
Chad Rosier149e8e02012-12-12 22:45:52 +00004210 if (Info.ParseError)
4211 return true;
4212
Benjamin Kramer1a136112013-02-15 20:37:21 +00004213 if (Info.Opcode == ~0U)
4214 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004215
Benjamin Kramer1a136112013-02-15 20:37:21 +00004216 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004217
Benjamin Kramer1a136112013-02-15 20:37:21 +00004218 // Build the list of clobbers, outputs and inputs.
4219 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4220 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004221
Benjamin Kramer1a136112013-02-15 20:37:21 +00004222 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004223 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004224 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004225
Benjamin Kramer1a136112013-02-15 20:37:21 +00004226 // Register operand.
4227 if (Operand->isReg() && !Operand->needAddressOf()) {
4228 unsigned NumDefs = Desc.getNumDefs();
4229 // Clobber.
4230 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4231 ClobberRegs.push_back(Operand->getReg());
4232 continue;
4233 }
4234
4235 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004236 StringRef SymName = Operand->getSymName();
4237 if (SymName.empty())
4238 continue;
4239
Chad Rosierdba3fe52013-04-22 22:12:12 +00004240 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004241 if (!OpDecl)
4242 continue;
4243
4244 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004245 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004246 if (isOutput) {
4247 ++InputIdx;
4248 OutputDecls.push_back(OpDecl);
4249 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4250 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004251 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004252 } else {
4253 InputDecls.push_back(OpDecl);
4254 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4255 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004256 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004257 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004258 }
Reid Kleckneree088972013-12-10 18:27:32 +00004259
4260 // Consider implicit defs to be clobbers. Think of cpuid and push.
4261 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4262 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4263 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004264 }
4265
4266 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004267 NumOutputs = OutputDecls.size();
4268 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004269
4270 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004271 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4272 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4273 ClobberRegs.end());
4274 Clobbers.assign(ClobberRegs.size(), std::string());
4275 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4276 raw_string_ostream OS(Clobbers[I]);
4277 IP->printRegName(OS, ClobberRegs[I]);
4278 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004279
4280 // Merge the various outputs and inputs. Output are expected first.
4281 if (NumOutputs || NumInputs) {
4282 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004283 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004284 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004285 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004286 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004287 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004288 }
4289 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004290 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004291 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004292 }
4293 }
4294
4295 // Build the IR assembly string.
4296 std::string AsmStringIR;
4297 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004298 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4299 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004300 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004301 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4302 E = AsmStrRewrites.end();
4303 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004304 AsmRewriteKind Kind = (*I).Kind;
4305 if (Kind == AOK_Delete)
4306 continue;
4307
Chad Rosier8bce6642012-10-18 15:49:34 +00004308 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004309 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004310
Chad Rosier120eefd2013-03-19 17:32:17 +00004311 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004312 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004313 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004314 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004315
Chad Rosier37e755c2012-10-23 17:43:43 +00004316 // Skip the original expression.
4317 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004318 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004319 continue;
4320 }
4321
Chad Rosierff10ed12013-04-12 16:26:42 +00004322 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004323 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004324 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004325 default:
4326 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004327 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004328 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004329 break;
4330 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004331 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004332 break;
4333 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004334 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004335 break;
4336 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004337 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004338 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004339 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004340 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004341 default: break;
4342 case 8: OS << "byte ptr "; break;
4343 case 16: OS << "word ptr "; break;
4344 case 32: OS << "dword ptr "; break;
4345 case 64: OS << "qword ptr "; break;
4346 case 80: OS << "xword ptr "; break;
4347 case 128: OS << "xmmword ptr "; break;
4348 case 256: OS << "ymmword ptr "; break;
4349 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004350 break;
4351 case AOK_Emit:
4352 OS << ".byte";
4353 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004354 case AOK_Align: {
4355 unsigned Val = (*I).Val;
4356 OS << ".align " << Val;
4357
4358 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004359 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004360 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4361 break;
4362 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004363 case AOK_DotOperator:
4364 OS << (*I).Val;
4365 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004366 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004367
Chad Rosier8bce6642012-10-18 15:49:34 +00004368 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004369 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004370 }
4371
4372 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004373 if (AsmStart != AsmEnd)
4374 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004375
4376 AsmString = OS.str();
4377 return false;
4378}
4379
Daniel Dunbar01e36072010-07-17 02:26:10 +00004380/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004381MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4382 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004383 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004384}