blob: 63a00fedfa7f8a036818ee48224a8c4df3ba22ca [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,
David Woodhoused6de0d92014-02-01 16:20:59 +0000340 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA,
341 DK_SINGLE, 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", ...
David Woodhoused6de0d92014-02-01 16:20:59 +0000370 bool parseDirectiveOctaValue(); // ".octa"
Jim Grosbach4b905842013-09-20 23:08:21 +0000371 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
372 bool parseDirectiveFill(); // ".fill"
373 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000374 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
376 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000377 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000378 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000379
Eli Bendersky17233942013-01-15 22:59:42 +0000380 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000381 bool parseDirectiveFile(SMLoc DirectiveLoc);
382 bool parseDirectiveLine();
383 bool parseDirectiveLoc();
384 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000385
386 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000387 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000388 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveCFISections();
390 bool parseDirectiveCFIStartProc();
391 bool parseDirectiveCFIEndProc();
392 bool parseDirectiveCFIDefCfaOffset();
393 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
394 bool parseDirectiveCFIAdjustCfaOffset();
395 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
399 bool parseDirectiveCFIRememberState();
400 bool parseDirectiveCFIRestoreState();
401 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIEscape();
404 bool parseDirectiveCFISignalFrame();
405 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000406
407 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000408 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
409 bool parseDirectiveEndMacro(StringRef Directive);
410 bool parseDirectiveMacro(SMLoc DirectiveLoc);
411 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000412
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000417 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419
Eli Bendersky17233942013-01-15 22:59:42 +0000420 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000422
423 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000429
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectiveAbort(); // ".abort"
433 bool parseDirectiveInclude(); // ".include"
434 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000437 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000439 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000441 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
443 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
444 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
445 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000446 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000447
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000449 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000450
Rafael Espindola34b9c512012-06-03 23:57:14 +0000451 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
453 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000454 raw_svector_ostream &OS);
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +0000455 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive);
Jim Grosbach4b905842013-09-20 23:08:21 +0000456 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
457 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
458 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000459
Chad Rosierc7f552c2013-02-12 21:33:51 +0000460 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000461 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000462 size_t Len);
463
464 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000466
Saleem Abdulrasool88186c42013-12-18 02:53:03 +0000467 // "end"
468 bool parseDirectiveEnd(SMLoc DirectiveLoc);
469
Eli Bendersky17233942013-01-15 22:59:42 +0000470 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000471};
Daniel Dunbar86033402010-07-12 17:54:38 +0000472}
473
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000474namespace llvm {
475
476extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000477extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000478extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000479
480}
481
Chris Lattnerc35681b2010-01-19 19:46:13 +0000482enum { DEFAULT_ADDRSPACE = 0 };
483
Jim Grosbach4b905842013-09-20 23:08:21 +0000484AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
485 const MCAsmInfo &_MAI)
486 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
487 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
488 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
489 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000490 // Save the old handler.
491 SavedDiagHandler = SrcMgr.getDiagHandler();
492 SavedDiagContext = SrcMgr.getDiagContext();
493 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000494 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000495 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000496
Daniel Dunbarc5011082010-07-12 18:12:02 +0000497 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000498 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
499 case MCObjectFileInfo::IsCOFF:
500 PlatformParser = createCOFFAsmParser();
501 PlatformParser->Initialize(*this);
502 break;
503 case MCObjectFileInfo::IsMachO:
504 PlatformParser = createDarwinAsmParser();
505 PlatformParser->Initialize(*this);
506 IsDarwin = true;
507 break;
508 case MCObjectFileInfo::IsELF:
509 PlatformParser = createELFAsmParser();
510 PlatformParser->Initialize(*this);
511 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000512 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000513
Eli Bendersky17233942013-01-15 22:59:42 +0000514 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000515}
516
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000517AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000518 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
519
520 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000521 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
522 ie = MacroMap.end();
523 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000524 delete it->getValue();
525
Daniel Dunbarc5011082010-07-12 18:12:02 +0000526 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000527}
528
Jim Grosbach4b905842013-09-20 23:08:21 +0000529void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000530 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000531 for (std::vector<MacroInstantiation *>::const_reverse_iterator
532 it = ActiveMacros.rbegin(),
533 ie = ActiveMacros.rend();
534 it != ie; ++it)
535 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000536 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000537}
538
Saleem Abdulrasool69c7caf2014-01-07 02:28:31 +0000539void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
540 printMessage(L, SourceMgr::DK_Note, Msg, Ranges);
541 printMacroInstantiations();
542}
543
Chris Lattnera3a06812011-10-16 04:47:35 +0000544bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000545 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000546 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000547 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
548 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000549 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000550}
551
Chris Lattnera3a06812011-10-16 04:47:35 +0000552bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000553 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000554 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
555 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000556 return true;
557}
558
Jim Grosbach4b905842013-09-20 23:08:21 +0000559bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000560 std::string IncludedFile;
561 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000562 if (NewBuf == -1)
563 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000564
Sean Callanan7a77eae2010-01-21 00:19:58 +0000565 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000566
Sean Callanan7a77eae2010-01-21 00:19:58 +0000567 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000568
Sean Callanan7a77eae2010-01-21 00:19:58 +0000569 return false;
570}
Daniel Dunbar43235712010-07-18 18:54:11 +0000571
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000572/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000573/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000574/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000575bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000576 std::string IncludedFile;
577 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
578 if (NewBuf == -1)
579 return true;
580
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000581 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000582 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000583 return false;
584}
585
Jim Grosbach4b905842013-09-20 23:08:21 +0000586void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000587 if (InBuffer != -1) {
588 CurBuffer = InBuffer;
589 } else {
590 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
591 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000592 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
593}
594
Sean Callanan7a77eae2010-01-21 00:19:58 +0000595const AsmToken &AsmParser::Lex() {
596 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000597
Sean Callanan7a77eae2010-01-21 00:19:58 +0000598 if (tok->is(AsmToken::Eof)) {
599 // If this is the end of an included file, pop the parent file off the
600 // include stack.
601 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
602 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000603 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000604 tok = &Lexer.Lex();
605 }
606 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000607
Sean Callanan7a77eae2010-01-21 00:19:58 +0000608 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000609 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000610
Sean Callanan7a77eae2010-01-21 00:19:58 +0000611 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000612}
613
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000614bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000615 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000616 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000617 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000618
Chris Lattner36e02122009-06-21 20:54:55 +0000619 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000620 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000621
622 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000623 AsmCond StartingCondState = TheCondState;
624
Kevin Enderby6469fc22011-11-01 22:27:22 +0000625 // If we are generating dwarf for assembly source files save the initial text
626 // section and generate a .file directive.
627 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000628 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000629 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
630 getStreamer().EmitLabel(SectionStartSym);
631 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000632 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000633 StringRef(),
634 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000635 }
636
Chris Lattner73f36112009-07-02 21:53:43 +0000637 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000638 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000639 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000640 if (!parseStatement(Info))
641 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000642
Daniel Dunbar43325c42010-09-09 22:42:56 +0000643 // We had an error, validate that one was emitted and recover by skipping to
644 // the next line.
645 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000646 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000647 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000648
649 if (TheCondState.TheCond != StartingCondState.TheCond ||
650 TheCondState.Ignore != StartingCondState.Ignore)
651 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000652
653 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000654 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000655 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000656 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000657 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000658 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000659 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000660
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000661 // Check to see that all assembler local symbols were actually defined.
662 // Targets that don't do subsections via symbols may not want this, though,
663 // so conservatively exclude them. Only do this if we're finalizing, though,
664 // as otherwise we won't necessarilly have seen everything yet.
665 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
666 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
667 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000668 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000669 i != e; ++i) {
670 MCSymbol *Sym = i->getValue();
671 // Variable symbols may not be marked as defined, so check those
672 // explicitly. If we know it's a variable, we have a definition for
673 // the purposes of this check.
674 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
675 // FIXME: We would really like to refer back to where the symbol was
676 // first referenced for a source location. We need to add something
677 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000678 printMessage(
679 getLexer().getLoc(), SourceMgr::DK_Error,
680 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000681 }
682 }
683
David Peixotto308e7e42013-12-19 18:08:08 +0000684 // Callback to the target parser in case it needs to do anything.
685 if (!HadError)
686 getTargetParser().finishParse();
687
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000688 // Finalize the output stream if there are no errors and if the client wants
689 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000690 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000691 Out.Finish();
692
Chris Lattner73f36112009-07-02 21:53:43 +0000693 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000694}
695
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000696void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000697 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000698 TokError("expected section directive before assembly directive");
Rafael Espindolaf1440342014-01-23 23:14:14 +0000699 Out.InitSections();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000700 }
701}
702
Jim Grosbach4b905842013-09-20 23:08:21 +0000703/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000704void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000705 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000706 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000707
Chris Lattnere5074c42009-06-22 01:29:09 +0000708 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000709 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000710 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000711}
712
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000713StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000714 const char *Start = getTok().getLoc().getPointer();
715
Jim Grosbach4b905842013-09-20 23:08:21 +0000716 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000717 Lex();
718
719 const char *End = getTok().getLoc().getPointer();
720 return StringRef(Start, End - Start);
721}
Chris Lattner78db3622009-06-22 05:51:26 +0000722
Jim Grosbach4b905842013-09-20 23:08:21 +0000723StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000724 const char *Start = getTok().getLoc().getPointer();
725
726 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000727 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000728 Lex();
729
730 const char *End = getTok().getLoc().getPointer();
731 return StringRef(Start, End - Start);
732}
733
Jim Grosbach4b905842013-09-20 23:08:21 +0000734/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000735/// NOTE: This assumes the leading '(' has already been consumed.
736///
737/// parenexpr ::= expr)
738///
Jim Grosbach4b905842013-09-20 23:08:21 +0000739bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
740 if (parseExpression(Res))
741 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000742 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000743 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000744 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000745 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000746 return false;
747}
Chris Lattner78db3622009-06-22 05:51:26 +0000748
Jim Grosbach4b905842013-09-20 23:08:21 +0000749/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000750/// NOTE: This assumes the leading '[' has already been consumed.
751///
752/// bracketexpr ::= expr]
753///
Jim Grosbach4b905842013-09-20 23:08:21 +0000754bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
755 if (parseExpression(Res))
756 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000757 if (Lexer.isNot(AsmToken::RBrac))
758 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000759 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000760 Lex();
761 return false;
762}
763
Jim Grosbach4b905842013-09-20 23:08:21 +0000764/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000765/// primaryexpr ::= (parenexpr
766/// primaryexpr ::= symbol
767/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000768/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000769/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000770bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000771 SMLoc FirstTokenLoc = getLexer().getLoc();
772 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
773 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000774 default:
775 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000776 // If we have an error assume that we've already handled it.
777 case AsmToken::Error:
778 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000779 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000780 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000781 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000782 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000783 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000784 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000785 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000786 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000787 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000788 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000789 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000790 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000791 if (FirstTokenKind == AsmToken::Dollar) {
792 if (Lexer.getMAI().getDollarIsPC()) {
793 // This is a '$' reference, which references the current PC. Emit a
794 // temporary label to the streamer and refer to it.
795 MCSymbol *Sym = Ctx.CreateTempSymbol();
796 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000797 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
798 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000799 EndLoc = FirstTokenLoc;
800 return false;
801 } else
802 return Error(FirstTokenLoc, "invalid token in expression");
803 return true;
804 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000805 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000806 // Parse symbol variant
807 std::pair<StringRef, StringRef> Split;
808 if (!MAI.useParensForSymbolVariant()) {
809 Split = Identifier.split('@');
810 } else if (Lexer.is(AsmToken::LParen)) {
811 Lexer.Lex(); // eat (
812 StringRef VName;
813 parseIdentifier(VName);
814 if (Lexer.isNot(AsmToken::RParen)) {
815 return Error(Lexer.getTok().getLoc(),
816 "unexpected token in variant, expected ')'");
817 }
818 Lexer.Lex(); // eat )
819 Split = std::make_pair(Identifier, VName);
820 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000821
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000822 EndLoc = SMLoc::getFromPointer(Identifier.end());
823
Daniel Dunbard20cda02009-10-16 01:34:54 +0000824 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000825 StringRef SymbolName = Identifier;
826 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000827
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000828 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000829 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000830 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000831 if (Variant != MCSymbolRefExpr::VK_Invalid) {
832 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000833 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000834 Variant = MCSymbolRefExpr::VK_None;
835 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000836 Variant = MCSymbolRefExpr::VK_None;
Saleem Abdulrasoola25e1e42014-01-26 22:29:43 +0000837 return Error(SMLoc::getFromPointer(Split.second.begin()),
838 "invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000839 }
840 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000841
Hans Wennborgce69d772013-10-18 20:46:28 +0000842 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
843
Daniel Dunbard20cda02009-10-16 01:34:54 +0000844 // If this is an absolute variable reference, substitute it now to preserve
845 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000846 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000847 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000848 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000849
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000850 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000851 return false;
852 }
853
854 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000855 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000856 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000857 }
David Woodhousef42a6662014-02-01 16:20:54 +0000858 case AsmToken::BigNum:
859 return TokError("literal value out of range for directive");
Kevin Enderby0510b482010-05-17 23:08:19 +0000860 case AsmToken::Integer: {
861 SMLoc Loc = getTok().getLoc();
862 int64_t IntVal = getTok().getIntVal();
863 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000864 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000865 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000866 // Look for 'b' or 'f' following an Integer as a directional label
867 if (Lexer.getKind() == AsmToken::Identifier) {
868 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000869 // Lookup the symbol variant if used.
870 std::pair<StringRef, StringRef> Split = IDVal.split('@');
871 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
872 if (Split.first.size() != IDVal.size()) {
873 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
874 if (Variant == MCSymbolRefExpr::VK_Invalid) {
875 Variant = MCSymbolRefExpr::VK_None;
876 return TokError("invalid variant '" + Split.second + "'");
877 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000878 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000879 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000880 if (IDVal == "f" || IDVal == "b") {
881 MCSymbol *Sym =
882 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000883 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000884 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000885 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000886 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000887 Lex(); // Eat identifier.
888 }
889 }
Chris Lattner78db3622009-06-22 05:51:26 +0000890 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000891 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000892 case AsmToken::Real: {
893 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000894 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000895 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000896 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000897 Lex(); // Eat token.
898 return false;
899 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000900 case AsmToken::Dot: {
901 // This is a '.' reference, which references the current PC. Emit a
902 // temporary label to the streamer and refer to it.
903 MCSymbol *Sym = Ctx.CreateTempSymbol();
904 Out.EmitLabel(Sym);
905 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000906 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000907 Lex(); // Eat identifier.
908 return false;
909 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000910 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000911 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000912 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000913 case AsmToken::LBrac:
914 if (!PlatformParser->HasBracketExpressions())
915 return TokError("brackets expression not supported on this target");
916 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000917 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000918 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000919 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000920 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000921 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000922 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000923 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000924 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000925 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000926 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000927 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000928 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000929 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000930 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000931 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000932 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000933 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000934 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000935 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000936 }
937}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000938
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000939bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000940 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000941 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000942}
943
Daniel Dunbar55f16672010-09-17 02:47:07 +0000944const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000945AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000946 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000947 // Ask the target implementation about this expression first.
948 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
949 if (NewE)
950 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000951 // Recurse over the given expression, rebuilding it to apply the given variant
952 // if there is exactly one symbol.
953 switch (E->getKind()) {
954 case MCExpr::Target:
955 case MCExpr::Constant:
956 return 0;
957
958 case MCExpr::SymbolRef: {
959 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
960
961 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000962 TokError("invalid variant on expression '" + getTok().getIdentifier() +
963 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000964 return E;
965 }
966
967 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
968 }
969
970 case MCExpr::Unary: {
971 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000972 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000973 if (!Sub)
974 return 0;
975 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
976 }
977
978 case MCExpr::Binary: {
979 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000980 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
981 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000982
983 if (!LHS && !RHS)
984 return 0;
985
Jim Grosbach4b905842013-09-20 23:08:21 +0000986 if (!LHS)
987 LHS = BE->getLHS();
988 if (!RHS)
989 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000990
991 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
992 }
993 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000994
Craig Toppera2886c22012-02-07 05:05:23 +0000995 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000996}
997
Jim Grosbach4b905842013-09-20 23:08:21 +0000998/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000999///
Jim Grosbachbd164242011-08-20 16:24:13 +00001000/// expr ::= expr &&,|| expr -> lowest.
1001/// expr ::= expr |,^,&,! expr
1002/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1003/// expr ::= expr <<,>> expr
1004/// expr ::= expr +,- expr
1005/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +00001006/// expr ::= primaryexpr
1007///
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001008bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001009 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001010 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001011 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001012 return true;
1013
Daniel Dunbar55f16672010-09-17 02:47:07 +00001014 // As a special case, we support 'a op b @ modifier' by rewriting the
1015 // expression to include the modifier. This is inefficient, but in general we
1016 // expect users to use 'a@modifier op b'.
1017 if (Lexer.getKind() == AsmToken::At) {
1018 Lex();
1019
1020 if (Lexer.isNot(AsmToken::Identifier))
1021 return TokError("unexpected symbol modifier following '@'");
1022
1023 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001024 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001025 if (Variant == MCSymbolRefExpr::VK_Invalid)
1026 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1027
Jim Grosbach4b905842013-09-20 23:08:21 +00001028 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001029 if (!ModifiedRes) {
1030 return TokError("invalid modifier '" + getTok().getIdentifier() +
1031 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001032 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001033
Daniel Dunbar55f16672010-09-17 02:47:07 +00001034 Res = ModifiedRes;
1035 Lex();
1036 }
1037
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001038 // Try to constant fold it up front, if possible.
1039 int64_t Value;
1040 if (Res->EvaluateAsAbsolute(Value))
1041 Res = MCConstantExpr::Create(Value, getContext());
1042
1043 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001044}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001045
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001046bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001047 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001048 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001049}
1050
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001051bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001052 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001053
Daniel Dunbar75630b32009-06-30 02:10:03 +00001054 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001055 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001056 return true;
1057
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001058 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001059 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001060
1061 return false;
1062}
1063
Michael J. Spencer530ce852010-10-09 11:00:50 +00001064static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001065 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001066 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001067 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001068 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001069
Jim Grosbach4b905842013-09-20 23:08:21 +00001070 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001071 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001072 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001073 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001074 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001075 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001076 return 1;
1077
Jim Grosbach4b905842013-09-20 23:08:21 +00001078 // Low Precedence: |, &, ^
1079 //
1080 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001081 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001082 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001083 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001084 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001085 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001086 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001087 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001088 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001089 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001090
Jim Grosbach4b905842013-09-20 23:08:21 +00001091 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001092 case AsmToken::EqualEqual:
1093 Kind = MCBinaryExpr::EQ;
1094 return 3;
1095 case AsmToken::ExclaimEqual:
1096 case AsmToken::LessGreater:
1097 Kind = MCBinaryExpr::NE;
1098 return 3;
1099 case AsmToken::Less:
1100 Kind = MCBinaryExpr::LT;
1101 return 3;
1102 case AsmToken::LessEqual:
1103 Kind = MCBinaryExpr::LTE;
1104 return 3;
1105 case AsmToken::Greater:
1106 Kind = MCBinaryExpr::GT;
1107 return 3;
1108 case AsmToken::GreaterEqual:
1109 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001110 return 3;
1111
Jim Grosbach4b905842013-09-20 23:08:21 +00001112 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001113 case AsmToken::LessLess:
1114 Kind = MCBinaryExpr::Shl;
1115 return 4;
1116 case AsmToken::GreaterGreater:
1117 Kind = MCBinaryExpr::Shr;
1118 return 4;
1119
Jim Grosbach4b905842013-09-20 23:08:21 +00001120 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001121 case AsmToken::Plus:
1122 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001123 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001124 case AsmToken::Minus:
1125 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001126 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001127
Jim Grosbach4b905842013-09-20 23:08:21 +00001128 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001129 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001130 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001131 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001132 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001133 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001134 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001135 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001136 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001137 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001138 }
1139}
1140
Jim Grosbach4b905842013-09-20 23:08:21 +00001141/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001142/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001143bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001144 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001145 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001146 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001147 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001148
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001149 // If the next token is lower precedence than we are allowed to eat, return
1150 // successfully with what we ate already.
1151 if (TokPrec < Precedence)
1152 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001153
Sean Callanan686ed8d2010-01-19 20:22:31 +00001154 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001155
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001156 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001157 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001158 if (parsePrimaryExpr(RHS, EndLoc))
1159 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001160
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001161 // If BinOp binds less tightly with RHS than the operator after RHS, let
1162 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001163 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001164 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001165 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1166 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001167
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001168 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001169 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001170 }
1171}
1172
Chris Lattner36e02122009-06-21 20:54:55 +00001173/// ParseStatement:
1174/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001175/// ::= Label* Directive ...Operands... EndOfStatement
1176/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001177bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001178 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001179 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001180 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001181 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001182 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001183
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001184 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001185 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001186 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001187 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001188 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001189 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001190 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001191 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001192
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001193 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001194 if (Lexer.is(AsmToken::Integer)) {
1195 LocalLabelVal = getTok().getIntVal();
1196 if (LocalLabelVal < 0) {
1197 if (!TheCondState.Ignore)
1198 return TokError("unexpected token at start of statement");
1199 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001200 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001201 IDVal = getTok().getString();
1202 Lex(); // Consume the integer token to be used as an identifier token.
1203 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001204 if (!TheCondState.Ignore)
1205 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001206 }
1207 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001208 } else if (Lexer.is(AsmToken::Dot)) {
1209 // Treat '.' as a valid identifier in this context.
1210 Lex();
1211 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001212 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001213 if (!TheCondState.Ignore)
1214 return TokError("unexpected token at start of statement");
1215 IDVal = "";
1216 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001217
Chris Lattner926885c2010-04-17 18:14:27 +00001218 // Handle conditional assembly here before checking for skipping. We
1219 // have to do this so that .endif isn't skipped in a ".if 0" block for
1220 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001221 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001222 DirectiveKindMap.find(IDVal);
1223 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1224 ? DK_NO_DIRECTIVE
1225 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001226 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001227 default:
1228 break;
1229 case DK_IF:
1230 return parseDirectiveIf(IDLoc);
1231 case DK_IFB:
1232 return parseDirectiveIfb(IDLoc, true);
1233 case DK_IFNB:
1234 return parseDirectiveIfb(IDLoc, false);
1235 case DK_IFC:
1236 return parseDirectiveIfc(IDLoc, true);
1237 case DK_IFNC:
1238 return parseDirectiveIfc(IDLoc, false);
1239 case DK_IFDEF:
1240 return parseDirectiveIfdef(IDLoc, true);
1241 case DK_IFNDEF:
1242 case DK_IFNOTDEF:
1243 return parseDirectiveIfdef(IDLoc, false);
1244 case DK_ELSEIF:
1245 return parseDirectiveElseIf(IDLoc);
1246 case DK_ELSE:
1247 return parseDirectiveElse(IDLoc);
1248 case DK_ENDIF:
1249 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001250 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001251
Eli Bendersky88024712013-01-16 19:32:36 +00001252 // Ignore the statement if in the middle of inactive conditional
1253 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001254 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001255 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001256 return false;
1257 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001258
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001259 // FIXME: Recurse on local labels?
1260
1261 // See what kind of statement we have.
1262 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001263 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001264 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001265
Chris Lattner36e02122009-06-21 20:54:55 +00001266 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001267 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001268
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001269 // Diagnose attempt to use '.' as a label.
1270 if (IDVal == ".")
1271 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1272
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001273 // Diagnose attempt to use a variable as a label.
1274 //
1275 // FIXME: Diagnostics. Note the location of the definition as a label.
1276 // FIXME: This doesn't diagnose assignment to a symbol which has been
1277 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001278 MCSymbol *Sym;
1279 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001280 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001281 else
1282 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001283 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001284 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001285
Daniel Dunbare73b2672009-08-26 22:13:22 +00001286 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001287 if (!ParsingInlineAsm)
1288 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001289
Kevin Enderbye7739d42011-12-09 18:09:40 +00001290 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001291 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001292 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001293 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1294 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001295
Tim Northover1744d0a2013-10-25 12:49:50 +00001296 getTargetParser().onLabelParsed(Sym);
1297
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001298 // Consume any end of statement token, if present, to avoid spurious
1299 // AddBlankLine calls().
1300 if (Lexer.is(AsmToken::EndOfStatement)) {
1301 Lex();
1302 if (Lexer.is(AsmToken::Eof))
1303 return false;
1304 }
1305
Eli Friedman0f4871d2012-10-22 23:58:19 +00001306 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001307 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001308
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001309 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001310 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001311 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001312
Jim Grosbach4b905842013-09-20 23:08:21 +00001313 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001314
1315 default: // Normal instruction or directive.
1316 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001317 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001318
1319 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001320 if (areMacrosEnabled())
1321 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1322 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001323 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001324
Michael J. Spencer530ce852010-10-09 11:00:50 +00001325 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001326
Eli Bendersky17233942013-01-15 22:59:42 +00001327 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001328 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001329 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001330 //
Eli Bendersky17233942013-01-15 22:59:42 +00001331 // 1. The target-specific assembly parser. Some directives are target
1332 // specific or may potentially behave differently on certain targets.
1333 // 2. Asm parser extensions. For example, platform-specific parsers
1334 // (like the ELF parser) register themselves as extensions.
1335 // 3. The generic directive parser implemented by this class. These are
1336 // all the directives that behave in a target and platform independent
1337 // manner, or at least have a default behavior that's shared between
1338 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001339
Eli Bendersky17233942013-01-15 22:59:42 +00001340 // First query the target-specific parser. It will return 'true' if it
1341 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001342 if (!getTargetParser().ParseDirective(ID))
1343 return false;
1344
Alp Tokercb402912014-01-24 17:20:08 +00001345 // Next, check the extension directive map to see if any extension has
Eli Bendersky17233942013-01-15 22:59:42 +00001346 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001347 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1348 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001349 if (Handler.first)
1350 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1351
1352 // Finally, if no one else is interested in this directive, it must be
1353 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001354 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001355 default:
1356 break;
1357 case DK_SET:
1358 case DK_EQU:
1359 return parseDirectiveSet(IDVal, true);
1360 case DK_EQUIV:
1361 return parseDirectiveSet(IDVal, false);
1362 case DK_ASCII:
1363 return parseDirectiveAscii(IDVal, false);
1364 case DK_ASCIZ:
1365 case DK_STRING:
1366 return parseDirectiveAscii(IDVal, true);
1367 case DK_BYTE:
1368 return parseDirectiveValue(1);
1369 case DK_SHORT:
1370 case DK_VALUE:
1371 case DK_2BYTE:
1372 return parseDirectiveValue(2);
1373 case DK_LONG:
1374 case DK_INT:
1375 case DK_4BYTE:
1376 return parseDirectiveValue(4);
1377 case DK_QUAD:
1378 case DK_8BYTE:
1379 return parseDirectiveValue(8);
David Woodhoused6de0d92014-02-01 16:20:59 +00001380 case DK_OCTA:
1381 return parseDirectiveOctaValue();
Jim Grosbach4b905842013-09-20 23:08:21 +00001382 case DK_SINGLE:
1383 case DK_FLOAT:
1384 return parseDirectiveRealValue(APFloat::IEEEsingle);
1385 case DK_DOUBLE:
1386 return parseDirectiveRealValue(APFloat::IEEEdouble);
1387 case DK_ALIGN: {
1388 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1389 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1390 }
1391 case DK_ALIGN32: {
1392 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1393 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1394 }
1395 case DK_BALIGN:
1396 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1397 case DK_BALIGNW:
1398 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1399 case DK_BALIGNL:
1400 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1401 case DK_P2ALIGN:
1402 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1403 case DK_P2ALIGNW:
1404 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1405 case DK_P2ALIGNL:
1406 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1407 case DK_ORG:
1408 return parseDirectiveOrg();
1409 case DK_FILL:
1410 return parseDirectiveFill();
1411 case DK_ZERO:
1412 return parseDirectiveZero();
1413 case DK_EXTERN:
1414 eatToEndOfStatement(); // .extern is the default, ignore it.
1415 return false;
1416 case DK_GLOBL:
1417 case DK_GLOBAL:
1418 return parseDirectiveSymbolAttribute(MCSA_Global);
1419 case DK_LAZY_REFERENCE:
1420 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1421 case DK_NO_DEAD_STRIP:
1422 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1423 case DK_SYMBOL_RESOLVER:
1424 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1425 case DK_PRIVATE_EXTERN:
1426 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1427 case DK_REFERENCE:
1428 return parseDirectiveSymbolAttribute(MCSA_Reference);
1429 case DK_WEAK_DEFINITION:
1430 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1431 case DK_WEAK_REFERENCE:
1432 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1433 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1434 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1435 case DK_COMM:
1436 case DK_COMMON:
1437 return parseDirectiveComm(/*IsLocal=*/false);
1438 case DK_LCOMM:
1439 return parseDirectiveComm(/*IsLocal=*/true);
1440 case DK_ABORT:
1441 return parseDirectiveAbort();
1442 case DK_INCLUDE:
1443 return parseDirectiveInclude();
1444 case DK_INCBIN:
1445 return parseDirectiveIncbin();
1446 case DK_CODE16:
1447 case DK_CODE16GCC:
1448 return TokError(Twine(IDVal) + " not supported yet");
1449 case DK_REPT:
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00001450 return parseDirectiveRept(IDLoc, IDVal);
Jim Grosbach4b905842013-09-20 23:08:21 +00001451 case DK_IRP:
1452 return parseDirectiveIrp(IDLoc);
1453 case DK_IRPC:
1454 return parseDirectiveIrpc(IDLoc);
1455 case DK_ENDR:
1456 return parseDirectiveEndr(IDLoc);
1457 case DK_BUNDLE_ALIGN_MODE:
1458 return parseDirectiveBundleAlignMode();
1459 case DK_BUNDLE_LOCK:
1460 return parseDirectiveBundleLock();
1461 case DK_BUNDLE_UNLOCK:
1462 return parseDirectiveBundleUnlock();
1463 case DK_SLEB128:
1464 return parseDirectiveLEB128(true);
1465 case DK_ULEB128:
1466 return parseDirectiveLEB128(false);
1467 case DK_SPACE:
1468 case DK_SKIP:
1469 return parseDirectiveSpace(IDVal);
1470 case DK_FILE:
1471 return parseDirectiveFile(IDLoc);
1472 case DK_LINE:
1473 return parseDirectiveLine();
1474 case DK_LOC:
1475 return parseDirectiveLoc();
1476 case DK_STABS:
1477 return parseDirectiveStabs();
1478 case DK_CFI_SECTIONS:
1479 return parseDirectiveCFISections();
1480 case DK_CFI_STARTPROC:
1481 return parseDirectiveCFIStartProc();
1482 case DK_CFI_ENDPROC:
1483 return parseDirectiveCFIEndProc();
1484 case DK_CFI_DEF_CFA:
1485 return parseDirectiveCFIDefCfa(IDLoc);
1486 case DK_CFI_DEF_CFA_OFFSET:
1487 return parseDirectiveCFIDefCfaOffset();
1488 case DK_CFI_ADJUST_CFA_OFFSET:
1489 return parseDirectiveCFIAdjustCfaOffset();
1490 case DK_CFI_DEF_CFA_REGISTER:
1491 return parseDirectiveCFIDefCfaRegister(IDLoc);
1492 case DK_CFI_OFFSET:
1493 return parseDirectiveCFIOffset(IDLoc);
1494 case DK_CFI_REL_OFFSET:
1495 return parseDirectiveCFIRelOffset(IDLoc);
1496 case DK_CFI_PERSONALITY:
1497 return parseDirectiveCFIPersonalityOrLsda(true);
1498 case DK_CFI_LSDA:
1499 return parseDirectiveCFIPersonalityOrLsda(false);
1500 case DK_CFI_REMEMBER_STATE:
1501 return parseDirectiveCFIRememberState();
1502 case DK_CFI_RESTORE_STATE:
1503 return parseDirectiveCFIRestoreState();
1504 case DK_CFI_SAME_VALUE:
1505 return parseDirectiveCFISameValue(IDLoc);
1506 case DK_CFI_RESTORE:
1507 return parseDirectiveCFIRestore(IDLoc);
1508 case DK_CFI_ESCAPE:
1509 return parseDirectiveCFIEscape();
1510 case DK_CFI_SIGNAL_FRAME:
1511 return parseDirectiveCFISignalFrame();
1512 case DK_CFI_UNDEFINED:
1513 return parseDirectiveCFIUndefined(IDLoc);
1514 case DK_CFI_REGISTER:
1515 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001516 case DK_CFI_WINDOW_SAVE:
1517 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001518 case DK_MACROS_ON:
1519 case DK_MACROS_OFF:
1520 return parseDirectiveMacrosOnOff(IDVal);
1521 case DK_MACRO:
1522 return parseDirectiveMacro(IDLoc);
1523 case DK_ENDM:
1524 case DK_ENDMACRO:
1525 return parseDirectiveEndMacro(IDVal);
1526 case DK_PURGEM:
1527 return parseDirectivePurgeMacro(IDLoc);
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00001528 case DK_END:
1529 return parseDirectiveEnd(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001530 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001531
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001532 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001533 }
Chris Lattner36e02122009-06-21 20:54:55 +00001534
Chad Rosierc7f552c2013-02-12 21:33:51 +00001535 // __asm _emit or __asm __emit
1536 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1537 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001538 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001539
1540 // __asm align
1541 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001542 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001543
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001544 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001545
Chris Lattner7cbfa442010-05-19 23:34:33 +00001546 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001547 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001548 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001549 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001550 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001551 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001552
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001553 // Dump the parsed representation, if requested.
1554 if (getShowParsedOperands()) {
1555 SmallString<256> Str;
1556 raw_svector_ostream OS(Str);
1557 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001558 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001559 if (i != 0)
1560 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001561 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001562 }
1563 OS << "]";
1564
Jim Grosbach4b905842013-09-20 23:08:21 +00001565 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001566 }
1567
Kevin Enderby6469fc22011-11-01 22:27:22 +00001568 // If we are generating dwarf for assembly source files and the current
1569 // section is the initial text section then generate a .loc directive for
1570 // the instruction.
1571 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001572 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001573 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001574
Eli Bendersky88024712013-01-16 19:32:36 +00001575 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001576
Eli Bendersky88024712013-01-16 19:32:36 +00001577 // If we previously parsed a cpp hash file line comment then make sure the
1578 // current Dwarf File is for the CppHashFilename if not then emit the
1579 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001580 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001581 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001582 if (CppHashFilename.size() != 0) {
1583 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001584 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001585 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001586 getContext().nextGenDwarfFileNumber(), StringRef(),
1587 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001588
Jim Grosbach4b905842013-09-20 23:08:21 +00001589 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1590 // cache with the different Loc from the call above we save the last
1591 // info we queried here with SrcMgr.FindLineNumber().
1592 unsigned CppHashLocLineNo;
1593 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1594 CppHashLocLineNo = LastQueryLine;
1595 else {
1596 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1597 LastQueryLine = CppHashLocLineNo;
1598 LastQueryIDLoc = CppHashLoc;
1599 LastQueryBuffer = CppHashBuf;
1600 }
1601 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001602 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001603
Jim Grosbach4b905842013-09-20 23:08:21 +00001604 getStreamer().EmitDwarfLocDirective(
1605 getContext().getGenDwarfFileNumber(), Line, 0,
1606 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1607 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001608 }
1609
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001610 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001611 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001612 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001613 HadError = getTargetParser().MatchAndEmitInstruction(
1614 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1615 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001616 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001617
Chris Lattnera2a9d162010-09-11 16:18:25 +00001618 // Don't skip the rest of the line, the instruction parser is responsible for
1619 // that.
1620 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001621}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001622
Jim Grosbach4b905842013-09-20 23:08:21 +00001623/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001624/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001625void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001626 if (!Lexer.is(AsmToken::EndOfStatement))
1627 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001628 // Eat EOL.
1629 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001630}
1631
Jim Grosbach4b905842013-09-20 23:08:21 +00001632/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001633/// ::= # number "filename"
1634/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001635bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001636 Lex(); // Eat the hash token.
1637
1638 if (getLexer().isNot(AsmToken::Integer)) {
1639 // Consume the line since in cases it is not a well-formed line directive,
1640 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001641 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001642 return false;
1643 }
1644
1645 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001646 Lex();
1647
1648 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001649 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001650 return false;
1651 }
1652
1653 StringRef Filename = getTok().getString();
1654 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001655 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001656
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001657 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1658 CppHashLoc = L;
1659 CppHashFilename = Filename;
1660 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001661 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001662
1663 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001664 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001665 return false;
1666}
1667
Jim Grosbach4b905842013-09-20 23:08:21 +00001668/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001669/// for the Filename and LineNo if any in the diagnostic.
1670void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001671 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001672 raw_ostream &OS = errs();
1673
1674 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1675 const SMLoc &DiagLoc = Diag.getLoc();
1676 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1677 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1678
Jim Grosbach4b905842013-09-20 23:08:21 +00001679 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001680 // before printing the message.
1681 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001682 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001683 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1684 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001685 }
1686
Eric Christophera7c32732012-12-18 00:30:54 +00001687 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001688 // manager changed or buffer changed (like in a nested include) then just
1689 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001690 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001691 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001692 if (Parser->SavedDiagHandler)
1693 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1694 else
1695 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001696 return;
1697 }
1698
Eric Christophera7c32732012-12-18 00:30:54 +00001699 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001700 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1701 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001702 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001703
1704 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1705 int CppHashLocLineNo =
1706 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001707 int LineNo =
1708 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001709
Jim Grosbach4b905842013-09-20 23:08:21 +00001710 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1711 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001712 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001713
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001714 if (Parser->SavedDiagHandler)
1715 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1716 else
1717 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001718}
1719
Rafael Espindola2c064482012-08-21 18:29:30 +00001720// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1721// difference being that that function accepts '@' as part of identifiers and
1722// we can't do that. AsmLexer.cpp should probably be changed to handle
1723// '@' as a special case when needed.
1724static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001725 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1726 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001727}
1728
Rafael Espindola34b9c512012-06-03 23:57:14 +00001729bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +00001730 const MCAsmMacroParameters &Parameters,
Jim Grosbach4b905842013-09-20 23:08:21 +00001731 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001732 unsigned NParameters = Parameters.size();
1733 if (NParameters != 0 && NParameters != A.size())
1734 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001735
Preston Gurd05500642012-09-19 20:36:12 +00001736 // A macro without parameters is handled differently on Darwin:
1737 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001738 while (!Body.empty()) {
1739 // Scan for the next substitution.
1740 std::size_t End = Body.size(), Pos = 0;
1741 for (; Pos != End; ++Pos) {
1742 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001743 if (!NParameters) {
1744 // This macro has no parameters, look for $0, $1, etc.
1745 if (Body[Pos] != '$' || Pos + 1 == End)
1746 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001747
Rafael Espindola1134ab232011-06-05 02:43:45 +00001748 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001749 if (Next == '$' || Next == 'n' ||
1750 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001751 break;
1752 } else {
1753 // This macro has parameters, look for \foo, \bar, etc.
1754 if (Body[Pos] == '\\' && Pos + 1 != End)
1755 break;
1756 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001757 }
1758
1759 // Add the prefix.
1760 OS << Body.slice(0, Pos);
1761
1762 // Check if we reached the end.
1763 if (Pos == End)
1764 break;
1765
Rafael Espindola1134ab232011-06-05 02:43:45 +00001766 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001767 switch (Body[Pos + 1]) {
1768 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001769 case '$':
1770 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001771 break;
1772
Jim Grosbach4b905842013-09-20 23:08:21 +00001773 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001774 case 'n':
1775 OS << A.size();
1776 break;
1777
Jim Grosbach4b905842013-09-20 23:08:21 +00001778 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001779 default: {
1780 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001781 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001782 if (Index >= A.size())
1783 break;
1784
1785 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001786 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001787 ie = A[Index].end();
1788 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001789 OS << it->getString();
1790 break;
1791 }
1792 }
1793 Pos += 2;
1794 } else {
1795 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001796 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001797 ++I;
1798
Jim Grosbach4b905842013-09-20 23:08:21 +00001799 const char *Begin = Body.data() + Pos + 1;
1800 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001801 unsigned Index = 0;
1802 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001803 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001804 break;
1805
Preston Gurd05500642012-09-19 20:36:12 +00001806 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001807 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1808 Pos += 3;
1809 else {
1810 OS << '\\' << Argument;
1811 Pos = I;
1812 }
Preston Gurd05500642012-09-19 20:36:12 +00001813 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001814 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001815 ie = A[Index].end();
1816 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001817 if (it->getKind() == AsmToken::String)
1818 OS << it->getStringContents();
1819 else
1820 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001821
Preston Gurd05500642012-09-19 20:36:12 +00001822 Pos += 1 + Argument.size();
1823 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001824 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001825 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001826 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001827 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001828
Rafael Espindola1134ab232011-06-05 02:43:45 +00001829 return false;
1830}
Daniel Dunbar43235712010-07-18 18:54:11 +00001831
Jim Grosbach4b905842013-09-20 23:08:21 +00001832MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1833 SMLoc EL, MemoryBuffer *I)
1834 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1835 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001836
Jim Grosbach4b905842013-09-20 23:08:21 +00001837static bool isOperator(AsmToken::TokenKind kind) {
1838 switch (kind) {
1839 default:
1840 return false;
1841 case AsmToken::Plus:
1842 case AsmToken::Minus:
1843 case AsmToken::Tilde:
1844 case AsmToken::Slash:
1845 case AsmToken::Star:
1846 case AsmToken::Dot:
1847 case AsmToken::Equal:
1848 case AsmToken::EqualEqual:
1849 case AsmToken::Pipe:
1850 case AsmToken::PipePipe:
1851 case AsmToken::Caret:
1852 case AsmToken::Amp:
1853 case AsmToken::AmpAmp:
1854 case AsmToken::Exclaim:
1855 case AsmToken::ExclaimEqual:
1856 case AsmToken::Percent:
1857 case AsmToken::Less:
1858 case AsmToken::LessEqual:
1859 case AsmToken::LessLess:
1860 case AsmToken::LessGreater:
1861 case AsmToken::Greater:
1862 case AsmToken::GreaterEqual:
1863 case AsmToken::GreaterGreater:
1864 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001865 }
1866}
1867
David Majnemer16252452014-01-29 00:07:39 +00001868namespace {
1869class AsmLexerSkipSpaceRAII {
1870public:
1871 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) {
1872 Lexer.setSkipSpace(SkipSpace);
1873 }
1874
1875 ~AsmLexerSkipSpaceRAII() {
1876 Lexer.setSkipSpace(true);
1877 }
1878
1879private:
1880 AsmLexer &Lexer;
1881};
1882}
1883
David Majnemer91fc4c22014-01-29 18:57:46 +00001884bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001885 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001886 unsigned AddTokens = 0;
1887
David Majnemer16252452014-01-29 00:07:39 +00001888 // Darwin doesn't use spaces to delmit arguments.
1889 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001890
1891 for (;;) {
David Majnemer16252452014-01-29 00:07:39 +00001892 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001893 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001894
David Majnemer91fc4c22014-01-29 18:57:46 +00001895 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
Preston Gurd05500642012-09-19 20:36:12 +00001896 break;
Preston Gurd05500642012-09-19 20:36:12 +00001897
1898 if (Lexer.is(AsmToken::Space)) {
1899 Lex(); // Eat spaces
1900
1901 // Spaces can delimit parameters, but could also be part an expression.
1902 // If the token after a space is an operator, add the token and the next
1903 // one into this argument
David Majnemer91fc4c22014-01-29 18:57:46 +00001904 if (!IsDarwin) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001905 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001906 // Check to see whether the token is used as an operator,
1907 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001908 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001909 if (*NextChar == ' ')
1910 AddTokens = 2;
1911 }
1912
1913 if (!AddTokens && ParenLevel == 0) {
Preston Gurd05500642012-09-19 20:36:12 +00001914 break;
1915 }
1916 }
1917 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001918
Jim Grosbach4b905842013-09-20 23:08:21 +00001919 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001920 // to be able to fill in the remaining default parameter values
1921 if (Lexer.is(AsmToken::EndOfStatement))
1922 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001923
1924 // Adjust the current parentheses level.
1925 if (Lexer.is(AsmToken::LParen))
1926 ++ParenLevel;
1927 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1928 --ParenLevel;
1929
1930 // Append the token to the current argument list.
1931 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001932 if (AddTokens)
1933 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001934 Lex();
1935 }
Preston Gurd05500642012-09-19 20:36:12 +00001936
Rafael Espindola768b41c2012-06-15 14:02:34 +00001937 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001938 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001939 return false;
1940}
1941
1942// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001943bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001944 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001945 const unsigned NParameters = M ? M->Parameters.size() : 0;
1946
1947 // Parse two kinds of macro invocations:
1948 // - macros defined without any parameters accept an arbitrary number of them
1949 // - macros defined with parameters accept at most that many of them
1950 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1951 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001952 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001953
David Majnemer91fc4c22014-01-29 18:57:46 +00001954 if (parseMacroArgument(MA))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001955 return true;
1956
David Majnemer91fc4c22014-01-29 18:57:46 +00001957 if (!MA.empty() || (!NParameters && !Lexer.is(AsmToken::EndOfStatement)))
Preston Gurd242ed3152012-09-19 20:29:04 +00001958 A.push_back(MA);
1959 else if (NParameters) {
1960 if (!M->Parameters[Parameter].second.empty())
1961 A.push_back(M->Parameters[Parameter].second);
David Majnemer91fc4c22014-01-29 18:57:46 +00001962 else
1963 A.push_back(MA);
Preston Gurd242ed3152012-09-19 20:29:04 +00001964 }
Jim Grosbach206661622012-07-30 22:44:17 +00001965
Preston Gurd242ed3152012-09-19 20:29:04 +00001966 // At the end of the statement, fill in remaining arguments that have
1967 // default values. If there aren't any, then the next argument is
1968 // required but missing
1969 if (Lexer.is(AsmToken::EndOfStatement)) {
1970 if (NParameters && Parameter < NParameters - 1) {
David Majnemer91fc4c22014-01-29 18:57:46 +00001971 continue;
Preston Gurd242ed3152012-09-19 20:29:04 +00001972 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001973 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001974 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001975
1976 if (Lexer.is(AsmToken::Comma))
1977 Lex();
1978 }
1979 return TokError("Too many arguments");
1980}
1981
Jim Grosbach4b905842013-09-20 23:08:21 +00001982const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1983 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001984 return (I == MacroMap.end()) ? NULL : I->getValue();
1985}
1986
Jim Grosbach4b905842013-09-20 23:08:21 +00001987void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001988 MacroMap[Name] = new MCAsmMacro(Macro);
1989}
1990
Jim Grosbach4b905842013-09-20 23:08:21 +00001991void AsmParser::undefineMacro(StringRef Name) {
1992 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001993 if (I != MacroMap.end()) {
1994 delete I->getValue();
1995 MacroMap.erase(I);
1996 }
1997}
1998
Jim Grosbach4b905842013-09-20 23:08:21 +00001999bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00002000 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
2001 // this, although we should protect against infinite loops.
2002 if (ActiveMacros.size() == 20)
2003 return TokError("macros cannot be nested more than 20 levels deep");
2004
Eli Bendersky38274122013-01-14 23:22:36 +00002005 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00002006 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00002007 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00002008
Rafael Espindola1134ab232011-06-05 02:43:45 +00002009 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2010 // to hold the macro body with substitutions.
2011 SmallString<256> Buf;
2012 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002013 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002014
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002015 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002016 return true;
2017
Eli Bendersky38274122013-01-14 23:22:36 +00002018 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002019 // instantiation.
2020 OS << ".endmacro\n";
2021
Rafael Espindola1134ab232011-06-05 02:43:45 +00002022 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002023 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002024
Daniel Dunbar43235712010-07-18 18:54:11 +00002025 // Create the macro instantiation object and add to the current macro
2026 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002027 MacroInstantiation *MI = new MacroInstantiation(
2028 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002029 ActiveMacros.push_back(MI);
2030
2031 // Jump to the macro instantiation and prime the lexer.
2032 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2033 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2034 Lex();
2035
2036 return false;
2037}
2038
Jim Grosbach4b905842013-09-20 23:08:21 +00002039void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002040 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002041 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002042 Lex();
2043
2044 // Pop the instantiation entry.
2045 delete ActiveMacros.back();
2046 ActiveMacros.pop_back();
2047}
2048
Jim Grosbach4b905842013-09-20 23:08:21 +00002049static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002050 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002051 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002052 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2053 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002054 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002055 case MCExpr::Target:
2056 case MCExpr::Constant:
2057 return false;
2058 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002059 const MCSymbol &S =
2060 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002061 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002062 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002063 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002064 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002065 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002066 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002067 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002068
2069 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002070}
2071
Jim Grosbach4b905842013-09-20 23:08:21 +00002072bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002073 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002074 // FIXME: Use better location, we should use proper tokens.
2075 SMLoc EqualLoc = Lexer.getLoc();
2076
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002077 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002078 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002079 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002080
Rafael Espindola72f5f172012-01-28 05:57:00 +00002081 // Note: we don't count b as used in "a = b". This is to allow
2082 // a = b
2083 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002084
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002085 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002086 return TokError("unexpected token in assignment");
2087
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002088 // Error on assignment to '.'.
2089 if (Name == ".") {
2090 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2091 "(use '.space' or '.org').)"));
2092 }
2093
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002094 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002095 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002096
Daniel Dunbar5f339242009-10-16 01:57:39 +00002097 // Validate that the LHS is allowed to be a variable (either it has not been
2098 // used as a symbol, or it is an absolute symbol).
2099 MCSymbol *Sym = getContext().LookupSymbol(Name);
2100 if (Sym) {
2101 // Diagnose assignment to a label.
2102 //
2103 // FIXME: Diagnostics. Note the location of the definition as a label.
2104 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002105 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002106 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2107 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002108 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002109 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2110 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002111 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002112 return Error(EqualLoc, "redefinition of '" + Name + "'");
2113 else if (!Sym->isVariable())
2114 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002115 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002116 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002117 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002118
2119 // Don't count these checks as uses.
2120 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002121 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002122 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002123
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002124 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002125
2126 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002127 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002128 if (NoDeadStrip)
2129 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2130
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002131 return false;
2132}
2133
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002134/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002135/// ::= identifier
2136/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002137bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002138 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002139 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2140 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002141 // handle this as a context dependent token, instead we detect adjacent tokens
2142 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002143 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2144 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002145
Hans Wennborgce69d772013-10-18 20:46:28 +00002146 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002147 Lex();
2148 if (Lexer.isNot(AsmToken::Identifier))
2149 return true;
2150
Hans Wennborgce69d772013-10-18 20:46:28 +00002151 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2152 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002153 return true;
2154
2155 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002156 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002157 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002158 Lex();
2159 return false;
2160 }
2161
Jim Grosbach4b905842013-09-20 23:08:21 +00002162 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002163 return true;
2164
Sean Callanan936b0d32010-01-19 21:44:56 +00002165 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002166
Sean Callanan686ed8d2010-01-19 20:22:31 +00002167 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002168
2169 return false;
2170}
2171
Jim Grosbach4b905842013-09-20 23:08:21 +00002172/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002173/// ::= .equ identifier ',' expression
2174/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002175/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002176bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002177 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002178
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002179 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002180 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002181
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002182 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002183 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002184 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002185
Jim Grosbach4b905842013-09-20 23:08:21 +00002186 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002187}
2188
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002189bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002190 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002191
2192 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002193 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002194 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2195 if (Str[i] != '\\') {
2196 Data += Str[i];
2197 continue;
2198 }
2199
2200 // Recognize escaped characters. Note that this escape semantics currently
2201 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2202 ++i;
2203 if (i == e)
2204 return TokError("unexpected backslash at end of string");
2205
2206 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002207 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002208 // Consume up to three octal characters.
2209 unsigned Value = Str[i] - '0';
2210
Jim Grosbach4b905842013-09-20 23:08:21 +00002211 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002212 ++i;
2213 Value = Value * 8 + (Str[i] - '0');
2214
Jim Grosbach4b905842013-09-20 23:08:21 +00002215 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002216 ++i;
2217 Value = Value * 8 + (Str[i] - '0');
2218 }
2219 }
2220
2221 if (Value > 255)
2222 return TokError("invalid octal escape sequence (out of range)");
2223
Jim Grosbach4b905842013-09-20 23:08:21 +00002224 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002225 continue;
2226 }
2227
2228 // Otherwise recognize individual escapes.
2229 switch (Str[i]) {
2230 default:
2231 // Just reject invalid escape sequences for now.
2232 return TokError("invalid escape sequence (unrecognized character)");
2233
2234 case 'b': Data += '\b'; break;
2235 case 'f': Data += '\f'; break;
2236 case 'n': Data += '\n'; break;
2237 case 'r': Data += '\r'; break;
2238 case 't': Data += '\t'; break;
2239 case '"': Data += '"'; break;
2240 case '\\': Data += '\\'; break;
2241 }
2242 }
2243
2244 return false;
2245}
2246
Jim Grosbach4b905842013-09-20 23:08:21 +00002247/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002248/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002249bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002250 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002251 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002252
Daniel Dunbara10e5192009-06-24 23:30:00 +00002253 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002254 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002255 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002256
Daniel Dunbaref668c12009-08-14 18:19:52 +00002257 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002258 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002259 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002260
Rafael Espindola64e1af82013-07-02 15:49:13 +00002261 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002262 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002263 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002264
Sean Callanan686ed8d2010-01-19 20:22:31 +00002265 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002266
2267 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002268 break;
2269
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002270 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002271 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002272 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002273 }
2274 }
2275
Sean Callanan686ed8d2010-01-19 20:22:31 +00002276 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002277 return false;
2278}
2279
Jim Grosbach4b905842013-09-20 23:08:21 +00002280/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002281/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002282bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002283 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002284 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002285
Daniel Dunbara10e5192009-06-24 23:30:00 +00002286 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002287 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002288 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002289 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002290 return true;
2291
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002292 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002293 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2294 assert(Size <= 8 && "Invalid size");
2295 uint64_t IntValue = MCE->getValue();
2296 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2297 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002298 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002299 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002300 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002301
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002302 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002303 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002304
Daniel Dunbara10e5192009-06-24 23:30:00 +00002305 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002306 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002307 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002308 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002309 }
2310 }
2311
Sean Callanan686ed8d2010-01-19 20:22:31 +00002312 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002313 return false;
2314}
2315
David Woodhoused6de0d92014-02-01 16:20:59 +00002316/// ParseDirectiveOctaValue
2317/// ::= .octa [ hexconstant (, hexconstant)* ]
2318bool AsmParser::parseDirectiveOctaValue() {
2319 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2320 checkForValidSection();
2321
2322 for (;;) {
2323 if (Lexer.getKind() == AsmToken::Error)
2324 return true;
2325 if (Lexer.getKind() != AsmToken::Integer &&
2326 Lexer.getKind() != AsmToken::BigNum)
2327 return TokError("unknown token in expression");
2328
2329 SMLoc ExprLoc = getLexer().getLoc();
2330 APInt IntValue = getTok().getAPIntVal();
2331 Lex();
2332
2333 uint64_t hi, lo;
2334 if (IntValue.isIntN(64)) {
2335 hi = 0;
2336 lo = IntValue.getZExtValue();
2337 } else if (IntValue.isIntN(128)) {
David Woodhouse6c9a6f92014-02-01 16:52:33 +00002338 // It might actually have more than 128 bits, but the top ones are zero.
2339 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
David Woodhoused6de0d92014-02-01 16:20:59 +00002340 lo = IntValue.getLoBits(64).getZExtValue();
2341 } else
2342 return Error(ExprLoc, "literal value out of range for directive");
2343
2344 if (MAI.isLittleEndian()) {
2345 getStreamer().EmitIntValue(lo, 8);
2346 getStreamer().EmitIntValue(hi, 8);
2347 } else {
2348 getStreamer().EmitIntValue(hi, 8);
2349 getStreamer().EmitIntValue(lo, 8);
2350 }
2351
2352 if (getLexer().is(AsmToken::EndOfStatement))
2353 break;
2354
2355 // FIXME: Improve diagnostic.
2356 if (getLexer().isNot(AsmToken::Comma))
2357 return TokError("unexpected token in directive");
2358 Lex();
2359 }
2360 }
2361
2362 Lex();
2363 return false;
2364}
2365
Jim Grosbach4b905842013-09-20 23:08:21 +00002366/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002367/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002368bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002369 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002370 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002371
2372 for (;;) {
2373 // We don't truly support arithmetic on floating point expressions, so we
2374 // have to manually parse unary prefixes.
2375 bool IsNeg = false;
2376 if (getLexer().is(AsmToken::Minus)) {
2377 Lex();
2378 IsNeg = true;
2379 } else if (getLexer().is(AsmToken::Plus))
2380 Lex();
2381
Michael J. Spencer530ce852010-10-09 11:00:50 +00002382 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002383 getLexer().isNot(AsmToken::Real) &&
2384 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002385 return TokError("unexpected token in directive");
2386
2387 // Convert to an APFloat.
2388 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002389 StringRef IDVal = getTok().getString();
2390 if (getLexer().is(AsmToken::Identifier)) {
2391 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2392 Value = APFloat::getInf(Semantics);
2393 else if (!IDVal.compare_lower("nan"))
2394 Value = APFloat::getNaN(Semantics, false, ~0);
2395 else
2396 return TokError("invalid floating point literal");
2397 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002398 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002399 return TokError("invalid floating point literal");
2400 if (IsNeg)
2401 Value.changeSign();
2402
2403 // Consume the numeric token.
2404 Lex();
2405
2406 // Emit the value as an integer.
2407 APInt AsInt = Value.bitcastToAPInt();
2408 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002409 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002410
2411 if (getLexer().is(AsmToken::EndOfStatement))
2412 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002413
Daniel Dunbar2af16532010-09-24 01:59:56 +00002414 if (getLexer().isNot(AsmToken::Comma))
2415 return TokError("unexpected token in directive");
2416 Lex();
2417 }
2418 }
2419
2420 Lex();
2421 return false;
2422}
2423
Jim Grosbach4b905842013-09-20 23:08:21 +00002424/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002425/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002426bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002427 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002428
2429 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002430 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002431 return true;
2432
Rafael Espindolab91bac62010-10-05 19:42:57 +00002433 int64_t Val = 0;
2434 if (getLexer().is(AsmToken::Comma)) {
2435 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002436 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002437 return true;
2438 }
2439
Rafael Espindola922e3f42010-09-16 15:03:59 +00002440 if (getLexer().isNot(AsmToken::EndOfStatement))
2441 return TokError("unexpected token in '.zero' directive");
2442
2443 Lex();
2444
Rafael Espindola64e1af82013-07-02 15:49:13 +00002445 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002446
2447 return false;
2448}
2449
Jim Grosbach4b905842013-09-20 23:08:21 +00002450/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002451/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002452bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002453 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002454
David Majnemer522d3db2014-02-01 07:19:38 +00002455 SMLoc RepeatLoc = getLexer().getLoc();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002456 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002457 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002458 return true;
2459
David Majnemer522d3db2014-02-01 07:19:38 +00002460 if (NumValues < 0) {
2461 Warning(RepeatLoc,
2462 "'.fill' directive with negative repeat count has no effect");
2463 NumValues = 0;
2464 }
2465
Roman Divackye33098f2013-09-24 17:44:41 +00002466 int64_t FillSize = 1;
2467 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002468
David Majnemer522d3db2014-02-01 07:19:38 +00002469 SMLoc SizeLoc, ExprLoc;
Roman Divackye33098f2013-09-24 17:44:41 +00002470 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2471 if (getLexer().isNot(AsmToken::Comma))
2472 return TokError("unexpected token in '.fill' directive");
2473 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002474
David Majnemer522d3db2014-02-01 07:19:38 +00002475 SizeLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002476 if (parseAbsoluteExpression(FillSize))
2477 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002478
Roman Divackye33098f2013-09-24 17:44:41 +00002479 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2480 if (getLexer().isNot(AsmToken::Comma))
2481 return TokError("unexpected token in '.fill' directive");
2482 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002483
David Majnemer522d3db2014-02-01 07:19:38 +00002484 ExprLoc = getLexer().getLoc();
Roman Divackye33098f2013-09-24 17:44:41 +00002485 if (parseAbsoluteExpression(FillExpr))
2486 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002487
Roman Divackye33098f2013-09-24 17:44:41 +00002488 if (getLexer().isNot(AsmToken::EndOfStatement))
2489 return TokError("unexpected token in '.fill' directive");
2490
2491 Lex();
2492 }
2493 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002494
David Majnemer522d3db2014-02-01 07:19:38 +00002495 if (FillSize < 0) {
2496 Warning(SizeLoc, "'.fill' directive with negative size has no effect");
2497 NumValues = 0;
2498 }
2499 if (FillSize > 8) {
2500 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8");
2501 FillSize = 8;
2502 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002503
David Majnemer522d3db2014-02-01 07:19:38 +00002504 if (!isUInt<32>(FillExpr) && FillSize > 4)
2505 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits");
2506
2507 int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
2508 FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
2509
2510 for (uint64_t i = 0, e = NumValues; i != e; ++i) {
2511 getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
2512 getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
2513 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002514
2515 return false;
2516}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002517
Jim Grosbach4b905842013-09-20 23:08:21 +00002518/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002519/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002520bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002521 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002522
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002523 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002524 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002525 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002526 return true;
2527
2528 // Parse optional fill expression.
2529 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002530 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2531 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002532 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002533 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002534
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002535 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002536 return true;
2537
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002538 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002539 return TokError("unexpected token in '.org' directive");
2540 }
2541
Sean Callanan686ed8d2010-01-19 20:22:31 +00002542 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002543
Jim Grosbachb5912772012-01-27 00:37:08 +00002544 // Only limited forms of relocatable expressions are accepted here, it
2545 // has to be relative to the current section. The streamer will return
2546 // 'true' if the expression wasn't evaluatable.
2547 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2548 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002549
2550 return false;
2551}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002552
Jim Grosbach4b905842013-09-20 23:08:21 +00002553/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002554/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002555bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002556 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002557
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002558 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002559 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002560 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002561 return true;
2562
2563 SMLoc MaxBytesLoc;
2564 bool HasFillExpr = false;
2565 int64_t FillExpr = 0;
2566 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2568 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002569 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002570 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002571
2572 // The fill expression can be omitted while specifying a maximum number of
2573 // alignment bytes, e.g:
2574 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002575 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002576 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002577 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002578 return true;
2579 }
2580
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002581 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2582 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002583 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002584 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002585
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002586 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002587 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002588 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002589
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002590 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002591 return TokError("unexpected token in directive");
2592 }
2593 }
2594
Sean Callanan686ed8d2010-01-19 20:22:31 +00002595 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002596
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002597 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002598 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002599
2600 // Compute alignment in bytes.
2601 if (IsPow2) {
2602 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002603 if (Alignment >= 32) {
2604 Error(AlignmentLoc, "invalid alignment value");
2605 Alignment = 31;
2606 }
2607
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002608 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002609 } else {
2610 // Reject alignments that aren't a power of two, for gas compatibility.
2611 if (!isPowerOf2_64(Alignment))
2612 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002613 }
2614
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002615 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002616 if (MaxBytesLoc.isValid()) {
2617 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002618 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002619 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002620 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002621 }
2622
2623 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002624 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002625 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002626 MaxBytesToFill = 0;
2627 }
2628 }
2629
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002630 // Check whether we should use optimal code alignment for this .align
2631 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002632 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002633 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2634 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002635 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002636 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002637 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002638 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2639 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002640 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002641
2642 return false;
2643}
2644
Jim Grosbach4b905842013-09-20 23:08:21 +00002645/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002646/// ::= .file [number] filename
2647/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002648bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002649 // FIXME: I'm not sure what this is.
2650 int64_t FileNumber = -1;
2651 SMLoc FileNumberLoc = getLexer().getLoc();
2652 if (getLexer().is(AsmToken::Integer)) {
2653 FileNumber = getTok().getIntVal();
2654 Lex();
2655
2656 if (FileNumber < 1)
2657 return TokError("file number less than one");
2658 }
2659
2660 if (getLexer().isNot(AsmToken::String))
2661 return TokError("unexpected token in '.file' directive");
2662
2663 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002664 // Allow the strings to have escaped octal character sequence.
2665 std::string Path = getTok().getString();
2666 if (parseEscapedString(Path))
2667 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002668 Lex();
2669
2670 StringRef Directory;
2671 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002672 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002673 if (getLexer().is(AsmToken::String)) {
2674 if (FileNumber == -1)
2675 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002676 if (parseEscapedString(FilenameData))
2677 return true;
2678 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002679 Directory = Path;
2680 Lex();
2681 } else {
2682 Filename = Path;
2683 }
2684
2685 if (getLexer().isNot(AsmToken::EndOfStatement))
2686 return TokError("unexpected token in '.file' directive");
2687
2688 if (FileNumber == -1)
2689 getStreamer().EmitFileDirective(Filename);
2690 else {
2691 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002692 Error(DirectiveLoc,
2693 "input can't have .file dwarf directives when -g is "
2694 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002695
2696 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2697 Error(FileNumberLoc, "file number already allocated");
2698 }
2699
2700 return false;
2701}
2702
Jim Grosbach4b905842013-09-20 23:08:21 +00002703/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002704/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002705bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2707 if (getLexer().isNot(AsmToken::Integer))
2708 return TokError("unexpected token in '.line' directive");
2709
2710 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002711 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002712 Lex();
2713
2714 // FIXME: Do something with the .line.
2715 }
2716
2717 if (getLexer().isNot(AsmToken::EndOfStatement))
2718 return TokError("unexpected token in '.line' directive");
2719
2720 return false;
2721}
2722
Jim Grosbach4b905842013-09-20 23:08:21 +00002723/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002724/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2725/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2726/// The first number is a file number, must have been previously assigned with
2727/// a .file directive, the second number is the line number and optionally the
2728/// third number is a column position (zero if not specified). The remaining
2729/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002730bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002731 if (getLexer().isNot(AsmToken::Integer))
2732 return TokError("unexpected token in '.loc' directive");
2733 int64_t FileNumber = getTok().getIntVal();
2734 if (FileNumber < 1)
2735 return TokError("file number less than one in '.loc' directive");
2736 if (!getContext().isValidDwarfFileNumber(FileNumber))
2737 return TokError("unassigned file number in '.loc' directive");
2738 Lex();
2739
2740 int64_t LineNumber = 0;
2741 if (getLexer().is(AsmToken::Integer)) {
2742 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002743 if (LineNumber < 0)
2744 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002745 Lex();
2746 }
2747
2748 int64_t ColumnPos = 0;
2749 if (getLexer().is(AsmToken::Integer)) {
2750 ColumnPos = getTok().getIntVal();
2751 if (ColumnPos < 0)
2752 return TokError("column position less than zero in '.loc' directive");
2753 Lex();
2754 }
2755
2756 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2757 unsigned Isa = 0;
2758 int64_t Discriminator = 0;
2759 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2760 for (;;) {
2761 if (getLexer().is(AsmToken::EndOfStatement))
2762 break;
2763
2764 StringRef Name;
2765 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002766 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002767 return TokError("unexpected token in '.loc' directive");
2768
2769 if (Name == "basic_block")
2770 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2771 else if (Name == "prologue_end")
2772 Flags |= DWARF2_FLAG_PROLOGUE_END;
2773 else if (Name == "epilogue_begin")
2774 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2775 else if (Name == "is_stmt") {
2776 Loc = getTok().getLoc();
2777 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002778 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002779 return true;
2780 // The expression must be the constant 0 or 1.
2781 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2782 int Value = MCE->getValue();
2783 if (Value == 0)
2784 Flags &= ~DWARF2_FLAG_IS_STMT;
2785 else if (Value == 1)
2786 Flags |= DWARF2_FLAG_IS_STMT;
2787 else
2788 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002789 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002790 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2791 }
Craig Topperf15655b2013-04-22 04:22:40 +00002792 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002793 Loc = getTok().getLoc();
2794 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002795 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002796 return true;
2797 // The expression must be a constant greater or equal to 0.
2798 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2799 int Value = MCE->getValue();
2800 if (Value < 0)
2801 return Error(Loc, "isa number less than zero");
2802 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002803 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002804 return Error(Loc, "isa number not a constant value");
2805 }
Craig Topperf15655b2013-04-22 04:22:40 +00002806 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002807 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002808 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002809 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002810 return Error(Loc, "unknown sub-directive in '.loc' directive");
2811 }
2812
2813 if (getLexer().is(AsmToken::EndOfStatement))
2814 break;
2815 }
2816 }
2817
2818 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2819 Isa, Discriminator, StringRef());
2820
2821 return false;
2822}
2823
Jim Grosbach4b905842013-09-20 23:08:21 +00002824/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002825/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002826bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002827 return TokError("unsupported directive '.stabs'");
2828}
2829
Jim Grosbach4b905842013-09-20 23:08:21 +00002830/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002831/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002832bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002833 StringRef Name;
2834 bool EH = false;
2835 bool Debug = false;
2836
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002837 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002838 return TokError("Expected an identifier");
2839
2840 if (Name == ".eh_frame")
2841 EH = true;
2842 else if (Name == ".debug_frame")
2843 Debug = true;
2844
2845 if (getLexer().is(AsmToken::Comma)) {
2846 Lex();
2847
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002848 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002849 return TokError("Expected an identifier");
2850
2851 if (Name == ".eh_frame")
2852 EH = true;
2853 else if (Name == ".debug_frame")
2854 Debug = true;
2855 }
2856
2857 getStreamer().EmitCFISections(EH, Debug);
2858 return false;
2859}
2860
Jim Grosbach4b905842013-09-20 23:08:21 +00002861/// parseDirectiveCFIStartProc
David Majnemere035cf92014-01-27 17:20:25 +00002862/// ::= .cfi_startproc [simple]
Jim Grosbach4b905842013-09-20 23:08:21 +00002863bool AsmParser::parseDirectiveCFIStartProc() {
David Majnemere035cf92014-01-27 17:20:25 +00002864 StringRef Simple;
2865 if (getLexer().isNot(AsmToken::EndOfStatement))
2866 if (parseIdentifier(Simple) || Simple != "simple")
2867 return TokError("unexpected token in .cfi_startproc directive");
2868
2869 getStreamer().EmitCFIStartProc(!Simple.empty());
Eli Bendersky17233942013-01-15 22:59:42 +00002870 return false;
2871}
2872
Jim Grosbach4b905842013-09-20 23:08:21 +00002873/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002874/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002875bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002876 getStreamer().EmitCFIEndProc();
2877 return false;
2878}
2879
Jim Grosbach4b905842013-09-20 23:08:21 +00002880/// \brief parse register name or number.
2881bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002882 SMLoc DirectiveLoc) {
2883 unsigned RegNo;
2884
2885 if (getLexer().isNot(AsmToken::Integer)) {
2886 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2887 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002888 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002889 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002890 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002891
2892 return false;
2893}
2894
Jim Grosbach4b905842013-09-20 23:08:21 +00002895/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002896/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002897bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002898 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002899 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002900 return true;
2901
2902 if (getLexer().isNot(AsmToken::Comma))
2903 return TokError("unexpected token in directive");
2904 Lex();
2905
2906 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002907 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002908 return true;
2909
2910 getStreamer().EmitCFIDefCfa(Register, Offset);
2911 return false;
2912}
2913
Jim Grosbach4b905842013-09-20 23:08:21 +00002914/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002915/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002916bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002917 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002918 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002919 return true;
2920
2921 getStreamer().EmitCFIDefCfaOffset(Offset);
2922 return false;
2923}
2924
Jim Grosbach4b905842013-09-20 23:08:21 +00002925/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002926/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002927bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002928 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002929 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002930 return true;
2931
2932 if (getLexer().isNot(AsmToken::Comma))
2933 return TokError("unexpected token in directive");
2934 Lex();
2935
2936 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002937 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002938 return true;
2939
2940 getStreamer().EmitCFIRegister(Register1, Register2);
2941 return false;
2942}
2943
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002944/// parseDirectiveCFIWindowSave
2945/// ::= .cfi_window_save
2946bool AsmParser::parseDirectiveCFIWindowSave() {
2947 getStreamer().EmitCFIWindowSave();
2948 return false;
2949}
2950
Jim Grosbach4b905842013-09-20 23:08:21 +00002951/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002952/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002953bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002954 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002955 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002956 return true;
2957
2958 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2959 return false;
2960}
2961
Jim Grosbach4b905842013-09-20 23:08:21 +00002962/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002963/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002964bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002965 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002966 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002967 return true;
2968
2969 getStreamer().EmitCFIDefCfaRegister(Register);
2970 return false;
2971}
2972
Jim Grosbach4b905842013-09-20 23:08:21 +00002973/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002974/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002975bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002976 int64_t Register = 0;
2977 int64_t Offset = 0;
2978
Jim Grosbach4b905842013-09-20 23:08:21 +00002979 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002980 return true;
2981
2982 if (getLexer().isNot(AsmToken::Comma))
2983 return TokError("unexpected token in directive");
2984 Lex();
2985
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002986 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002987 return true;
2988
2989 getStreamer().EmitCFIOffset(Register, Offset);
2990 return false;
2991}
2992
Jim Grosbach4b905842013-09-20 23:08:21 +00002993/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002994/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002995bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002996 int64_t Register = 0;
2997
Jim Grosbach4b905842013-09-20 23:08:21 +00002998 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002999 return true;
3000
3001 if (getLexer().isNot(AsmToken::Comma))
3002 return TokError("unexpected token in directive");
3003 Lex();
3004
3005 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003006 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00003007 return true;
3008
3009 getStreamer().EmitCFIRelOffset(Register, Offset);
3010 return false;
3011}
3012
3013static bool isValidEncoding(int64_t Encoding) {
3014 if (Encoding & ~0xff)
3015 return false;
3016
3017 if (Encoding == dwarf::DW_EH_PE_omit)
3018 return true;
3019
3020 const unsigned Format = Encoding & 0xf;
3021 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3022 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3023 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3024 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3025 return false;
3026
3027 const unsigned Application = Encoding & 0x70;
3028 if (Application != dwarf::DW_EH_PE_absptr &&
3029 Application != dwarf::DW_EH_PE_pcrel)
3030 return false;
3031
3032 return true;
3033}
3034
Jim Grosbach4b905842013-09-20 23:08:21 +00003035/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00003036/// IsPersonality true for cfi_personality, false for cfi_lsda
3037/// ::= .cfi_personality encoding, [symbol_name]
3038/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00003039bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00003040 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003041 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00003042 return true;
3043 if (Encoding == dwarf::DW_EH_PE_omit)
3044 return false;
3045
3046 if (!isValidEncoding(Encoding))
3047 return TokError("unsupported encoding.");
3048
3049 if (getLexer().isNot(AsmToken::Comma))
3050 return TokError("unexpected token in directive");
3051 Lex();
3052
3053 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003054 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003055 return TokError("expected identifier in directive");
3056
3057 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3058
3059 if (IsPersonality)
3060 getStreamer().EmitCFIPersonality(Sym, Encoding);
3061 else
3062 getStreamer().EmitCFILsda(Sym, Encoding);
3063 return false;
3064}
3065
Jim Grosbach4b905842013-09-20 23:08:21 +00003066/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00003067/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003068bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003069 getStreamer().EmitCFIRememberState();
3070 return false;
3071}
3072
Jim Grosbach4b905842013-09-20 23:08:21 +00003073/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00003074/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00003075bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00003076 getStreamer().EmitCFIRestoreState();
3077 return false;
3078}
3079
Jim Grosbach4b905842013-09-20 23:08:21 +00003080/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00003081/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00003082bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003083 int64_t Register = 0;
3084
Jim Grosbach4b905842013-09-20 23:08:21 +00003085 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003086 return true;
3087
3088 getStreamer().EmitCFISameValue(Register);
3089 return false;
3090}
3091
Jim Grosbach4b905842013-09-20 23:08:21 +00003092/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003093/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003094bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003095 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003096 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003097 return true;
3098
3099 getStreamer().EmitCFIRestore(Register);
3100 return false;
3101}
3102
Jim Grosbach4b905842013-09-20 23:08:21 +00003103/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003104/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003105bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003106 std::string Values;
3107 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003108 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003109 return true;
3110
3111 Values.push_back((uint8_t)CurrValue);
3112
3113 while (getLexer().is(AsmToken::Comma)) {
3114 Lex();
3115
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003116 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003117 return true;
3118
3119 Values.push_back((uint8_t)CurrValue);
3120 }
3121
3122 getStreamer().EmitCFIEscape(Values);
3123 return false;
3124}
3125
Jim Grosbach4b905842013-09-20 23:08:21 +00003126/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003127/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003128bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003129 if (getLexer().isNot(AsmToken::EndOfStatement))
3130 return Error(getLexer().getLoc(),
3131 "unexpected token in '.cfi_signal_frame'");
3132
3133 getStreamer().EmitCFISignalFrame();
3134 return false;
3135}
3136
Jim Grosbach4b905842013-09-20 23:08:21 +00003137/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003138/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003139bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003140 int64_t Register = 0;
3141
Jim Grosbach4b905842013-09-20 23:08:21 +00003142 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003143 return true;
3144
3145 getStreamer().EmitCFIUndefined(Register);
3146 return false;
3147}
3148
Jim Grosbach4b905842013-09-20 23:08:21 +00003149/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003150/// ::= .macros_on
3151/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003152bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003153 if (getLexer().isNot(AsmToken::EndOfStatement))
3154 return Error(getLexer().getLoc(),
3155 "unexpected token in '" + Directive + "' directive");
3156
Jim Grosbach4b905842013-09-20 23:08:21 +00003157 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003158 return false;
3159}
3160
Jim Grosbach4b905842013-09-20 23:08:21 +00003161/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003162/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003163bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003164 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003165 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003166 return TokError("expected identifier in '.macro' directive");
3167
3168 MCAsmMacroParameters Parameters;
David Majnemer91fc4c22014-01-29 18:57:46 +00003169 while (getLexer().isNot(AsmToken::EndOfStatement)) {
3170 MCAsmMacroParameter Parameter;
3171 if (parseIdentifier(Parameter.first))
3172 return TokError("expected identifier in '.macro' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003173
David Majnemer91fc4c22014-01-29 18:57:46 +00003174 if (getLexer().is(AsmToken::Equal)) {
3175 Lex();
3176 if (parseMacroArgument(Parameter.second))
3177 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00003178 }
David Majnemer91fc4c22014-01-29 18:57:46 +00003179
3180 Parameters.push_back(Parameter);
3181
3182 if (getLexer().is(AsmToken::Comma))
3183 Lex();
Eli Bendersky17233942013-01-15 22:59:42 +00003184 }
3185
3186 // Eat the end of statement.
3187 Lex();
3188
3189 AsmToken EndToken, StartToken = getTok();
3190
3191 // Lex the macro definition.
3192 for (;;) {
3193 // Check whether we have reached the end of the file.
3194 if (getLexer().is(AsmToken::Eof))
3195 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3196
3197 // Otherwise, check whether we have reach the .endmacro.
3198 if (getLexer().is(AsmToken::Identifier) &&
3199 (getTok().getIdentifier() == ".endm" ||
3200 getTok().getIdentifier() == ".endmacro")) {
3201 EndToken = getTok();
3202 Lex();
3203 if (getLexer().isNot(AsmToken::EndOfStatement))
3204 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3205 "' directive");
3206 break;
3207 }
3208
3209 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003210 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003211 }
3212
Jim Grosbach4b905842013-09-20 23:08:21 +00003213 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003214 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3215 }
3216
3217 const char *BodyStart = StartToken.getLoc().getPointer();
3218 const char *BodyEnd = EndToken.getLoc().getPointer();
3219 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003220 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3221 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003222 return false;
3223}
3224
Jim Grosbach4b905842013-09-20 23:08:21 +00003225/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003226///
3227/// With the support added for named parameters there may be code out there that
3228/// is transitioning from positional parameters. In versions of gas that did
Alp Tokercb402912014-01-24 17:20:08 +00003229/// not support named parameters they would be ignored on the macro definition.
Kevin Enderby81c944c2013-01-22 21:44:53 +00003230/// But to support both styles of parameters this is not possible so if a macro
Alp Tokercb402912014-01-24 17:20:08 +00003231/// definition has named parameters but does not use them and has what appears
Kevin Enderby81c944c2013-01-22 21:44:53 +00003232/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3233/// warning that the positional parameter found in body which have no effect.
3234/// Hoping the developer will either remove the named parameters from the macro
Alp Tokercb402912014-01-24 17:20:08 +00003235/// definition so the positional parameters get used if that was what was
Kevin Enderby81c944c2013-01-22 21:44:53 +00003236/// intended or change the macro to use the named parameters. It is possible
3237/// this warning will trigger when the none of the named parameters are used
3238/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003239void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003240 StringRef Body,
3241 MCAsmMacroParameters Parameters) {
3242 // If this macro is not defined with named parameters the warning we are
3243 // checking for here doesn't apply.
3244 unsigned NParameters = Parameters.size();
3245 if (NParameters == 0)
3246 return;
3247
3248 bool NamedParametersFound = false;
3249 bool PositionalParametersFound = false;
3250
3251 // Look at the body of the macro for use of both the named parameters and what
3252 // are likely to be positional parameters. This is what expandMacro() is
3253 // doing when it finds the parameters in the body.
3254 while (!Body.empty()) {
3255 // Scan for the next possible parameter.
3256 std::size_t End = Body.size(), Pos = 0;
3257 for (; Pos != End; ++Pos) {
3258 // Check for a substitution or escape.
3259 // This macro is defined with parameters, look for \foo, \bar, etc.
3260 if (Body[Pos] == '\\' && Pos + 1 != End)
3261 break;
3262
3263 // This macro should have parameters, but look for $0, $1, ..., $n too.
3264 if (Body[Pos] != '$' || Pos + 1 == End)
3265 continue;
3266 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003267 if (Next == '$' || Next == 'n' ||
3268 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003269 break;
3270 }
3271
3272 // Check if we reached the end.
3273 if (Pos == End)
3274 break;
3275
3276 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003277 switch (Body[Pos + 1]) {
3278 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003279 case '$':
3280 break;
3281
Jim Grosbach4b905842013-09-20 23:08:21 +00003282 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003283 case 'n':
3284 PositionalParametersFound = true;
3285 break;
3286
Jim Grosbach4b905842013-09-20 23:08:21 +00003287 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003288 default: {
3289 PositionalParametersFound = true;
3290 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003291 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003292 }
3293 Pos += 2;
3294 } else {
3295 unsigned I = Pos + 1;
3296 while (isIdentifierChar(Body[I]) && I + 1 != End)
3297 ++I;
3298
Jim Grosbach4b905842013-09-20 23:08:21 +00003299 const char *Begin = Body.data() + Pos + 1;
3300 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003301 unsigned Index = 0;
3302 for (; Index < NParameters; ++Index)
3303 if (Parameters[Index].first == Argument)
3304 break;
3305
3306 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003307 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3308 Pos += 3;
3309 else {
3310 Pos = I;
3311 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003312 } else {
3313 NamedParametersFound = true;
3314 Pos += 1 + Argument.size();
3315 }
3316 }
3317 // Update the scan point.
3318 Body = Body.substr(Pos);
3319 }
3320
3321 if (!NamedParametersFound && PositionalParametersFound)
3322 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3323 "used in macro body, possible positional parameter "
3324 "found in body which will have no effect");
3325}
3326
Jim Grosbach4b905842013-09-20 23:08:21 +00003327/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003328/// ::= .endm
3329/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003330bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003331 if (getLexer().isNot(AsmToken::EndOfStatement))
3332 return TokError("unexpected token in '" + Directive + "' directive");
3333
3334 // If we are inside a macro instantiation, terminate the current
3335 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003336 if (isInsideMacroInstantiation()) {
3337 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003338 return false;
3339 }
3340
3341 // Otherwise, this .endmacro is a stray entry in the file; well formed
3342 // .endmacro directives are handled during the macro definition parsing.
3343 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003344 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003345}
3346
Jim Grosbach4b905842013-09-20 23:08:21 +00003347/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003348/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003349bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003350 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003351 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003352 return TokError("expected identifier in '.purgem' directive");
3353
3354 if (getLexer().isNot(AsmToken::EndOfStatement))
3355 return TokError("unexpected token in '.purgem' directive");
3356
Jim Grosbach4b905842013-09-20 23:08:21 +00003357 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003358 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3359
Jim Grosbach4b905842013-09-20 23:08:21 +00003360 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003361 return false;
3362}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003363
Jim Grosbach4b905842013-09-20 23:08:21 +00003364/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003365/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003366bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003367 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003368
3369 // Expect a single argument: an expression that evaluates to a constant
3370 // in the inclusive range 0-30.
3371 SMLoc ExprLoc = getLexer().getLoc();
3372 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003373 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003374 return true;
3375 else if (getLexer().isNot(AsmToken::EndOfStatement))
3376 return TokError("unexpected token after expression in"
3377 " '.bundle_align_mode' directive");
3378 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3379 return Error(ExprLoc,
3380 "invalid bundle alignment size (expected between 0 and 30)");
3381
3382 Lex();
3383
3384 // Because of AlignSizePow2's verified range we can safely truncate it to
3385 // unsigned.
3386 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3387 return false;
3388}
3389
Jim Grosbach4b905842013-09-20 23:08:21 +00003390/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003391/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003392bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003393 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003394 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003395
Eli Bendersky802b6282013-01-07 21:51:08 +00003396 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3397 StringRef Option;
3398 SMLoc Loc = getTok().getLoc();
3399 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003400 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003401
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003402 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003403 return Error(Loc, kInvalidOptionError);
3404
3405 if (Option != "align_to_end")
3406 return Error(Loc, kInvalidOptionError);
3407 else if (getLexer().isNot(AsmToken::EndOfStatement))
3408 return Error(Loc,
3409 "unexpected token after '.bundle_lock' directive option");
3410 AlignToEnd = true;
3411 }
3412
Eli Benderskyf483ff92012-12-20 19:05:53 +00003413 Lex();
3414
Eli Bendersky802b6282013-01-07 21:51:08 +00003415 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003416 return false;
3417}
3418
Jim Grosbach4b905842013-09-20 23:08:21 +00003419/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003420/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003421bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003422 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003423
3424 if (getLexer().isNot(AsmToken::EndOfStatement))
3425 return TokError("unexpected token in '.bundle_unlock' directive");
3426 Lex();
3427
3428 getStreamer().EmitBundleUnlock();
3429 return false;
3430}
3431
Jim Grosbach4b905842013-09-20 23:08:21 +00003432/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003433/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003434bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003435 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003436
3437 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003438 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003439 return true;
3440
3441 int64_t FillExpr = 0;
3442 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3443 if (getLexer().isNot(AsmToken::Comma))
3444 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3445 Lex();
3446
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003447 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003448 return true;
3449
3450 if (getLexer().isNot(AsmToken::EndOfStatement))
3451 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3452 }
3453
3454 Lex();
3455
3456 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003457 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3458 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003459
3460 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003461 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003462
3463 return false;
3464}
3465
Jim Grosbach4b905842013-09-20 23:08:21 +00003466/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003467/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003468bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003469 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003470 const MCExpr *Value;
3471
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003472 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003473 return true;
3474
3475 if (getLexer().isNot(AsmToken::EndOfStatement))
3476 return TokError("unexpected token in directive");
3477
3478 if (Signed)
3479 getStreamer().EmitSLEB128Value(Value);
3480 else
3481 getStreamer().EmitULEB128Value(Value);
3482
3483 return false;
3484}
3485
Jim Grosbach4b905842013-09-20 23:08:21 +00003486/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003487/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003488bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003489 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003490 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003491 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003492 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003493
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003494 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003495 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003496
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003497 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003498
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003499 // Assembler local symbols don't make any sense here. Complain loudly.
3500 if (Sym->isTemporary())
3501 return Error(Loc, "non-local symbol required in directive");
3502
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003503 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3504 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003505
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003506 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003507 break;
3508
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003509 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003510 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003511 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003512 }
3513 }
3514
Sean Callanan686ed8d2010-01-19 20:22:31 +00003515 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003516 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003517}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003518
Jim Grosbach4b905842013-09-20 23:08:21 +00003519/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003520/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003521bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003522 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003523
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003524 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003525 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003526 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003527 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003528
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003529 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003530 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003531
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003532 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003533 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003534 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003535
3536 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003537 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003538 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003539 return true;
3540
3541 int64_t Pow2Alignment = 0;
3542 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003543 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003544 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003545 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003546 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003547 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003548
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003549 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3550 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003551 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3552
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003553 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003554 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3555 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003556 if (!isPowerOf2_64(Pow2Alignment))
3557 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3558 Pow2Alignment = Log2_64(Pow2Alignment);
3559 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003560 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003561
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003562 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003563 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003564
Sean Callanan686ed8d2010-01-19 20:22:31 +00003565 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003566
Chris Lattner28ad7542009-07-09 17:25:12 +00003567 // NOTE: a size of zero for a .comm should create a undefined symbol
3568 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003569 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003570 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003571 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003572
Eric Christopherbc818852010-05-14 01:38:54 +00003573 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003574 // may internally end up wanting an alignment in bytes.
3575 // FIXME: Diagnose overflow.
3576 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003577 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003578 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003579
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003580 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003581 return Error(IDLoc, "invalid symbol redefinition");
3582
Chris Lattner28ad7542009-07-09 17:25:12 +00003583 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003584 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003585 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003586 return false;
3587 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003588
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003589 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003590 return false;
3591}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003592
Jim Grosbach4b905842013-09-20 23:08:21 +00003593/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003594/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003595bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003596 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003597 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003598
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003599 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003600 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003601 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003602
Sean Callanan686ed8d2010-01-19 20:22:31 +00003603 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003604
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003605 if (Str.empty())
3606 Error(Loc, ".abort detected. Assembly stopping.");
3607 else
3608 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003609 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003610
3611 return false;
3612}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003613
Jim Grosbach4b905842013-09-20 23:08:21 +00003614/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003615/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003616bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003617 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003618 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003619
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003620 // Allow the strings to have escaped octal character sequence.
3621 std::string Filename;
3622 if (parseEscapedString(Filename))
3623 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003624 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003625 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003626
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003627 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003628 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003629
Chris Lattner693fbb82009-07-16 06:14:39 +00003630 // Attempt to switch the lexer to the included file before consuming the end
3631 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003632 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003633 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003634 return true;
3635 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003636
3637 return false;
3638}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003639
Jim Grosbach4b905842013-09-20 23:08:21 +00003640/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003641/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003642bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003643 if (getLexer().isNot(AsmToken::String))
3644 return TokError("expected string in '.incbin' directive");
3645
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003646 // Allow the strings to have escaped octal character sequence.
3647 std::string Filename;
3648 if (parseEscapedString(Filename))
3649 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003650 SMLoc IncbinLoc = getLexer().getLoc();
3651 Lex();
3652
3653 if (getLexer().isNot(AsmToken::EndOfStatement))
3654 return TokError("unexpected token in '.incbin' directive");
3655
Kevin Enderby109f25c2011-12-14 21:47:48 +00003656 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003657 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003658 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3659 return true;
3660 }
3661
3662 return false;
3663}
3664
Jim Grosbach4b905842013-09-20 23:08:21 +00003665/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003666/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003667bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003668 TheCondStack.push_back(TheCondState);
3669 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003670 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003671 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003672 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003673 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003674 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003675 return true;
3676
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003677 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003678 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003679
Sean Callanan686ed8d2010-01-19 20:22:31 +00003680 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003681
3682 TheCondState.CondMet = ExprValue;
3683 TheCondState.Ignore = !TheCondState.CondMet;
3684 }
3685
3686 return false;
3687}
3688
Jim Grosbach4b905842013-09-20 23:08:21 +00003689/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003690/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003691bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003692 TheCondStack.push_back(TheCondState);
3693 TheCondState.TheCond = AsmCond::IfCond;
3694
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003695 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003696 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003697 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003698 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003699
3700 if (getLexer().isNot(AsmToken::EndOfStatement))
3701 return TokError("unexpected token in '.ifb' directive");
3702
3703 Lex();
3704
3705 TheCondState.CondMet = ExpectBlank == Str.empty();
3706 TheCondState.Ignore = !TheCondState.CondMet;
3707 }
3708
3709 return false;
3710}
3711
Jim Grosbach4b905842013-09-20 23:08:21 +00003712/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003713/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003714bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003715 TheCondStack.push_back(TheCondState);
3716 TheCondState.TheCond = AsmCond::IfCond;
3717
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003718 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003719 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003720 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003721 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003722
3723 if (getLexer().isNot(AsmToken::Comma))
3724 return TokError("unexpected token in '.ifc' directive");
3725
3726 Lex();
3727
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003728 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003729
3730 if (getLexer().isNot(AsmToken::EndOfStatement))
3731 return TokError("unexpected token in '.ifc' directive");
3732
3733 Lex();
3734
3735 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3736 TheCondState.Ignore = !TheCondState.CondMet;
3737 }
3738
3739 return false;
3740}
3741
Jim Grosbach4b905842013-09-20 23:08:21 +00003742/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003743/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003744bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003745 StringRef Name;
3746 TheCondStack.push_back(TheCondState);
3747 TheCondState.TheCond = AsmCond::IfCond;
3748
3749 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003750 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003751 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003752 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003753 return TokError("expected identifier after '.ifdef'");
3754
3755 Lex();
3756
3757 MCSymbol *Sym = getContext().LookupSymbol(Name);
3758
3759 if (expect_defined)
3760 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3761 else
3762 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3763 TheCondState.Ignore = !TheCondState.CondMet;
3764 }
3765
3766 return false;
3767}
3768
Jim Grosbach4b905842013-09-20 23:08:21 +00003769/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003770/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003771bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003772 if (TheCondState.TheCond != AsmCond::IfCond &&
3773 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003774 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3775 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003776 TheCondState.TheCond = AsmCond::ElseIfCond;
3777
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003778 bool LastIgnoreState = false;
3779 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003780 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003781 if (LastIgnoreState || TheCondState.CondMet) {
3782 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003783 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003784 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003785 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003786 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003787 return true;
3788
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003789 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003790 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003791
Sean Callanan686ed8d2010-01-19 20:22:31 +00003792 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003793 TheCondState.CondMet = ExprValue;
3794 TheCondState.Ignore = !TheCondState.CondMet;
3795 }
3796
3797 return false;
3798}
3799
Jim Grosbach4b905842013-09-20 23:08:21 +00003800/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003801/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003802bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003803 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003804 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003805
Sean Callanan686ed8d2010-01-19 20:22:31 +00003806 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003807
3808 if (TheCondState.TheCond != AsmCond::IfCond &&
3809 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003810 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3811 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003812 TheCondState.TheCond = AsmCond::ElseCond;
3813 bool LastIgnoreState = false;
3814 if (!TheCondStack.empty())
3815 LastIgnoreState = TheCondStack.back().Ignore;
3816 if (LastIgnoreState || TheCondState.CondMet)
3817 TheCondState.Ignore = true;
3818 else
3819 TheCondState.Ignore = false;
3820
3821 return false;
3822}
3823
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003824/// parseDirectiveEnd
3825/// ::= .end
3826bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
3827 if (getLexer().isNot(AsmToken::EndOfStatement))
3828 return TokError("unexpected token in '.end' directive");
3829
3830 Lex();
3831
3832 while (Lexer.isNot(AsmToken::Eof))
3833 Lex();
3834
3835 return false;
3836}
3837
Jim Grosbach4b905842013-09-20 23:08:21 +00003838/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003839/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003840bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003841 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003842 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003843
Sean Callanan686ed8d2010-01-19 20:22:31 +00003844 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003845
Jim Grosbach4b905842013-09-20 23:08:21 +00003846 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003847 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3848 ".else");
3849 if (!TheCondStack.empty()) {
3850 TheCondState = TheCondStack.back();
3851 TheCondStack.pop_back();
3852 }
3853
3854 return false;
3855}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003856
Eli Bendersky17233942013-01-15 22:59:42 +00003857void AsmParser::initializeDirectiveKindMap() {
3858 DirectiveKindMap[".set"] = DK_SET;
3859 DirectiveKindMap[".equ"] = DK_EQU;
3860 DirectiveKindMap[".equiv"] = DK_EQUIV;
3861 DirectiveKindMap[".ascii"] = DK_ASCII;
3862 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3863 DirectiveKindMap[".string"] = DK_STRING;
3864 DirectiveKindMap[".byte"] = DK_BYTE;
3865 DirectiveKindMap[".short"] = DK_SHORT;
3866 DirectiveKindMap[".value"] = DK_VALUE;
3867 DirectiveKindMap[".2byte"] = DK_2BYTE;
3868 DirectiveKindMap[".long"] = DK_LONG;
3869 DirectiveKindMap[".int"] = DK_INT;
3870 DirectiveKindMap[".4byte"] = DK_4BYTE;
3871 DirectiveKindMap[".quad"] = DK_QUAD;
3872 DirectiveKindMap[".8byte"] = DK_8BYTE;
David Woodhoused6de0d92014-02-01 16:20:59 +00003873 DirectiveKindMap[".octa"] = DK_OCTA;
Eli Bendersky17233942013-01-15 22:59:42 +00003874 DirectiveKindMap[".single"] = DK_SINGLE;
3875 DirectiveKindMap[".float"] = DK_FLOAT;
3876 DirectiveKindMap[".double"] = DK_DOUBLE;
3877 DirectiveKindMap[".align"] = DK_ALIGN;
3878 DirectiveKindMap[".align32"] = DK_ALIGN32;
3879 DirectiveKindMap[".balign"] = DK_BALIGN;
3880 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3881 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3882 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3883 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3884 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3885 DirectiveKindMap[".org"] = DK_ORG;
3886 DirectiveKindMap[".fill"] = DK_FILL;
3887 DirectiveKindMap[".zero"] = DK_ZERO;
3888 DirectiveKindMap[".extern"] = DK_EXTERN;
3889 DirectiveKindMap[".globl"] = DK_GLOBL;
3890 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003891 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3892 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3893 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3894 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3895 DirectiveKindMap[".reference"] = DK_REFERENCE;
3896 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3897 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3898 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3899 DirectiveKindMap[".comm"] = DK_COMM;
3900 DirectiveKindMap[".common"] = DK_COMMON;
3901 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3902 DirectiveKindMap[".abort"] = DK_ABORT;
3903 DirectiveKindMap[".include"] = DK_INCLUDE;
3904 DirectiveKindMap[".incbin"] = DK_INCBIN;
3905 DirectiveKindMap[".code16"] = DK_CODE16;
3906 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3907 DirectiveKindMap[".rept"] = DK_REPT;
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00003908 DirectiveKindMap[".rep"] = DK_REPT;
Eli Bendersky17233942013-01-15 22:59:42 +00003909 DirectiveKindMap[".irp"] = DK_IRP;
3910 DirectiveKindMap[".irpc"] = DK_IRPC;
3911 DirectiveKindMap[".endr"] = DK_ENDR;
3912 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3913 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3914 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3915 DirectiveKindMap[".if"] = DK_IF;
3916 DirectiveKindMap[".ifb"] = DK_IFB;
3917 DirectiveKindMap[".ifnb"] = DK_IFNB;
3918 DirectiveKindMap[".ifc"] = DK_IFC;
3919 DirectiveKindMap[".ifnc"] = DK_IFNC;
3920 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3921 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3922 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3923 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3924 DirectiveKindMap[".else"] = DK_ELSE;
Saleem Abdulrasool88186c42013-12-18 02:53:03 +00003925 DirectiveKindMap[".end"] = DK_END;
Eli Bendersky17233942013-01-15 22:59:42 +00003926 DirectiveKindMap[".endif"] = DK_ENDIF;
3927 DirectiveKindMap[".skip"] = DK_SKIP;
3928 DirectiveKindMap[".space"] = DK_SPACE;
3929 DirectiveKindMap[".file"] = DK_FILE;
3930 DirectiveKindMap[".line"] = DK_LINE;
3931 DirectiveKindMap[".loc"] = DK_LOC;
3932 DirectiveKindMap[".stabs"] = DK_STABS;
3933 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3934 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3935 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3936 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3937 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3938 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3939 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3940 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3941 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3942 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3943 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3944 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3945 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3946 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3947 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3948 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3949 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3950 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3951 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3952 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3953 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003954 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003955 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3956 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3957 DirectiveKindMap[".macro"] = DK_MACRO;
3958 DirectiveKindMap[".endm"] = DK_ENDM;
3959 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3960 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003961}
3962
Jim Grosbach4b905842013-09-20 23:08:21 +00003963MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003964 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003965
Rafael Espindola34b9c512012-06-03 23:57:14 +00003966 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003967 for (;;) {
3968 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003969 if (getLexer().is(AsmToken::Eof)) {
3970 Error(DirectiveLoc, "no matching '.endr' in definition");
3971 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003972 }
3973
Rafael Espindola34b9c512012-06-03 23:57:14 +00003974 if (Lexer.is(AsmToken::Identifier) &&
3975 (getTok().getIdentifier() == ".rept")) {
3976 ++NestLevel;
3977 }
3978
3979 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003980 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003981 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003982 EndToken = getTok();
3983 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003984 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3985 TokError("unexpected token in '.endr' directive");
3986 return 0;
3987 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003988 break;
3989 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003990 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003991 }
3992
Rafael Espindola34b9c512012-06-03 23:57:14 +00003993 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003994 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003995 }
3996
3997 const char *BodyStart = StartToken.getLoc().getPointer();
3998 const char *BodyEnd = EndToken.getLoc().getPointer();
3999 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4000
Rafael Espindola34b9c512012-06-03 23:57:14 +00004001 // We Are Anonymous.
4002 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00004003 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00004004 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
4005 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00004006}
4007
Jim Grosbach4b905842013-09-20 23:08:21 +00004008void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00004009 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004010 OS << ".endr\n";
4011
4012 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00004013 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004014
Rafael Espindola34b9c512012-06-03 23:57:14 +00004015 // Create the macro instantiation object and add to the current macro
4016 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00004017 MacroInstantiation *MI = new MacroInstantiation(
4018 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004019 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004020
Rafael Espindola34b9c512012-06-03 23:57:14 +00004021 // Jump to the macro instantiation and prime the lexer.
4022 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
4023 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
4024 Lex();
4025}
4026
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004027/// parseDirectiveRept
4028/// ::= .rep | .rept count
4029bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) {
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004030 const MCExpr *CountExpr;
4031 SMLoc CountLoc = getTok().getLoc();
4032 if (parseExpression(CountExpr))
4033 return true;
4034
Rafael Espindola34b9c512012-06-03 23:57:14 +00004035 int64_t Count;
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004036 if (!CountExpr->EvaluateAsAbsolute(Count)) {
4037 eatToEndOfStatement();
4038 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
4039 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00004040
4041 if (Count < 0)
Saleem Abdulrasool51cff712013-12-28 06:39:29 +00004042 return Error(CountLoc, "Count is negative");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004043
4044 if (Lexer.isNot(AsmToken::EndOfStatement))
Saleem Abdulrasoold743d0a2013-12-28 05:54:33 +00004045 return TokError("unexpected token in '" + Dir + "' directive");
Rafael Espindola34b9c512012-06-03 23:57:14 +00004046
4047 // Eat the end of statement.
4048 Lex();
4049
4050 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004051 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00004052 if (!M)
4053 return true;
4054
4055 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4056 // to hold the macro body with substitutions.
4057 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00004058 MCAsmMacroParameters Parameters;
4059 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00004060 raw_svector_ostream OS(Buf);
4061 while (Count--) {
4062 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
4063 return true;
4064 }
Jim Grosbach4b905842013-09-20 23:08:21 +00004065 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004066
4067 return false;
4068}
4069
Jim Grosbach4b905842013-09-20 23:08:21 +00004070/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00004071/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004072bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004073 MCAsmMacroParameters Parameters;
4074 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004075
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004076 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004077 return TokError("expected identifier in '.irp' directive");
4078
4079 Parameters.push_back(Parameter);
4080
4081 if (Lexer.isNot(AsmToken::Comma))
4082 return TokError("expected comma in '.irp' directive");
4083
4084 Lex();
4085
Eli Bendersky38274122013-01-14 23:22:36 +00004086 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004087 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00004088 return true;
4089
4090 // Eat the end of statement.
4091 Lex();
4092
4093 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004094 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004095 if (!M)
4096 return true;
4097
4098 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4099 // to hold the macro body with substitutions.
4100 SmallString<256> Buf;
4101 raw_svector_ostream OS(Buf);
4102
Eli Bendersky38274122013-01-14 23:22:36 +00004103 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4104 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004105 Args.push_back(*i);
4106
4107 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4108 return true;
4109 }
4110
Jim Grosbach4b905842013-09-20 23:08:21 +00004111 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004112
4113 return false;
4114}
4115
Jim Grosbach4b905842013-09-20 23:08:21 +00004116/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004117/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004118bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004119 MCAsmMacroParameters Parameters;
4120 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004121
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004122 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004123 return TokError("expected identifier in '.irpc' directive");
4124
4125 Parameters.push_back(Parameter);
4126
4127 if (Lexer.isNot(AsmToken::Comma))
4128 return TokError("expected comma in '.irpc' directive");
4129
4130 Lex();
4131
Eli Bendersky38274122013-01-14 23:22:36 +00004132 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004133 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004134 return true;
4135
4136 if (A.size() != 1 || A.front().size() != 1)
4137 return TokError("unexpected token in '.irpc' directive");
4138
4139 // Eat the end of statement.
4140 Lex();
4141
4142 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004143 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004144 if (!M)
4145 return true;
4146
4147 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4148 // to hold the macro body with substitutions.
4149 SmallString<256> Buf;
4150 raw_svector_ostream OS(Buf);
4151
4152 StringRef Values = A.front().front().getString();
4153 std::size_t I, End = Values.size();
4154 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004155 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004156 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004157
Eli Bendersky38274122013-01-14 23:22:36 +00004158 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004159 Args.push_back(Arg);
4160
4161 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4162 return true;
4163 }
4164
Jim Grosbach4b905842013-09-20 23:08:21 +00004165 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004166
4167 return false;
4168}
4169
Jim Grosbach4b905842013-09-20 23:08:21 +00004170bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004171 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004172 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004173
4174 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004175 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004176 assert(getLexer().is(AsmToken::EndOfStatement));
4177
Jim Grosbach4b905842013-09-20 23:08:21 +00004178 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004179 return false;
4180}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004181
Jim Grosbach4b905842013-09-20 23:08:21 +00004182bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004183 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004184 const MCExpr *Value;
4185 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004186 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004187 return true;
4188 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4189 if (!MCE)
4190 return Error(ExprLoc, "unexpected expression in _emit");
4191 uint64_t IntValue = MCE->getValue();
4192 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4193 return Error(ExprLoc, "literal value out of range for directive");
4194
Chad Rosierc7f552c2013-02-12 21:33:51 +00004195 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4196 return false;
4197}
4198
Jim Grosbach4b905842013-09-20 23:08:21 +00004199bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004200 const MCExpr *Value;
4201 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004202 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004203 return true;
4204 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4205 if (!MCE)
4206 return Error(ExprLoc, "unexpected expression in align");
4207 uint64_t IntValue = MCE->getValue();
4208 if (!isPowerOf2_64(IntValue))
4209 return Error(ExprLoc, "literal value not a power of two greater then zero");
4210
Jim Grosbach4b905842013-09-20 23:08:21 +00004211 Info.AsmRewrites->push_back(
4212 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004213 return false;
4214}
4215
Chad Rosierf43fcf52013-02-13 21:27:17 +00004216// We are comparing pointers, but the pointers are relative to a single string.
4217// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004218static int rewritesSort(const AsmRewrite *AsmRewriteA,
4219 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004220 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4221 return -1;
4222 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4223 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004224
Chad Rosierfce4fab2013-04-08 17:43:47 +00004225 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4226 // rewrite to the same location. Make sure the SizeDirective rewrite is
4227 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4228 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004229 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4230 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004231 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004232
Jim Grosbach4b905842013-09-20 23:08:21 +00004233 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4234 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004235 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004236 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004237}
4238
Jim Grosbach4b905842013-09-20 23:08:21 +00004239bool AsmParser::parseMSInlineAsm(
4240 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4241 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4242 SmallVectorImpl<std::string> &Constraints,
4243 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4244 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004245 SmallVector<void *, 4> InputDecls;
4246 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004247 SmallVector<bool, 4> InputDeclsAddressOf;
4248 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004249 SmallVector<std::string, 4> InputConstraints;
4250 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004251 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004252
Benjamin Kramer1a136112013-02-15 20:37:21 +00004253 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004254
4255 // Prime the lexer.
4256 Lex();
4257
4258 // While we have input, parse each statement.
4259 unsigned InputIdx = 0;
4260 unsigned OutputIdx = 0;
4261 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004262 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004263 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004264 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004265
Chad Rosier149e8e02012-12-12 22:45:52 +00004266 if (Info.ParseError)
4267 return true;
4268
Benjamin Kramer1a136112013-02-15 20:37:21 +00004269 if (Info.Opcode == ~0U)
4270 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004271
Benjamin Kramer1a136112013-02-15 20:37:21 +00004272 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004273
Benjamin Kramer1a136112013-02-15 20:37:21 +00004274 // Build the list of clobbers, outputs and inputs.
4275 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4276 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004277
Benjamin Kramer1a136112013-02-15 20:37:21 +00004278 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004279 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004280 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004281
Benjamin Kramer1a136112013-02-15 20:37:21 +00004282 // Register operand.
4283 if (Operand->isReg() && !Operand->needAddressOf()) {
4284 unsigned NumDefs = Desc.getNumDefs();
4285 // Clobber.
4286 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4287 ClobberRegs.push_back(Operand->getReg());
4288 continue;
4289 }
4290
4291 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004292 StringRef SymName = Operand->getSymName();
4293 if (SymName.empty())
4294 continue;
4295
Chad Rosierdba3fe52013-04-22 22:12:12 +00004296 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004297 if (!OpDecl)
4298 continue;
4299
4300 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004301 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004302 if (isOutput) {
4303 ++InputIdx;
4304 OutputDecls.push_back(OpDecl);
4305 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4306 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004307 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004308 } else {
4309 InputDecls.push_back(OpDecl);
4310 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4311 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004312 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004313 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004314 }
Reid Kleckneree088972013-12-10 18:27:32 +00004315
4316 // Consider implicit defs to be clobbers. Think of cpuid and push.
4317 const uint16_t *ImpDefs = Desc.getImplicitDefs();
4318 for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I)
4319 ClobberRegs.push_back(ImpDefs[I]);
Chad Rosier8bce6642012-10-18 15:49:34 +00004320 }
4321
4322 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004323 NumOutputs = OutputDecls.size();
4324 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004325
4326 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004327 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4328 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4329 ClobberRegs.end());
4330 Clobbers.assign(ClobberRegs.size(), std::string());
4331 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4332 raw_string_ostream OS(Clobbers[I]);
4333 IP->printRegName(OS, ClobberRegs[I]);
4334 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004335
4336 // Merge the various outputs and inputs. Output are expected first.
4337 if (NumOutputs || NumInputs) {
4338 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004339 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004340 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004341 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004342 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004343 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004344 }
4345 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004346 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004347 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004348 }
4349 }
4350
4351 // Build the IR assembly string.
4352 std::string AsmStringIR;
4353 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004354 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4355 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004356 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004357 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4358 E = AsmStrRewrites.end();
4359 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004360 AsmRewriteKind Kind = (*I).Kind;
4361 if (Kind == AOK_Delete)
4362 continue;
4363
Chad Rosier8bce6642012-10-18 15:49:34 +00004364 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004365 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004366
Chad Rosier120eefd2013-03-19 17:32:17 +00004367 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004368 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004369 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004370 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004371
Chad Rosier37e755c2012-10-23 17:43:43 +00004372 // Skip the original expression.
4373 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004374 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004375 continue;
4376 }
4377
Chad Rosierff10ed12013-04-12 16:26:42 +00004378 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004379 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004380 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004381 default:
4382 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004383 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004384 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004385 break;
4386 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004387 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004388 break;
4389 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004390 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004391 break;
4392 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004393 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004394 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004395 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004396 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004397 default: break;
4398 case 8: OS << "byte ptr "; break;
4399 case 16: OS << "word ptr "; break;
4400 case 32: OS << "dword ptr "; break;
4401 case 64: OS << "qword ptr "; break;
4402 case 80: OS << "xword ptr "; break;
4403 case 128: OS << "xmmword ptr "; break;
4404 case 256: OS << "ymmword ptr "; break;
4405 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004406 break;
4407 case AOK_Emit:
4408 OS << ".byte";
4409 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004410 case AOK_Align: {
4411 unsigned Val = (*I).Val;
4412 OS << ".align " << Val;
4413
4414 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004415 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004416 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4417 break;
4418 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004419 case AOK_DotOperator:
4420 OS << (*I).Val;
4421 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004422 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004423
Chad Rosier8bce6642012-10-18 15:49:34 +00004424 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004425 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004426 }
4427
4428 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004429 if (AsmStart != AsmEnd)
4430 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004431
4432 AsmString = OS.str();
4433 return false;
4434}
4435
Daniel Dunbar01e36072010-07-17 02:26:10 +00004436/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004437MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4438 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004439 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004440}