blob: 1267dc814cde2c61d11e981868ed3c4529f840b7 [file] [log] [blame]
Chris Lattner27aa7d22009-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 Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Chad Rosierabde6752013-02-13 18:38:58 +000016#include "llvm/ADT/STLExtras.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosierb1f8c132012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000025#include "llvm/MC/MCParser/AsmCond.h"
26#include "llvm/MC/MCParser/AsmLexer.h"
27#include "llvm/MC/MCParser/MCAsmParser.h"
28#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000029#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000030#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000031#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000033#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000034#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000035#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000036#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000037#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000039#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000040#include <cctype>
Chad Rosierb1f8c132012-10-18 15:49:34 +000041#include <set>
42#include <string>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000043#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000044using namespace llvm;
45
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000046static cl::opt<bool>
47FatalAssemblerWarnings("fatal-assembler-warnings",
48 cl::desc("Consider warnings as error"));
49
Eric Christopher2318ba12012-12-18 00:30:54 +000050MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewycky0d7d11d2012-10-19 07:00:09 +000051
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000052namespace {
53
Eli Benderskyf9f40bd2013-01-16 18:56:50 +000054/// \brief Helper types for tracking macro definitions.
55typedef std::vector<AsmToken> MCAsmMacroArgument;
56typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
57typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
58typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
59
60struct MCAsmMacro {
61 StringRef Name;
62 StringRef Body;
63 MCAsmMacroParameters Parameters;
64
65public:
66 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
67 Name(N), Body(B), Parameters(P) {}
68
69 MCAsmMacro(const MCAsmMacro& Other)
70 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
71};
72
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000073/// \brief Helper class for storing information about an active macro
74/// instantiation.
75struct MacroInstantiation {
76 /// The macro being instantiated.
Eli Benderskyc0c67b02013-01-14 23:22:36 +000077 const MCAsmMacro *TheMacro;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000078
79 /// The macro instantiation with substitutions.
80 MemoryBuffer *Instantiation;
81
82 /// The location of the instantiation.
83 SMLoc InstantiationLoc;
84
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000085 /// The buffer where parsing should resume upon instantiation completion.
86 int ExitBuffer;
87
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000088 /// The location where parsing should resume upon instantiation completion.
89 SMLoc ExitLoc;
90
91public:
Eli Benderskyc0c67b02013-01-14 23:22:36 +000092 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000093 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000094};
95
Eli Friedman2128aae2012-10-22 23:58:19 +000096struct ParseStatementInfo {
Jim Grosbach4a200922013-09-20 23:08:21 +000097 /// \brief The parsed operands from the last parsed statement.
Eli Friedman2128aae2012-10-22 23:58:19 +000098 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
99
Jim Grosbach4a200922013-09-20 23:08:21 +0000100 /// \brief The opcode from the last parsed instruction.
Eli Friedman2128aae2012-10-22 23:58:19 +0000101 unsigned Opcode;
102
Jim Grosbach4a200922013-09-20 23:08:21 +0000103 /// \brief Was there an error parsing the inline assembly?
Chad Rosier57498012012-12-12 22:45:52 +0000104 bool ParseError;
105
Eli Friedman2128aae2012-10-22 23:58:19 +0000106 SmallVectorImpl<AsmRewrite> *AsmRewrites;
107
Chad Rosier57498012012-12-12 22:45:52 +0000108 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000109 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier57498012012-12-12 22:45:52 +0000110 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000111
112 ~ParseStatementInfo() {
113 // Free any parsed operands.
114 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
115 delete ParsedOperands[i];
116 ParsedOperands.clear();
117 }
118};
119
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000120/// \brief The concrete assembly parser instance.
121class AsmParser : public MCAsmParser {
Craig Topper85aadc02012-09-15 16:23:52 +0000122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000124private:
125 AsmLexer Lexer;
126 MCContext &Ctx;
127 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000128 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000129 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000130 SourceMgr::DiagHandlerTy SavedDiagHandler;
131 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000132 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000133
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
136 int CurBuffer;
137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
Jim Grosbach4a200922013-09-20 23:08:21 +0000141 /// \brief maps directive names to handler methods in parser
Eli Bendersky6ee13082013-01-15 22:59:42 +0000142 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000143 /// addDirectiveHandler.
Eli Bendersky6ee13082013-01-15 22:59:42 +0000144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000145
Jim Grosbach4a200922013-09-20 23:08:21 +0000146 /// \brief Map of currently defined macros.
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000147 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000148
Jim Grosbach4a200922013-09-20 23:08:21 +0000149 /// \brief Stack of active macro instantiations.
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000150 std::vector<MacroInstantiation*> ActiveMacros;
151
Jim Grosbach4a200922013-09-20 23:08:21 +0000152 /// \brief List of bodies of anonymous macros.
Benjamin Kramera2b0c332013-08-04 09:06:29 +0000153 std::deque<MCAsmMacro> MacroLikeBodies;
154
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000155 /// Boolean tracking whether macro substitution is enabled.
Eli Bendersky733c3362013-01-14 18:08:41 +0000156 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000157
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000158 /// Flag tracking whether any errors have been encountered.
159 unsigned HadError : 1;
160
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000161 /// The values from the last parsed cpp hash file line comment if any.
162 StringRef CppHashFilename;
163 int64_t CppHashLineNumber;
164 SMLoc CppHashLoc;
Kevin Enderby32c1a822012-11-05 21:55:41 +0000165 int CppHashBuf;
Kevin Enderbya8959492013-06-21 20:51:39 +0000166 /// When generating dwarf for assembly source files we need to calculate the
167 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic7b0a7962013-08-20 13:33:18 +0000168 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderbya8959492013-06-21 20:51:39 +0000169 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
170 SMLoc LastQueryIDLoc;
171 int LastQueryBuffer;
172 unsigned LastQueryLine;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000173
Devang Patel0db58bf2012-01-31 18:14:05 +0000174 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
175 unsigned AssemblerDialect;
176
Jim Grosbach4a200922013-09-20 23:08:21 +0000177 /// \brief is Darwin compatibility enabled?
Preston Gurd7b6f2032012-09-19 20:36:12 +0000178 bool IsDarwin;
179
Jim Grosbach4a200922013-09-20 23:08:21 +0000180 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000181 bool ParsingInlineAsm;
182
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000184 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000186 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187
188 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
189
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000190 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky171192f2013-01-16 00:50:52 +0000191 ExtensionDirectiveHandler Handler) {
192 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000193 }
194
195public:
196 /// @name MCAsmParser Interface
197 /// {
198
199 virtual SourceMgr &getSourceManager() { return SrcMgr; }
200 virtual MCAsmLexer &getLexer() { return Lexer; }
201 virtual MCContext &getContext() { return Ctx; }
202 virtual MCStreamer &getStreamer() { return Out; }
Eric Christopher2318ba12012-12-18 00:30:54 +0000203 virtual unsigned getAssemblerDialect() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000204 if (AssemblerDialect == ~0U)
Eric Christopher2318ba12012-12-18 00:30:54 +0000205 return MAI.getAssemblerDialect();
Devang Patel0db58bf2012-01-31 18:14:05 +0000206 else
207 return AssemblerDialect;
208 }
209 virtual void setAssemblerDialect(unsigned i) {
210 AssemblerDialect = i;
211 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000212
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000213 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko5c332db2013-05-05 00:40:33 +0000214 ArrayRef<SMRange> Ranges = None);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000215 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko5c332db2013-05-05 00:40:33 +0000216 ArrayRef<SMRange> Ranges = None);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000217
Craig Topper345d16d2012-08-29 05:48:09 +0000218 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000219
Chad Rosier84125ca2012-10-13 00:26:04 +0000220 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000221 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000222
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000223 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000224 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000225 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000226 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000227 SmallVectorImpl<std::string> &Clobbers,
228 const MCInstrInfo *MII,
229 const MCInstPrinter *IP,
230 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000231
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000232 bool parseExpression(const MCExpr *&Res);
233 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosierba69b362013-04-10 17:35:30 +0000234 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000235 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
236 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000237
Jim Grosbach4a200922013-09-20 23:08:21 +0000238 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Benderskybf706b32013-01-12 00:05:00 +0000239 /// and set \p Res to the identifier contents.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000240 virtual bool parseIdentifier(StringRef &Res);
241 virtual void eatToEndOfStatement();
Eli Benderskybf706b32013-01-12 00:05:00 +0000242
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000243 virtual void checkForValidSection();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244 /// }
245
246private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000247
Jim Grosbach4a200922013-09-20 23:08:21 +0000248 bool parseStatement(ParseStatementInfo &Info);
249 void eatToEndOfLine();
250 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000251
Jim Grosbach4a200922013-09-20 23:08:21 +0000252 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Kevin Enderby221514e2013-01-22 21:44:53 +0000253 MCAsmMacroParameters Parameters);
Rafael Espindola761cb062012-06-03 23:57:14 +0000254 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000255 const MCAsmMacroParameters &Parameters,
256 const MCAsmMacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000257 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000258
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000259 /// \brief Are macros enabled in the parser?
Jim Grosbach4a200922013-09-20 23:08:21 +0000260 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000261
262 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4a200922013-09-20 23:08:21 +0000263 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000264
265 /// \brief Lookup a previously defined macro.
266 /// \param Name Macro name.
267 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4a200922013-09-20 23:08:21 +0000268 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000269
270 /// \brief Define a new macro with the given name and information.
Jim Grosbach4a200922013-09-20 23:08:21 +0000271 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000272
273 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4a200922013-09-20 23:08:21 +0000274 void undefineMacro(StringRef Name);
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000275
276 /// \brief Are we inside a macro instantiation?
Jim Grosbach4a200922013-09-20 23:08:21 +0000277 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000278
Vladimir Medic7b0a7962013-08-20 13:33:18 +0000279 /// \brief Handle entry to macro instantiation.
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000280 ///
281 /// \param M The macro.
282 /// \param NameLoc Instantiation location.
Jim Grosbach4a200922013-09-20 23:08:21 +0000283 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000284
285 /// \brief Handle exit from macro instantiation.
Jim Grosbach4a200922013-09-20 23:08:21 +0000286 void handleMacroExit();
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000287
288 /// \brief Extract AsmTokens for a macro argument. If the argument delimiter
289 /// is initially unknown, set it to AsmToken::Eof. It will be set to the
290 /// correct delimiter by the method.
Jim Grosbach4a200922013-09-20 23:08:21 +0000291 bool parseMacroArgument(MCAsmMacroArgument &MA,
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000292 AsmToken::TokenKind &ArgumentDelimiter);
293
294 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4a200922013-09-20 23:08:21 +0000295 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskyf9f40bd2013-01-16 18:56:50 +0000296
Jim Grosbach4a200922013-09-20 23:08:21 +0000297 void printMacroInstantiations();
298 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko5c332db2013-05-05 00:40:33 +0000299 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner462b43c2011-10-16 05:47:55 +0000300 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000301 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000302 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000303
Jim Grosbach4a200922013-09-20 23:08:21 +0000304 /// \brief Enter the specified file. This returns true on failure.
305 bool enterIncludeFile(const std::string &Filename);
306
307 /// \brief Process the specified file for the .incbin directive.
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000308 /// This returns true on failure.
Jim Grosbach4a200922013-09-20 23:08:21 +0000309 bool processIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000310
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000311 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000312 /// current token is not set; clients should ensure Lex() is called
313 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000314 ///
315 /// \param InBuffer If not -1, should be the known buffer id that contains the
316 /// location.
Jim Grosbach4a200922013-09-20 23:08:21 +0000317 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000318
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000319 /// \brief Parse up to the end of statement and a return the contents from the
320 /// current token until the end of the statement; the current token on exit
321 /// will be either the EndOfStatement or EOF.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000322 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000323
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000324 /// \brief Parse until the end of a statement or a comma is encountered,
325 /// return the contents from the current token up to the end or comma.
Jim Grosbach4a200922013-09-20 23:08:21 +0000326 StringRef parseStringToComma();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000327
Jim Grosbach4a200922013-09-20 23:08:21 +0000328 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000329 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000330
Jim Grosbach4a200922013-09-20 23:08:21 +0000331 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
332 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
333 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000334
Jim Grosbach4a200922013-09-20 23:08:21 +0000335 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola787c3372010-10-28 20:02:27 +0000336
Eli Bendersky6ee13082013-01-15 22:59:42 +0000337 // Generic (target and platform independent) directive parsing.
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000338 enum DirectiveKind {
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000339 DK_NO_DIRECTIVE, // Placeholder
340 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
341 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
342 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000343 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000344 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby4f066b62013-08-28 17:50:59 +0000345 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000346 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
347 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
348 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
349 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
350 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky6ee13082013-01-15 22:59:42 +0000351 DK_ELSEIF, DK_ELSE, DK_ENDIF,
352 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
353 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
354 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
355 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
356 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
357 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
358 DK_CFI_REGISTER,
359 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
360 DK_SLEB128, DK_ULEB128
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000361 };
362
Jim Grosbach4a200922013-09-20 23:08:21 +0000363 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky6ee13082013-01-15 22:59:42 +0000364 /// directives parsed by this class.
365 StringMap<DirectiveKind> DirectiveKindMap;
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000366
367 // ".ascii", ".asciz", ".string"
Jim Grosbach4a200922013-09-20 23:08:21 +0000368 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
369 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
370 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
371 bool parseDirectiveFill(); // ".fill"
372 bool parseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000373 // ".set", ".equ", ".equiv"
Jim Grosbach4a200922013-09-20 23:08:21 +0000374 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
375 bool parseDirectiveOrg(); // ".org"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000376 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4a200922013-09-20 23:08:21 +0000377 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000378
Eli Bendersky6ee13082013-01-15 22:59:42 +0000379 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4a200922013-09-20 23:08:21 +0000380 bool parseDirectiveFile(SMLoc DirectiveLoc);
381 bool parseDirectiveLine();
382 bool parseDirectiveLoc();
383 bool parseDirectiveStabs();
Eli Bendersky6ee13082013-01-15 22:59:42 +0000384
385 // .cfi directives
Jim Grosbach4a200922013-09-20 23:08:21 +0000386 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
387 bool parseDirectiveCFISections();
388 bool parseDirectiveCFIStartProc();
389 bool parseDirectiveCFIEndProc();
390 bool parseDirectiveCFIDefCfaOffset();
391 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
392 bool parseDirectiveCFIAdjustCfaOffset();
393 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
394 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
397 bool parseDirectiveCFIRememberState();
398 bool parseDirectiveCFIRestoreState();
399 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
400 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIEscape();
402 bool parseDirectiveCFISignalFrame();
403 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000404
405 // macro directives
Jim Grosbach4a200922013-09-20 23:08:21 +0000406 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
407 bool parseDirectiveEndMacro(StringRef Directive);
408 bool parseDirectiveMacro(SMLoc DirectiveLoc);
409 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000410
Eli Bendersky4766ef42012-12-20 19:05:53 +0000411 // ".bundle_align_mode"
Jim Grosbach4a200922013-09-20 23:08:21 +0000412 bool parseDirectiveBundleAlignMode();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000413 // ".bundle_lock"
Jim Grosbach4a200922013-09-20 23:08:21 +0000414 bool parseDirectiveBundleLock();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000415 // ".bundle_unlock"
Jim Grosbach4a200922013-09-20 23:08:21 +0000416 bool parseDirectiveBundleUnlock();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000417
Eli Bendersky6ee13082013-01-15 22:59:42 +0000418 // ".space", ".skip"
Jim Grosbach4a200922013-09-20 23:08:21 +0000419 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000420
421 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4a200922013-09-20 23:08:21 +0000422 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000423
Jim Grosbach4a200922013-09-20 23:08:21 +0000424 /// \brief Parse a directive like ".globl" which
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000425 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4a200922013-09-20 23:08:21 +0000426 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000427
Jim Grosbach4a200922013-09-20 23:08:21 +0000428 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000429
Jim Grosbach4a200922013-09-20 23:08:21 +0000430 bool parseDirectiveAbort(); // ".abort"
431 bool parseDirectiveInclude(); // ".include"
432 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000433
Jim Grosbach4a200922013-09-20 23:08:21 +0000434 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000435 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4a200922013-09-20 23:08:21 +0000436 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000437 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4a200922013-09-20 23:08:21 +0000438 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000439 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4a200922013-09-20 23:08:21 +0000440 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
441 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
442 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
443 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000444 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000445
Jim Grosbach4a200922013-09-20 23:08:21 +0000446 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbarcceba832010-09-17 02:47:07 +0000447 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000448
Rafael Espindola761cb062012-06-03 23:57:14 +0000449 // Macro-like directives
Jim Grosbach4a200922013-09-20 23:08:21 +0000450 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
451 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +0000452 raw_svector_ostream &OS);
Jim Grosbach4a200922013-09-20 23:08:21 +0000453 bool parseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
454 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
455 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
456 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000457
Chad Rosier469b1442013-02-12 21:33:51 +0000458 // "_emit" or "__emit"
Jim Grosbach4a200922013-09-20 23:08:21 +0000459 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosier469b1442013-02-12 21:33:51 +0000460 size_t Len);
461
462 // "align"
Jim Grosbach4a200922013-09-20 23:08:21 +0000463 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000464
Eli Bendersky6ee13082013-01-15 22:59:42 +0000465 void initializeDirectiveKindMap();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000466};
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000467}
468
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000469namespace llvm {
470
471extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000472extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000473extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000474
475}
476
Chris Lattneraaec2052010-01-19 19:46:13 +0000477enum { DEFAULT_ADDRSPACE = 0 };
478
Jim Grosbach4a200922013-09-20 23:08:21 +0000479AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
480 const MCAsmInfo &_MAI)
481 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
482 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
483 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
484 ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000485 // Save the old handler.
486 SavedDiagHandler = SrcMgr.getDiagHandler();
487 SavedDiagContext = SrcMgr.getDiagContext();
488 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000489 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000490 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000491
Daniel Dunbare4749702010-07-12 18:12:02 +0000492 // Initialize the platform / file format parser.
493 //
494 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
495 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000496 if (_MAI.hasMicrosoftFastStdCallMangling()) {
497 PlatformParser = createCOFFAsmParser();
498 PlatformParser->Initialize(*this);
499 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000500 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000501 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000502 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000503 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000504 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000505 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000506 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000507
Eli Bendersky6ee13082013-01-15 22:59:42 +0000508 initializeDirectiveKindMap();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000509}
510
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000511AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000512 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
513
514 // Destroy any macros.
Jim Grosbach4a200922013-09-20 23:08:21 +0000515 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
516 ie = MacroMap.end();
517 it != ie; ++it)
Daniel Dunbar56491302010-07-29 01:51:55 +0000518 delete it->getValue();
519
Daniel Dunbare4749702010-07-12 18:12:02 +0000520 delete PlatformParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000521}
522
Jim Grosbach4a200922013-09-20 23:08:21 +0000523void AsmParser::printMacroInstantiations() {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000524 // Print the active macro instantiation stack.
Jim Grosbach4a200922013-09-20 23:08:21 +0000525 for (std::vector<MacroInstantiation *>::const_reverse_iterator
526 it = ActiveMacros.rbegin(),
527 ie = ActiveMacros.rend();
528 it != ie; ++it)
529 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000530 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000531}
532
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000533bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000534 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000535 return Error(L, Msg, Ranges);
Jim Grosbach4a200922013-09-20 23:08:21 +0000536 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
537 printMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000538 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000539}
540
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000541bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000542 HadError = true;
Jim Grosbach4a200922013-09-20 23:08:21 +0000543 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
544 printMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000545 return true;
546}
547
Jim Grosbach4a200922013-09-20 23:08:21 +0000548bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000549 std::string IncludedFile;
550 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000551 if (NewBuf == -1)
552 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000553
Sean Callananfd0b0282010-01-21 00:19:58 +0000554 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000555
Sean Callananfd0b0282010-01-21 00:19:58 +0000556 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000557
Sean Callananfd0b0282010-01-21 00:19:58 +0000558 return false;
559}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000560
Sylvestre Ledruda2ed452013-05-14 23:36:24 +0000561/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000562/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000563/// returns true on failure.
Jim Grosbach4a200922013-09-20 23:08:21 +0000564bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000565 std::string IncludedFile;
566 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
567 if (NewBuf == -1)
568 return true;
569
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000570 // Pick up the bytes from the file and emit them.
Rafael Espindolaa3863ea2013-07-02 15:49:13 +0000571 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000572 return false;
573}
574
Jim Grosbach4a200922013-09-20 23:08:21 +0000575void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000576 if (InBuffer != -1) {
577 CurBuffer = InBuffer;
578 } else {
579 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
580 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000581 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
582}
583
Sean Callananfd0b0282010-01-21 00:19:58 +0000584const AsmToken &AsmParser::Lex() {
585 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000586
Sean Callananfd0b0282010-01-21 00:19:58 +0000587 if (tok->is(AsmToken::Eof)) {
588 // If this is the end of an included file, pop the parent file off the
589 // include stack.
590 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
591 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4a200922013-09-20 23:08:21 +0000592 jumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000593 tok = &Lexer.Lex();
594 }
595 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000596
Sean Callananfd0b0282010-01-21 00:19:58 +0000597 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000598 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000599
Sean Callananfd0b0282010-01-21 00:19:58 +0000600 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000601}
602
Chris Lattner79180e22010-04-05 23:15:42 +0000603bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000604 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000605 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000606 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000607
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000608 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000609 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000610
611 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000612 AsmCond StartingCondState = TheCondState;
613
Kevin Enderby613b7572011-11-01 22:27:22 +0000614 // If we are generating dwarf for assembly source files save the initial text
615 // section and generate a .file directive.
616 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000617 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000618 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
619 getStreamer().EmitLabel(SectionStartSym);
620 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000621 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000622 StringRef(),
623 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000624 }
625
Chris Lattnerb717fb02009-07-02 21:53:43 +0000626 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000627 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000628 ParseStatementInfo Info;
Jim Grosbach4a200922013-09-20 23:08:21 +0000629 if (!parseStatement(Info))
630 continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000631
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000632 // We had an error, validate that one was emitted and recover by skipping to
633 // the next line.
634 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000635 eatToEndOfStatement();
Chris Lattnerb717fb02009-07-02 21:53:43 +0000636 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000637
638 if (TheCondState.TheCond != StartingCondState.TheCond ||
639 TheCondState.Ignore != StartingCondState.Ignore)
640 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000641
642 // Check to see there are no empty DwarfFile slots.
Manman Ren9e999ad2013-03-12 20:17:00 +0000643 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4a200922013-09-20 23:08:21 +0000644 getContext().getMCDwarfFiles();
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000645 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000646 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000647 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000648 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000649
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000650 // Check to see that all assembler local symbols were actually defined.
651 // Targets that don't do subsections via symbols may not want this, though,
652 // so conservatively exclude them. Only do this if we're finalizing, though,
653 // as otherwise we won't necessarilly have seen everything yet.
654 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
655 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
656 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4a200922013-09-20 23:08:21 +0000657 e = Symbols.end();
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000658 i != e; ++i) {
659 MCSymbol *Sym = i->getValue();
660 // Variable symbols may not be marked as defined, so check those
661 // explicitly. If we know it's a variable, we have a definition for
662 // the purposes of this check.
663 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
664 // FIXME: We would really like to refer back to where the symbol was
665 // first referenced for a source location. We need to add something
666 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4a200922013-09-20 23:08:21 +0000667 printMessage(
668 getLexer().getLoc(), SourceMgr::DK_Error,
669 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000670 }
671 }
672
Chris Lattner79180e22010-04-05 23:15:42 +0000673 // Finalize the output stream if there are no errors and if the client wants
674 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000675 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000676 Out.Finish();
677
Chris Lattnerb717fb02009-07-02 21:53:43 +0000678 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000679}
680
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000681void AsmParser::checkForValidSection() {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000682 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000683 TokError("expected section directive before assembly directive");
Eli Bendersky030f63a2013-01-14 19:04:57 +0000684 Out.InitToTextSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000685 }
686}
687
Jim Grosbach4a200922013-09-20 23:08:21 +0000688/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000689void AsmParser::eatToEndOfStatement() {
Jim Grosbach4a200922013-09-20 23:08:21 +0000690 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000691 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000692
Chris Lattner2cf5f142009-06-22 01:29:09 +0000693 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000694 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000695 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000696}
697
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000698StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000699 const char *Start = getTok().getLoc().getPointer();
700
Jim Grosbach4a200922013-09-20 23:08:21 +0000701 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000702 Lex();
703
704 const char *End = getTok().getLoc().getPointer();
705 return StringRef(Start, End - Start);
706}
Chris Lattnerc4193832009-06-22 05:51:26 +0000707
Jim Grosbach4a200922013-09-20 23:08:21 +0000708StringRef AsmParser::parseStringToComma() {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000709 const char *Start = getTok().getLoc().getPointer();
710
711 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4a200922013-09-20 23:08:21 +0000712 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000713 Lex();
714
715 const char *End = getTok().getLoc().getPointer();
716 return StringRef(Start, End - Start);
717}
718
Jim Grosbach4a200922013-09-20 23:08:21 +0000719/// \brief Parse a paren expression and return it.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000720/// NOTE: This assumes the leading '(' has already been consumed.
721///
722/// parenexpr ::= expr)
723///
Jim Grosbach4a200922013-09-20 23:08:21 +0000724bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
725 if (parseExpression(Res))
726 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000727 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000728 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000729 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000730 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000731 return false;
732}
Chris Lattnerc4193832009-06-22 05:51:26 +0000733
Jim Grosbach4a200922013-09-20 23:08:21 +0000734/// \brief Parse a bracket expression and return it.
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000735/// NOTE: This assumes the leading '[' has already been consumed.
736///
737/// bracketexpr ::= expr]
738///
Jim Grosbach4a200922013-09-20 23:08:21 +0000739bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
740 if (parseExpression(Res))
741 return true;
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000742 if (Lexer.isNot(AsmToken::RBrac))
743 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000744 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000745 Lex();
746 return false;
747}
748
Jim Grosbach4a200922013-09-20 23:08:21 +0000749/// \brief Parse a primary expression and return it.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000750/// primaryexpr ::= (parenexpr
751/// primaryexpr ::= symbol
752/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000753/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000754/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4a200922013-09-20 23:08:21 +0000755bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby5de048e2013-01-22 21:09:20 +0000756 SMLoc FirstTokenLoc = getLexer().getLoc();
757 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
758 switch (FirstTokenKind) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000759 default:
760 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000761 // If we have an error assume that we've already handled it.
762 case AsmToken::Error:
763 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000764 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000765 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000766 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000768 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000769 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000770 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000771 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000772 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000773 StringRef Identifier;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000774 if (parseIdentifier(Identifier)) {
Kevin Enderby5de048e2013-01-22 21:09:20 +0000775 if (FirstTokenKind == AsmToken::Dollar)
776 return Error(FirstTokenLoc, "invalid token in expression");
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000777 return true;
Kevin Enderby5de048e2013-01-22 21:09:20 +0000778 }
Daniel Dunbare17edff2010-08-24 19:13:42 +0000779
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000780 EndLoc = SMLoc::getFromPointer(Identifier.end());
781
Daniel Dunbarfffff912009-10-16 01:34:54 +0000782 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000783 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000784 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000785
786 // Lookup the symbol variant if used.
787 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000788 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000789 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000790 if (Variant == MCSymbolRefExpr::VK_Invalid) {
791 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000792 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000793 }
794 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000795
Daniel Dunbarfffff912009-10-16 01:34:54 +0000796 // If this is an absolute variable reference, substitute it now to preserve
797 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000798 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000799 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000800 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000801
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000802 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000803 return false;
804 }
805
806 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000807 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000808 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000809 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000810 case AsmToken::Integer: {
811 SMLoc Loc = getTok().getLoc();
812 int64_t IntVal = getTok().getIntVal();
813 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000814 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000815 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000816 // Look for 'b' or 'f' following an Integer as a directional label
817 if (Lexer.getKind() == AsmToken::Identifier) {
818 StringRef IDVal = getTok().getString();
Ulrich Weigand151ad372013-06-20 16:24:17 +0000819 // Lookup the symbol variant if used.
820 std::pair<StringRef, StringRef> Split = IDVal.split('@');
821 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
822 if (Split.first.size() != IDVal.size()) {
823 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
824 if (Variant == MCSymbolRefExpr::VK_Invalid) {
825 Variant = MCSymbolRefExpr::VK_None;
826 return TokError("invalid variant '" + Split.second + "'");
827 }
Vladimir Medic7b0a7962013-08-20 13:33:18 +0000828 IDVal = Split.first;
Ulrich Weigand151ad372013-06-20 16:24:17 +0000829 }
Jim Grosbach4a200922013-09-20 23:08:21 +0000830 if (IDVal == "f" || IDVal == "b") {
831 MCSymbol *Sym =
832 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigand151ad372013-06-20 16:24:17 +0000833 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000834 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000835 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000836 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000837 Lex(); // Eat identifier.
838 }
839 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000840 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000841 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000842 case AsmToken::Real: {
843 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000844 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000845 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000846 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000847 Lex(); // Eat token.
848 return false;
849 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000850 case AsmToken::Dot: {
851 // This is a '.' reference, which references the current PC. Emit a
852 // temporary label to the streamer and refer to it.
853 MCSymbol *Sym = Ctx.CreateTempSymbol();
854 Out.EmitLabel(Sym);
855 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000856 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000857 Lex(); // Eat identifier.
858 return false;
859 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000860 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000861 Lex(); // Eat the '('.
Jim Grosbach4a200922013-09-20 23:08:21 +0000862 return parseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000863 case AsmToken::LBrac:
864 if (!PlatformParser->HasBracketExpressions())
865 return TokError("brackets expression not supported on this target");
866 Lex(); // Eat the '['.
Jim Grosbach4a200922013-09-20 23:08:21 +0000867 return parseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000868 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000870 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000871 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000872 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000873 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000874 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000875 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000876 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000877 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000878 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000879 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000880 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000881 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000882 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000883 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000884 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000885 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000886 }
887}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000888
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000889bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000890 SMLoc EndLoc;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000891 return parseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000892}
893
Daniel Dunbarcceba832010-09-17 02:47:07 +0000894const MCExpr *
Jim Grosbach4a200922013-09-20 23:08:21 +0000895AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbarcceba832010-09-17 02:47:07 +0000896 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenberger66b71392013-08-27 20:23:19 +0000897 // Ask the target implementation about this expression first.
898 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
899 if (NewE)
900 return NewE;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000901 // Recurse over the given expression, rebuilding it to apply the given variant
902 // if there is exactly one symbol.
903 switch (E->getKind()) {
904 case MCExpr::Target:
905 case MCExpr::Constant:
906 return 0;
907
908 case MCExpr::SymbolRef: {
909 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
910
911 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4a200922013-09-20 23:08:21 +0000912 TokError("invalid variant on expression '" + getTok().getIdentifier() +
913 "' (already modified)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000914 return E;
915 }
916
917 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
918 }
919
920 case MCExpr::Unary: {
921 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4a200922013-09-20 23:08:21 +0000922 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000923 if (!Sub)
924 return 0;
925 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
926 }
927
928 case MCExpr::Binary: {
929 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4a200922013-09-20 23:08:21 +0000930 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
931 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000932
933 if (!LHS && !RHS)
934 return 0;
935
Jim Grosbach4a200922013-09-20 23:08:21 +0000936 if (!LHS)
937 LHS = BE->getLHS();
938 if (!RHS)
939 RHS = BE->getRHS();
Daniel Dunbarcceba832010-09-17 02:47:07 +0000940
941 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
942 }
943 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000944
Craig Topper85814382012-02-07 05:05:23 +0000945 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000946}
947
Jim Grosbach4a200922013-09-20 23:08:21 +0000948/// \brief Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000949///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000950/// expr ::= expr &&,|| expr -> lowest.
951/// expr ::= expr |,^,&,! expr
952/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
953/// expr ::= expr <<,>> expr
954/// expr ::= expr +,- expr
955/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000956/// expr ::= primaryexpr
957///
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000958bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000959 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000960 Res = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +0000961 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000962 return true;
963
Daniel Dunbarcceba832010-09-17 02:47:07 +0000964 // As a special case, we support 'a op b @ modifier' by rewriting the
965 // expression to include the modifier. This is inefficient, but in general we
966 // expect users to use 'a@modifier op b'.
967 if (Lexer.getKind() == AsmToken::At) {
968 Lex();
969
970 if (Lexer.isNot(AsmToken::Identifier))
971 return TokError("unexpected symbol modifier following '@'");
972
973 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4a200922013-09-20 23:08:21 +0000974 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbarcceba832010-09-17 02:47:07 +0000975 if (Variant == MCSymbolRefExpr::VK_Invalid)
976 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
977
Jim Grosbach4a200922013-09-20 23:08:21 +0000978 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000979 if (!ModifiedRes) {
980 return TokError("invalid modifier '" + getTok().getIdentifier() +
981 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000982 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000983
Daniel Dunbarcceba832010-09-17 02:47:07 +0000984 Res = ModifiedRes;
985 Lex();
986 }
987
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000988 // Try to constant fold it up front, if possible.
989 int64_t Value;
990 if (Res->EvaluateAsAbsolute(Value))
991 Res = MCConstantExpr::Create(Value, getContext());
992
993 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000994}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000995
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000996bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000997 Res = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +0000998 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000999}
1000
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001001bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001002 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001003
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001004 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001005 if (parseExpression(Expr))
Daniel Dunbar475839e2009-06-29 20:37:27 +00001006 return true;
1007
Daniel Dunbare00b0112009-10-16 01:57:52 +00001008 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001009 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001010
1011 return false;
1012}
1013
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001014static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001015 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001016 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001017 default:
Jim Grosbach4a200922013-09-20 23:08:21 +00001018 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001019
Jim Grosbach4a200922013-09-20 23:08:21 +00001020 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001021 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001022 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001023 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001024 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001025 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001026 return 1;
1027
Jim Grosbach4a200922013-09-20 23:08:21 +00001028 // Low Precedence: |, &, ^
1029 //
1030 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001031 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001032 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001033 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001034 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001035 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001036 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001037 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001038 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001039 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001040
Jim Grosbach4a200922013-09-20 23:08:21 +00001041 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001042 case AsmToken::EqualEqual:
1043 Kind = MCBinaryExpr::EQ;
1044 return 3;
1045 case AsmToken::ExclaimEqual:
1046 case AsmToken::LessGreater:
1047 Kind = MCBinaryExpr::NE;
1048 return 3;
1049 case AsmToken::Less:
1050 Kind = MCBinaryExpr::LT;
1051 return 3;
1052 case AsmToken::LessEqual:
1053 Kind = MCBinaryExpr::LTE;
1054 return 3;
1055 case AsmToken::Greater:
1056 Kind = MCBinaryExpr::GT;
1057 return 3;
1058 case AsmToken::GreaterEqual:
1059 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001060 return 3;
1061
Jim Grosbach4a200922013-09-20 23:08:21 +00001062 // Intermediate Precedence: <<, >>
Jim Grosbachfbe16812011-08-20 16:24:13 +00001063 case AsmToken::LessLess:
1064 Kind = MCBinaryExpr::Shl;
1065 return 4;
1066 case AsmToken::GreaterGreater:
1067 Kind = MCBinaryExpr::Shr;
1068 return 4;
1069
Jim Grosbach4a200922013-09-20 23:08:21 +00001070 // High Intermediate Precedence: +, -
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001071 case AsmToken::Plus:
1072 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001073 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001074 case AsmToken::Minus:
1075 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001076 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001077
Jim Grosbach4a200922013-09-20 23:08:21 +00001078 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001080 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001081 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001082 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001083 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001084 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001085 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001086 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001087 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001088 }
1089}
1090
Jim Grosbach4a200922013-09-20 23:08:21 +00001091/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001092/// Res contains the LHS of the expression on input.
Jim Grosbach4a200922013-09-20 23:08:21 +00001093bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattnerb4307b32010-01-15 19:28:38 +00001094 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001095 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001096 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001097 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001098
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001099 // If the next token is lower precedence than we are allowed to eat, return
1100 // successfully with what we ate already.
1101 if (TokPrec < Precedence)
1102 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001103
Sean Callanan79ed1a82010-01-19 20:22:31 +00001104 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001105
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001106 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001107 const MCExpr *RHS;
Jim Grosbach4a200922013-09-20 23:08:21 +00001108 if (parsePrimaryExpr(RHS, EndLoc))
1109 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001110
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001111 // If BinOp binds less tightly with RHS than the operator after RHS, let
1112 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001113 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001114 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4a200922013-09-20 23:08:21 +00001115 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1116 return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001117
Daniel Dunbar475839e2009-06-29 20:37:27 +00001118 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001119 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001120 }
1121}
1122
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001123/// ParseStatement:
1124/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001125/// ::= Label* Directive ...Operands... EndOfStatement
1126/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4a200922013-09-20 23:08:21 +00001127bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001128 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001129 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001130 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001131 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001132 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001133
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001134 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001135 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001136 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001137 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001138 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001139 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001140 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4a200922013-09-20 23:08:21 +00001141 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001142
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001143 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001144 if (Lexer.is(AsmToken::Integer)) {
1145 LocalLabelVal = getTok().getIntVal();
1146 if (LocalLabelVal < 0) {
1147 if (!TheCondState.Ignore)
1148 return TokError("unexpected token at start of statement");
1149 IDVal = "";
Eli Benderskyed5df012013-01-16 19:32:36 +00001150 } else {
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001151 IDVal = getTok().getString();
1152 Lex(); // Consume the integer token to be used as an identifier token.
1153 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001154 if (!TheCondState.Ignore)
1155 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001156 }
1157 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001158 } else if (Lexer.is(AsmToken::Dot)) {
1159 // Treat '.' as a valid identifier in this context.
1160 Lex();
1161 IDVal = ".";
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001162 } else if (parseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001163 if (!TheCondState.Ignore)
1164 return TokError("unexpected token at start of statement");
1165 IDVal = "";
1166 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001167
Chris Lattner7834fac2010-04-17 18:14:27 +00001168 // Handle conditional assembly here before checking for skipping. We
1169 // have to do this so that .endif isn't skipped in a ".if 0" block for
1170 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001171 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4a200922013-09-20 23:08:21 +00001172 DirectiveKindMap.find(IDVal);
1173 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1174 ? DK_NO_DIRECTIVE
1175 : DirKindIt->getValue();
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001176 switch (DirKind) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001177 default:
1178 break;
1179 case DK_IF:
1180 return parseDirectiveIf(IDLoc);
1181 case DK_IFB:
1182 return parseDirectiveIfb(IDLoc, true);
1183 case DK_IFNB:
1184 return parseDirectiveIfb(IDLoc, false);
1185 case DK_IFC:
1186 return parseDirectiveIfc(IDLoc, true);
1187 case DK_IFNC:
1188 return parseDirectiveIfc(IDLoc, false);
1189 case DK_IFDEF:
1190 return parseDirectiveIfdef(IDLoc, true);
1191 case DK_IFNDEF:
1192 case DK_IFNOTDEF:
1193 return parseDirectiveIfdef(IDLoc, false);
1194 case DK_ELSEIF:
1195 return parseDirectiveElseIf(IDLoc);
1196 case DK_ELSE:
1197 return parseDirectiveElse(IDLoc);
1198 case DK_ENDIF:
1199 return parseDirectiveEndIf(IDLoc);
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001200 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001201
Eli Benderskyed5df012013-01-16 19:32:36 +00001202 // Ignore the statement if in the middle of inactive conditional
1203 // (e.g. ".if 0").
Chad Rosier17feeec2012-10-20 00:47:08 +00001204 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001205 eatToEndOfStatement();
Chris Lattner7834fac2010-04-17 18:14:27 +00001206 return false;
1207 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001208
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001209 // FIXME: Recurse on local labels?
1210
1211 // See what kind of statement we have.
1212 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001213 case AsmToken::Colon: {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001214 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001215
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001216 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001217 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001218
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001219 // Diagnose attempt to use '.' as a label.
1220 if (IDVal == ".")
1221 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1222
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001223 // Diagnose attempt to use a variable as a label.
1224 //
1225 // FIXME: Diagnostics. Note the location of the definition as a label.
1226 // FIXME: This doesn't diagnose assignment to a symbol which has been
1227 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001228 MCSymbol *Sym;
1229 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001230 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001231 else
1232 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001233 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001234 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001235
Daniel Dunbar959fd882009-08-26 22:13:22 +00001236 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001237 if (!ParsingInlineAsm)
1238 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001239
Kevin Enderby94c2e852011-12-09 18:09:40 +00001240 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001241 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001242 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001243 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1244 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001245
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001246 // Consume any end of statement token, if present, to avoid spurious
1247 // AddBlankLine calls().
1248 if (Lexer.is(AsmToken::EndOfStatement)) {
1249 Lex();
1250 if (Lexer.is(AsmToken::Eof))
1251 return false;
1252 }
1253
Eli Friedman2128aae2012-10-22 23:58:19 +00001254 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001255 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001256
Daniel Dunbar3f872332009-07-28 16:08:33 +00001257 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001258 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001259 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001260
Jim Grosbach4a200922013-09-20 23:08:21 +00001261 return parseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001262
1263 default: // Normal instruction or directive.
1264 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001265 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001266
1267 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4a200922013-09-20 23:08:21 +00001268 if (areMacrosEnabled())
1269 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1270 return handleMacroEntry(M, IDLoc);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001271 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001272
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001273 // Otherwise, we have a normal instruction or directive.
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001274
Eli Bendersky6ee13082013-01-15 22:59:42 +00001275 // Directives start with "."
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001276 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky6ee13082013-01-15 22:59:42 +00001277 // There are several entities interested in parsing directives:
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001278 //
Eli Bendersky6ee13082013-01-15 22:59:42 +00001279 // 1. The target-specific assembly parser. Some directives are target
1280 // specific or may potentially behave differently on certain targets.
1281 // 2. Asm parser extensions. For example, platform-specific parsers
1282 // (like the ELF parser) register themselves as extensions.
1283 // 3. The generic directive parser implemented by this class. These are
1284 // all the directives that behave in a target and platform independent
1285 // manner, or at least have a default behavior that's shared between
1286 // all targets and platforms.
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001287
Eli Bendersky6ee13082013-01-15 22:59:42 +00001288 // First query the target-specific parser. It will return 'true' if it
1289 // isn't interested in this directive.
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001290 if (!getTargetParser().ParseDirective(ID))
1291 return false;
1292
Eli Bendersky6ee13082013-01-15 22:59:42 +00001293 // Next, check the extention directive map to see if any extension has
1294 // registered itself to parse this directive.
Jim Grosbach4a200922013-09-20 23:08:21 +00001295 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1296 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky6ee13082013-01-15 22:59:42 +00001297 if (Handler.first)
1298 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1299
1300 // Finally, if no one else is interested in this directive, it must be
1301 // generic and familiar to this class.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001302 switch (DirKind) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001303 default:
1304 break;
1305 case DK_SET:
1306 case DK_EQU:
1307 return parseDirectiveSet(IDVal, true);
1308 case DK_EQUIV:
1309 return parseDirectiveSet(IDVal, false);
1310 case DK_ASCII:
1311 return parseDirectiveAscii(IDVal, false);
1312 case DK_ASCIZ:
1313 case DK_STRING:
1314 return parseDirectiveAscii(IDVal, true);
1315 case DK_BYTE:
1316 return parseDirectiveValue(1);
1317 case DK_SHORT:
1318 case DK_VALUE:
1319 case DK_2BYTE:
1320 return parseDirectiveValue(2);
1321 case DK_LONG:
1322 case DK_INT:
1323 case DK_4BYTE:
1324 return parseDirectiveValue(4);
1325 case DK_QUAD:
1326 case DK_8BYTE:
1327 return parseDirectiveValue(8);
1328 case DK_SINGLE:
1329 case DK_FLOAT:
1330 return parseDirectiveRealValue(APFloat::IEEEsingle);
1331 case DK_DOUBLE:
1332 return parseDirectiveRealValue(APFloat::IEEEdouble);
1333 case DK_ALIGN: {
1334 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1335 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1336 }
1337 case DK_ALIGN32: {
1338 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1339 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1340 }
1341 case DK_BALIGN:
1342 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1343 case DK_BALIGNW:
1344 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1345 case DK_BALIGNL:
1346 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1347 case DK_P2ALIGN:
1348 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1349 case DK_P2ALIGNW:
1350 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1351 case DK_P2ALIGNL:
1352 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1353 case DK_ORG:
1354 return parseDirectiveOrg();
1355 case DK_FILL:
1356 return parseDirectiveFill();
1357 case DK_ZERO:
1358 return parseDirectiveZero();
1359 case DK_EXTERN:
1360 eatToEndOfStatement(); // .extern is the default, ignore it.
1361 return false;
1362 case DK_GLOBL:
1363 case DK_GLOBAL:
1364 return parseDirectiveSymbolAttribute(MCSA_Global);
1365 case DK_LAZY_REFERENCE:
1366 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1367 case DK_NO_DEAD_STRIP:
1368 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1369 case DK_SYMBOL_RESOLVER:
1370 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1371 case DK_PRIVATE_EXTERN:
1372 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1373 case DK_REFERENCE:
1374 return parseDirectiveSymbolAttribute(MCSA_Reference);
1375 case DK_WEAK_DEFINITION:
1376 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1377 case DK_WEAK_REFERENCE:
1378 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1379 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1380 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1381 case DK_COMM:
1382 case DK_COMMON:
1383 return parseDirectiveComm(/*IsLocal=*/false);
1384 case DK_LCOMM:
1385 return parseDirectiveComm(/*IsLocal=*/true);
1386 case DK_ABORT:
1387 return parseDirectiveAbort();
1388 case DK_INCLUDE:
1389 return parseDirectiveInclude();
1390 case DK_INCBIN:
1391 return parseDirectiveIncbin();
1392 case DK_CODE16:
1393 case DK_CODE16GCC:
1394 return TokError(Twine(IDVal) + " not supported yet");
1395 case DK_REPT:
1396 return parseDirectiveRept(IDLoc);
1397 case DK_IRP:
1398 return parseDirectiveIrp(IDLoc);
1399 case DK_IRPC:
1400 return parseDirectiveIrpc(IDLoc);
1401 case DK_ENDR:
1402 return parseDirectiveEndr(IDLoc);
1403 case DK_BUNDLE_ALIGN_MODE:
1404 return parseDirectiveBundleAlignMode();
1405 case DK_BUNDLE_LOCK:
1406 return parseDirectiveBundleLock();
1407 case DK_BUNDLE_UNLOCK:
1408 return parseDirectiveBundleUnlock();
1409 case DK_SLEB128:
1410 return parseDirectiveLEB128(true);
1411 case DK_ULEB128:
1412 return parseDirectiveLEB128(false);
1413 case DK_SPACE:
1414 case DK_SKIP:
1415 return parseDirectiveSpace(IDVal);
1416 case DK_FILE:
1417 return parseDirectiveFile(IDLoc);
1418 case DK_LINE:
1419 return parseDirectiveLine();
1420 case DK_LOC:
1421 return parseDirectiveLoc();
1422 case DK_STABS:
1423 return parseDirectiveStabs();
1424 case DK_CFI_SECTIONS:
1425 return parseDirectiveCFISections();
1426 case DK_CFI_STARTPROC:
1427 return parseDirectiveCFIStartProc();
1428 case DK_CFI_ENDPROC:
1429 return parseDirectiveCFIEndProc();
1430 case DK_CFI_DEF_CFA:
1431 return parseDirectiveCFIDefCfa(IDLoc);
1432 case DK_CFI_DEF_CFA_OFFSET:
1433 return parseDirectiveCFIDefCfaOffset();
1434 case DK_CFI_ADJUST_CFA_OFFSET:
1435 return parseDirectiveCFIAdjustCfaOffset();
1436 case DK_CFI_DEF_CFA_REGISTER:
1437 return parseDirectiveCFIDefCfaRegister(IDLoc);
1438 case DK_CFI_OFFSET:
1439 return parseDirectiveCFIOffset(IDLoc);
1440 case DK_CFI_REL_OFFSET:
1441 return parseDirectiveCFIRelOffset(IDLoc);
1442 case DK_CFI_PERSONALITY:
1443 return parseDirectiveCFIPersonalityOrLsda(true);
1444 case DK_CFI_LSDA:
1445 return parseDirectiveCFIPersonalityOrLsda(false);
1446 case DK_CFI_REMEMBER_STATE:
1447 return parseDirectiveCFIRememberState();
1448 case DK_CFI_RESTORE_STATE:
1449 return parseDirectiveCFIRestoreState();
1450 case DK_CFI_SAME_VALUE:
1451 return parseDirectiveCFISameValue(IDLoc);
1452 case DK_CFI_RESTORE:
1453 return parseDirectiveCFIRestore(IDLoc);
1454 case DK_CFI_ESCAPE:
1455 return parseDirectiveCFIEscape();
1456 case DK_CFI_SIGNAL_FRAME:
1457 return parseDirectiveCFISignalFrame();
1458 case DK_CFI_UNDEFINED:
1459 return parseDirectiveCFIUndefined(IDLoc);
1460 case DK_CFI_REGISTER:
1461 return parseDirectiveCFIRegister(IDLoc);
1462 case DK_MACROS_ON:
1463 case DK_MACROS_OFF:
1464 return parseDirectiveMacrosOnOff(IDVal);
1465 case DK_MACRO:
1466 return parseDirectiveMacro(IDLoc);
1467 case DK_ENDM:
1468 case DK_ENDMACRO:
1469 return parseDirectiveEndMacro(IDVal);
1470 case DK_PURGEM:
1471 return parseDirectivePurgeMacro(IDLoc);
Eli Friedman5d68ec22010-07-19 04:17:25 +00001472 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001473
Jim Grosbach686c0182012-05-01 18:38:27 +00001474 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001475 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001476
Chad Rosier469b1442013-02-12 21:33:51 +00001477 // __asm _emit or __asm __emit
1478 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1479 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4a200922013-09-20 23:08:21 +00001480 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosier469b1442013-02-12 21:33:51 +00001481
1482 // __asm align
1483 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4a200922013-09-20 23:08:21 +00001484 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman2128aae2012-10-22 23:58:19 +00001485
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001486 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001487
Chris Lattnera7f13542010-05-19 23:34:33 +00001488 // Canonicalize the opcode to lower case.
Eli Benderskyed5df012013-01-16 19:32:36 +00001489 std::string OpcodeStr = IDVal.lower();
Chad Rosier6a020a72012-10-25 20:41:34 +00001490 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4a200922013-09-20 23:08:21 +00001491 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001492 Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001493 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001494
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001495 // Dump the parsed representation, if requested.
1496 if (getShowParsedOperands()) {
1497 SmallString<256> Str;
1498 raw_svector_ostream OS(Str);
1499 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001500 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001501 if (i != 0)
1502 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001503 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001504 }
1505 OS << "]";
1506
Jim Grosbach4a200922013-09-20 23:08:21 +00001507 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001508 }
1509
Kevin Enderby613b7572011-11-01 22:27:22 +00001510 // If we are generating dwarf for assembly source files and the current
1511 // section is the initial text section then generate a .loc directive for
1512 // the instruction.
1513 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbournedf39be62013-04-17 21:18:16 +00001514 getContext().getGenDwarfSection() ==
Jim Grosbach4a200922013-09-20 23:08:21 +00001515 getStreamer().getCurrentSection().first) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001516
Eli Benderskyed5df012013-01-16 19:32:36 +00001517 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby938482f2012-11-01 17:31:35 +00001518
Eli Benderskyed5df012013-01-16 19:32:36 +00001519 // If we previously parsed a cpp hash file line comment then make sure the
1520 // current Dwarf File is for the CppHashFilename if not then emit the
1521 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001522 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4a200922013-09-20 23:08:21 +00001523 getContext().getMCDwarfFiles();
Eli Benderskyed5df012013-01-16 19:32:36 +00001524 if (CppHashFilename.size() != 0) {
1525 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby938482f2012-11-01 17:31:35 +00001526 CppHashFilename)
Eli Benderskyed5df012013-01-16 19:32:36 +00001527 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4a200922013-09-20 23:08:21 +00001528 getContext().nextGenDwarfFileNumber(), StringRef(),
1529 CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001530
Jim Grosbach4a200922013-09-20 23:08:21 +00001531 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1532 // cache with the different Loc from the call above we save the last
1533 // info we queried here with SrcMgr.FindLineNumber().
1534 unsigned CppHashLocLineNo;
1535 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1536 CppHashLocLineNo = LastQueryLine;
1537 else {
1538 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1539 LastQueryLine = CppHashLocLineNo;
1540 LastQueryIDLoc = CppHashLoc;
1541 LastQueryBuffer = CppHashBuf;
1542 }
1543 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Benderskyed5df012013-01-16 19:32:36 +00001544 }
Kevin Enderby938482f2012-11-01 17:31:35 +00001545
Jim Grosbach4a200922013-09-20 23:08:21 +00001546 getStreamer().EmitDwarfLocDirective(
1547 getContext().getGenDwarfFileNumber(), Line, 0,
1548 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1549 StringRef());
Kevin Enderby613b7572011-11-01 22:27:22 +00001550 }
1551
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001552 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001553 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001554 unsigned ErrorInfo;
Jim Grosbach4a200922013-09-20 23:08:21 +00001555 HadError = getTargetParser().MatchAndEmitInstruction(
1556 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1557 ParsingInlineAsm);
Chad Rosier84125ca2012-10-13 00:26:04 +00001558 }
Chris Lattner98986712010-01-14 22:21:20 +00001559
Chris Lattnercbf8a982010-09-11 16:18:25 +00001560 // Don't skip the rest of the line, the instruction parser is responsible for
1561 // that.
1562 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001563}
Chris Lattner9a023f72009-06-24 04:43:34 +00001564
Jim Grosbach4a200922013-09-20 23:08:21 +00001565/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001566/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4a200922013-09-20 23:08:21 +00001567void AsmParser::eatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001568 if (!Lexer.is(AsmToken::EndOfStatement))
1569 Lexer.LexUntilEndOfLine();
Jim Grosbach4a200922013-09-20 23:08:21 +00001570 // Eat EOL.
1571 Lex();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001572}
1573
Jim Grosbach4a200922013-09-20 23:08:21 +00001574/// parseCppHashLineFilenameComment as this:
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001575/// ::= # number "filename"
1576/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4a200922013-09-20 23:08:21 +00001577bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001578 Lex(); // Eat the hash token.
1579
1580 if (getLexer().isNot(AsmToken::Integer)) {
1581 // Consume the line since in cases it is not a well-formed line directive,
1582 // as if were simply a full line comment.
Jim Grosbach4a200922013-09-20 23:08:21 +00001583 eatToEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001584 return false;
1585 }
1586
1587 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001588 Lex();
1589
1590 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001591 eatToEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001592 return false;
1593 }
1594
1595 StringRef Filename = getTok().getString();
1596 // Get rid of the enclosing quotes.
Jim Grosbach4a200922013-09-20 23:08:21 +00001597 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001598
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001599 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1600 CppHashLoc = L;
1601 CppHashFilename = Filename;
1602 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001603 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001604
1605 // Ignore any trailing characters, they're just comment.
Jim Grosbach4a200922013-09-20 23:08:21 +00001606 eatToEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001607 return false;
1608}
1609
Jim Grosbach4a200922013-09-20 23:08:21 +00001610/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001611/// for the Filename and LineNo if any in the diagnostic.
1612void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001613 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001614 raw_ostream &OS = errs();
1615
1616 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1617 const SMLoc &DiagLoc = Diag.getLoc();
1618 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1619 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1620
Jim Grosbach4a200922013-09-20 23:08:21 +00001621 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001622 // before printing the message.
1623 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001624 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001625 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1626 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001627 }
1628
Eric Christopher2318ba12012-12-18 00:30:54 +00001629 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001630 // manager changed or buffer changed (like in a nested include) then just
1631 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4a200922013-09-20 23:08:21 +00001632 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001633 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001634 if (Parser->SavedDiagHandler)
1635 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1636 else
1637 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001638 return;
1639 }
1640
Eric Christopher2318ba12012-12-18 00:30:54 +00001641 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001642 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1643 // the diagnostic.
Jakub Staszakb06ea252013-09-16 22:03:38 +00001644 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001645
1646 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1647 int CppHashLocLineNo =
1648 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4a200922013-09-20 23:08:21 +00001649 int LineNo =
1650 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001651
Jim Grosbach4a200922013-09-20 23:08:21 +00001652 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1653 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001654 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001655
Benjamin Kramer04a04262011-10-16 10:48:29 +00001656 if (Parser->SavedDiagHandler)
1657 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1658 else
1659 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001660}
1661
Rafael Espindola799aacf2012-08-21 18:29:30 +00001662// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1663// difference being that that function accepts '@' as part of identifiers and
1664// we can't do that. AsmLexer.cpp should probably be changed to handle
1665// '@' as a special case when needed.
1666static bool isIdentifierChar(char c) {
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001667 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1668 c == '.';
Rafael Espindola799aacf2012-08-21 18:29:30 +00001669}
1670
Rafael Espindola761cb062012-06-03 23:57:14 +00001671bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001672 const MCAsmMacroParameters &Parameters,
Jim Grosbach4a200922013-09-20 23:08:21 +00001673 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001674 unsigned NParameters = Parameters.size();
1675 if (NParameters != 0 && NParameters != A.size())
1676 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001677
Preston Gurd7b6f2032012-09-19 20:36:12 +00001678 // A macro without parameters is handled differently on Darwin:
1679 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001680 while (!Body.empty()) {
1681 // Scan for the next substitution.
1682 std::size_t End = Body.size(), Pos = 0;
1683 for (; Pos != End; ++Pos) {
1684 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001685 if (!NParameters) {
1686 // This macro has no parameters, look for $0, $1, etc.
1687 if (Body[Pos] != '$' || Pos + 1 == End)
1688 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001689
Rafael Espindola65366442011-06-05 02:43:45 +00001690 char Next = Body[Pos + 1];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001691 if (Next == '$' || Next == 'n' ||
1692 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola65366442011-06-05 02:43:45 +00001693 break;
1694 } else {
1695 // This macro has parameters, look for \foo, \bar, etc.
1696 if (Body[Pos] == '\\' && Pos + 1 != End)
1697 break;
1698 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001699 }
1700
1701 // Add the prefix.
1702 OS << Body.slice(0, Pos);
1703
1704 // Check if we reached the end.
1705 if (Pos == End)
1706 break;
1707
Rafael Espindola65366442011-06-05 02:43:45 +00001708 if (!NParameters) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001709 switch (Body[Pos + 1]) {
1710 // $$ => $
Rafael Espindola65366442011-06-05 02:43:45 +00001711 case '$':
1712 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001713 break;
1714
Jim Grosbach4a200922013-09-20 23:08:21 +00001715 // $n => number of arguments
Rafael Espindola65366442011-06-05 02:43:45 +00001716 case 'n':
1717 OS << A.size();
1718 break;
1719
Jim Grosbach4a200922013-09-20 23:08:21 +00001720 // $[0-9] => argument
Rafael Espindola65366442011-06-05 02:43:45 +00001721 default: {
1722 // Missing arguments are ignored.
Jim Grosbach4a200922013-09-20 23:08:21 +00001723 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola65366442011-06-05 02:43:45 +00001724 if (Index >= A.size())
1725 break;
1726
1727 // Otherwise substitute with the token values, with spaces eliminated.
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001728 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4a200922013-09-20 23:08:21 +00001729 ie = A[Index].end();
1730 it != ie; ++it)
Rafael Espindola65366442011-06-05 02:43:45 +00001731 OS << it->getString();
1732 break;
1733 }
1734 }
1735 Pos += 2;
1736 } else {
1737 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001738 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001739 ++I;
1740
Jim Grosbach4a200922013-09-20 23:08:21 +00001741 const char *Begin = Body.data() + Pos + 1;
1742 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola65366442011-06-05 02:43:45 +00001743 unsigned Index = 0;
1744 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001745 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001746 break;
1747
Preston Gurd7b6f2032012-09-19 20:36:12 +00001748 if (Index == NParameters) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001749 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1750 Pos += 3;
1751 else {
1752 OS << '\\' << Argument;
1753 Pos = I;
1754 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001755 } else {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001756 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4a200922013-09-20 23:08:21 +00001757 ie = A[Index].end();
1758 it != ie; ++it)
Preston Gurd7b6f2032012-09-19 20:36:12 +00001759 if (it->getKind() == AsmToken::String)
1760 OS << it->getStringContents();
1761 else
1762 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001763
Preston Gurd7b6f2032012-09-19 20:36:12 +00001764 Pos += 1 + Argument.size();
1765 }
Rafael Espindola65366442011-06-05 02:43:45 +00001766 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001767 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001768 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001769 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001770
Rafael Espindola65366442011-06-05 02:43:45 +00001771 return false;
1772}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001773
Jim Grosbach4a200922013-09-20 23:08:21 +00001774MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1775 SMLoc EL, MemoryBuffer *I)
1776 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1777 ExitLoc(EL) {}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001778
Jim Grosbach4a200922013-09-20 23:08:21 +00001779static bool isOperator(AsmToken::TokenKind kind) {
1780 switch (kind) {
1781 default:
1782 return false;
1783 case AsmToken::Plus:
1784 case AsmToken::Minus:
1785 case AsmToken::Tilde:
1786 case AsmToken::Slash:
1787 case AsmToken::Star:
1788 case AsmToken::Dot:
1789 case AsmToken::Equal:
1790 case AsmToken::EqualEqual:
1791 case AsmToken::Pipe:
1792 case AsmToken::PipePipe:
1793 case AsmToken::Caret:
1794 case AsmToken::Amp:
1795 case AsmToken::AmpAmp:
1796 case AsmToken::Exclaim:
1797 case AsmToken::ExclaimEqual:
1798 case AsmToken::Percent:
1799 case AsmToken::Less:
1800 case AsmToken::LessEqual:
1801 case AsmToken::LessLess:
1802 case AsmToken::LessGreater:
1803 case AsmToken::Greater:
1804 case AsmToken::GreaterEqual:
1805 case AsmToken::GreaterGreater:
1806 return true;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001807 }
1808}
1809
Jim Grosbach4a200922013-09-20 23:08:21 +00001810bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +00001811 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001812 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001813 unsigned AddTokens = 0;
1814
1815 // gas accepts arguments separated by whitespace, except on Darwin
1816 if (!IsDarwin)
1817 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001818
1819 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001820 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1821 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001822 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001823 }
1824
1825 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1826 // Spaces and commas cannot be mixed to delimit parameters
1827 if (ArgumentDelimiter == AsmToken::Eof)
1828 ArgumentDelimiter = AsmToken::Comma;
1829 else if (ArgumentDelimiter != AsmToken::Comma) {
1830 Lexer.setSkipSpace(true);
1831 return TokError("expected ' ' for macro argument separator");
1832 }
1833 break;
1834 }
1835
1836 if (Lexer.is(AsmToken::Space)) {
1837 Lex(); // Eat spaces
1838
1839 // Spaces can delimit parameters, but could also be part an expression.
1840 // If the token after a space is an operator, add the token and the next
1841 // one into this argument
1842 if (ArgumentDelimiter == AsmToken::Space ||
1843 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001844 if (isOperator(Lexer.getKind())) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001845 // Check to see whether the token is used as an operator,
1846 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001847 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001848 if (*NextChar == ' ')
1849 AddTokens = 2;
1850 }
1851
1852 if (!AddTokens && ParenLevel == 0) {
1853 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4a200922013-09-20 23:08:21 +00001854 !isOperator(Lexer.getKind()))
Preston Gurd7b6f2032012-09-19 20:36:12 +00001855 ArgumentDelimiter = AsmToken::Space;
1856 break;
1857 }
1858 }
1859 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001860
Jim Grosbach4a200922013-09-20 23:08:21 +00001861 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001862 // to be able to fill in the remaining default parameter values
1863 if (Lexer.is(AsmToken::EndOfStatement))
1864 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001865
1866 // Adjust the current parentheses level.
1867 if (Lexer.is(AsmToken::LParen))
1868 ++ParenLevel;
1869 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1870 --ParenLevel;
1871
1872 // Append the token to the current argument list.
1873 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001874 if (AddTokens)
1875 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001876 Lex();
1877 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001878
1879 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001880 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001881 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001882 return false;
1883}
1884
1885// Parse the macro instantiation arguments.
Jim Grosbach4a200922013-09-20 23:08:21 +00001886bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001887 MCAsmMacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001888 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001889 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4a200922013-09-20 23:08:21 +00001890 // parseMacroArgument()
Preston Gurd7b6f2032012-09-19 20:36:12 +00001891 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001892
1893 // Parse two kinds of macro invocations:
1894 // - macros defined without any parameters accept an arbitrary number of them
1895 // - macros defined with parameters accept at most that many of them
1896 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1897 ++Parameter) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001898 MCAsmMacroArgument MA;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001899
Jim Grosbach4a200922013-09-20 23:08:21 +00001900 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001901 return true;
1902
Preston Gurd6c9176a2012-09-19 20:29:04 +00001903 if (!MA.empty() || !NParameters)
1904 A.push_back(MA);
1905 else if (NParameters) {
1906 if (!M->Parameters[Parameter].second.empty())
1907 A.push_back(M->Parameters[Parameter].second);
1908 }
Jim Grosbach97146442012-07-30 22:44:17 +00001909
Preston Gurd6c9176a2012-09-19 20:29:04 +00001910 // At the end of the statement, fill in remaining arguments that have
1911 // default values. If there aren't any, then the next argument is
1912 // required but missing
1913 if (Lexer.is(AsmToken::EndOfStatement)) {
1914 if (NParameters && Parameter < NParameters - 1) {
1915 if (M->Parameters[Parameter + 1].second.empty())
1916 return TokError("macro argument '" +
1917 Twine(M->Parameters[Parameter + 1].first) +
1918 "' is missing");
1919 else
1920 continue;
1921 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001922 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001923 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001924
1925 if (Lexer.is(AsmToken::Comma))
1926 Lex();
1927 }
1928 return TokError("Too many arguments");
1929}
1930
Jim Grosbach4a200922013-09-20 23:08:21 +00001931const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1932 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001933 return (I == MacroMap.end()) ? NULL : I->getValue();
1934}
1935
Jim Grosbach4a200922013-09-20 23:08:21 +00001936void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001937 MacroMap[Name] = new MCAsmMacro(Macro);
1938}
1939
Jim Grosbach4a200922013-09-20 23:08:21 +00001940void AsmParser::undefineMacro(StringRef Name) {
1941 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001942 if (I != MacroMap.end()) {
1943 delete I->getValue();
1944 MacroMap.erase(I);
1945 }
1946}
1947
Jim Grosbach4a200922013-09-20 23:08:21 +00001948bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001949 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1950 // this, although we should protect against infinite loops.
1951 if (ActiveMacros.size() == 20)
1952 return TokError("macros cannot be nested more than 20 levels deep");
1953
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001954 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00001955 if (parseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001956 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001957
Jim Grosbach97146442012-07-30 22:44:17 +00001958 // Remove any trailing empty arguments. Do this after-the-fact as we have
1959 // to keep empty arguments in the middle of the list or positionality
1960 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001961 while (!A.empty() && A.back().empty())
1962 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001963
Rafael Espindola65366442011-06-05 02:43:45 +00001964 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1965 // to hold the macro body with substitutions.
1966 SmallString<256> Buf;
1967 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001968 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001969
Rafael Espindola8a403d32012-08-08 14:51:03 +00001970 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001971 return true;
1972
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001973 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola761cb062012-06-03 23:57:14 +00001974 // instantiation.
1975 OS << ".endmacro\n";
1976
Rafael Espindola65366442011-06-05 02:43:45 +00001977 MemoryBuffer *Instantiation =
Jim Grosbach4a200922013-09-20 23:08:21 +00001978 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001979
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001980 // Create the macro instantiation object and add to the current macro
1981 // instantiation stack.
Jim Grosbach4a200922013-09-20 23:08:21 +00001982 MacroInstantiation *MI = new MacroInstantiation(
1983 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001984 ActiveMacros.push_back(MI);
1985
1986 // Jump to the macro instantiation and prime the lexer.
1987 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1988 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1989 Lex();
1990
1991 return false;
1992}
1993
Jim Grosbach4a200922013-09-20 23:08:21 +00001994void AsmParser::handleMacroExit() {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001995 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4a200922013-09-20 23:08:21 +00001996 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001997 Lex();
1998
1999 // Pop the instantiation entry.
2000 delete ActiveMacros.back();
2001 ActiveMacros.pop_back();
2002}
2003
Jim Grosbach4a200922013-09-20 23:08:21 +00002004static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002005 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00002006 case MCExpr::Binary: {
Jim Grosbach4a200922013-09-20 23:08:21 +00002007 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2008 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002009 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00002010 case MCExpr::Target:
2011 case MCExpr::Constant:
2012 return false;
2013 case MCExpr::SymbolRef: {
Jim Grosbach4a200922013-09-20 23:08:21 +00002014 const MCSymbol &S =
2015 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00002016 if (S.isVariable())
Jim Grosbach4a200922013-09-20 23:08:21 +00002017 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola8b01c822012-01-28 06:22:14 +00002018 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00002019 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002020 case MCExpr::Unary:
Jim Grosbach4a200922013-09-20 23:08:21 +00002021 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002022 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00002023
2024 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002025}
2026
Jim Grosbach4a200922013-09-20 23:08:21 +00002027bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002028 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00002029 // FIXME: Use better location, we should use proper tokens.
2030 SMLoc EqualLoc = Lexer.getLoc();
2031
Daniel Dunbar821e3332009-08-31 08:09:28 +00002032 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002033 if (parseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002034 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002035
Rafael Espindolae71cc862012-01-28 05:57:00 +00002036 // Note: we don't count b as used in "a = b". This is to allow
2037 // a = b
2038 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002039
Daniel Dunbar3f872332009-07-28 16:08:33 +00002040 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002041 return TokError("unexpected token in assignment");
2042
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00002043 // Error on assignment to '.'.
2044 if (Name == ".") {
2045 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2046 "(use '.space' or '.org').)"));
2047 }
2048
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002049 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00002050 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002051
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002052 // Validate that the LHS is allowed to be a variable (either it has not been
2053 // used as a symbol, or it is an absolute symbol).
2054 MCSymbol *Sym = getContext().LookupSymbol(Name);
2055 if (Sym) {
2056 // Diagnose assignment to a label.
2057 //
2058 // FIXME: Diagnostics. Note the location of the definition as a label.
2059 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4a200922013-09-20 23:08:21 +00002060 if (isUsedIn(Sym, Value))
Rafael Espindolae71cc862012-01-28 05:57:00 +00002061 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2062 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00002063 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00002064 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2065 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00002066 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002067 return Error(EqualLoc, "redefinition of '" + Name + "'");
2068 else if (!Sym->isVariable())
2069 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00002070 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002071 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4a200922013-09-20 23:08:21 +00002072 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002073
2074 // Don't count these checks as uses.
2075 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002076 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002077 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002078
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002079 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00002080
2081 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00002082 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002083 if (NoDeadStrip)
2084 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2085
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002086 return false;
2087}
2088
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002089/// parseIdentifier:
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002090/// ::= identifier
2091/// ::= string
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002092bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002093 // The assembler has relaxed rules for accepting identifiers, in particular we
2094 // allow things like '.globl $foo', which would normally be separate
2095 // tokens. At this level, we have already lexed so we cannot (currently)
2096 // handle this as a context dependent token, instead we detect adjacent tokens
2097 // and return the combined identifier.
2098 if (Lexer.is(AsmToken::Dollar)) {
2099 SMLoc DollarLoc = getLexer().getLoc();
2100
2101 // Consume the dollar sign, and check for a following identifier.
2102 Lex();
2103 if (Lexer.isNot(AsmToken::Identifier))
2104 return true;
2105
2106 // We have a '$' followed by an identifier, make sure they are adjacent.
2107 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2108 return true;
2109
2110 // Construct the joined identifier and consume the token.
Jim Grosbach4a200922013-09-20 23:08:21 +00002111 Res =
2112 StringRef(DollarLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002113 Lex();
2114 return false;
2115 }
2116
Jim Grosbach4a200922013-09-20 23:08:21 +00002117 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002118 return true;
2119
Sean Callanan18b83232010-01-19 21:44:56 +00002120 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002121
Sean Callanan79ed1a82010-01-19 20:22:31 +00002122 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002123
2124 return false;
2125}
2126
Jim Grosbach4a200922013-09-20 23:08:21 +00002127/// parseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002128/// ::= .equ identifier ',' expression
2129/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002130/// ::= .set identifier ',' expression
Jim Grosbach4a200922013-09-20 23:08:21 +00002131bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002132 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002133
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002134 if (parseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002135 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002136
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002137 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002138 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002139 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002140
Jim Grosbach4a200922013-09-20 23:08:21 +00002141 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002142}
2143
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002144bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002145 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002146
2147 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002148 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002149 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2150 if (Str[i] != '\\') {
2151 Data += Str[i];
2152 continue;
2153 }
2154
2155 // Recognize escaped characters. Note that this escape semantics currently
2156 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2157 ++i;
2158 if (i == e)
2159 return TokError("unexpected backslash at end of string");
2160
2161 // Recognize octal sequences.
Jim Grosbach4a200922013-09-20 23:08:21 +00002162 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002163 // Consume up to three octal characters.
2164 unsigned Value = Str[i] - '0';
2165
Jim Grosbach4a200922013-09-20 23:08:21 +00002166 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002167 ++i;
2168 Value = Value * 8 + (Str[i] - '0');
2169
Jim Grosbach4a200922013-09-20 23:08:21 +00002170 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002171 ++i;
2172 Value = Value * 8 + (Str[i] - '0');
2173 }
2174 }
2175
2176 if (Value > 255)
2177 return TokError("invalid octal escape sequence (out of range)");
2178
Jim Grosbach4a200922013-09-20 23:08:21 +00002179 Data += (unsigned char)Value;
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002180 continue;
2181 }
2182
2183 // Otherwise recognize individual escapes.
2184 switch (Str[i]) {
2185 default:
2186 // Just reject invalid escape sequences for now.
2187 return TokError("invalid escape sequence (unrecognized character)");
2188
2189 case 'b': Data += '\b'; break;
2190 case 'f': Data += '\f'; break;
2191 case 'n': Data += '\n'; break;
2192 case 'r': Data += '\r'; break;
2193 case 't': Data += '\t'; break;
2194 case '"': Data += '"'; break;
2195 case '\\': Data += '\\'; break;
2196 }
2197 }
2198
2199 return false;
2200}
2201
Jim Grosbach4a200922013-09-20 23:08:21 +00002202/// parseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002203/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002204bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002205 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002206 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002207
Daniel Dunbara0d14262009-06-24 23:30:00 +00002208 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002209 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002210 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002211
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002212 std::string Data;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002213 if (parseEscapedString(Data))
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002214 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002215
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002216 getStreamer().EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002217 if (ZeroTerminated)
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002218 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002219
Sean Callanan79ed1a82010-01-19 20:22:31 +00002220 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002221
2222 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002223 break;
2224
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002225 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002226 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002227 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002228 }
2229 }
2230
Sean Callanan79ed1a82010-01-19 20:22:31 +00002231 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002232 return false;
2233}
2234
Jim Grosbach4a200922013-09-20 23:08:21 +00002235/// parseDirectiveValue
Daniel Dunbara0d14262009-06-24 23:30:00 +00002236/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002237bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002238 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002239 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002240
Daniel Dunbara0d14262009-06-24 23:30:00 +00002241 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002242 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002243 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002244 if (parseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002245 return true;
2246
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002247 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002248 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2249 assert(Size <= 8 && "Invalid size");
2250 uint64_t IntValue = MCE->getValue();
2251 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2252 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002253 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach254cf032011-06-29 16:05:14 +00002254 } else
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002255 getStreamer().EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002256
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002257 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002258 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002259
Daniel Dunbara0d14262009-06-24 23:30:00 +00002260 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002261 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002262 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002263 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002264 }
2265 }
2266
Sean Callanan79ed1a82010-01-19 20:22:31 +00002267 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002268 return false;
2269}
2270
Jim Grosbach4a200922013-09-20 23:08:21 +00002271/// parseDirectiveRealValue
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002272/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002273bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002274 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002275 checkForValidSection();
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002276
2277 for (;;) {
2278 // We don't truly support arithmetic on floating point expressions, so we
2279 // have to manually parse unary prefixes.
2280 bool IsNeg = false;
2281 if (getLexer().is(AsmToken::Minus)) {
2282 Lex();
2283 IsNeg = true;
2284 } else if (getLexer().is(AsmToken::Plus))
2285 Lex();
2286
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002287 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002288 getLexer().isNot(AsmToken::Real) &&
2289 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002290 return TokError("unexpected token in directive");
2291
2292 // Convert to an APFloat.
2293 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002294 StringRef IDVal = getTok().getString();
2295 if (getLexer().is(AsmToken::Identifier)) {
2296 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2297 Value = APFloat::getInf(Semantics);
2298 else if (!IDVal.compare_lower("nan"))
2299 Value = APFloat::getNaN(Semantics, false, ~0);
2300 else
2301 return TokError("invalid floating point literal");
2302 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4a200922013-09-20 23:08:21 +00002303 APFloat::opInvalidOp)
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002304 return TokError("invalid floating point literal");
2305 if (IsNeg)
2306 Value.changeSign();
2307
2308 // Consume the numeric token.
2309 Lex();
2310
2311 // Emit the value as an integer.
2312 APInt AsInt = Value.bitcastToAPInt();
2313 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002314 AsInt.getBitWidth() / 8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002315
2316 if (getLexer().is(AsmToken::EndOfStatement))
2317 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002318
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002319 if (getLexer().isNot(AsmToken::Comma))
2320 return TokError("unexpected token in directive");
2321 Lex();
2322 }
2323 }
2324
2325 Lex();
2326 return false;
2327}
2328
Jim Grosbach4a200922013-09-20 23:08:21 +00002329/// parseDirectiveZero
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002330/// ::= .zero expression
Jim Grosbach4a200922013-09-20 23:08:21 +00002331bool AsmParser::parseDirectiveZero() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002332 checkForValidSection();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002333
2334 int64_t NumBytes;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002335 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002336 return true;
2337
Rafael Espindolae452b172010-10-05 19:42:57 +00002338 int64_t Val = 0;
2339 if (getLexer().is(AsmToken::Comma)) {
2340 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002341 if (parseAbsoluteExpression(Val))
Rafael Espindolae452b172010-10-05 19:42:57 +00002342 return true;
2343 }
2344
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002345 if (getLexer().isNot(AsmToken::EndOfStatement))
2346 return TokError("unexpected token in '.zero' directive");
2347
2348 Lex();
2349
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002350 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002351
2352 return false;
2353}
2354
Jim Grosbach4a200922013-09-20 23:08:21 +00002355/// parseDirectiveFill
Roman Divacky9c607102013-09-24 17:44:41 +00002356/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002357bool AsmParser::parseDirectiveFill() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002358 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002359
Daniel Dunbara0d14262009-06-24 23:30:00 +00002360 int64_t NumValues;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002361 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002362 return true;
2363
Roman Divacky9c607102013-09-24 17:44:41 +00002364 int64_t FillSize = 1;
2365 int64_t FillExpr = 0;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002366
Roman Divacky9c607102013-09-24 17:44:41 +00002367 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2368 if (getLexer().isNot(AsmToken::Comma))
2369 return TokError("unexpected token in '.fill' directive");
2370 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002371
Roman Divacky9c607102013-09-24 17:44:41 +00002372 if (parseAbsoluteExpression(FillSize))
2373 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002374
Roman Divacky9c607102013-09-24 17:44:41 +00002375 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2376 if (getLexer().isNot(AsmToken::Comma))
2377 return TokError("unexpected token in '.fill' directive");
2378 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002379
Roman Divacky9c607102013-09-24 17:44:41 +00002380 if (parseAbsoluteExpression(FillExpr))
2381 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002382
Roman Divacky9c607102013-09-24 17:44:41 +00002383 if (getLexer().isNot(AsmToken::EndOfStatement))
2384 return TokError("unexpected token in '.fill' directive");
2385
2386 Lex();
2387 }
2388 }
Daniel Dunbara0d14262009-06-24 23:30:00 +00002389
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002390 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2391 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002392
2393 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002394 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002395
2396 return false;
2397}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002398
Jim Grosbach4a200922013-09-20 23:08:21 +00002399/// parseDirectiveOrg
Daniel Dunbarc238b582009-06-25 22:44:51 +00002400/// ::= .org expression [ , expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002401bool AsmParser::parseDirectiveOrg() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002402 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002403
Daniel Dunbar821e3332009-08-31 08:09:28 +00002404 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002405 SMLoc Loc = getTok().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002406 if (parseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002407 return true;
2408
2409 // Parse optional fill expression.
2410 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002411 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2412 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002413 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002414 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002415
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002416 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002417 return true;
2418
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002419 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002420 return TokError("unexpected token in '.org' directive");
2421 }
2422
Sean Callanan79ed1a82010-01-19 20:22:31 +00002423 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002424
Jim Grosbachebd4c052012-01-27 00:37:08 +00002425 // Only limited forms of relocatable expressions are accepted here, it
2426 // has to be relative to the current section. The streamer will return
2427 // 'true' if the expression wasn't evaluatable.
2428 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2429 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002430
2431 return false;
2432}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002433
Jim Grosbach4a200922013-09-20 23:08:21 +00002434/// parseDirectiveAlign
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002435/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4a200922013-09-20 23:08:21 +00002436bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002437 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002438
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002439 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002440 int64_t Alignment;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002441 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002442 return true;
2443
2444 SMLoc MaxBytesLoc;
2445 bool HasFillExpr = false;
2446 int64_t FillExpr = 0;
2447 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002448 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2449 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002450 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002451 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002452
2453 // The fill expression can be omitted while specifying a maximum number of
2454 // alignment bytes, e.g:
2455 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002456 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002457 HasFillExpr = true;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002458 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002459 return true;
2460 }
2461
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002462 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2463 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002464 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002465 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002466
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002467 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002468 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002469 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002470
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002471 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002472 return TokError("unexpected token in directive");
2473 }
2474 }
2475
Sean Callanan79ed1a82010-01-19 20:22:31 +00002476 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002477
Daniel Dunbar648ac512010-05-17 21:54:30 +00002478 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002479 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002480
2481 // Compute alignment in bytes.
2482 if (IsPow2) {
2483 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002484 if (Alignment >= 32) {
2485 Error(AlignmentLoc, "invalid alignment value");
2486 Alignment = 31;
2487 }
2488
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002489 Alignment = 1ULL << Alignment;
Benjamin Kramer8a89cf22013-02-16 15:00:16 +00002490 } else {
2491 // Reject alignments that aren't a power of two, for gas compatibility.
2492 if (!isPowerOf2_64(Alignment))
2493 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002494 }
2495
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002496 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002497 if (MaxBytesLoc.isValid()) {
2498 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002499 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4a200922013-09-20 23:08:21 +00002500 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002501 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002502 }
2503
2504 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002505 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4a200922013-09-20 23:08:21 +00002506 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002507 MaxBytesToFill = 0;
2508 }
2509 }
2510
Daniel Dunbar648ac512010-05-17 21:54:30 +00002511 // Check whether we should use optimal code alignment for this .align
2512 // directive.
Peter Collingbournedf39be62013-04-17 21:18:16 +00002513 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002514 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2515 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002516 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002517 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002518 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002519 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2520 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002521 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002522
2523 return false;
2524}
2525
Jim Grosbach4a200922013-09-20 23:08:21 +00002526/// parseDirectiveFile
Eli Bendersky6ee13082013-01-15 22:59:42 +00002527/// ::= .file [number] filename
2528/// ::= .file number directory filename
Jim Grosbach4a200922013-09-20 23:08:21 +00002529bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002530 // FIXME: I'm not sure what this is.
2531 int64_t FileNumber = -1;
2532 SMLoc FileNumberLoc = getLexer().getLoc();
2533 if (getLexer().is(AsmToken::Integer)) {
2534 FileNumber = getTok().getIntVal();
2535 Lex();
2536
2537 if (FileNumber < 1)
2538 return TokError("file number less than one");
2539 }
2540
2541 if (getLexer().isNot(AsmToken::String))
2542 return TokError("unexpected token in '.file' directive");
2543
2544 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gaoed119822013-09-05 19:14:26 +00002545 // Allow the strings to have escaped octal character sequence.
2546 std::string Path = getTok().getString();
2547 if (parseEscapedString(Path))
2548 return true;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002549 Lex();
2550
2551 StringRef Directory;
2552 StringRef Filename;
Yunzhong Gaoed119822013-09-05 19:14:26 +00002553 std::string FilenameData;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002554 if (getLexer().is(AsmToken::String)) {
2555 if (FileNumber == -1)
2556 return TokError("explicit path specified, but no file number");
Yunzhong Gaoed119822013-09-05 19:14:26 +00002557 if (parseEscapedString(FilenameData))
2558 return true;
2559 Filename = FilenameData;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002560 Directory = Path;
2561 Lex();
2562 } else {
2563 Filename = Path;
2564 }
2565
2566 if (getLexer().isNot(AsmToken::EndOfStatement))
2567 return TokError("unexpected token in '.file' directive");
2568
2569 if (FileNumber == -1)
2570 getStreamer().EmitFileDirective(Filename);
2571 else {
2572 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4a200922013-09-20 23:08:21 +00002573 Error(DirectiveLoc,
2574 "input can't have .file dwarf directives when -g is "
2575 "used to generate dwarf debug info for assembly code");
Eli Bendersky6ee13082013-01-15 22:59:42 +00002576
2577 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2578 Error(FileNumberLoc, "file number already allocated");
2579 }
2580
2581 return false;
2582}
2583
Jim Grosbach4a200922013-09-20 23:08:21 +00002584/// parseDirectiveLine
Eli Bendersky6ee13082013-01-15 22:59:42 +00002585/// ::= .line [number]
Jim Grosbach4a200922013-09-20 23:08:21 +00002586bool AsmParser::parseDirectiveLine() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002587 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2588 if (getLexer().isNot(AsmToken::Integer))
2589 return TokError("unexpected token in '.line' directive");
2590
2591 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4a200922013-09-20 23:08:21 +00002592 (void)LineNumber;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002593 Lex();
2594
2595 // FIXME: Do something with the .line.
2596 }
2597
2598 if (getLexer().isNot(AsmToken::EndOfStatement))
2599 return TokError("unexpected token in '.line' directive");
2600
2601 return false;
2602}
2603
Jim Grosbach4a200922013-09-20 23:08:21 +00002604/// parseDirectiveLoc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002605/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2606/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2607/// The first number is a file number, must have been previously assigned with
2608/// a .file directive, the second number is the line number and optionally the
2609/// third number is a column position (zero if not specified). The remaining
2610/// optional items are .loc sub-directives.
Jim Grosbach4a200922013-09-20 23:08:21 +00002611bool AsmParser::parseDirectiveLoc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002612 if (getLexer().isNot(AsmToken::Integer))
2613 return TokError("unexpected token in '.loc' directive");
2614 int64_t FileNumber = getTok().getIntVal();
2615 if (FileNumber < 1)
2616 return TokError("file number less than one in '.loc' directive");
2617 if (!getContext().isValidDwarfFileNumber(FileNumber))
2618 return TokError("unassigned file number in '.loc' directive");
2619 Lex();
2620
2621 int64_t LineNumber = 0;
2622 if (getLexer().is(AsmToken::Integer)) {
2623 LineNumber = getTok().getIntVal();
2624 if (LineNumber < 1)
2625 return TokError("line number less than one in '.loc' directive");
2626 Lex();
2627 }
2628
2629 int64_t ColumnPos = 0;
2630 if (getLexer().is(AsmToken::Integer)) {
2631 ColumnPos = getTok().getIntVal();
2632 if (ColumnPos < 0)
2633 return TokError("column position less than zero in '.loc' directive");
2634 Lex();
2635 }
2636
2637 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2638 unsigned Isa = 0;
2639 int64_t Discriminator = 0;
2640 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2641 for (;;) {
2642 if (getLexer().is(AsmToken::EndOfStatement))
2643 break;
2644
2645 StringRef Name;
2646 SMLoc Loc = getTok().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002647 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002648 return TokError("unexpected token in '.loc' directive");
2649
2650 if (Name == "basic_block")
2651 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2652 else if (Name == "prologue_end")
2653 Flags |= DWARF2_FLAG_PROLOGUE_END;
2654 else if (Name == "epilogue_begin")
2655 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2656 else if (Name == "is_stmt") {
2657 Loc = getTok().getLoc();
2658 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002659 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002660 return true;
2661 // The expression must be the constant 0 or 1.
2662 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2663 int Value = MCE->getValue();
2664 if (Value == 0)
2665 Flags &= ~DWARF2_FLAG_IS_STMT;
2666 else if (Value == 1)
2667 Flags |= DWARF2_FLAG_IS_STMT;
2668 else
2669 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperefa703d2013-04-22 04:22:40 +00002670 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002671 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2672 }
Craig Topperefa703d2013-04-22 04:22:40 +00002673 } else if (Name == "isa") {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002674 Loc = getTok().getLoc();
2675 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002676 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002677 return true;
2678 // The expression must be a constant greater or equal to 0.
2679 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2680 int Value = MCE->getValue();
2681 if (Value < 0)
2682 return Error(Loc, "isa number less than zero");
2683 Isa = Value;
Craig Topperefa703d2013-04-22 04:22:40 +00002684 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002685 return Error(Loc, "isa number not a constant value");
2686 }
Craig Topperefa703d2013-04-22 04:22:40 +00002687 } else if (Name == "discriminator") {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002688 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002689 return true;
Craig Topperefa703d2013-04-22 04:22:40 +00002690 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002691 return Error(Loc, "unknown sub-directive in '.loc' directive");
2692 }
2693
2694 if (getLexer().is(AsmToken::EndOfStatement))
2695 break;
2696 }
2697 }
2698
2699 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2700 Isa, Discriminator, StringRef());
2701
2702 return false;
2703}
2704
Jim Grosbach4a200922013-09-20 23:08:21 +00002705/// parseDirectiveStabs
Eli Bendersky6ee13082013-01-15 22:59:42 +00002706/// ::= .stabs string, number, number, number
Jim Grosbach4a200922013-09-20 23:08:21 +00002707bool AsmParser::parseDirectiveStabs() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002708 return TokError("unsupported directive '.stabs'");
2709}
2710
Jim Grosbach4a200922013-09-20 23:08:21 +00002711/// parseDirectiveCFISections
Eli Bendersky6ee13082013-01-15 22:59:42 +00002712/// ::= .cfi_sections section [, section]
Jim Grosbach4a200922013-09-20 23:08:21 +00002713bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002714 StringRef Name;
2715 bool EH = false;
2716 bool Debug = false;
2717
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002718 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002719 return TokError("Expected an identifier");
2720
2721 if (Name == ".eh_frame")
2722 EH = true;
2723 else if (Name == ".debug_frame")
2724 Debug = true;
2725
2726 if (getLexer().is(AsmToken::Comma)) {
2727 Lex();
2728
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002729 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002730 return TokError("Expected an identifier");
2731
2732 if (Name == ".eh_frame")
2733 EH = true;
2734 else if (Name == ".debug_frame")
2735 Debug = true;
2736 }
2737
2738 getStreamer().EmitCFISections(EH, Debug);
2739 return false;
2740}
2741
Jim Grosbach4a200922013-09-20 23:08:21 +00002742/// parseDirectiveCFIStartProc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002743/// ::= .cfi_startproc
Jim Grosbach4a200922013-09-20 23:08:21 +00002744bool AsmParser::parseDirectiveCFIStartProc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002745 getStreamer().EmitCFIStartProc();
2746 return false;
2747}
2748
Jim Grosbach4a200922013-09-20 23:08:21 +00002749/// parseDirectiveCFIEndProc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002750/// ::= .cfi_endproc
Jim Grosbach4a200922013-09-20 23:08:21 +00002751bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002752 getStreamer().EmitCFIEndProc();
2753 return false;
2754}
2755
Jim Grosbach4a200922013-09-20 23:08:21 +00002756/// \brief parse register name or number.
2757bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky6ee13082013-01-15 22:59:42 +00002758 SMLoc DirectiveLoc) {
2759 unsigned RegNo;
2760
2761 if (getLexer().isNot(AsmToken::Integer)) {
2762 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2763 return true;
Bill Wendling99cb6222013-06-18 07:20:20 +00002764 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky6ee13082013-01-15 22:59:42 +00002765 } else
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002766 return parseAbsoluteExpression(Register);
Eli Bendersky6ee13082013-01-15 22:59:42 +00002767
2768 return false;
2769}
2770
Jim Grosbach4a200922013-09-20 23:08:21 +00002771/// parseDirectiveCFIDefCfa
Eli Bendersky6ee13082013-01-15 22:59:42 +00002772/// ::= .cfi_def_cfa register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002773bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002774 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002775 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002776 return true;
2777
2778 if (getLexer().isNot(AsmToken::Comma))
2779 return TokError("unexpected token in directive");
2780 Lex();
2781
2782 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002783 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002784 return true;
2785
2786 getStreamer().EmitCFIDefCfa(Register, Offset);
2787 return false;
2788}
2789
Jim Grosbach4a200922013-09-20 23:08:21 +00002790/// parseDirectiveCFIDefCfaOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002791/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002792bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002793 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002794 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002795 return true;
2796
2797 getStreamer().EmitCFIDefCfaOffset(Offset);
2798 return false;
2799}
2800
Jim Grosbach4a200922013-09-20 23:08:21 +00002801/// parseDirectiveCFIRegister
Eli Bendersky6ee13082013-01-15 22:59:42 +00002802/// ::= .cfi_register register, register
Jim Grosbach4a200922013-09-20 23:08:21 +00002803bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002804 int64_t Register1 = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002805 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002806 return true;
2807
2808 if (getLexer().isNot(AsmToken::Comma))
2809 return TokError("unexpected token in directive");
2810 Lex();
2811
2812 int64_t Register2 = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002813 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002814 return true;
2815
2816 getStreamer().EmitCFIRegister(Register1, Register2);
2817 return false;
2818}
2819
Jim Grosbach4a200922013-09-20 23:08:21 +00002820/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002821/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4a200922013-09-20 23:08:21 +00002822bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002823 int64_t Adjustment = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002824 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002825 return true;
2826
2827 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2828 return false;
2829}
2830
Jim Grosbach4a200922013-09-20 23:08:21 +00002831/// parseDirectiveCFIDefCfaRegister
Eli Bendersky6ee13082013-01-15 22:59:42 +00002832/// ::= .cfi_def_cfa_register register
Jim Grosbach4a200922013-09-20 23:08:21 +00002833bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002834 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002835 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002836 return true;
2837
2838 getStreamer().EmitCFIDefCfaRegister(Register);
2839 return false;
2840}
2841
Jim Grosbach4a200922013-09-20 23:08:21 +00002842/// parseDirectiveCFIOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002843/// ::= .cfi_offset register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002844bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002845 int64_t Register = 0;
2846 int64_t Offset = 0;
2847
Jim Grosbach4a200922013-09-20 23:08:21 +00002848 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002849 return true;
2850
2851 if (getLexer().isNot(AsmToken::Comma))
2852 return TokError("unexpected token in directive");
2853 Lex();
2854
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002855 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002856 return true;
2857
2858 getStreamer().EmitCFIOffset(Register, Offset);
2859 return false;
2860}
2861
Jim Grosbach4a200922013-09-20 23:08:21 +00002862/// parseDirectiveCFIRelOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002863/// ::= .cfi_rel_offset register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002864bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002865 int64_t Register = 0;
2866
Jim Grosbach4a200922013-09-20 23:08:21 +00002867 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002868 return true;
2869
2870 if (getLexer().isNot(AsmToken::Comma))
2871 return TokError("unexpected token in directive");
2872 Lex();
2873
2874 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002875 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002876 return true;
2877
2878 getStreamer().EmitCFIRelOffset(Register, Offset);
2879 return false;
2880}
2881
2882static bool isValidEncoding(int64_t Encoding) {
2883 if (Encoding & ~0xff)
2884 return false;
2885
2886 if (Encoding == dwarf::DW_EH_PE_omit)
2887 return true;
2888
2889 const unsigned Format = Encoding & 0xf;
2890 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2891 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2892 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2893 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2894 return false;
2895
2896 const unsigned Application = Encoding & 0x70;
2897 if (Application != dwarf::DW_EH_PE_absptr &&
2898 Application != dwarf::DW_EH_PE_pcrel)
2899 return false;
2900
2901 return true;
2902}
2903
Jim Grosbach4a200922013-09-20 23:08:21 +00002904/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky6ee13082013-01-15 22:59:42 +00002905/// IsPersonality true for cfi_personality, false for cfi_lsda
2906/// ::= .cfi_personality encoding, [symbol_name]
2907/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4a200922013-09-20 23:08:21 +00002908bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002909 int64_t Encoding = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002910 if (parseAbsoluteExpression(Encoding))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002911 return true;
2912 if (Encoding == dwarf::DW_EH_PE_omit)
2913 return false;
2914
2915 if (!isValidEncoding(Encoding))
2916 return TokError("unsupported encoding.");
2917
2918 if (getLexer().isNot(AsmToken::Comma))
2919 return TokError("unexpected token in directive");
2920 Lex();
2921
2922 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002923 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002924 return TokError("expected identifier in directive");
2925
2926 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2927
2928 if (IsPersonality)
2929 getStreamer().EmitCFIPersonality(Sym, Encoding);
2930 else
2931 getStreamer().EmitCFILsda(Sym, Encoding);
2932 return false;
2933}
2934
Jim Grosbach4a200922013-09-20 23:08:21 +00002935/// parseDirectiveCFIRememberState
Eli Bendersky6ee13082013-01-15 22:59:42 +00002936/// ::= .cfi_remember_state
Jim Grosbach4a200922013-09-20 23:08:21 +00002937bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002938 getStreamer().EmitCFIRememberState();
2939 return false;
2940}
2941
Jim Grosbach4a200922013-09-20 23:08:21 +00002942/// parseDirectiveCFIRestoreState
Eli Bendersky6ee13082013-01-15 22:59:42 +00002943/// ::= .cfi_remember_state
Jim Grosbach4a200922013-09-20 23:08:21 +00002944bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002945 getStreamer().EmitCFIRestoreState();
2946 return false;
2947}
2948
Jim Grosbach4a200922013-09-20 23:08:21 +00002949/// parseDirectiveCFISameValue
Eli Bendersky6ee13082013-01-15 22:59:42 +00002950/// ::= .cfi_same_value register
Jim Grosbach4a200922013-09-20 23:08:21 +00002951bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002952 int64_t Register = 0;
2953
Jim Grosbach4a200922013-09-20 23:08:21 +00002954 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002955 return true;
2956
2957 getStreamer().EmitCFISameValue(Register);
2958 return false;
2959}
2960
Jim Grosbach4a200922013-09-20 23:08:21 +00002961/// parseDirectiveCFIRestore
Eli Bendersky6ee13082013-01-15 22:59:42 +00002962/// ::= .cfi_restore register
Jim Grosbach4a200922013-09-20 23:08:21 +00002963bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002964 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002965 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002966 return true;
2967
2968 getStreamer().EmitCFIRestore(Register);
2969 return false;
2970}
2971
Jim Grosbach4a200922013-09-20 23:08:21 +00002972/// parseDirectiveCFIEscape
Eli Bendersky6ee13082013-01-15 22:59:42 +00002973/// ::= .cfi_escape expression[,...]
Jim Grosbach4a200922013-09-20 23:08:21 +00002974bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002975 std::string Values;
2976 int64_t CurrValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002977 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002978 return true;
2979
2980 Values.push_back((uint8_t)CurrValue);
2981
2982 while (getLexer().is(AsmToken::Comma)) {
2983 Lex();
2984
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002985 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002986 return true;
2987
2988 Values.push_back((uint8_t)CurrValue);
2989 }
2990
2991 getStreamer().EmitCFIEscape(Values);
2992 return false;
2993}
2994
Jim Grosbach4a200922013-09-20 23:08:21 +00002995/// parseDirectiveCFISignalFrame
Eli Bendersky6ee13082013-01-15 22:59:42 +00002996/// ::= .cfi_signal_frame
Jim Grosbach4a200922013-09-20 23:08:21 +00002997bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002998 if (getLexer().isNot(AsmToken::EndOfStatement))
2999 return Error(getLexer().getLoc(),
3000 "unexpected token in '.cfi_signal_frame'");
3001
3002 getStreamer().EmitCFISignalFrame();
3003 return false;
3004}
3005
Jim Grosbach4a200922013-09-20 23:08:21 +00003006/// parseDirectiveCFIUndefined
Eli Bendersky6ee13082013-01-15 22:59:42 +00003007/// ::= .cfi_undefined register
Jim Grosbach4a200922013-09-20 23:08:21 +00003008bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003009 int64_t Register = 0;
3010
Jim Grosbach4a200922013-09-20 23:08:21 +00003011 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003012 return true;
3013
3014 getStreamer().EmitCFIUndefined(Register);
3015 return false;
3016}
3017
Jim Grosbach4a200922013-09-20 23:08:21 +00003018/// parseDirectiveMacrosOnOff
Eli Bendersky6ee13082013-01-15 22:59:42 +00003019/// ::= .macros_on
3020/// ::= .macros_off
Jim Grosbach4a200922013-09-20 23:08:21 +00003021bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003022 if (getLexer().isNot(AsmToken::EndOfStatement))
3023 return Error(getLexer().getLoc(),
3024 "unexpected token in '" + Directive + "' directive");
3025
Jim Grosbach4a200922013-09-20 23:08:21 +00003026 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003027 return false;
3028}
3029
Jim Grosbach4a200922013-09-20 23:08:21 +00003030/// parseDirectiveMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003031/// ::= .macro name [parameters]
Jim Grosbach4a200922013-09-20 23:08:21 +00003032bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003033 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003034 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003035 return TokError("expected identifier in '.macro' directive");
3036
3037 MCAsmMacroParameters Parameters;
3038 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4a200922013-09-20 23:08:21 +00003039 // parseMacroArgument()
Eli Bendersky6ee13082013-01-15 22:59:42 +00003040 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3041 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3042 for (;;) {
3043 MCAsmMacroParameter Parameter;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003044 if (parseIdentifier(Parameter.first))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003045 return TokError("expected identifier in '.macro' directive");
3046
3047 if (getLexer().is(AsmToken::Equal)) {
3048 Lex();
Jim Grosbach4a200922013-09-20 23:08:21 +00003049 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003050 return true;
3051 }
3052
3053 Parameters.push_back(Parameter);
3054
3055 if (getLexer().is(AsmToken::Comma))
3056 Lex();
3057 else if (getLexer().is(AsmToken::EndOfStatement))
3058 break;
3059 }
3060 }
3061
3062 // Eat the end of statement.
3063 Lex();
3064
3065 AsmToken EndToken, StartToken = getTok();
3066
3067 // Lex the macro definition.
3068 for (;;) {
3069 // Check whether we have reached the end of the file.
3070 if (getLexer().is(AsmToken::Eof))
3071 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3072
3073 // Otherwise, check whether we have reach the .endmacro.
3074 if (getLexer().is(AsmToken::Identifier) &&
3075 (getTok().getIdentifier() == ".endm" ||
3076 getTok().getIdentifier() == ".endmacro")) {
3077 EndToken = getTok();
3078 Lex();
3079 if (getLexer().isNot(AsmToken::EndOfStatement))
3080 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3081 "' directive");
3082 break;
3083 }
3084
3085 // Otherwise, scan til the end of the statement.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003086 eatToEndOfStatement();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003087 }
3088
Jim Grosbach4a200922013-09-20 23:08:21 +00003089 if (lookupMacro(Name)) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003090 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3091 }
3092
3093 const char *BodyStart = StartToken.getLoc().getPointer();
3094 const char *BodyEnd = EndToken.getLoc().getPointer();
3095 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4a200922013-09-20 23:08:21 +00003096 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3097 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky6ee13082013-01-15 22:59:42 +00003098 return false;
3099}
3100
Jim Grosbach4a200922013-09-20 23:08:21 +00003101/// checkForBadMacro
Kevin Enderby221514e2013-01-22 21:44:53 +00003102///
3103/// With the support added for named parameters there may be code out there that
3104/// is transitioning from positional parameters. In versions of gas that did
3105/// not support named parameters they would be ignored on the macro defintion.
3106/// But to support both styles of parameters this is not possible so if a macro
3107/// defintion has named parameters but does not use them and has what appears
3108/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3109/// warning that the positional parameter found in body which have no effect.
3110/// Hoping the developer will either remove the named parameters from the macro
3111/// definiton so the positional parameters get used if that was what was
3112/// intended or change the macro to use the named parameters. It is possible
3113/// this warning will trigger when the none of the named parameters are used
3114/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4a200922013-09-20 23:08:21 +00003115void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby221514e2013-01-22 21:44:53 +00003116 StringRef Body,
3117 MCAsmMacroParameters Parameters) {
3118 // If this macro is not defined with named parameters the warning we are
3119 // checking for here doesn't apply.
3120 unsigned NParameters = Parameters.size();
3121 if (NParameters == 0)
3122 return;
3123
3124 bool NamedParametersFound = false;
3125 bool PositionalParametersFound = false;
3126
3127 // Look at the body of the macro for use of both the named parameters and what
3128 // are likely to be positional parameters. This is what expandMacro() is
3129 // doing when it finds the parameters in the body.
3130 while (!Body.empty()) {
3131 // Scan for the next possible parameter.
3132 std::size_t End = Body.size(), Pos = 0;
3133 for (; Pos != End; ++Pos) {
3134 // Check for a substitution or escape.
3135 // This macro is defined with parameters, look for \foo, \bar, etc.
3136 if (Body[Pos] == '\\' && Pos + 1 != End)
3137 break;
3138
3139 // This macro should have parameters, but look for $0, $1, ..., $n too.
3140 if (Body[Pos] != '$' || Pos + 1 == End)
3141 continue;
3142 char Next = Body[Pos + 1];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00003143 if (Next == '$' || Next == 'n' ||
3144 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby221514e2013-01-22 21:44:53 +00003145 break;
3146 }
3147
3148 // Check if we reached the end.
3149 if (Pos == End)
3150 break;
3151
3152 if (Body[Pos] == '$') {
Jim Grosbach4a200922013-09-20 23:08:21 +00003153 switch (Body[Pos + 1]) {
3154 // $$ => $
Kevin Enderby221514e2013-01-22 21:44:53 +00003155 case '$':
3156 break;
3157
Jim Grosbach4a200922013-09-20 23:08:21 +00003158 // $n => number of arguments
Kevin Enderby221514e2013-01-22 21:44:53 +00003159 case 'n':
3160 PositionalParametersFound = true;
3161 break;
3162
Jim Grosbach4a200922013-09-20 23:08:21 +00003163 // $[0-9] => argument
Kevin Enderby221514e2013-01-22 21:44:53 +00003164 default: {
3165 PositionalParametersFound = true;
3166 break;
Jim Grosbach4a200922013-09-20 23:08:21 +00003167 }
Kevin Enderby221514e2013-01-22 21:44:53 +00003168 }
3169 Pos += 2;
3170 } else {
3171 unsigned I = Pos + 1;
3172 while (isIdentifierChar(Body[I]) && I + 1 != End)
3173 ++I;
3174
Jim Grosbach4a200922013-09-20 23:08:21 +00003175 const char *Begin = Body.data() + Pos + 1;
3176 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby221514e2013-01-22 21:44:53 +00003177 unsigned Index = 0;
3178 for (; Index < NParameters; ++Index)
3179 if (Parameters[Index].first == Argument)
3180 break;
3181
3182 if (Index == NParameters) {
Jim Grosbach4a200922013-09-20 23:08:21 +00003183 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3184 Pos += 3;
3185 else {
3186 Pos = I;
3187 }
Kevin Enderby221514e2013-01-22 21:44:53 +00003188 } else {
3189 NamedParametersFound = true;
3190 Pos += 1 + Argument.size();
3191 }
3192 }
3193 // Update the scan point.
3194 Body = Body.substr(Pos);
3195 }
3196
3197 if (!NamedParametersFound && PositionalParametersFound)
3198 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3199 "used in macro body, possible positional parameter "
3200 "found in body which will have no effect");
3201}
3202
Jim Grosbach4a200922013-09-20 23:08:21 +00003203/// parseDirectiveEndMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003204/// ::= .endm
3205/// ::= .endmacro
Jim Grosbach4a200922013-09-20 23:08:21 +00003206bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003207 if (getLexer().isNot(AsmToken::EndOfStatement))
3208 return TokError("unexpected token in '" + Directive + "' directive");
3209
3210 // If we are inside a macro instantiation, terminate the current
3211 // instantiation.
Jim Grosbach4a200922013-09-20 23:08:21 +00003212 if (isInsideMacroInstantiation()) {
3213 handleMacroExit();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003214 return false;
3215 }
3216
3217 // Otherwise, this .endmacro is a stray entry in the file; well formed
3218 // .endmacro directives are handled during the macro definition parsing.
3219 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4a200922013-09-20 23:08:21 +00003220 "no current macro definition");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003221}
3222
Jim Grosbach4a200922013-09-20 23:08:21 +00003223/// parseDirectivePurgeMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003224/// ::= .purgem
Jim Grosbach4a200922013-09-20 23:08:21 +00003225bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003226 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003227 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003228 return TokError("expected identifier in '.purgem' directive");
3229
3230 if (getLexer().isNot(AsmToken::EndOfStatement))
3231 return TokError("unexpected token in '.purgem' directive");
3232
Jim Grosbach4a200922013-09-20 23:08:21 +00003233 if (!lookupMacro(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003234 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3235
Jim Grosbach4a200922013-09-20 23:08:21 +00003236 undefineMacro(Name);
Eli Bendersky6ee13082013-01-15 22:59:42 +00003237 return false;
3238}
Eli Bendersky4766ef42012-12-20 19:05:53 +00003239
Jim Grosbach4a200922013-09-20 23:08:21 +00003240/// parseDirectiveBundleAlignMode
Eli Bendersky4766ef42012-12-20 19:05:53 +00003241/// ::= {.bundle_align_mode} expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003242bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003243 checkForValidSection();
Eli Bendersky4766ef42012-12-20 19:05:53 +00003244
3245 // Expect a single argument: an expression that evaluates to a constant
3246 // in the inclusive range 0-30.
3247 SMLoc ExprLoc = getLexer().getLoc();
3248 int64_t AlignSizePow2;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003249 if (parseAbsoluteExpression(AlignSizePow2))
Eli Bendersky4766ef42012-12-20 19:05:53 +00003250 return true;
3251 else if (getLexer().isNot(AsmToken::EndOfStatement))
3252 return TokError("unexpected token after expression in"
3253 " '.bundle_align_mode' directive");
3254 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3255 return Error(ExprLoc,
3256 "invalid bundle alignment size (expected between 0 and 30)");
3257
3258 Lex();
3259
3260 // Because of AlignSizePow2's verified range we can safely truncate it to
3261 // unsigned.
3262 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3263 return false;
3264}
3265
Jim Grosbach4a200922013-09-20 23:08:21 +00003266/// parseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003267/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4a200922013-09-20 23:08:21 +00003268bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003269 checkForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003270 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00003271
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003272 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3273 StringRef Option;
3274 SMLoc Loc = getTok().getLoc();
3275 const char *kInvalidOptionError =
Jim Grosbach4a200922013-09-20 23:08:21 +00003276 "invalid option for '.bundle_lock' directive";
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003277
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003278 if (parseIdentifier(Option))
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003279 return Error(Loc, kInvalidOptionError);
3280
3281 if (Option != "align_to_end")
3282 return Error(Loc, kInvalidOptionError);
3283 else if (getLexer().isNot(AsmToken::EndOfStatement))
3284 return Error(Loc,
3285 "unexpected token after '.bundle_lock' directive option");
3286 AlignToEnd = true;
3287 }
3288
Eli Bendersky4766ef42012-12-20 19:05:53 +00003289 Lex();
3290
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003291 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00003292 return false;
3293}
3294
Jim Grosbach4a200922013-09-20 23:08:21 +00003295/// parseDirectiveBundleLock
Eli Bendersky4766ef42012-12-20 19:05:53 +00003296/// ::= {.bundle_lock}
Jim Grosbach4a200922013-09-20 23:08:21 +00003297bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003298 checkForValidSection();
Eli Bendersky4766ef42012-12-20 19:05:53 +00003299
3300 if (getLexer().isNot(AsmToken::EndOfStatement))
3301 return TokError("unexpected token in '.bundle_unlock' directive");
3302 Lex();
3303
3304 getStreamer().EmitBundleUnlock();
3305 return false;
3306}
3307
Jim Grosbach4a200922013-09-20 23:08:21 +00003308/// parseDirectiveSpace
Eli Bendersky6ee13082013-01-15 22:59:42 +00003309/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003310bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003311 checkForValidSection();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003312
3313 int64_t NumBytes;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003314 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003315 return true;
3316
3317 int64_t FillExpr = 0;
3318 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3319 if (getLexer().isNot(AsmToken::Comma))
3320 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3321 Lex();
3322
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003323 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003324 return true;
3325
3326 if (getLexer().isNot(AsmToken::EndOfStatement))
3327 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3328 }
3329
3330 Lex();
3331
3332 if (NumBytes <= 0)
Jim Grosbach4a200922013-09-20 23:08:21 +00003333 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3334 "' directive");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003335
3336 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00003337 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky6ee13082013-01-15 22:59:42 +00003338
3339 return false;
3340}
3341
Jim Grosbach4a200922013-09-20 23:08:21 +00003342/// parseDirectiveLEB128
Eli Bendersky6ee13082013-01-15 22:59:42 +00003343/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003344bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003345 checkForValidSection();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003346 const MCExpr *Value;
3347
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003348 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003349 return true;
3350
3351 if (getLexer().isNot(AsmToken::EndOfStatement))
3352 return TokError("unexpected token in directive");
3353
3354 if (Signed)
3355 getStreamer().EmitSLEB128Value(Value);
3356 else
3357 getStreamer().EmitULEB128Value(Value);
3358
3359 return false;
3360}
3361
Jim Grosbach4a200922013-09-20 23:08:21 +00003362/// parseDirectiveSymbolAttribute
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003363/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003364bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003365 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003366 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003367 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00003368 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003369
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003370 if (parseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00003371 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003372
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00003373 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003374
Jim Grosbach10ec6502011-09-15 17:56:49 +00003375 // Assembler local symbols don't make any sense here. Complain loudly.
3376 if (Sym->isTemporary())
3377 return Error(Loc, "non-local symbol required in directive");
3378
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +00003379 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3380 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003381
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003382 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003383 break;
3384
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003385 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003386 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003387 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003388 }
3389 }
3390
Sean Callanan79ed1a82010-01-19 20:22:31 +00003391 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00003392 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003393}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003394
Jim Grosbach4a200922013-09-20 23:08:21 +00003395/// parseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00003396/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003397bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003398 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00003399
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003400 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003401 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003402 if (parseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003403 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003404
Daniel Dunbar76c4d762009-07-31 21:55:09 +00003405 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00003406 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003407
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003408 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003409 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003410 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003411
3412 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003413 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003414 if (parseAbsoluteExpression(Size))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003415 return true;
3416
3417 int64_t Pow2Alignment = 0;
3418 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003419 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00003420 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003421 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003422 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003423 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003424
Benjamin Kramera9e37c52012-09-07 21:08:01 +00003425 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3426 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00003427 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3428
Chris Lattner258281d2010-01-19 06:22:22 +00003429 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00003430 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3431 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00003432 if (!isPowerOf2_64(Pow2Alignment))
3433 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3434 Pow2Alignment = Log2_64(Pow2Alignment);
3435 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003436 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003437
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003438 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00003439 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003440
Sean Callanan79ed1a82010-01-19 20:22:31 +00003441 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003442
Chris Lattner1fc3d752009-07-09 17:25:12 +00003443 // NOTE: a size of zero for a .comm should create a undefined symbol
3444 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003445 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00003446 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4a200922013-09-20 23:08:21 +00003447 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003448
Eric Christopherc260a3e2010-05-14 01:38:54 +00003449 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003450 // may internally end up wanting an alignment in bytes.
3451 // FIXME: Diagnose overflow.
3452 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00003453 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4a200922013-09-20 23:08:21 +00003454 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003455
Daniel Dunbar8906ff12009-08-22 07:22:36 +00003456 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003457 return Error(IDLoc, "invalid symbol redefinition");
3458
Chris Lattner1fc3d752009-07-09 17:25:12 +00003459 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00003460 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00003461 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00003462 return false;
3463 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003464
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003465 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003466 return false;
3467}
Chris Lattner9be3fee2009-07-10 22:20:30 +00003468
Jim Grosbach4a200922013-09-20 23:08:21 +00003469/// parseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003470/// ::= .abort [... message ...]
Jim Grosbach4a200922013-09-20 23:08:21 +00003471bool AsmParser::parseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003472 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003473 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003474
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003475 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003476 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003477 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003478
Sean Callanan79ed1a82010-01-19 20:22:31 +00003479 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003480
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003481 if (Str.empty())
3482 Error(Loc, ".abort detected. Assembly stopping.");
3483 else
3484 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003485 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003486
3487 return false;
3488}
Kevin Enderby71148242009-07-14 21:35:03 +00003489
Jim Grosbach4a200922013-09-20 23:08:21 +00003490/// parseDirectiveInclude
Kevin Enderby1f049b22009-07-14 23:21:55 +00003491/// ::= .include "filename"
Jim Grosbach4a200922013-09-20 23:08:21 +00003492bool AsmParser::parseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003493 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00003494 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003495
Yunzhong Gaoed119822013-09-05 19:14:26 +00003496 // Allow the strings to have escaped octal character sequence.
3497 std::string Filename;
3498 if (parseEscapedString(Filename))
3499 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003500 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00003501 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00003502
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003503 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00003504 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003505
Chris Lattner8e25e2d2009-07-16 06:14:39 +00003506 // Attempt to switch the lexer to the included file before consuming the end
3507 // of statement to avoid losing it when we switch.
Jim Grosbach4a200922013-09-20 23:08:21 +00003508 if (enterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00003509 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00003510 return true;
3511 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00003512
3513 return false;
3514}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00003515
Jim Grosbach4a200922013-09-20 23:08:21 +00003516/// parseDirectiveIncbin
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003517/// ::= .incbin "filename"
Jim Grosbach4a200922013-09-20 23:08:21 +00003518bool AsmParser::parseDirectiveIncbin() {
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003519 if (getLexer().isNot(AsmToken::String))
3520 return TokError("expected string in '.incbin' directive");
3521
Yunzhong Gaoed119822013-09-05 19:14:26 +00003522 // Allow the strings to have escaped octal character sequence.
3523 std::string Filename;
3524 if (parseEscapedString(Filename))
3525 return true;
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003526 SMLoc IncbinLoc = getLexer().getLoc();
3527 Lex();
3528
3529 if (getLexer().isNot(AsmToken::EndOfStatement))
3530 return TokError("unexpected token in '.incbin' directive");
3531
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003532 // Attempt to process the included file.
Jim Grosbach4a200922013-09-20 23:08:21 +00003533 if (processIncbinFile(Filename)) {
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003534 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3535 return true;
3536 }
3537
3538 return false;
3539}
3540
Jim Grosbach4a200922013-09-20 23:08:21 +00003541/// parseDirectiveIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003542/// ::= .if expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003543bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003544 TheCondStack.push_back(TheCondState);
3545 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00003546 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003547 eatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00003548 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003549 int64_t ExprValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003550 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003551 return true;
3552
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003553 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003554 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003555
Sean Callanan79ed1a82010-01-19 20:22:31 +00003556 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003557
3558 TheCondState.CondMet = ExprValue;
3559 TheCondState.Ignore = !TheCondState.CondMet;
3560 }
3561
3562 return false;
3563}
3564
Jim Grosbach4a200922013-09-20 23:08:21 +00003565/// parseDirectiveIfb
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003566/// ::= .ifb string
Jim Grosbach4a200922013-09-20 23:08:21 +00003567bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003568 TheCondStack.push_back(TheCondState);
3569 TheCondState.TheCond = AsmCond::IfCond;
3570
Benjamin Kramer29739e72012-05-12 16:52:21 +00003571 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003572 eatToEndOfStatement();
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003573 } else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003574 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003575
3576 if (getLexer().isNot(AsmToken::EndOfStatement))
3577 return TokError("unexpected token in '.ifb' directive");
3578
3579 Lex();
3580
3581 TheCondState.CondMet = ExpectBlank == Str.empty();
3582 TheCondState.Ignore = !TheCondState.CondMet;
3583 }
3584
3585 return false;
3586}
3587
Jim Grosbach4a200922013-09-20 23:08:21 +00003588/// parseDirectiveIfc
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003589/// ::= .ifc string1, string2
Jim Grosbach4a200922013-09-20 23:08:21 +00003590bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003591 TheCondStack.push_back(TheCondState);
3592 TheCondState.TheCond = AsmCond::IfCond;
3593
Benjamin Kramer29739e72012-05-12 16:52:21 +00003594 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003595 eatToEndOfStatement();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003596 } else {
Jim Grosbach4a200922013-09-20 23:08:21 +00003597 StringRef Str1 = parseStringToComma();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003598
3599 if (getLexer().isNot(AsmToken::Comma))
3600 return TokError("unexpected token in '.ifc' directive");
3601
3602 Lex();
3603
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003604 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003605
3606 if (getLexer().isNot(AsmToken::EndOfStatement))
3607 return TokError("unexpected token in '.ifc' directive");
3608
3609 Lex();
3610
3611 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3612 TheCondState.Ignore = !TheCondState.CondMet;
3613 }
3614
3615 return false;
3616}
3617
Jim Grosbach4a200922013-09-20 23:08:21 +00003618/// parseDirectiveIfdef
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003619/// ::= .ifdef symbol
Jim Grosbach4a200922013-09-20 23:08:21 +00003620bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003621 StringRef Name;
3622 TheCondStack.push_back(TheCondState);
3623 TheCondState.TheCond = AsmCond::IfCond;
3624
3625 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003626 eatToEndOfStatement();
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003627 } else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003628 if (parseIdentifier(Name))
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003629 return TokError("expected identifier after '.ifdef'");
3630
3631 Lex();
3632
3633 MCSymbol *Sym = getContext().LookupSymbol(Name);
3634
3635 if (expect_defined)
3636 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3637 else
3638 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3639 TheCondState.Ignore = !TheCondState.CondMet;
3640 }
3641
3642 return false;
3643}
3644
Jim Grosbach4a200922013-09-20 23:08:21 +00003645/// parseDirectiveElseIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003646/// ::= .elseif expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003647bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003648 if (TheCondState.TheCond != AsmCond::IfCond &&
3649 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper955d1e92013-04-22 04:24:02 +00003650 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3651 " an .elseif");
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003652 TheCondState.TheCond = AsmCond::ElseIfCond;
3653
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003654 bool LastIgnoreState = false;
3655 if (!TheCondStack.empty())
Craig Topper955d1e92013-04-22 04:24:02 +00003656 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003657 if (LastIgnoreState || TheCondState.CondMet) {
3658 TheCondState.Ignore = true;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003659 eatToEndOfStatement();
Craig Topperefa703d2013-04-22 04:22:40 +00003660 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003661 int64_t ExprValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003662 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003663 return true;
3664
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003665 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003666 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003667
Sean Callanan79ed1a82010-01-19 20:22:31 +00003668 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003669 TheCondState.CondMet = ExprValue;
3670 TheCondState.Ignore = !TheCondState.CondMet;
3671 }
3672
3673 return false;
3674}
3675
Jim Grosbach4a200922013-09-20 23:08:21 +00003676/// parseDirectiveElse
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003677/// ::= .else
Jim Grosbach4a200922013-09-20 23:08:21 +00003678bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003679 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003680 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003681
Sean Callanan79ed1a82010-01-19 20:22:31 +00003682 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003683
3684 if (TheCondState.TheCond != AsmCond::IfCond &&
3685 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper955d1e92013-04-22 04:24:02 +00003686 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3687 ".elseif");
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003688 TheCondState.TheCond = AsmCond::ElseCond;
3689 bool LastIgnoreState = false;
3690 if (!TheCondStack.empty())
3691 LastIgnoreState = TheCondStack.back().Ignore;
3692 if (LastIgnoreState || TheCondState.CondMet)
3693 TheCondState.Ignore = true;
3694 else
3695 TheCondState.Ignore = false;
3696
3697 return false;
3698}
3699
Jim Grosbach4a200922013-09-20 23:08:21 +00003700/// parseDirectiveEndIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003701/// ::= .endif
Jim Grosbach4a200922013-09-20 23:08:21 +00003702bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003703 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003704 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003705
Sean Callanan79ed1a82010-01-19 20:22:31 +00003706 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003707
Jim Grosbach4a200922013-09-20 23:08:21 +00003708 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003709 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3710 ".else");
3711 if (!TheCondStack.empty()) {
3712 TheCondState = TheCondStack.back();
3713 TheCondStack.pop_back();
3714 }
3715
3716 return false;
3717}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003718
Eli Bendersky6ee13082013-01-15 22:59:42 +00003719void AsmParser::initializeDirectiveKindMap() {
3720 DirectiveKindMap[".set"] = DK_SET;
3721 DirectiveKindMap[".equ"] = DK_EQU;
3722 DirectiveKindMap[".equiv"] = DK_EQUIV;
3723 DirectiveKindMap[".ascii"] = DK_ASCII;
3724 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3725 DirectiveKindMap[".string"] = DK_STRING;
3726 DirectiveKindMap[".byte"] = DK_BYTE;
3727 DirectiveKindMap[".short"] = DK_SHORT;
3728 DirectiveKindMap[".value"] = DK_VALUE;
3729 DirectiveKindMap[".2byte"] = DK_2BYTE;
3730 DirectiveKindMap[".long"] = DK_LONG;
3731 DirectiveKindMap[".int"] = DK_INT;
3732 DirectiveKindMap[".4byte"] = DK_4BYTE;
3733 DirectiveKindMap[".quad"] = DK_QUAD;
3734 DirectiveKindMap[".8byte"] = DK_8BYTE;
3735 DirectiveKindMap[".single"] = DK_SINGLE;
3736 DirectiveKindMap[".float"] = DK_FLOAT;
3737 DirectiveKindMap[".double"] = DK_DOUBLE;
3738 DirectiveKindMap[".align"] = DK_ALIGN;
3739 DirectiveKindMap[".align32"] = DK_ALIGN32;
3740 DirectiveKindMap[".balign"] = DK_BALIGN;
3741 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3742 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3743 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3744 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3745 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3746 DirectiveKindMap[".org"] = DK_ORG;
3747 DirectiveKindMap[".fill"] = DK_FILL;
3748 DirectiveKindMap[".zero"] = DK_ZERO;
3749 DirectiveKindMap[".extern"] = DK_EXTERN;
3750 DirectiveKindMap[".globl"] = DK_GLOBL;
3751 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky6ee13082013-01-15 22:59:42 +00003752 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3753 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3754 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3755 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3756 DirectiveKindMap[".reference"] = DK_REFERENCE;
3757 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3758 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3759 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3760 DirectiveKindMap[".comm"] = DK_COMM;
3761 DirectiveKindMap[".common"] = DK_COMMON;
3762 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3763 DirectiveKindMap[".abort"] = DK_ABORT;
3764 DirectiveKindMap[".include"] = DK_INCLUDE;
3765 DirectiveKindMap[".incbin"] = DK_INCBIN;
3766 DirectiveKindMap[".code16"] = DK_CODE16;
3767 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3768 DirectiveKindMap[".rept"] = DK_REPT;
3769 DirectiveKindMap[".irp"] = DK_IRP;
3770 DirectiveKindMap[".irpc"] = DK_IRPC;
3771 DirectiveKindMap[".endr"] = DK_ENDR;
3772 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3773 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3774 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3775 DirectiveKindMap[".if"] = DK_IF;
3776 DirectiveKindMap[".ifb"] = DK_IFB;
3777 DirectiveKindMap[".ifnb"] = DK_IFNB;
3778 DirectiveKindMap[".ifc"] = DK_IFC;
3779 DirectiveKindMap[".ifnc"] = DK_IFNC;
3780 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3781 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3782 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3783 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3784 DirectiveKindMap[".else"] = DK_ELSE;
3785 DirectiveKindMap[".endif"] = DK_ENDIF;
3786 DirectiveKindMap[".skip"] = DK_SKIP;
3787 DirectiveKindMap[".space"] = DK_SPACE;
3788 DirectiveKindMap[".file"] = DK_FILE;
3789 DirectiveKindMap[".line"] = DK_LINE;
3790 DirectiveKindMap[".loc"] = DK_LOC;
3791 DirectiveKindMap[".stabs"] = DK_STABS;
3792 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3793 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3794 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3795 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3796 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3797 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3798 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3799 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3800 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3801 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3802 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3803 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3804 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3805 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3806 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3807 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3808 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3809 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3810 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3811 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3812 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
3813 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3814 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3815 DirectiveKindMap[".macro"] = DK_MACRO;
3816 DirectiveKindMap[".endm"] = DK_ENDM;
3817 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3818 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00003819}
3820
Jim Grosbach4a200922013-09-20 23:08:21 +00003821MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003822 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003823
Rafael Espindola761cb062012-06-03 23:57:14 +00003824 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003825 for (;;) {
3826 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003827 if (getLexer().is(AsmToken::Eof)) {
3828 Error(DirectiveLoc, "no matching '.endr' in definition");
3829 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003830 }
3831
Rafael Espindola761cb062012-06-03 23:57:14 +00003832 if (Lexer.is(AsmToken::Identifier) &&
3833 (getTok().getIdentifier() == ".rept")) {
3834 ++NestLevel;
3835 }
3836
3837 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4a200922013-09-20 23:08:21 +00003838 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola761cb062012-06-03 23:57:14 +00003839 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003840 EndToken = getTok();
3841 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003842 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3843 TokError("unexpected token in '.endr' directive");
3844 return 0;
3845 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003846 break;
3847 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003848 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003849 }
3850
Rafael Espindola761cb062012-06-03 23:57:14 +00003851 // Otherwise, scan till the end of the statement.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003852 eatToEndOfStatement();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003853 }
3854
3855 const char *BodyStart = StartToken.getLoc().getPointer();
3856 const char *BodyEnd = EndToken.getLoc().getPointer();
3857 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3858
Rafael Espindola761cb062012-06-03 23:57:14 +00003859 // We Are Anonymous.
3860 StringRef Name;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003861 MCAsmMacroParameters Parameters;
Benjamin Kramera2b0c332013-08-04 09:06:29 +00003862 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3863 return &MacroLikeBodies.back();
Rafael Espindola761cb062012-06-03 23:57:14 +00003864}
3865
Jim Grosbach4a200922013-09-20 23:08:21 +00003866void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +00003867 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003868 OS << ".endr\n";
3869
3870 MemoryBuffer *Instantiation =
Jim Grosbach4a200922013-09-20 23:08:21 +00003871 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003872
Rafael Espindola761cb062012-06-03 23:57:14 +00003873 // Create the macro instantiation object and add to the current macro
3874 // instantiation stack.
Jim Grosbach4a200922013-09-20 23:08:21 +00003875 MacroInstantiation *MI = new MacroInstantiation(
3876 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola761cb062012-06-03 23:57:14 +00003877 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003878
Rafael Espindola761cb062012-06-03 23:57:14 +00003879 // Jump to the macro instantiation and prime the lexer.
3880 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3881 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3882 Lex();
3883}
3884
Jim Grosbach4a200922013-09-20 23:08:21 +00003885bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc) {
Rafael Espindola761cb062012-06-03 23:57:14 +00003886 int64_t Count;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003887 if (parseAbsoluteExpression(Count))
Rafael Espindola761cb062012-06-03 23:57:14 +00003888 return TokError("unexpected token in '.rept' directive");
3889
3890 if (Count < 0)
3891 return TokError("Count is negative");
3892
3893 if (Lexer.isNot(AsmToken::EndOfStatement))
3894 return TokError("unexpected token in '.rept' directive");
3895
3896 // Eat the end of statement.
3897 Lex();
3898
3899 // Lex the rept definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003900 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola761cb062012-06-03 23:57:14 +00003901 if (!M)
3902 return true;
3903
3904 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3905 // to hold the macro body with substitutions.
3906 SmallString<256> Buf;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003907 MCAsmMacroParameters Parameters;
3908 MCAsmMacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003909 raw_svector_ostream OS(Buf);
3910 while (Count--) {
3911 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3912 return true;
3913 }
Jim Grosbach4a200922013-09-20 23:08:21 +00003914 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003915
3916 return false;
3917}
3918
Jim Grosbach4a200922013-09-20 23:08:21 +00003919/// parseDirectiveIrp
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003920/// ::= .irp symbol,values
Jim Grosbach4a200922013-09-20 23:08:21 +00003921bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003922 MCAsmMacroParameters Parameters;
3923 MCAsmMacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003924
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003925 if (parseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003926 return TokError("expected identifier in '.irp' directive");
3927
3928 Parameters.push_back(Parameter);
3929
3930 if (Lexer.isNot(AsmToken::Comma))
3931 return TokError("expected comma in '.irp' directive");
3932
3933 Lex();
3934
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003935 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00003936 if (parseMacroArguments(0, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003937 return true;
3938
3939 // Eat the end of statement.
3940 Lex();
3941
3942 // Lex the irp definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003943 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003944 if (!M)
3945 return true;
3946
3947 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3948 // to hold the macro body with substitutions.
3949 SmallString<256> Buf;
3950 raw_svector_ostream OS(Buf);
3951
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003952 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3953 MCAsmMacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003954 Args.push_back(*i);
3955
3956 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3957 return true;
3958 }
3959
Jim Grosbach4a200922013-09-20 23:08:21 +00003960 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003961
3962 return false;
3963}
3964
Jim Grosbach4a200922013-09-20 23:08:21 +00003965/// parseDirectiveIrpc
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003966/// ::= .irpc symbol,values
Jim Grosbach4a200922013-09-20 23:08:21 +00003967bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003968 MCAsmMacroParameters Parameters;
3969 MCAsmMacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003970
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003971 if (parseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003972 return TokError("expected identifier in '.irpc' directive");
3973
3974 Parameters.push_back(Parameter);
3975
3976 if (Lexer.isNot(AsmToken::Comma))
3977 return TokError("expected comma in '.irpc' directive");
3978
3979 Lex();
3980
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003981 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00003982 if (parseMacroArguments(0, A))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003983 return true;
3984
3985 if (A.size() != 1 || A.front().size() != 1)
3986 return TokError("unexpected token in '.irpc' directive");
3987
3988 // Eat the end of statement.
3989 Lex();
3990
3991 // Lex the irpc definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003992 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003993 if (!M)
3994 return true;
3995
3996 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3997 // to hold the macro body with substitutions.
3998 SmallString<256> Buf;
3999 raw_svector_ostream OS(Buf);
4000
4001 StringRef Values = A.front().front().getString();
4002 std::size_t I, End = Values.size();
4003 for (I = 0; I < End; ++I) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00004004 MCAsmMacroArgument Arg;
Jim Grosbach4a200922013-09-20 23:08:21 +00004005 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004006
Eli Benderskyc0c67b02013-01-14 23:22:36 +00004007 MCAsmMacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004008 Args.push_back(Arg);
4009
4010 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4011 return true;
4012 }
4013
Jim Grosbach4a200922013-09-20 23:08:21 +00004014 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004015
4016 return false;
4017}
4018
Jim Grosbach4a200922013-09-20 23:08:21 +00004019bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola761cb062012-06-03 23:57:14 +00004020 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00004021 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004022
4023 // The only .repl that should get here are the ones created by
Jim Grosbach4a200922013-09-20 23:08:21 +00004024 // instantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004025 assert(getLexer().is(AsmToken::EndOfStatement));
4026
Jim Grosbach4a200922013-09-20 23:08:21 +00004027 handleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004028 return false;
4029}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00004030
Jim Grosbach4a200922013-09-20 23:08:21 +00004031bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer75234372013-02-15 20:37:21 +00004032 size_t Len) {
Eli Friedman2128aae2012-10-22 23:58:19 +00004033 const MCExpr *Value;
4034 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00004035 if (parseExpression(Value))
Eli Friedman2128aae2012-10-22 23:58:19 +00004036 return true;
4037 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4038 if (!MCE)
4039 return Error(ExprLoc, "unexpected expression in _emit");
4040 uint64_t IntValue = MCE->getValue();
4041 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4042 return Error(ExprLoc, "literal value out of range for directive");
4043
Chad Rosier469b1442013-02-12 21:33:51 +00004044 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4045 return false;
4046}
4047
Jim Grosbach4a200922013-09-20 23:08:21 +00004048bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosier469b1442013-02-12 21:33:51 +00004049 const MCExpr *Value;
4050 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00004051 if (parseExpression(Value))
Chad Rosier469b1442013-02-12 21:33:51 +00004052 return true;
4053 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4054 if (!MCE)
4055 return Error(ExprLoc, "unexpected expression in align");
4056 uint64_t IntValue = MCE->getValue();
4057 if (!isPowerOf2_64(IntValue))
4058 return Error(ExprLoc, "literal value not a power of two greater then zero");
4059
Jim Grosbach4a200922013-09-20 23:08:21 +00004060 Info.AsmRewrites->push_back(
4061 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman2128aae2012-10-22 23:58:19 +00004062 return false;
4063}
4064
Chad Rosier19aa3e32013-02-13 21:27:17 +00004065// We are comparing pointers, but the pointers are relative to a single string.
4066// Thus, this should always be deterministic.
Benjamin Kramer0d293e42013-09-22 14:09:50 +00004067static int rewritesSort(const AsmRewrite *AsmRewriteA,
4068 const AsmRewrite *AsmRewriteB) {
Chad Rosierabde6752013-02-13 18:38:58 +00004069 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4070 return -1;
4071 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4072 return 1;
Chad Rosierb54562b2013-02-15 22:54:16 +00004073
Chad Rosier6b369ce2013-04-08 17:43:47 +00004074 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4075 // rewrite to the same location. Make sure the SizeDirective rewrite is
4076 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4077 // ensures the sort algorithm is stable.
Jim Grosbach4a200922013-09-20 23:08:21 +00004078 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4079 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosierb54562b2013-02-15 22:54:16 +00004080 return -1;
Chad Rosier6b369ce2013-04-08 17:43:47 +00004081
Jim Grosbach4a200922013-09-20 23:08:21 +00004082 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4083 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosierb54562b2013-02-15 22:54:16 +00004084 return 1;
Jim Grosbach4a200922013-09-20 23:08:21 +00004085 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb1953982013-02-13 01:03:13 +00004086}
4087
Jim Grosbach4a200922013-09-20 23:08:21 +00004088bool AsmParser::parseMSInlineAsm(
4089 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4090 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4091 SmallVectorImpl<std::string> &Constraints,
4092 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4093 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004094 SmallVector<void *, 4> InputDecls;
4095 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00004096 SmallVector<bool, 4> InputDeclsAddressOf;
4097 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004098 SmallVector<std::string, 4> InputConstraints;
4099 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer75234372013-02-15 20:37:21 +00004100 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004101
Benjamin Kramer75234372013-02-15 20:37:21 +00004102 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004103
4104 // Prime the lexer.
4105 Lex();
4106
4107 // While we have input, parse each statement.
4108 unsigned InputIdx = 0;
4109 unsigned OutputIdx = 0;
4110 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00004111 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4a200922013-09-20 23:08:21 +00004112 if (parseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00004113 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004114
Chad Rosier57498012012-12-12 22:45:52 +00004115 if (Info.ParseError)
4116 return true;
4117
Benjamin Kramer75234372013-02-15 20:37:21 +00004118 if (Info.Opcode == ~0U)
4119 continue;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004120
Benjamin Kramer75234372013-02-15 20:37:21 +00004121 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004122
Benjamin Kramer75234372013-02-15 20:37:21 +00004123 // Build the list of clobbers, outputs and inputs.
4124 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4125 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004126
Benjamin Kramer75234372013-02-15 20:37:21 +00004127 // Immediate.
Chad Rosier811ddf62013-03-19 21:58:18 +00004128 if (Operand->isImm())
Benjamin Kramer75234372013-02-15 20:37:21 +00004129 continue;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004130
Benjamin Kramer75234372013-02-15 20:37:21 +00004131 // Register operand.
4132 if (Operand->isReg() && !Operand->needAddressOf()) {
4133 unsigned NumDefs = Desc.getNumDefs();
4134 // Clobber.
4135 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4136 ClobberRegs.push_back(Operand->getReg());
4137 continue;
4138 }
4139
4140 // Expr/Input or Output.
Chad Rosierb976e402013-04-09 17:53:49 +00004141 StringRef SymName = Operand->getSymName();
4142 if (SymName.empty())
4143 continue;
4144
Chad Rosier087c3092013-04-22 22:12:12 +00004145 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer75234372013-02-15 20:37:21 +00004146 if (!OpDecl)
4147 continue;
4148
4149 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierb976e402013-04-09 17:53:49 +00004150 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer75234372013-02-15 20:37:21 +00004151 if (isOutput) {
4152 ++InputIdx;
4153 OutputDecls.push_back(OpDecl);
4154 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4155 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosierb976e402013-04-09 17:53:49 +00004156 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer75234372013-02-15 20:37:21 +00004157 } else {
4158 InputDecls.push_back(OpDecl);
4159 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4160 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosierb976e402013-04-09 17:53:49 +00004161 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00004162 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00004163 }
4164 }
4165
4166 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00004167 NumOutputs = OutputDecls.size();
4168 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00004169
4170 // Set the unique clobbers.
Benjamin Kramer75234372013-02-15 20:37:21 +00004171 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4172 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4173 ClobberRegs.end());
4174 Clobbers.assign(ClobberRegs.size(), std::string());
4175 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4176 raw_string_ostream OS(Clobbers[I]);
4177 IP->printRegName(OS, ClobberRegs[I]);
4178 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00004179
4180 // Merge the various outputs and inputs. Output are expected first.
4181 if (NumOutputs || NumInputs) {
4182 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00004183 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004184 Constraints.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004185 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00004186 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier1c99a7f2013-01-15 23:07:53 +00004187 Constraints[i] = OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004188 }
4189 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00004190 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier1c99a7f2013-01-15 23:07:53 +00004191 Constraints[j] = InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004192 }
4193 }
4194
4195 // Build the IR assembly string.
4196 std::string AsmStringIR;
4197 raw_string_ostream OS(AsmStringIR);
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004198 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4199 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4a200922013-09-20 23:08:21 +00004200 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer75234372013-02-15 20:37:21 +00004201 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4202 E = AsmStrRewrites.end();
4203 I != E; ++I) {
Chad Rosierdda4b6b2013-04-12 16:26:42 +00004204 AsmRewriteKind Kind = (*I).Kind;
4205 if (Kind == AOK_Delete)
4206 continue;
4207
Chad Rosierb1f8c132012-10-18 15:49:34 +00004208 const char *Loc = (*I).Loc.getPointer();
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004209 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier96d58e62012-10-19 20:57:14 +00004210
Chad Rosier023c8802013-03-19 17:32:17 +00004211 // Emit everything up to the immediate/expression.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004212 unsigned Len = Loc - AsmStart;
Chad Rosierf06cc982013-04-11 21:49:30 +00004213 if (Len)
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004214 OS << StringRef(AsmStart, Len);
Chad Rosier96d58e62012-10-19 20:57:14 +00004215
Chad Rosier5a719fc2012-10-23 17:43:43 +00004216 // Skip the original expression.
4217 if (Kind == AOK_Skip) {
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004218 AsmStart = Loc + (*I).Len;
Chad Rosier5a719fc2012-10-23 17:43:43 +00004219 continue;
4220 }
4221
Chad Rosierdda4b6b2013-04-12 16:26:42 +00004222 unsigned AdditionalSkip = 0;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004223 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004224 switch (Kind) {
Jim Grosbach4a200922013-09-20 23:08:21 +00004225 default:
4226 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004227 case AOK_Imm:
Benjamin Kramer75234372013-02-15 20:37:21 +00004228 OS << "$$" << (*I).Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +00004229 break;
4230 case AOK_ImmPrefix:
Benjamin Kramer75234372013-02-15 20:37:21 +00004231 OS << "$$";
Chad Rosierb1f8c132012-10-18 15:49:34 +00004232 break;
4233 case AOK_Input:
Benjamin Kramer75234372013-02-15 20:37:21 +00004234 OS << '$' << InputIdx++;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004235 break;
4236 case AOK_Output:
Benjamin Kramer75234372013-02-15 20:37:21 +00004237 OS << '$' << OutputIdx++;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004238 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004239 case AOK_SizeDirective:
Benjamin Kramer75234372013-02-15 20:37:21 +00004240 switch ((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004241 default: break;
4242 case 8: OS << "byte ptr "; break;
4243 case 16: OS << "word ptr "; break;
4244 case 32: OS << "dword ptr "; break;
4245 case 64: OS << "qword ptr "; break;
4246 case 80: OS << "xword ptr "; break;
4247 case 128: OS << "xmmword ptr "; break;
4248 case 256: OS << "ymmword ptr "; break;
4249 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004250 break;
4251 case AOK_Emit:
4252 OS << ".byte";
4253 break;
Chad Rosier469b1442013-02-12 21:33:51 +00004254 case AOK_Align: {
4255 unsigned Val = (*I).Val;
4256 OS << ".align " << Val;
4257
4258 // Skip the original immediate.
Benjamin Kramer75234372013-02-15 20:37:21 +00004259 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosier469b1442013-02-12 21:33:51 +00004260 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4261 break;
4262 }
Chad Rosier6a020a72012-10-25 20:41:34 +00004263 case AOK_DotOperator:
4264 OS << (*I).Val;
4265 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004266 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004267
Chad Rosierb1f8c132012-10-18 15:49:34 +00004268 // Skip the original expression.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004269 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004270 }
4271
4272 // Emit the remainder of the asm string.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004273 if (AsmStart != AsmEnd)
4274 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004275
4276 AsmString = OS.str();
4277 return false;
4278}
4279
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004280/// \brief Create an MCAsmParser instance.
Jim Grosbach4a200922013-09-20 23:08:21 +00004281MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4282 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004283 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004284}