blob: dcf54b0b94249f0333874ccab7db0b38e3ce6163 [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
Daniel Dunbara0d14262009-06-24 23:30:00 +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
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002364 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002365 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002366 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002367
Daniel Dunbara0d14262009-06-24 23:30:00 +00002368 int64_t FillSize;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002369 if (parseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002370 return true;
2371
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002372 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002373 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002374 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002375
Daniel Dunbara0d14262009-06-24 23:30:00 +00002376 int64_t FillExpr;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002377 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002378 return true;
2379
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002381 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002382
Sean Callanan79ed1a82010-01-19 20:22:31 +00002383 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002384
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002385 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2386 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002387
2388 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002389 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002390
2391 return false;
2392}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002393
Jim Grosbach4a200922013-09-20 23:08:21 +00002394/// parseDirectiveOrg
Daniel Dunbarc238b582009-06-25 22:44:51 +00002395/// ::= .org expression [ , expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002396bool AsmParser::parseDirectiveOrg() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002397 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002398
Daniel Dunbar821e3332009-08-31 08:09:28 +00002399 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002400 SMLoc Loc = getTok().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002401 if (parseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002402 return true;
2403
2404 // Parse optional fill expression.
2405 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002406 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2407 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002408 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002409 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002410
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002411 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002412 return true;
2413
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002414 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002415 return TokError("unexpected token in '.org' directive");
2416 }
2417
Sean Callanan79ed1a82010-01-19 20:22:31 +00002418 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002419
Jim Grosbachebd4c052012-01-27 00:37:08 +00002420 // Only limited forms of relocatable expressions are accepted here, it
2421 // has to be relative to the current section. The streamer will return
2422 // 'true' if the expression wasn't evaluatable.
2423 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2424 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002425
2426 return false;
2427}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002428
Jim Grosbach4a200922013-09-20 23:08:21 +00002429/// parseDirectiveAlign
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002430/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4a200922013-09-20 23:08:21 +00002431bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002432 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002433
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002434 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002435 int64_t Alignment;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002436 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002437 return true;
2438
2439 SMLoc MaxBytesLoc;
2440 bool HasFillExpr = false;
2441 int64_t FillExpr = 0;
2442 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002443 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2444 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002445 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002446 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002447
2448 // The fill expression can be omitted while specifying a maximum number of
2449 // alignment bytes, e.g:
2450 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002451 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002452 HasFillExpr = true;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002453 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002454 return true;
2455 }
2456
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002457 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2458 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002459 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002460 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002461
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002462 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002463 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002464 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002465
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002467 return TokError("unexpected token in directive");
2468 }
2469 }
2470
Sean Callanan79ed1a82010-01-19 20:22:31 +00002471 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002472
Daniel Dunbar648ac512010-05-17 21:54:30 +00002473 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002474 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002475
2476 // Compute alignment in bytes.
2477 if (IsPow2) {
2478 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002479 if (Alignment >= 32) {
2480 Error(AlignmentLoc, "invalid alignment value");
2481 Alignment = 31;
2482 }
2483
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002484 Alignment = 1ULL << Alignment;
Benjamin Kramer8a89cf22013-02-16 15:00:16 +00002485 } else {
2486 // Reject alignments that aren't a power of two, for gas compatibility.
2487 if (!isPowerOf2_64(Alignment))
2488 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002489 }
2490
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002491 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002492 if (MaxBytesLoc.isValid()) {
2493 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002494 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4a200922013-09-20 23:08:21 +00002495 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002496 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002497 }
2498
2499 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002500 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4a200922013-09-20 23:08:21 +00002501 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002502 MaxBytesToFill = 0;
2503 }
2504 }
2505
Daniel Dunbar648ac512010-05-17 21:54:30 +00002506 // Check whether we should use optimal code alignment for this .align
2507 // directive.
Peter Collingbournedf39be62013-04-17 21:18:16 +00002508 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002509 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2510 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002511 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002512 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002513 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002514 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2515 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002516 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002517
2518 return false;
2519}
2520
Jim Grosbach4a200922013-09-20 23:08:21 +00002521/// parseDirectiveFile
Eli Bendersky6ee13082013-01-15 22:59:42 +00002522/// ::= .file [number] filename
2523/// ::= .file number directory filename
Jim Grosbach4a200922013-09-20 23:08:21 +00002524bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002525 // FIXME: I'm not sure what this is.
2526 int64_t FileNumber = -1;
2527 SMLoc FileNumberLoc = getLexer().getLoc();
2528 if (getLexer().is(AsmToken::Integer)) {
2529 FileNumber = getTok().getIntVal();
2530 Lex();
2531
2532 if (FileNumber < 1)
2533 return TokError("file number less than one");
2534 }
2535
2536 if (getLexer().isNot(AsmToken::String))
2537 return TokError("unexpected token in '.file' directive");
2538
2539 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gaoed119822013-09-05 19:14:26 +00002540 // Allow the strings to have escaped octal character sequence.
2541 std::string Path = getTok().getString();
2542 if (parseEscapedString(Path))
2543 return true;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002544 Lex();
2545
2546 StringRef Directory;
2547 StringRef Filename;
Yunzhong Gaoed119822013-09-05 19:14:26 +00002548 std::string FilenameData;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002549 if (getLexer().is(AsmToken::String)) {
2550 if (FileNumber == -1)
2551 return TokError("explicit path specified, but no file number");
Yunzhong Gaoed119822013-09-05 19:14:26 +00002552 if (parseEscapedString(FilenameData))
2553 return true;
2554 Filename = FilenameData;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002555 Directory = Path;
2556 Lex();
2557 } else {
2558 Filename = Path;
2559 }
2560
2561 if (getLexer().isNot(AsmToken::EndOfStatement))
2562 return TokError("unexpected token in '.file' directive");
2563
2564 if (FileNumber == -1)
2565 getStreamer().EmitFileDirective(Filename);
2566 else {
2567 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4a200922013-09-20 23:08:21 +00002568 Error(DirectiveLoc,
2569 "input can't have .file dwarf directives when -g is "
2570 "used to generate dwarf debug info for assembly code");
Eli Bendersky6ee13082013-01-15 22:59:42 +00002571
2572 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2573 Error(FileNumberLoc, "file number already allocated");
2574 }
2575
2576 return false;
2577}
2578
Jim Grosbach4a200922013-09-20 23:08:21 +00002579/// parseDirectiveLine
Eli Bendersky6ee13082013-01-15 22:59:42 +00002580/// ::= .line [number]
Jim Grosbach4a200922013-09-20 23:08:21 +00002581bool AsmParser::parseDirectiveLine() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002582 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2583 if (getLexer().isNot(AsmToken::Integer))
2584 return TokError("unexpected token in '.line' directive");
2585
2586 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4a200922013-09-20 23:08:21 +00002587 (void)LineNumber;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002588 Lex();
2589
2590 // FIXME: Do something with the .line.
2591 }
2592
2593 if (getLexer().isNot(AsmToken::EndOfStatement))
2594 return TokError("unexpected token in '.line' directive");
2595
2596 return false;
2597}
2598
Jim Grosbach4a200922013-09-20 23:08:21 +00002599/// parseDirectiveLoc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002600/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2601/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2602/// The first number is a file number, must have been previously assigned with
2603/// a .file directive, the second number is the line number and optionally the
2604/// third number is a column position (zero if not specified). The remaining
2605/// optional items are .loc sub-directives.
Jim Grosbach4a200922013-09-20 23:08:21 +00002606bool AsmParser::parseDirectiveLoc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002607 if (getLexer().isNot(AsmToken::Integer))
2608 return TokError("unexpected token in '.loc' directive");
2609 int64_t FileNumber = getTok().getIntVal();
2610 if (FileNumber < 1)
2611 return TokError("file number less than one in '.loc' directive");
2612 if (!getContext().isValidDwarfFileNumber(FileNumber))
2613 return TokError("unassigned file number in '.loc' directive");
2614 Lex();
2615
2616 int64_t LineNumber = 0;
2617 if (getLexer().is(AsmToken::Integer)) {
2618 LineNumber = getTok().getIntVal();
2619 if (LineNumber < 1)
2620 return TokError("line number less than one in '.loc' directive");
2621 Lex();
2622 }
2623
2624 int64_t ColumnPos = 0;
2625 if (getLexer().is(AsmToken::Integer)) {
2626 ColumnPos = getTok().getIntVal();
2627 if (ColumnPos < 0)
2628 return TokError("column position less than zero in '.loc' directive");
2629 Lex();
2630 }
2631
2632 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2633 unsigned Isa = 0;
2634 int64_t Discriminator = 0;
2635 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2636 for (;;) {
2637 if (getLexer().is(AsmToken::EndOfStatement))
2638 break;
2639
2640 StringRef Name;
2641 SMLoc Loc = getTok().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002642 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002643 return TokError("unexpected token in '.loc' directive");
2644
2645 if (Name == "basic_block")
2646 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2647 else if (Name == "prologue_end")
2648 Flags |= DWARF2_FLAG_PROLOGUE_END;
2649 else if (Name == "epilogue_begin")
2650 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2651 else if (Name == "is_stmt") {
2652 Loc = getTok().getLoc();
2653 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002654 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002655 return true;
2656 // The expression must be the constant 0 or 1.
2657 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2658 int Value = MCE->getValue();
2659 if (Value == 0)
2660 Flags &= ~DWARF2_FLAG_IS_STMT;
2661 else if (Value == 1)
2662 Flags |= DWARF2_FLAG_IS_STMT;
2663 else
2664 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperefa703d2013-04-22 04:22:40 +00002665 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002666 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2667 }
Craig Topperefa703d2013-04-22 04:22:40 +00002668 } else if (Name == "isa") {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002669 Loc = getTok().getLoc();
2670 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002671 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002672 return true;
2673 // The expression must be a constant greater or equal to 0.
2674 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2675 int Value = MCE->getValue();
2676 if (Value < 0)
2677 return Error(Loc, "isa number less than zero");
2678 Isa = Value;
Craig Topperefa703d2013-04-22 04:22:40 +00002679 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002680 return Error(Loc, "isa number not a constant value");
2681 }
Craig Topperefa703d2013-04-22 04:22:40 +00002682 } else if (Name == "discriminator") {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002683 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002684 return true;
Craig Topperefa703d2013-04-22 04:22:40 +00002685 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002686 return Error(Loc, "unknown sub-directive in '.loc' directive");
2687 }
2688
2689 if (getLexer().is(AsmToken::EndOfStatement))
2690 break;
2691 }
2692 }
2693
2694 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2695 Isa, Discriminator, StringRef());
2696
2697 return false;
2698}
2699
Jim Grosbach4a200922013-09-20 23:08:21 +00002700/// parseDirectiveStabs
Eli Bendersky6ee13082013-01-15 22:59:42 +00002701/// ::= .stabs string, number, number, number
Jim Grosbach4a200922013-09-20 23:08:21 +00002702bool AsmParser::parseDirectiveStabs() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002703 return TokError("unsupported directive '.stabs'");
2704}
2705
Jim Grosbach4a200922013-09-20 23:08:21 +00002706/// parseDirectiveCFISections
Eli Bendersky6ee13082013-01-15 22:59:42 +00002707/// ::= .cfi_sections section [, section]
Jim Grosbach4a200922013-09-20 23:08:21 +00002708bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002709 StringRef Name;
2710 bool EH = false;
2711 bool Debug = false;
2712
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002713 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002714 return TokError("Expected an identifier");
2715
2716 if (Name == ".eh_frame")
2717 EH = true;
2718 else if (Name == ".debug_frame")
2719 Debug = true;
2720
2721 if (getLexer().is(AsmToken::Comma)) {
2722 Lex();
2723
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002724 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002725 return TokError("Expected an identifier");
2726
2727 if (Name == ".eh_frame")
2728 EH = true;
2729 else if (Name == ".debug_frame")
2730 Debug = true;
2731 }
2732
2733 getStreamer().EmitCFISections(EH, Debug);
2734 return false;
2735}
2736
Jim Grosbach4a200922013-09-20 23:08:21 +00002737/// parseDirectiveCFIStartProc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002738/// ::= .cfi_startproc
Jim Grosbach4a200922013-09-20 23:08:21 +00002739bool AsmParser::parseDirectiveCFIStartProc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002740 getStreamer().EmitCFIStartProc();
2741 return false;
2742}
2743
Jim Grosbach4a200922013-09-20 23:08:21 +00002744/// parseDirectiveCFIEndProc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002745/// ::= .cfi_endproc
Jim Grosbach4a200922013-09-20 23:08:21 +00002746bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002747 getStreamer().EmitCFIEndProc();
2748 return false;
2749}
2750
Jim Grosbach4a200922013-09-20 23:08:21 +00002751/// \brief parse register name or number.
2752bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky6ee13082013-01-15 22:59:42 +00002753 SMLoc DirectiveLoc) {
2754 unsigned RegNo;
2755
2756 if (getLexer().isNot(AsmToken::Integer)) {
2757 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2758 return true;
Bill Wendling99cb6222013-06-18 07:20:20 +00002759 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky6ee13082013-01-15 22:59:42 +00002760 } else
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002761 return parseAbsoluteExpression(Register);
Eli Bendersky6ee13082013-01-15 22:59:42 +00002762
2763 return false;
2764}
2765
Jim Grosbach4a200922013-09-20 23:08:21 +00002766/// parseDirectiveCFIDefCfa
Eli Bendersky6ee13082013-01-15 22:59:42 +00002767/// ::= .cfi_def_cfa register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002768bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002769 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002770 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002771 return true;
2772
2773 if (getLexer().isNot(AsmToken::Comma))
2774 return TokError("unexpected token in directive");
2775 Lex();
2776
2777 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002778 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002779 return true;
2780
2781 getStreamer().EmitCFIDefCfa(Register, Offset);
2782 return false;
2783}
2784
Jim Grosbach4a200922013-09-20 23:08:21 +00002785/// parseDirectiveCFIDefCfaOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002786/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002787bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002788 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002789 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002790 return true;
2791
2792 getStreamer().EmitCFIDefCfaOffset(Offset);
2793 return false;
2794}
2795
Jim Grosbach4a200922013-09-20 23:08:21 +00002796/// parseDirectiveCFIRegister
Eli Bendersky6ee13082013-01-15 22:59:42 +00002797/// ::= .cfi_register register, register
Jim Grosbach4a200922013-09-20 23:08:21 +00002798bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002799 int64_t Register1 = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002800 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002801 return true;
2802
2803 if (getLexer().isNot(AsmToken::Comma))
2804 return TokError("unexpected token in directive");
2805 Lex();
2806
2807 int64_t Register2 = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002808 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002809 return true;
2810
2811 getStreamer().EmitCFIRegister(Register1, Register2);
2812 return false;
2813}
2814
Jim Grosbach4a200922013-09-20 23:08:21 +00002815/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002816/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4a200922013-09-20 23:08:21 +00002817bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002818 int64_t Adjustment = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002819 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002820 return true;
2821
2822 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2823 return false;
2824}
2825
Jim Grosbach4a200922013-09-20 23:08:21 +00002826/// parseDirectiveCFIDefCfaRegister
Eli Bendersky6ee13082013-01-15 22:59:42 +00002827/// ::= .cfi_def_cfa_register register
Jim Grosbach4a200922013-09-20 23:08:21 +00002828bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002829 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002830 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002831 return true;
2832
2833 getStreamer().EmitCFIDefCfaRegister(Register);
2834 return false;
2835}
2836
Jim Grosbach4a200922013-09-20 23:08:21 +00002837/// parseDirectiveCFIOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002838/// ::= .cfi_offset register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002839bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002840 int64_t Register = 0;
2841 int64_t Offset = 0;
2842
Jim Grosbach4a200922013-09-20 23:08:21 +00002843 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002844 return true;
2845
2846 if (getLexer().isNot(AsmToken::Comma))
2847 return TokError("unexpected token in directive");
2848 Lex();
2849
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002850 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002851 return true;
2852
2853 getStreamer().EmitCFIOffset(Register, Offset);
2854 return false;
2855}
2856
Jim Grosbach4a200922013-09-20 23:08:21 +00002857/// parseDirectiveCFIRelOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002858/// ::= .cfi_rel_offset register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002859bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002860 int64_t Register = 0;
2861
Jim Grosbach4a200922013-09-20 23:08:21 +00002862 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002863 return true;
2864
2865 if (getLexer().isNot(AsmToken::Comma))
2866 return TokError("unexpected token in directive");
2867 Lex();
2868
2869 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002870 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002871 return true;
2872
2873 getStreamer().EmitCFIRelOffset(Register, Offset);
2874 return false;
2875}
2876
2877static bool isValidEncoding(int64_t Encoding) {
2878 if (Encoding & ~0xff)
2879 return false;
2880
2881 if (Encoding == dwarf::DW_EH_PE_omit)
2882 return true;
2883
2884 const unsigned Format = Encoding & 0xf;
2885 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2886 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2887 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2888 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2889 return false;
2890
2891 const unsigned Application = Encoding & 0x70;
2892 if (Application != dwarf::DW_EH_PE_absptr &&
2893 Application != dwarf::DW_EH_PE_pcrel)
2894 return false;
2895
2896 return true;
2897}
2898
Jim Grosbach4a200922013-09-20 23:08:21 +00002899/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky6ee13082013-01-15 22:59:42 +00002900/// IsPersonality true for cfi_personality, false for cfi_lsda
2901/// ::= .cfi_personality encoding, [symbol_name]
2902/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4a200922013-09-20 23:08:21 +00002903bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002904 int64_t Encoding = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002905 if (parseAbsoluteExpression(Encoding))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002906 return true;
2907 if (Encoding == dwarf::DW_EH_PE_omit)
2908 return false;
2909
2910 if (!isValidEncoding(Encoding))
2911 return TokError("unsupported encoding.");
2912
2913 if (getLexer().isNot(AsmToken::Comma))
2914 return TokError("unexpected token in directive");
2915 Lex();
2916
2917 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002918 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002919 return TokError("expected identifier in directive");
2920
2921 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2922
2923 if (IsPersonality)
2924 getStreamer().EmitCFIPersonality(Sym, Encoding);
2925 else
2926 getStreamer().EmitCFILsda(Sym, Encoding);
2927 return false;
2928}
2929
Jim Grosbach4a200922013-09-20 23:08:21 +00002930/// parseDirectiveCFIRememberState
Eli Bendersky6ee13082013-01-15 22:59:42 +00002931/// ::= .cfi_remember_state
Jim Grosbach4a200922013-09-20 23:08:21 +00002932bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002933 getStreamer().EmitCFIRememberState();
2934 return false;
2935}
2936
Jim Grosbach4a200922013-09-20 23:08:21 +00002937/// parseDirectiveCFIRestoreState
Eli Bendersky6ee13082013-01-15 22:59:42 +00002938/// ::= .cfi_remember_state
Jim Grosbach4a200922013-09-20 23:08:21 +00002939bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002940 getStreamer().EmitCFIRestoreState();
2941 return false;
2942}
2943
Jim Grosbach4a200922013-09-20 23:08:21 +00002944/// parseDirectiveCFISameValue
Eli Bendersky6ee13082013-01-15 22:59:42 +00002945/// ::= .cfi_same_value register
Jim Grosbach4a200922013-09-20 23:08:21 +00002946bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002947 int64_t Register = 0;
2948
Jim Grosbach4a200922013-09-20 23:08:21 +00002949 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002950 return true;
2951
2952 getStreamer().EmitCFISameValue(Register);
2953 return false;
2954}
2955
Jim Grosbach4a200922013-09-20 23:08:21 +00002956/// parseDirectiveCFIRestore
Eli Bendersky6ee13082013-01-15 22:59:42 +00002957/// ::= .cfi_restore register
Jim Grosbach4a200922013-09-20 23:08:21 +00002958bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002959 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002960 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002961 return true;
2962
2963 getStreamer().EmitCFIRestore(Register);
2964 return false;
2965}
2966
Jim Grosbach4a200922013-09-20 23:08:21 +00002967/// parseDirectiveCFIEscape
Eli Bendersky6ee13082013-01-15 22:59:42 +00002968/// ::= .cfi_escape expression[,...]
Jim Grosbach4a200922013-09-20 23:08:21 +00002969bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002970 std::string Values;
2971 int64_t CurrValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002972 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002973 return true;
2974
2975 Values.push_back((uint8_t)CurrValue);
2976
2977 while (getLexer().is(AsmToken::Comma)) {
2978 Lex();
2979
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002980 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002981 return true;
2982
2983 Values.push_back((uint8_t)CurrValue);
2984 }
2985
2986 getStreamer().EmitCFIEscape(Values);
2987 return false;
2988}
2989
Jim Grosbach4a200922013-09-20 23:08:21 +00002990/// parseDirectiveCFISignalFrame
Eli Bendersky6ee13082013-01-15 22:59:42 +00002991/// ::= .cfi_signal_frame
Jim Grosbach4a200922013-09-20 23:08:21 +00002992bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002993 if (getLexer().isNot(AsmToken::EndOfStatement))
2994 return Error(getLexer().getLoc(),
2995 "unexpected token in '.cfi_signal_frame'");
2996
2997 getStreamer().EmitCFISignalFrame();
2998 return false;
2999}
3000
Jim Grosbach4a200922013-09-20 23:08:21 +00003001/// parseDirectiveCFIUndefined
Eli Bendersky6ee13082013-01-15 22:59:42 +00003002/// ::= .cfi_undefined register
Jim Grosbach4a200922013-09-20 23:08:21 +00003003bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003004 int64_t Register = 0;
3005
Jim Grosbach4a200922013-09-20 23:08:21 +00003006 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003007 return true;
3008
3009 getStreamer().EmitCFIUndefined(Register);
3010 return false;
3011}
3012
Jim Grosbach4a200922013-09-20 23:08:21 +00003013/// parseDirectiveMacrosOnOff
Eli Bendersky6ee13082013-01-15 22:59:42 +00003014/// ::= .macros_on
3015/// ::= .macros_off
Jim Grosbach4a200922013-09-20 23:08:21 +00003016bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003017 if (getLexer().isNot(AsmToken::EndOfStatement))
3018 return Error(getLexer().getLoc(),
3019 "unexpected token in '" + Directive + "' directive");
3020
Jim Grosbach4a200922013-09-20 23:08:21 +00003021 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003022 return false;
3023}
3024
Jim Grosbach4a200922013-09-20 23:08:21 +00003025/// parseDirectiveMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003026/// ::= .macro name [parameters]
Jim Grosbach4a200922013-09-20 23:08:21 +00003027bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003028 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003029 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003030 return TokError("expected identifier in '.macro' directive");
3031
3032 MCAsmMacroParameters Parameters;
3033 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4a200922013-09-20 23:08:21 +00003034 // parseMacroArgument()
Eli Bendersky6ee13082013-01-15 22:59:42 +00003035 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3036 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3037 for (;;) {
3038 MCAsmMacroParameter Parameter;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003039 if (parseIdentifier(Parameter.first))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003040 return TokError("expected identifier in '.macro' directive");
3041
3042 if (getLexer().is(AsmToken::Equal)) {
3043 Lex();
Jim Grosbach4a200922013-09-20 23:08:21 +00003044 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003045 return true;
3046 }
3047
3048 Parameters.push_back(Parameter);
3049
3050 if (getLexer().is(AsmToken::Comma))
3051 Lex();
3052 else if (getLexer().is(AsmToken::EndOfStatement))
3053 break;
3054 }
3055 }
3056
3057 // Eat the end of statement.
3058 Lex();
3059
3060 AsmToken EndToken, StartToken = getTok();
3061
3062 // Lex the macro definition.
3063 for (;;) {
3064 // Check whether we have reached the end of the file.
3065 if (getLexer().is(AsmToken::Eof))
3066 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3067
3068 // Otherwise, check whether we have reach the .endmacro.
3069 if (getLexer().is(AsmToken::Identifier) &&
3070 (getTok().getIdentifier() == ".endm" ||
3071 getTok().getIdentifier() == ".endmacro")) {
3072 EndToken = getTok();
3073 Lex();
3074 if (getLexer().isNot(AsmToken::EndOfStatement))
3075 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3076 "' directive");
3077 break;
3078 }
3079
3080 // Otherwise, scan til the end of the statement.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003081 eatToEndOfStatement();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003082 }
3083
Jim Grosbach4a200922013-09-20 23:08:21 +00003084 if (lookupMacro(Name)) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003085 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3086 }
3087
3088 const char *BodyStart = StartToken.getLoc().getPointer();
3089 const char *BodyEnd = EndToken.getLoc().getPointer();
3090 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4a200922013-09-20 23:08:21 +00003091 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3092 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky6ee13082013-01-15 22:59:42 +00003093 return false;
3094}
3095
Jim Grosbach4a200922013-09-20 23:08:21 +00003096/// checkForBadMacro
Kevin Enderby221514e2013-01-22 21:44:53 +00003097///
3098/// With the support added for named parameters there may be code out there that
3099/// is transitioning from positional parameters. In versions of gas that did
3100/// not support named parameters they would be ignored on the macro defintion.
3101/// But to support both styles of parameters this is not possible so if a macro
3102/// defintion has named parameters but does not use them and has what appears
3103/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3104/// warning that the positional parameter found in body which have no effect.
3105/// Hoping the developer will either remove the named parameters from the macro
3106/// definiton so the positional parameters get used if that was what was
3107/// intended or change the macro to use the named parameters. It is possible
3108/// this warning will trigger when the none of the named parameters are used
3109/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4a200922013-09-20 23:08:21 +00003110void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby221514e2013-01-22 21:44:53 +00003111 StringRef Body,
3112 MCAsmMacroParameters Parameters) {
3113 // If this macro is not defined with named parameters the warning we are
3114 // checking for here doesn't apply.
3115 unsigned NParameters = Parameters.size();
3116 if (NParameters == 0)
3117 return;
3118
3119 bool NamedParametersFound = false;
3120 bool PositionalParametersFound = false;
3121
3122 // Look at the body of the macro for use of both the named parameters and what
3123 // are likely to be positional parameters. This is what expandMacro() is
3124 // doing when it finds the parameters in the body.
3125 while (!Body.empty()) {
3126 // Scan for the next possible parameter.
3127 std::size_t End = Body.size(), Pos = 0;
3128 for (; Pos != End; ++Pos) {
3129 // Check for a substitution or escape.
3130 // This macro is defined with parameters, look for \foo, \bar, etc.
3131 if (Body[Pos] == '\\' && Pos + 1 != End)
3132 break;
3133
3134 // This macro should have parameters, but look for $0, $1, ..., $n too.
3135 if (Body[Pos] != '$' || Pos + 1 == End)
3136 continue;
3137 char Next = Body[Pos + 1];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00003138 if (Next == '$' || Next == 'n' ||
3139 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby221514e2013-01-22 21:44:53 +00003140 break;
3141 }
3142
3143 // Check if we reached the end.
3144 if (Pos == End)
3145 break;
3146
3147 if (Body[Pos] == '$') {
Jim Grosbach4a200922013-09-20 23:08:21 +00003148 switch (Body[Pos + 1]) {
3149 // $$ => $
Kevin Enderby221514e2013-01-22 21:44:53 +00003150 case '$':
3151 break;
3152
Jim Grosbach4a200922013-09-20 23:08:21 +00003153 // $n => number of arguments
Kevin Enderby221514e2013-01-22 21:44:53 +00003154 case 'n':
3155 PositionalParametersFound = true;
3156 break;
3157
Jim Grosbach4a200922013-09-20 23:08:21 +00003158 // $[0-9] => argument
Kevin Enderby221514e2013-01-22 21:44:53 +00003159 default: {
3160 PositionalParametersFound = true;
3161 break;
Jim Grosbach4a200922013-09-20 23:08:21 +00003162 }
Kevin Enderby221514e2013-01-22 21:44:53 +00003163 }
3164 Pos += 2;
3165 } else {
3166 unsigned I = Pos + 1;
3167 while (isIdentifierChar(Body[I]) && I + 1 != End)
3168 ++I;
3169
Jim Grosbach4a200922013-09-20 23:08:21 +00003170 const char *Begin = Body.data() + Pos + 1;
3171 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby221514e2013-01-22 21:44:53 +00003172 unsigned Index = 0;
3173 for (; Index < NParameters; ++Index)
3174 if (Parameters[Index].first == Argument)
3175 break;
3176
3177 if (Index == NParameters) {
Jim Grosbach4a200922013-09-20 23:08:21 +00003178 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3179 Pos += 3;
3180 else {
3181 Pos = I;
3182 }
Kevin Enderby221514e2013-01-22 21:44:53 +00003183 } else {
3184 NamedParametersFound = true;
3185 Pos += 1 + Argument.size();
3186 }
3187 }
3188 // Update the scan point.
3189 Body = Body.substr(Pos);
3190 }
3191
3192 if (!NamedParametersFound && PositionalParametersFound)
3193 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3194 "used in macro body, possible positional parameter "
3195 "found in body which will have no effect");
3196}
3197
Jim Grosbach4a200922013-09-20 23:08:21 +00003198/// parseDirectiveEndMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003199/// ::= .endm
3200/// ::= .endmacro
Jim Grosbach4a200922013-09-20 23:08:21 +00003201bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003202 if (getLexer().isNot(AsmToken::EndOfStatement))
3203 return TokError("unexpected token in '" + Directive + "' directive");
3204
3205 // If we are inside a macro instantiation, terminate the current
3206 // instantiation.
Jim Grosbach4a200922013-09-20 23:08:21 +00003207 if (isInsideMacroInstantiation()) {
3208 handleMacroExit();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003209 return false;
3210 }
3211
3212 // Otherwise, this .endmacro is a stray entry in the file; well formed
3213 // .endmacro directives are handled during the macro definition parsing.
3214 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4a200922013-09-20 23:08:21 +00003215 "no current macro definition");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003216}
3217
Jim Grosbach4a200922013-09-20 23:08:21 +00003218/// parseDirectivePurgeMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003219/// ::= .purgem
Jim Grosbach4a200922013-09-20 23:08:21 +00003220bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003221 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003222 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003223 return TokError("expected identifier in '.purgem' directive");
3224
3225 if (getLexer().isNot(AsmToken::EndOfStatement))
3226 return TokError("unexpected token in '.purgem' directive");
3227
Jim Grosbach4a200922013-09-20 23:08:21 +00003228 if (!lookupMacro(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003229 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3230
Jim Grosbach4a200922013-09-20 23:08:21 +00003231 undefineMacro(Name);
Eli Bendersky6ee13082013-01-15 22:59:42 +00003232 return false;
3233}
Eli Bendersky4766ef42012-12-20 19:05:53 +00003234
Jim Grosbach4a200922013-09-20 23:08:21 +00003235/// parseDirectiveBundleAlignMode
Eli Bendersky4766ef42012-12-20 19:05:53 +00003236/// ::= {.bundle_align_mode} expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003237bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003238 checkForValidSection();
Eli Bendersky4766ef42012-12-20 19:05:53 +00003239
3240 // Expect a single argument: an expression that evaluates to a constant
3241 // in the inclusive range 0-30.
3242 SMLoc ExprLoc = getLexer().getLoc();
3243 int64_t AlignSizePow2;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003244 if (parseAbsoluteExpression(AlignSizePow2))
Eli Bendersky4766ef42012-12-20 19:05:53 +00003245 return true;
3246 else if (getLexer().isNot(AsmToken::EndOfStatement))
3247 return TokError("unexpected token after expression in"
3248 " '.bundle_align_mode' directive");
3249 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3250 return Error(ExprLoc,
3251 "invalid bundle alignment size (expected between 0 and 30)");
3252
3253 Lex();
3254
3255 // Because of AlignSizePow2's verified range we can safely truncate it to
3256 // unsigned.
3257 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3258 return false;
3259}
3260
Jim Grosbach4a200922013-09-20 23:08:21 +00003261/// parseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003262/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4a200922013-09-20 23:08:21 +00003263bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003264 checkForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003265 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00003266
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003267 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3268 StringRef Option;
3269 SMLoc Loc = getTok().getLoc();
3270 const char *kInvalidOptionError =
Jim Grosbach4a200922013-09-20 23:08:21 +00003271 "invalid option for '.bundle_lock' directive";
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003272
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003273 if (parseIdentifier(Option))
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003274 return Error(Loc, kInvalidOptionError);
3275
3276 if (Option != "align_to_end")
3277 return Error(Loc, kInvalidOptionError);
3278 else if (getLexer().isNot(AsmToken::EndOfStatement))
3279 return Error(Loc,
3280 "unexpected token after '.bundle_lock' directive option");
3281 AlignToEnd = true;
3282 }
3283
Eli Bendersky4766ef42012-12-20 19:05:53 +00003284 Lex();
3285
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003286 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00003287 return false;
3288}
3289
Jim Grosbach4a200922013-09-20 23:08:21 +00003290/// parseDirectiveBundleLock
Eli Bendersky4766ef42012-12-20 19:05:53 +00003291/// ::= {.bundle_lock}
Jim Grosbach4a200922013-09-20 23:08:21 +00003292bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003293 checkForValidSection();
Eli Bendersky4766ef42012-12-20 19:05:53 +00003294
3295 if (getLexer().isNot(AsmToken::EndOfStatement))
3296 return TokError("unexpected token in '.bundle_unlock' directive");
3297 Lex();
3298
3299 getStreamer().EmitBundleUnlock();
3300 return false;
3301}
3302
Jim Grosbach4a200922013-09-20 23:08:21 +00003303/// parseDirectiveSpace
Eli Bendersky6ee13082013-01-15 22:59:42 +00003304/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003305bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003306 checkForValidSection();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003307
3308 int64_t NumBytes;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003309 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003310 return true;
3311
3312 int64_t FillExpr = 0;
3313 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3314 if (getLexer().isNot(AsmToken::Comma))
3315 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3316 Lex();
3317
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003318 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003319 return true;
3320
3321 if (getLexer().isNot(AsmToken::EndOfStatement))
3322 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3323 }
3324
3325 Lex();
3326
3327 if (NumBytes <= 0)
Jim Grosbach4a200922013-09-20 23:08:21 +00003328 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3329 "' directive");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003330
3331 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00003332 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky6ee13082013-01-15 22:59:42 +00003333
3334 return false;
3335}
3336
Jim Grosbach4a200922013-09-20 23:08:21 +00003337/// parseDirectiveLEB128
Eli Bendersky6ee13082013-01-15 22:59:42 +00003338/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003339bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003340 checkForValidSection();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003341 const MCExpr *Value;
3342
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003343 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003344 return true;
3345
3346 if (getLexer().isNot(AsmToken::EndOfStatement))
3347 return TokError("unexpected token in directive");
3348
3349 if (Signed)
3350 getStreamer().EmitSLEB128Value(Value);
3351 else
3352 getStreamer().EmitULEB128Value(Value);
3353
3354 return false;
3355}
3356
Jim Grosbach4a200922013-09-20 23:08:21 +00003357/// parseDirectiveSymbolAttribute
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003358/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003359bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003360 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003361 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003362 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00003363 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003364
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003365 if (parseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00003366 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003367
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00003368 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003369
Jim Grosbach10ec6502011-09-15 17:56:49 +00003370 // Assembler local symbols don't make any sense here. Complain loudly.
3371 if (Sym->isTemporary())
3372 return Error(Loc, "non-local symbol required in directive");
3373
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +00003374 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3375 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003376
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003377 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003378 break;
3379
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003380 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003381 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003382 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003383 }
3384 }
3385
Sean Callanan79ed1a82010-01-19 20:22:31 +00003386 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00003387 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003388}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003389
Jim Grosbach4a200922013-09-20 23:08:21 +00003390/// parseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00003391/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003392bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003393 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00003394
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003395 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003396 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003397 if (parseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003398 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003399
Daniel Dunbar76c4d762009-07-31 21:55:09 +00003400 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00003401 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003402
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003403 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003404 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003405 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003406
3407 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003408 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003409 if (parseAbsoluteExpression(Size))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003410 return true;
3411
3412 int64_t Pow2Alignment = 0;
3413 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003414 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00003415 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003416 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003417 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003418 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003419
Benjamin Kramera9e37c52012-09-07 21:08:01 +00003420 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3421 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00003422 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3423
Chris Lattner258281d2010-01-19 06:22:22 +00003424 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00003425 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3426 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00003427 if (!isPowerOf2_64(Pow2Alignment))
3428 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3429 Pow2Alignment = Log2_64(Pow2Alignment);
3430 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003431 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003432
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003433 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00003434 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003435
Sean Callanan79ed1a82010-01-19 20:22:31 +00003436 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003437
Chris Lattner1fc3d752009-07-09 17:25:12 +00003438 // NOTE: a size of zero for a .comm should create a undefined symbol
3439 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003440 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00003441 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4a200922013-09-20 23:08:21 +00003442 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003443
Eric Christopherc260a3e2010-05-14 01:38:54 +00003444 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003445 // may internally end up wanting an alignment in bytes.
3446 // FIXME: Diagnose overflow.
3447 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00003448 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4a200922013-09-20 23:08:21 +00003449 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003450
Daniel Dunbar8906ff12009-08-22 07:22:36 +00003451 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003452 return Error(IDLoc, "invalid symbol redefinition");
3453
Chris Lattner1fc3d752009-07-09 17:25:12 +00003454 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00003455 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00003456 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00003457 return false;
3458 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003459
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003460 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003461 return false;
3462}
Chris Lattner9be3fee2009-07-10 22:20:30 +00003463
Jim Grosbach4a200922013-09-20 23:08:21 +00003464/// parseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003465/// ::= .abort [... message ...]
Jim Grosbach4a200922013-09-20 23:08:21 +00003466bool AsmParser::parseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003467 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003468 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003469
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003470 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003471 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003472 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003473
Sean Callanan79ed1a82010-01-19 20:22:31 +00003474 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003475
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003476 if (Str.empty())
3477 Error(Loc, ".abort detected. Assembly stopping.");
3478 else
3479 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003480 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003481
3482 return false;
3483}
Kevin Enderby71148242009-07-14 21:35:03 +00003484
Jim Grosbach4a200922013-09-20 23:08:21 +00003485/// parseDirectiveInclude
Kevin Enderby1f049b22009-07-14 23:21:55 +00003486/// ::= .include "filename"
Jim Grosbach4a200922013-09-20 23:08:21 +00003487bool AsmParser::parseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003488 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00003489 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003490
Yunzhong Gaoed119822013-09-05 19:14:26 +00003491 // Allow the strings to have escaped octal character sequence.
3492 std::string Filename;
3493 if (parseEscapedString(Filename))
3494 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003495 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00003496 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00003497
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003498 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00003499 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003500
Chris Lattner8e25e2d2009-07-16 06:14:39 +00003501 // Attempt to switch the lexer to the included file before consuming the end
3502 // of statement to avoid losing it when we switch.
Jim Grosbach4a200922013-09-20 23:08:21 +00003503 if (enterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00003504 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00003505 return true;
3506 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00003507
3508 return false;
3509}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00003510
Jim Grosbach4a200922013-09-20 23:08:21 +00003511/// parseDirectiveIncbin
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003512/// ::= .incbin "filename"
Jim Grosbach4a200922013-09-20 23:08:21 +00003513bool AsmParser::parseDirectiveIncbin() {
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003514 if (getLexer().isNot(AsmToken::String))
3515 return TokError("expected string in '.incbin' directive");
3516
Yunzhong Gaoed119822013-09-05 19:14:26 +00003517 // Allow the strings to have escaped octal character sequence.
3518 std::string Filename;
3519 if (parseEscapedString(Filename))
3520 return true;
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003521 SMLoc IncbinLoc = getLexer().getLoc();
3522 Lex();
3523
3524 if (getLexer().isNot(AsmToken::EndOfStatement))
3525 return TokError("unexpected token in '.incbin' directive");
3526
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003527 // Attempt to process the included file.
Jim Grosbach4a200922013-09-20 23:08:21 +00003528 if (processIncbinFile(Filename)) {
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003529 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3530 return true;
3531 }
3532
3533 return false;
3534}
3535
Jim Grosbach4a200922013-09-20 23:08:21 +00003536/// parseDirectiveIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003537/// ::= .if expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003538bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003539 TheCondStack.push_back(TheCondState);
3540 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00003541 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003542 eatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00003543 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003544 int64_t ExprValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003545 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003546 return true;
3547
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003548 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003549 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003550
Sean Callanan79ed1a82010-01-19 20:22:31 +00003551 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003552
3553 TheCondState.CondMet = ExprValue;
3554 TheCondState.Ignore = !TheCondState.CondMet;
3555 }
3556
3557 return false;
3558}
3559
Jim Grosbach4a200922013-09-20 23:08:21 +00003560/// parseDirectiveIfb
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003561/// ::= .ifb string
Jim Grosbach4a200922013-09-20 23:08:21 +00003562bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003563 TheCondStack.push_back(TheCondState);
3564 TheCondState.TheCond = AsmCond::IfCond;
3565
Benjamin Kramer29739e72012-05-12 16:52:21 +00003566 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003567 eatToEndOfStatement();
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003568 } else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003569 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003570
3571 if (getLexer().isNot(AsmToken::EndOfStatement))
3572 return TokError("unexpected token in '.ifb' directive");
3573
3574 Lex();
3575
3576 TheCondState.CondMet = ExpectBlank == Str.empty();
3577 TheCondState.Ignore = !TheCondState.CondMet;
3578 }
3579
3580 return false;
3581}
3582
Jim Grosbach4a200922013-09-20 23:08:21 +00003583/// parseDirectiveIfc
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003584/// ::= .ifc string1, string2
Jim Grosbach4a200922013-09-20 23:08:21 +00003585bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003586 TheCondStack.push_back(TheCondState);
3587 TheCondState.TheCond = AsmCond::IfCond;
3588
Benjamin Kramer29739e72012-05-12 16:52:21 +00003589 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003590 eatToEndOfStatement();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003591 } else {
Jim Grosbach4a200922013-09-20 23:08:21 +00003592 StringRef Str1 = parseStringToComma();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003593
3594 if (getLexer().isNot(AsmToken::Comma))
3595 return TokError("unexpected token in '.ifc' directive");
3596
3597 Lex();
3598
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003599 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003600
3601 if (getLexer().isNot(AsmToken::EndOfStatement))
3602 return TokError("unexpected token in '.ifc' directive");
3603
3604 Lex();
3605
3606 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3607 TheCondState.Ignore = !TheCondState.CondMet;
3608 }
3609
3610 return false;
3611}
3612
Jim Grosbach4a200922013-09-20 23:08:21 +00003613/// parseDirectiveIfdef
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003614/// ::= .ifdef symbol
Jim Grosbach4a200922013-09-20 23:08:21 +00003615bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003616 StringRef Name;
3617 TheCondStack.push_back(TheCondState);
3618 TheCondState.TheCond = AsmCond::IfCond;
3619
3620 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003621 eatToEndOfStatement();
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003622 } else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003623 if (parseIdentifier(Name))
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003624 return TokError("expected identifier after '.ifdef'");
3625
3626 Lex();
3627
3628 MCSymbol *Sym = getContext().LookupSymbol(Name);
3629
3630 if (expect_defined)
3631 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3632 else
3633 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3634 TheCondState.Ignore = !TheCondState.CondMet;
3635 }
3636
3637 return false;
3638}
3639
Jim Grosbach4a200922013-09-20 23:08:21 +00003640/// parseDirectiveElseIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003641/// ::= .elseif expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003642bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003643 if (TheCondState.TheCond != AsmCond::IfCond &&
3644 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper955d1e92013-04-22 04:24:02 +00003645 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3646 " an .elseif");
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003647 TheCondState.TheCond = AsmCond::ElseIfCond;
3648
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003649 bool LastIgnoreState = false;
3650 if (!TheCondStack.empty())
Craig Topper955d1e92013-04-22 04:24:02 +00003651 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003652 if (LastIgnoreState || TheCondState.CondMet) {
3653 TheCondState.Ignore = true;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003654 eatToEndOfStatement();
Craig Topperefa703d2013-04-22 04:22:40 +00003655 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003656 int64_t ExprValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003657 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003658 return true;
3659
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003660 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003661 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003662
Sean Callanan79ed1a82010-01-19 20:22:31 +00003663 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003664 TheCondState.CondMet = ExprValue;
3665 TheCondState.Ignore = !TheCondState.CondMet;
3666 }
3667
3668 return false;
3669}
3670
Jim Grosbach4a200922013-09-20 23:08:21 +00003671/// parseDirectiveElse
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003672/// ::= .else
Jim Grosbach4a200922013-09-20 23:08:21 +00003673bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003674 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003675 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003676
Sean Callanan79ed1a82010-01-19 20:22:31 +00003677 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003678
3679 if (TheCondState.TheCond != AsmCond::IfCond &&
3680 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper955d1e92013-04-22 04:24:02 +00003681 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3682 ".elseif");
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003683 TheCondState.TheCond = AsmCond::ElseCond;
3684 bool LastIgnoreState = false;
3685 if (!TheCondStack.empty())
3686 LastIgnoreState = TheCondStack.back().Ignore;
3687 if (LastIgnoreState || TheCondState.CondMet)
3688 TheCondState.Ignore = true;
3689 else
3690 TheCondState.Ignore = false;
3691
3692 return false;
3693}
3694
Jim Grosbach4a200922013-09-20 23:08:21 +00003695/// parseDirectiveEndIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003696/// ::= .endif
Jim Grosbach4a200922013-09-20 23:08:21 +00003697bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003698 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003699 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003700
Sean Callanan79ed1a82010-01-19 20:22:31 +00003701 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003702
Jim Grosbach4a200922013-09-20 23:08:21 +00003703 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003704 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3705 ".else");
3706 if (!TheCondStack.empty()) {
3707 TheCondState = TheCondStack.back();
3708 TheCondStack.pop_back();
3709 }
3710
3711 return false;
3712}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003713
Eli Bendersky6ee13082013-01-15 22:59:42 +00003714void AsmParser::initializeDirectiveKindMap() {
3715 DirectiveKindMap[".set"] = DK_SET;
3716 DirectiveKindMap[".equ"] = DK_EQU;
3717 DirectiveKindMap[".equiv"] = DK_EQUIV;
3718 DirectiveKindMap[".ascii"] = DK_ASCII;
3719 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3720 DirectiveKindMap[".string"] = DK_STRING;
3721 DirectiveKindMap[".byte"] = DK_BYTE;
3722 DirectiveKindMap[".short"] = DK_SHORT;
3723 DirectiveKindMap[".value"] = DK_VALUE;
3724 DirectiveKindMap[".2byte"] = DK_2BYTE;
3725 DirectiveKindMap[".long"] = DK_LONG;
3726 DirectiveKindMap[".int"] = DK_INT;
3727 DirectiveKindMap[".4byte"] = DK_4BYTE;
3728 DirectiveKindMap[".quad"] = DK_QUAD;
3729 DirectiveKindMap[".8byte"] = DK_8BYTE;
3730 DirectiveKindMap[".single"] = DK_SINGLE;
3731 DirectiveKindMap[".float"] = DK_FLOAT;
3732 DirectiveKindMap[".double"] = DK_DOUBLE;
3733 DirectiveKindMap[".align"] = DK_ALIGN;
3734 DirectiveKindMap[".align32"] = DK_ALIGN32;
3735 DirectiveKindMap[".balign"] = DK_BALIGN;
3736 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3737 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3738 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3739 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3740 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3741 DirectiveKindMap[".org"] = DK_ORG;
3742 DirectiveKindMap[".fill"] = DK_FILL;
3743 DirectiveKindMap[".zero"] = DK_ZERO;
3744 DirectiveKindMap[".extern"] = DK_EXTERN;
3745 DirectiveKindMap[".globl"] = DK_GLOBL;
3746 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky6ee13082013-01-15 22:59:42 +00003747 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3748 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3749 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3750 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3751 DirectiveKindMap[".reference"] = DK_REFERENCE;
3752 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3753 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3754 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3755 DirectiveKindMap[".comm"] = DK_COMM;
3756 DirectiveKindMap[".common"] = DK_COMMON;
3757 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3758 DirectiveKindMap[".abort"] = DK_ABORT;
3759 DirectiveKindMap[".include"] = DK_INCLUDE;
3760 DirectiveKindMap[".incbin"] = DK_INCBIN;
3761 DirectiveKindMap[".code16"] = DK_CODE16;
3762 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3763 DirectiveKindMap[".rept"] = DK_REPT;
3764 DirectiveKindMap[".irp"] = DK_IRP;
3765 DirectiveKindMap[".irpc"] = DK_IRPC;
3766 DirectiveKindMap[".endr"] = DK_ENDR;
3767 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3768 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3769 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3770 DirectiveKindMap[".if"] = DK_IF;
3771 DirectiveKindMap[".ifb"] = DK_IFB;
3772 DirectiveKindMap[".ifnb"] = DK_IFNB;
3773 DirectiveKindMap[".ifc"] = DK_IFC;
3774 DirectiveKindMap[".ifnc"] = DK_IFNC;
3775 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3776 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3777 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3778 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3779 DirectiveKindMap[".else"] = DK_ELSE;
3780 DirectiveKindMap[".endif"] = DK_ENDIF;
3781 DirectiveKindMap[".skip"] = DK_SKIP;
3782 DirectiveKindMap[".space"] = DK_SPACE;
3783 DirectiveKindMap[".file"] = DK_FILE;
3784 DirectiveKindMap[".line"] = DK_LINE;
3785 DirectiveKindMap[".loc"] = DK_LOC;
3786 DirectiveKindMap[".stabs"] = DK_STABS;
3787 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3788 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3789 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3790 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3791 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3792 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3793 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3794 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3795 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3796 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3797 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3798 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3799 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3800 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3801 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3802 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3803 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3804 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3805 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3806 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3807 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
3808 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3809 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3810 DirectiveKindMap[".macro"] = DK_MACRO;
3811 DirectiveKindMap[".endm"] = DK_ENDM;
3812 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3813 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00003814}
3815
Jim Grosbach4a200922013-09-20 23:08:21 +00003816MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003817 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003818
Rafael Espindola761cb062012-06-03 23:57:14 +00003819 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003820 for (;;) {
3821 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003822 if (getLexer().is(AsmToken::Eof)) {
3823 Error(DirectiveLoc, "no matching '.endr' in definition");
3824 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003825 }
3826
Rafael Espindola761cb062012-06-03 23:57:14 +00003827 if (Lexer.is(AsmToken::Identifier) &&
3828 (getTok().getIdentifier() == ".rept")) {
3829 ++NestLevel;
3830 }
3831
3832 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4a200922013-09-20 23:08:21 +00003833 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola761cb062012-06-03 23:57:14 +00003834 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003835 EndToken = getTok();
3836 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003837 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3838 TokError("unexpected token in '.endr' directive");
3839 return 0;
3840 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003841 break;
3842 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003843 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003844 }
3845
Rafael Espindola761cb062012-06-03 23:57:14 +00003846 // Otherwise, scan till the end of the statement.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003847 eatToEndOfStatement();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003848 }
3849
3850 const char *BodyStart = StartToken.getLoc().getPointer();
3851 const char *BodyEnd = EndToken.getLoc().getPointer();
3852 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3853
Rafael Espindola761cb062012-06-03 23:57:14 +00003854 // We Are Anonymous.
3855 StringRef Name;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003856 MCAsmMacroParameters Parameters;
Benjamin Kramera2b0c332013-08-04 09:06:29 +00003857 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3858 return &MacroLikeBodies.back();
Rafael Espindola761cb062012-06-03 23:57:14 +00003859}
3860
Jim Grosbach4a200922013-09-20 23:08:21 +00003861void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +00003862 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003863 OS << ".endr\n";
3864
3865 MemoryBuffer *Instantiation =
Jim Grosbach4a200922013-09-20 23:08:21 +00003866 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003867
Rafael Espindola761cb062012-06-03 23:57:14 +00003868 // Create the macro instantiation object and add to the current macro
3869 // instantiation stack.
Jim Grosbach4a200922013-09-20 23:08:21 +00003870 MacroInstantiation *MI = new MacroInstantiation(
3871 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola761cb062012-06-03 23:57:14 +00003872 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003873
Rafael Espindola761cb062012-06-03 23:57:14 +00003874 // Jump to the macro instantiation and prime the lexer.
3875 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3876 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3877 Lex();
3878}
3879
Jim Grosbach4a200922013-09-20 23:08:21 +00003880bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc) {
Rafael Espindola761cb062012-06-03 23:57:14 +00003881 int64_t Count;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003882 if (parseAbsoluteExpression(Count))
Rafael Espindola761cb062012-06-03 23:57:14 +00003883 return TokError("unexpected token in '.rept' directive");
3884
3885 if (Count < 0)
3886 return TokError("Count is negative");
3887
3888 if (Lexer.isNot(AsmToken::EndOfStatement))
3889 return TokError("unexpected token in '.rept' directive");
3890
3891 // Eat the end of statement.
3892 Lex();
3893
3894 // Lex the rept definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003895 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola761cb062012-06-03 23:57:14 +00003896 if (!M)
3897 return true;
3898
3899 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3900 // to hold the macro body with substitutions.
3901 SmallString<256> Buf;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003902 MCAsmMacroParameters Parameters;
3903 MCAsmMacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003904 raw_svector_ostream OS(Buf);
3905 while (Count--) {
3906 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3907 return true;
3908 }
Jim Grosbach4a200922013-09-20 23:08:21 +00003909 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003910
3911 return false;
3912}
3913
Jim Grosbach4a200922013-09-20 23:08:21 +00003914/// parseDirectiveIrp
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003915/// ::= .irp symbol,values
Jim Grosbach4a200922013-09-20 23:08:21 +00003916bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003917 MCAsmMacroParameters Parameters;
3918 MCAsmMacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003919
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003920 if (parseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003921 return TokError("expected identifier in '.irp' directive");
3922
3923 Parameters.push_back(Parameter);
3924
3925 if (Lexer.isNot(AsmToken::Comma))
3926 return TokError("expected comma in '.irp' directive");
3927
3928 Lex();
3929
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003930 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00003931 if (parseMacroArguments(0, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003932 return true;
3933
3934 // Eat the end of statement.
3935 Lex();
3936
3937 // Lex the irp definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003938 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003939 if (!M)
3940 return true;
3941
3942 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3943 // to hold the macro body with substitutions.
3944 SmallString<256> Buf;
3945 raw_svector_ostream OS(Buf);
3946
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003947 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3948 MCAsmMacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003949 Args.push_back(*i);
3950
3951 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3952 return true;
3953 }
3954
Jim Grosbach4a200922013-09-20 23:08:21 +00003955 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003956
3957 return false;
3958}
3959
Jim Grosbach4a200922013-09-20 23:08:21 +00003960/// parseDirectiveIrpc
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003961/// ::= .irpc symbol,values
Jim Grosbach4a200922013-09-20 23:08:21 +00003962bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003963 MCAsmMacroParameters Parameters;
3964 MCAsmMacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003965
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003966 if (parseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003967 return TokError("expected identifier in '.irpc' directive");
3968
3969 Parameters.push_back(Parameter);
3970
3971 if (Lexer.isNot(AsmToken::Comma))
3972 return TokError("expected comma in '.irpc' directive");
3973
3974 Lex();
3975
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003976 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00003977 if (parseMacroArguments(0, A))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003978 return true;
3979
3980 if (A.size() != 1 || A.front().size() != 1)
3981 return TokError("unexpected token in '.irpc' directive");
3982
3983 // Eat the end of statement.
3984 Lex();
3985
3986 // Lex the irpc definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003987 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003988 if (!M)
3989 return true;
3990
3991 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3992 // to hold the macro body with substitutions.
3993 SmallString<256> Buf;
3994 raw_svector_ostream OS(Buf);
3995
3996 StringRef Values = A.front().front().getString();
3997 std::size_t I, End = Values.size();
3998 for (I = 0; I < End; ++I) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00003999 MCAsmMacroArgument Arg;
Jim Grosbach4a200922013-09-20 23:08:21 +00004000 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004001
Eli Benderskyc0c67b02013-01-14 23:22:36 +00004002 MCAsmMacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004003 Args.push_back(Arg);
4004
4005 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4006 return true;
4007 }
4008
Jim Grosbach4a200922013-09-20 23:08:21 +00004009 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004010
4011 return false;
4012}
4013
Jim Grosbach4a200922013-09-20 23:08:21 +00004014bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola761cb062012-06-03 23:57:14 +00004015 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00004016 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004017
4018 // The only .repl that should get here are the ones created by
Jim Grosbach4a200922013-09-20 23:08:21 +00004019 // instantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004020 assert(getLexer().is(AsmToken::EndOfStatement));
4021
Jim Grosbach4a200922013-09-20 23:08:21 +00004022 handleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004023 return false;
4024}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00004025
Jim Grosbach4a200922013-09-20 23:08:21 +00004026bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer75234372013-02-15 20:37:21 +00004027 size_t Len) {
Eli Friedman2128aae2012-10-22 23:58:19 +00004028 const MCExpr *Value;
4029 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00004030 if (parseExpression(Value))
Eli Friedman2128aae2012-10-22 23:58:19 +00004031 return true;
4032 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4033 if (!MCE)
4034 return Error(ExprLoc, "unexpected expression in _emit");
4035 uint64_t IntValue = MCE->getValue();
4036 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4037 return Error(ExprLoc, "literal value out of range for directive");
4038
Chad Rosier469b1442013-02-12 21:33:51 +00004039 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4040 return false;
4041}
4042
Jim Grosbach4a200922013-09-20 23:08:21 +00004043bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosier469b1442013-02-12 21:33:51 +00004044 const MCExpr *Value;
4045 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00004046 if (parseExpression(Value))
Chad Rosier469b1442013-02-12 21:33:51 +00004047 return true;
4048 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4049 if (!MCE)
4050 return Error(ExprLoc, "unexpected expression in align");
4051 uint64_t IntValue = MCE->getValue();
4052 if (!isPowerOf2_64(IntValue))
4053 return Error(ExprLoc, "literal value not a power of two greater then zero");
4054
Jim Grosbach4a200922013-09-20 23:08:21 +00004055 Info.AsmRewrites->push_back(
4056 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman2128aae2012-10-22 23:58:19 +00004057 return false;
4058}
4059
Chad Rosier19aa3e32013-02-13 21:27:17 +00004060// We are comparing pointers, but the pointers are relative to a single string.
4061// Thus, this should always be deterministic.
Jim Grosbach4a200922013-09-20 23:08:21 +00004062static int rewritesSort(const void *A, const void *B) {
Benjamin Kramer75234372013-02-15 20:37:21 +00004063 const AsmRewrite *AsmRewriteA = static_cast<const AsmRewrite *>(A);
4064 const AsmRewrite *AsmRewriteB = static_cast<const AsmRewrite *>(B);
Chad Rosierabde6752013-02-13 18:38:58 +00004065 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4066 return -1;
4067 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4068 return 1;
Chad Rosierb54562b2013-02-15 22:54:16 +00004069
Chad Rosier6b369ce2013-04-08 17:43:47 +00004070 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4071 // rewrite to the same location. Make sure the SizeDirective rewrite is
4072 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4073 // ensures the sort algorithm is stable.
Jim Grosbach4a200922013-09-20 23:08:21 +00004074 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4075 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosierb54562b2013-02-15 22:54:16 +00004076 return -1;
Chad Rosier6b369ce2013-04-08 17:43:47 +00004077
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;
Jim Grosbach4a200922013-09-20 23:08:21 +00004081 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb1953982013-02-13 01:03:13 +00004082}
4083
Jim Grosbach4a200922013-09-20 23:08:21 +00004084bool AsmParser::parseMSInlineAsm(
4085 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4086 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4087 SmallVectorImpl<std::string> &Constraints,
4088 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4089 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004090 SmallVector<void *, 4> InputDecls;
4091 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00004092 SmallVector<bool, 4> InputDeclsAddressOf;
4093 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004094 SmallVector<std::string, 4> InputConstraints;
4095 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer75234372013-02-15 20:37:21 +00004096 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004097
Benjamin Kramer75234372013-02-15 20:37:21 +00004098 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004099
4100 // Prime the lexer.
4101 Lex();
4102
4103 // While we have input, parse each statement.
4104 unsigned InputIdx = 0;
4105 unsigned OutputIdx = 0;
4106 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00004107 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4a200922013-09-20 23:08:21 +00004108 if (parseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00004109 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004110
Chad Rosier57498012012-12-12 22:45:52 +00004111 if (Info.ParseError)
4112 return true;
4113
Benjamin Kramer75234372013-02-15 20:37:21 +00004114 if (Info.Opcode == ~0U)
4115 continue;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004116
Benjamin Kramer75234372013-02-15 20:37:21 +00004117 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004118
Benjamin Kramer75234372013-02-15 20:37:21 +00004119 // Build the list of clobbers, outputs and inputs.
4120 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4121 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004122
Benjamin Kramer75234372013-02-15 20:37:21 +00004123 // Immediate.
Chad Rosier811ddf62013-03-19 21:58:18 +00004124 if (Operand->isImm())
Benjamin Kramer75234372013-02-15 20:37:21 +00004125 continue;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004126
Benjamin Kramer75234372013-02-15 20:37:21 +00004127 // Register operand.
4128 if (Operand->isReg() && !Operand->needAddressOf()) {
4129 unsigned NumDefs = Desc.getNumDefs();
4130 // Clobber.
4131 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4132 ClobberRegs.push_back(Operand->getReg());
4133 continue;
4134 }
4135
4136 // Expr/Input or Output.
Chad Rosierb976e402013-04-09 17:53:49 +00004137 StringRef SymName = Operand->getSymName();
4138 if (SymName.empty())
4139 continue;
4140
Chad Rosier087c3092013-04-22 22:12:12 +00004141 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer75234372013-02-15 20:37:21 +00004142 if (!OpDecl)
4143 continue;
4144
4145 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierb976e402013-04-09 17:53:49 +00004146 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer75234372013-02-15 20:37:21 +00004147 if (isOutput) {
4148 ++InputIdx;
4149 OutputDecls.push_back(OpDecl);
4150 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4151 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosierb976e402013-04-09 17:53:49 +00004152 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer75234372013-02-15 20:37:21 +00004153 } else {
4154 InputDecls.push_back(OpDecl);
4155 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4156 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosierb976e402013-04-09 17:53:49 +00004157 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00004158 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00004159 }
4160 }
4161
4162 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00004163 NumOutputs = OutputDecls.size();
4164 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00004165
4166 // Set the unique clobbers.
Benjamin Kramer75234372013-02-15 20:37:21 +00004167 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4168 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4169 ClobberRegs.end());
4170 Clobbers.assign(ClobberRegs.size(), std::string());
4171 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4172 raw_string_ostream OS(Clobbers[I]);
4173 IP->printRegName(OS, ClobberRegs[I]);
4174 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00004175
4176 // Merge the various outputs and inputs. Output are expected first.
4177 if (NumOutputs || NumInputs) {
4178 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00004179 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004180 Constraints.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004181 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00004182 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier1c99a7f2013-01-15 23:07:53 +00004183 Constraints[i] = OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004184 }
4185 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00004186 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier1c99a7f2013-01-15 23:07:53 +00004187 Constraints[j] = InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004188 }
4189 }
4190
4191 // Build the IR assembly string.
4192 std::string AsmStringIR;
4193 raw_string_ostream OS(AsmStringIR);
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004194 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4195 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4a200922013-09-20 23:08:21 +00004196 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer75234372013-02-15 20:37:21 +00004197 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4198 E = AsmStrRewrites.end();
4199 I != E; ++I) {
Chad Rosierdda4b6b2013-04-12 16:26:42 +00004200 AsmRewriteKind Kind = (*I).Kind;
4201 if (Kind == AOK_Delete)
4202 continue;
4203
Chad Rosierb1f8c132012-10-18 15:49:34 +00004204 const char *Loc = (*I).Loc.getPointer();
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004205 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier96d58e62012-10-19 20:57:14 +00004206
Chad Rosier023c8802013-03-19 17:32:17 +00004207 // Emit everything up to the immediate/expression.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004208 unsigned Len = Loc - AsmStart;
Chad Rosierf06cc982013-04-11 21:49:30 +00004209 if (Len)
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004210 OS << StringRef(AsmStart, Len);
Chad Rosier96d58e62012-10-19 20:57:14 +00004211
Chad Rosier5a719fc2012-10-23 17:43:43 +00004212 // Skip the original expression.
4213 if (Kind == AOK_Skip) {
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004214 AsmStart = Loc + (*I).Len;
Chad Rosier5a719fc2012-10-23 17:43:43 +00004215 continue;
4216 }
4217
Chad Rosierdda4b6b2013-04-12 16:26:42 +00004218 unsigned AdditionalSkip = 0;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004219 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004220 switch (Kind) {
Jim Grosbach4a200922013-09-20 23:08:21 +00004221 default:
4222 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004223 case AOK_Imm:
Benjamin Kramer75234372013-02-15 20:37:21 +00004224 OS << "$$" << (*I).Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +00004225 break;
4226 case AOK_ImmPrefix:
Benjamin Kramer75234372013-02-15 20:37:21 +00004227 OS << "$$";
Chad Rosierb1f8c132012-10-18 15:49:34 +00004228 break;
4229 case AOK_Input:
Benjamin Kramer75234372013-02-15 20:37:21 +00004230 OS << '$' << InputIdx++;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004231 break;
4232 case AOK_Output:
Benjamin Kramer75234372013-02-15 20:37:21 +00004233 OS << '$' << OutputIdx++;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004234 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004235 case AOK_SizeDirective:
Benjamin Kramer75234372013-02-15 20:37:21 +00004236 switch ((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004237 default: break;
4238 case 8: OS << "byte ptr "; break;
4239 case 16: OS << "word ptr "; break;
4240 case 32: OS << "dword ptr "; break;
4241 case 64: OS << "qword ptr "; break;
4242 case 80: OS << "xword ptr "; break;
4243 case 128: OS << "xmmword ptr "; break;
4244 case 256: OS << "ymmword ptr "; break;
4245 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004246 break;
4247 case AOK_Emit:
4248 OS << ".byte";
4249 break;
Chad Rosier469b1442013-02-12 21:33:51 +00004250 case AOK_Align: {
4251 unsigned Val = (*I).Val;
4252 OS << ".align " << Val;
4253
4254 // Skip the original immediate.
Benjamin Kramer75234372013-02-15 20:37:21 +00004255 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosier469b1442013-02-12 21:33:51 +00004256 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4257 break;
4258 }
Chad Rosier6a020a72012-10-25 20:41:34 +00004259 case AOK_DotOperator:
4260 OS << (*I).Val;
4261 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004262 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004263
Chad Rosierb1f8c132012-10-18 15:49:34 +00004264 // Skip the original expression.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004265 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004266 }
4267
4268 // Emit the remainder of the asm string.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004269 if (AsmStart != AsmEnd)
4270 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004271
4272 AsmString = OS.str();
4273 return false;
4274}
4275
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004276/// \brief Create an MCAsmParser instance.
Jim Grosbach4a200922013-09-20 23:08:21 +00004277MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4278 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004279 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004280}