blob: 75f5c78b70bbe63f420b6a976a573e43f037880f [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,
Venkatraman Govindaraju83ba58e2013-09-26 14:49:40 +0000358 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky6ee13082013-01-15 22:59:42 +0000359 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);
Venkatraman Govindaraju83ba58e2013-09-26 14:49:40 +0000387 bool parseDirectiveCFIWindowSave();
Jim Grosbach4a200922013-09-20 23:08:21 +0000388 bool parseDirectiveCFISections();
389 bool parseDirectiveCFIStartProc();
390 bool parseDirectiveCFIEndProc();
391 bool parseDirectiveCFIDefCfaOffset();
392 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
393 bool parseDirectiveCFIAdjustCfaOffset();
394 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
395 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
398 bool parseDirectiveCFIRememberState();
399 bool parseDirectiveCFIRestoreState();
400 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
401 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIEscape();
403 bool parseDirectiveCFISignalFrame();
404 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000405
406 // macro directives
Jim Grosbach4a200922013-09-20 23:08:21 +0000407 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
408 bool parseDirectiveEndMacro(StringRef Directive);
409 bool parseDirectiveMacro(SMLoc DirectiveLoc);
410 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000411
Eli Bendersky4766ef42012-12-20 19:05:53 +0000412 // ".bundle_align_mode"
Jim Grosbach4a200922013-09-20 23:08:21 +0000413 bool parseDirectiveBundleAlignMode();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000414 // ".bundle_lock"
Jim Grosbach4a200922013-09-20 23:08:21 +0000415 bool parseDirectiveBundleLock();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000416 // ".bundle_unlock"
Jim Grosbach4a200922013-09-20 23:08:21 +0000417 bool parseDirectiveBundleUnlock();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000418
Eli Bendersky6ee13082013-01-15 22:59:42 +0000419 // ".space", ".skip"
Jim Grosbach4a200922013-09-20 23:08:21 +0000420 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000421
422 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4a200922013-09-20 23:08:21 +0000423 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky6ee13082013-01-15 22:59:42 +0000424
Jim Grosbach4a200922013-09-20 23:08:21 +0000425 /// \brief Parse a directive like ".globl" which
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000426 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4a200922013-09-20 23:08:21 +0000427 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000428
Jim Grosbach4a200922013-09-20 23:08:21 +0000429 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000430
Jim Grosbach4a200922013-09-20 23:08:21 +0000431 bool parseDirectiveAbort(); // ".abort"
432 bool parseDirectiveInclude(); // ".include"
433 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000434
Jim Grosbach4a200922013-09-20 23:08:21 +0000435 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000436 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4a200922013-09-20 23:08:21 +0000437 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000438 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4a200922013-09-20 23:08:21 +0000439 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000440 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4a200922013-09-20 23:08:21 +0000441 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
442 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
443 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
444 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000445 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000446
Jim Grosbach4a200922013-09-20 23:08:21 +0000447 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbarcceba832010-09-17 02:47:07 +0000448 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000449
Rafael Espindola761cb062012-06-03 23:57:14 +0000450 // Macro-like directives
Jim Grosbach4a200922013-09-20 23:08:21 +0000451 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
452 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +0000453 raw_svector_ostream &OS);
Jim Grosbach4a200922013-09-20 23:08:21 +0000454 bool parseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
455 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
456 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
457 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000458
Chad Rosier469b1442013-02-12 21:33:51 +0000459 // "_emit" or "__emit"
Jim Grosbach4a200922013-09-20 23:08:21 +0000460 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosier469b1442013-02-12 21:33:51 +0000461 size_t Len);
462
463 // "align"
Jim Grosbach4a200922013-09-20 23:08:21 +0000464 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000465
Eli Bendersky6ee13082013-01-15 22:59:42 +0000466 void initializeDirectiveKindMap();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000467};
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000468}
469
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000470namespace llvm {
471
472extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000473extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000474extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000475
476}
477
Chris Lattneraaec2052010-01-19 19:46:13 +0000478enum { DEFAULT_ADDRSPACE = 0 };
479
Jim Grosbach4a200922013-09-20 23:08:21 +0000480AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
481 const MCAsmInfo &_MAI)
482 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
483 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
484 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
485 ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000486 // Save the old handler.
487 SavedDiagHandler = SrcMgr.getDiagHandler();
488 SavedDiagContext = SrcMgr.getDiagContext();
489 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000490 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000491 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000492
Daniel Dunbare4749702010-07-12 18:12:02 +0000493 // Initialize the platform / file format parser.
494 //
495 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
496 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000497 if (_MAI.hasMicrosoftFastStdCallMangling()) {
498 PlatformParser = createCOFFAsmParser();
499 PlatformParser->Initialize(*this);
500 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000501 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000502 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000503 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000504 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000505 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000506 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000507 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000508
Eli Bendersky6ee13082013-01-15 22:59:42 +0000509 initializeDirectiveKindMap();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000510}
511
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000512AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000513 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
514
515 // Destroy any macros.
Jim Grosbach4a200922013-09-20 23:08:21 +0000516 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
517 ie = MacroMap.end();
518 it != ie; ++it)
Daniel Dunbar56491302010-07-29 01:51:55 +0000519 delete it->getValue();
520
Daniel Dunbare4749702010-07-12 18:12:02 +0000521 delete PlatformParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000522}
523
Jim Grosbach4a200922013-09-20 23:08:21 +0000524void AsmParser::printMacroInstantiations() {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000525 // Print the active macro instantiation stack.
Jim Grosbach4a200922013-09-20 23:08:21 +0000526 for (std::vector<MacroInstantiation *>::const_reverse_iterator
527 it = ActiveMacros.rbegin(),
528 ie = ActiveMacros.rend();
529 it != ie; ++it)
530 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000531 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000532}
533
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000534bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000535 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000536 return Error(L, Msg, Ranges);
Jim Grosbach4a200922013-09-20 23:08:21 +0000537 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
538 printMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000539 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000540}
541
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000542bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000543 HadError = true;
Jim Grosbach4a200922013-09-20 23:08:21 +0000544 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
545 printMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000546 return true;
547}
548
Jim Grosbach4a200922013-09-20 23:08:21 +0000549bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000550 std::string IncludedFile;
551 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000552 if (NewBuf == -1)
553 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000554
Sean Callananfd0b0282010-01-21 00:19:58 +0000555 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000556
Sean Callananfd0b0282010-01-21 00:19:58 +0000557 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000558
Sean Callananfd0b0282010-01-21 00:19:58 +0000559 return false;
560}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000561
Sylvestre Ledruda2ed452013-05-14 23:36:24 +0000562/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000563/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000564/// returns true on failure.
Jim Grosbach4a200922013-09-20 23:08:21 +0000565bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000566 std::string IncludedFile;
567 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
568 if (NewBuf == -1)
569 return true;
570
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000571 // Pick up the bytes from the file and emit them.
Rafael Espindolaa3863ea2013-07-02 15:49:13 +0000572 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000573 return false;
574}
575
Jim Grosbach4a200922013-09-20 23:08:21 +0000576void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000577 if (InBuffer != -1) {
578 CurBuffer = InBuffer;
579 } else {
580 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
581 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000582 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
583}
584
Sean Callananfd0b0282010-01-21 00:19:58 +0000585const AsmToken &AsmParser::Lex() {
586 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000587
Sean Callananfd0b0282010-01-21 00:19:58 +0000588 if (tok->is(AsmToken::Eof)) {
589 // If this is the end of an included file, pop the parent file off the
590 // include stack.
591 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
592 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4a200922013-09-20 23:08:21 +0000593 jumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000594 tok = &Lexer.Lex();
595 }
596 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000597
Sean Callananfd0b0282010-01-21 00:19:58 +0000598 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000599 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000600
Sean Callananfd0b0282010-01-21 00:19:58 +0000601 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000602}
603
Chris Lattner79180e22010-04-05 23:15:42 +0000604bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000605 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000606 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000607 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000608
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000609 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000610 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000611
612 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000613 AsmCond StartingCondState = TheCondState;
614
Kevin Enderby613b7572011-11-01 22:27:22 +0000615 // If we are generating dwarf for assembly source files save the initial text
616 // section and generate a .file directive.
617 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000618 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000619 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
620 getStreamer().EmitLabel(SectionStartSym);
621 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000622 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000623 StringRef(),
624 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000625 }
626
Chris Lattnerb717fb02009-07-02 21:53:43 +0000627 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000628 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000629 ParseStatementInfo Info;
Jim Grosbach4a200922013-09-20 23:08:21 +0000630 if (!parseStatement(Info))
631 continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000632
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000633 // We had an error, validate that one was emitted and recover by skipping to
634 // the next line.
635 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000636 eatToEndOfStatement();
Chris Lattnerb717fb02009-07-02 21:53:43 +0000637 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000638
639 if (TheCondState.TheCond != StartingCondState.TheCond ||
640 TheCondState.Ignore != StartingCondState.Ignore)
641 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000642
643 // Check to see there are no empty DwarfFile slots.
Manman Ren9e999ad2013-03-12 20:17:00 +0000644 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4a200922013-09-20 23:08:21 +0000645 getContext().getMCDwarfFiles();
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000646 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000647 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000648 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000649 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000650
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000651 // Check to see that all assembler local symbols were actually defined.
652 // Targets that don't do subsections via symbols may not want this, though,
653 // so conservatively exclude them. Only do this if we're finalizing, though,
654 // as otherwise we won't necessarilly have seen everything yet.
655 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
656 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
657 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4a200922013-09-20 23:08:21 +0000658 e = Symbols.end();
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000659 i != e; ++i) {
660 MCSymbol *Sym = i->getValue();
661 // Variable symbols may not be marked as defined, so check those
662 // explicitly. If we know it's a variable, we have a definition for
663 // the purposes of this check.
664 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
665 // FIXME: We would really like to refer back to where the symbol was
666 // first referenced for a source location. We need to add something
667 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4a200922013-09-20 23:08:21 +0000668 printMessage(
669 getLexer().getLoc(), SourceMgr::DK_Error,
670 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000671 }
672 }
673
Chris Lattner79180e22010-04-05 23:15:42 +0000674 // Finalize the output stream if there are no errors and if the client wants
675 // us to.
Jack Carter6d389f52013-10-04 22:52:31 +0000676 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000677 Out.Finish();
678
Chris Lattnerb717fb02009-07-02 21:53:43 +0000679 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000680}
681
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000682void AsmParser::checkForValidSection() {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000683 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000684 TokError("expected section directive before assembly directive");
Eli Bendersky030f63a2013-01-14 19:04:57 +0000685 Out.InitToTextSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000686 }
687}
688
Jim Grosbach4a200922013-09-20 23:08:21 +0000689/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000690void AsmParser::eatToEndOfStatement() {
Jim Grosbach4a200922013-09-20 23:08:21 +0000691 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000692 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000693
Chris Lattner2cf5f142009-06-22 01:29:09 +0000694 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000695 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000696 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000697}
698
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000699StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000700 const char *Start = getTok().getLoc().getPointer();
701
Jim Grosbach4a200922013-09-20 23:08:21 +0000702 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000703 Lex();
704
705 const char *End = getTok().getLoc().getPointer();
706 return StringRef(Start, End - Start);
707}
Chris Lattnerc4193832009-06-22 05:51:26 +0000708
Jim Grosbach4a200922013-09-20 23:08:21 +0000709StringRef AsmParser::parseStringToComma() {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000710 const char *Start = getTok().getLoc().getPointer();
711
712 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4a200922013-09-20 23:08:21 +0000713 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000714 Lex();
715
716 const char *End = getTok().getLoc().getPointer();
717 return StringRef(Start, End - Start);
718}
719
Jim Grosbach4a200922013-09-20 23:08:21 +0000720/// \brief Parse a paren expression and return it.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000721/// NOTE: This assumes the leading '(' has already been consumed.
722///
723/// parenexpr ::= expr)
724///
Jim Grosbach4a200922013-09-20 23:08:21 +0000725bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
726 if (parseExpression(Res))
727 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000728 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000729 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000730 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000731 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000732 return false;
733}
Chris Lattnerc4193832009-06-22 05:51:26 +0000734
Jim Grosbach4a200922013-09-20 23:08:21 +0000735/// \brief Parse a bracket expression and return it.
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000736/// NOTE: This assumes the leading '[' has already been consumed.
737///
738/// bracketexpr ::= expr]
739///
Jim Grosbach4a200922013-09-20 23:08:21 +0000740bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
741 if (parseExpression(Res))
742 return true;
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000743 if (Lexer.isNot(AsmToken::RBrac))
744 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000745 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000746 Lex();
747 return false;
748}
749
Jim Grosbach4a200922013-09-20 23:08:21 +0000750/// \brief Parse a primary expression and return it.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000751/// primaryexpr ::= (parenexpr
752/// primaryexpr ::= symbol
753/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000754/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000755/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4a200922013-09-20 23:08:21 +0000756bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby5de048e2013-01-22 21:09:20 +0000757 SMLoc FirstTokenLoc = getLexer().getLoc();
758 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
759 switch (FirstTokenKind) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000760 default:
761 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000762 // If we have an error assume that we've already handled it.
763 case AsmToken::Error:
764 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000765 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000766 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000767 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000768 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000769 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000770 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000771 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000772 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000773 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000774 StringRef Identifier;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000775 if (parseIdentifier(Identifier)) {
David Majnemer3f22cc12013-09-25 10:47:21 +0000776 if (FirstTokenKind == AsmToken::Dollar) {
777 if (Lexer.getMAI().getDollarIsPC()) {
778 // This is a '$' reference, which references the current PC. Emit a
779 // temporary label to the streamer and refer to it.
780 MCSymbol *Sym = Ctx.CreateTempSymbol();
781 Out.EmitLabel(Sym);
Jack Carter8e48edc2013-10-04 21:26:15 +0000782 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
783 getContext());
David Majnemer3f22cc12013-09-25 10:47:21 +0000784 EndLoc = FirstTokenLoc;
785 return false;
786 } else
787 return Error(FirstTokenLoc, "invalid token in expression");
788 return true;
789 }
Kevin Enderby5de048e2013-01-22 21:09:20 +0000790 }
Daniel Dunbare17edff2010-08-24 19:13:42 +0000791
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000792 EndLoc = SMLoc::getFromPointer(Identifier.end());
793
Daniel Dunbarfffff912009-10-16 01:34:54 +0000794 // This is a symbol reference.
Hans Wennborgb74b88e2013-10-17 01:13:02 +0000795 StringRef SymbolName = Identifier;
NAKAMURA Takumi32c24da2013-10-16 08:22:49 +0000796 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborgb74b88e2013-10-17 01:13:02 +0000797 std::pair<StringRef, StringRef> Split = Identifier.split('@');
798
799 if (Split.first.size() != Identifier.size() &&
800 FirstTokenKind != AsmToken::String) {
801 SymbolName = Split.first;
802 StringRef VariantName = Split.second;
803
804 // Lookup the symbol variant.
805 Variant = MCSymbolRefExpr::getVariantKindForName(VariantName);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000806 if (Variant == MCSymbolRefExpr::VK_Invalid) {
807 Variant = MCSymbolRefExpr::VK_None;
Hans Wennborgb74b88e2013-10-17 01:13:02 +0000808 return TokError("invalid variant '" + VariantName + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000809 }
810 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000811
Hans Wennborgb74b88e2013-10-17 01:13:02 +0000812 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
813
Daniel Dunbarfffff912009-10-16 01:34:54 +0000814 // If this is an absolute variable reference, substitute it now to preserve
815 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000816 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000817 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000818 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000819
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000820 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000821 return false;
822 }
823
824 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000825 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000826 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000827 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000828 case AsmToken::Integer: {
829 SMLoc Loc = getTok().getLoc();
830 int64_t IntVal = getTok().getIntVal();
831 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000832 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000833 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000834 // Look for 'b' or 'f' following an Integer as a directional label
835 if (Lexer.getKind() == AsmToken::Identifier) {
836 StringRef IDVal = getTok().getString();
Ulrich Weigand151ad372013-06-20 16:24:17 +0000837 // Lookup the symbol variant if used.
838 std::pair<StringRef, StringRef> Split = IDVal.split('@');
839 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
840 if (Split.first.size() != IDVal.size()) {
841 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
842 if (Variant == MCSymbolRefExpr::VK_Invalid) {
843 Variant = MCSymbolRefExpr::VK_None;
844 return TokError("invalid variant '" + Split.second + "'");
845 }
Vladimir Medic7b0a7962013-08-20 13:33:18 +0000846 IDVal = Split.first;
Ulrich Weigand151ad372013-06-20 16:24:17 +0000847 }
Jim Grosbach4a200922013-09-20 23:08:21 +0000848 if (IDVal == "f" || IDVal == "b") {
849 MCSymbol *Sym =
850 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigand151ad372013-06-20 16:24:17 +0000851 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000852 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000853 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000854 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000855 Lex(); // Eat identifier.
856 }
857 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000858 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000859 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000860 case AsmToken::Real: {
861 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000862 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000863 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000864 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000865 Lex(); // Eat token.
866 return false;
867 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000868 case AsmToken::Dot: {
869 // This is a '.' reference, which references the current PC. Emit a
870 // temporary label to the streamer and refer to it.
871 MCSymbol *Sym = Ctx.CreateTempSymbol();
872 Out.EmitLabel(Sym);
873 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000874 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000875 Lex(); // Eat identifier.
876 return false;
877 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000878 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000879 Lex(); // Eat the '('.
Jim Grosbach4a200922013-09-20 23:08:21 +0000880 return parseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000881 case AsmToken::LBrac:
882 if (!PlatformParser->HasBracketExpressions())
883 return TokError("brackets expression not supported on this target");
884 Lex(); // Eat the '['.
Jim Grosbach4a200922013-09-20 23:08:21 +0000885 return parseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000886 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000887 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000888 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000889 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000890 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000891 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000892 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000893 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000894 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000895 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000896 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000897 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000898 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000899 Lex(); // Eat the operator.
Jim Grosbach4a200922013-09-20 23:08:21 +0000900 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000901 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000902 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000903 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000904 }
905}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000906
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000907bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000908 SMLoc EndLoc;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000909 return parseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000910}
911
Daniel Dunbarcceba832010-09-17 02:47:07 +0000912const MCExpr *
Jim Grosbach4a200922013-09-20 23:08:21 +0000913AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbarcceba832010-09-17 02:47:07 +0000914 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenberger66b71392013-08-27 20:23:19 +0000915 // Ask the target implementation about this expression first.
916 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
917 if (NewE)
918 return NewE;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000919 // Recurse over the given expression, rebuilding it to apply the given variant
920 // if there is exactly one symbol.
921 switch (E->getKind()) {
922 case MCExpr::Target:
923 case MCExpr::Constant:
924 return 0;
925
926 case MCExpr::SymbolRef: {
927 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
928
929 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4a200922013-09-20 23:08:21 +0000930 TokError("invalid variant on expression '" + getTok().getIdentifier() +
931 "' (already modified)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000932 return E;
933 }
934
935 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
936 }
937
938 case MCExpr::Unary: {
939 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4a200922013-09-20 23:08:21 +0000940 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000941 if (!Sub)
942 return 0;
943 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
944 }
945
946 case MCExpr::Binary: {
947 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4a200922013-09-20 23:08:21 +0000948 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
949 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000950
951 if (!LHS && !RHS)
952 return 0;
953
Jim Grosbach4a200922013-09-20 23:08:21 +0000954 if (!LHS)
955 LHS = BE->getLHS();
956 if (!RHS)
957 RHS = BE->getRHS();
Daniel Dunbarcceba832010-09-17 02:47:07 +0000958
959 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
960 }
961 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000962
Craig Topper85814382012-02-07 05:05:23 +0000963 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000964}
965
Jim Grosbach4a200922013-09-20 23:08:21 +0000966/// \brief Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000967///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000968/// expr ::= expr &&,|| expr -> lowest.
969/// expr ::= expr |,^,&,! expr
970/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
971/// expr ::= expr <<,>> expr
972/// expr ::= expr +,- expr
973/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000974/// expr ::= primaryexpr
975///
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000976bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000977 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000978 Res = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +0000979 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000980 return true;
981
Daniel Dunbarcceba832010-09-17 02:47:07 +0000982 // As a special case, we support 'a op b @ modifier' by rewriting the
983 // expression to include the modifier. This is inefficient, but in general we
984 // expect users to use 'a@modifier op b'.
985 if (Lexer.getKind() == AsmToken::At) {
986 Lex();
987
988 if (Lexer.isNot(AsmToken::Identifier))
989 return TokError("unexpected symbol modifier following '@'");
990
991 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4a200922013-09-20 23:08:21 +0000992 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbarcceba832010-09-17 02:47:07 +0000993 if (Variant == MCSymbolRefExpr::VK_Invalid)
994 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
995
Jim Grosbach4a200922013-09-20 23:08:21 +0000996 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000997 if (!ModifiedRes) {
998 return TokError("invalid modifier '" + getTok().getIdentifier() +
999 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +00001000 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001001
Daniel Dunbarcceba832010-09-17 02:47:07 +00001002 Res = ModifiedRes;
1003 Lex();
1004 }
1005
Daniel Dunbare9a60eb2010-02-13 01:28:07 +00001006 // Try to constant fold it up front, if possible.
1007 int64_t Value;
1008 if (Res->EvaluateAsAbsolute(Value))
1009 Res = MCConstantExpr::Create(Value, getContext());
1010
1011 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +00001012}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001013
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001014bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +00001015 Res = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00001016 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +00001017}
1018
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001019bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001020 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001021
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001022 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001023 if (parseExpression(Expr))
Daniel Dunbar475839e2009-06-29 20:37:27 +00001024 return true;
1025
Daniel Dunbare00b0112009-10-16 01:57:52 +00001026 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001027 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001028
1029 return false;
1030}
1031
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001032static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001033 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001034 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001035 default:
Jim Grosbach4a200922013-09-20 23:08:21 +00001036 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001037
Jim Grosbach4a200922013-09-20 23:08:21 +00001038 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001039 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001040 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001041 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001042 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001043 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001044 return 1;
1045
Jim Grosbach4a200922013-09-20 23:08:21 +00001046 // Low Precedence: |, &, ^
1047 //
1048 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001050 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001051 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001052 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001053 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001054 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001055 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001056 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001057 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001058
Jim Grosbach4a200922013-09-20 23:08:21 +00001059 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001060 case AsmToken::EqualEqual:
1061 Kind = MCBinaryExpr::EQ;
1062 return 3;
1063 case AsmToken::ExclaimEqual:
1064 case AsmToken::LessGreater:
1065 Kind = MCBinaryExpr::NE;
1066 return 3;
1067 case AsmToken::Less:
1068 Kind = MCBinaryExpr::LT;
1069 return 3;
1070 case AsmToken::LessEqual:
1071 Kind = MCBinaryExpr::LTE;
1072 return 3;
1073 case AsmToken::Greater:
1074 Kind = MCBinaryExpr::GT;
1075 return 3;
1076 case AsmToken::GreaterEqual:
1077 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001078 return 3;
1079
Jim Grosbach4a200922013-09-20 23:08:21 +00001080 // Intermediate Precedence: <<, >>
Jim Grosbachfbe16812011-08-20 16:24:13 +00001081 case AsmToken::LessLess:
1082 Kind = MCBinaryExpr::Shl;
1083 return 4;
1084 case AsmToken::GreaterGreater:
1085 Kind = MCBinaryExpr::Shr;
1086 return 4;
1087
Jim Grosbach4a200922013-09-20 23:08:21 +00001088 // High Intermediate Precedence: +, -
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001089 case AsmToken::Plus:
1090 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001091 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001092 case AsmToken::Minus:
1093 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001094 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001095
Jim Grosbach4a200922013-09-20 23:08:21 +00001096 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001097 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001098 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001099 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001100 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001101 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001102 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001103 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001104 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001105 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001106 }
1107}
1108
Jim Grosbach4a200922013-09-20 23:08:21 +00001109/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001110/// Res contains the LHS of the expression on input.
Jim Grosbach4a200922013-09-20 23:08:21 +00001111bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattnerb4307b32010-01-15 19:28:38 +00001112 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001113 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001114 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001115 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001116
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001117 // If the next token is lower precedence than we are allowed to eat, return
1118 // successfully with what we ate already.
1119 if (TokPrec < Precedence)
1120 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001121
Sean Callanan79ed1a82010-01-19 20:22:31 +00001122 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001123
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001124 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001125 const MCExpr *RHS;
Jim Grosbach4a200922013-09-20 23:08:21 +00001126 if (parsePrimaryExpr(RHS, EndLoc))
1127 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001128
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001129 // If BinOp binds less tightly with RHS than the operator after RHS, let
1130 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001131 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001132 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4a200922013-09-20 23:08:21 +00001133 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1134 return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001135
Daniel Dunbar475839e2009-06-29 20:37:27 +00001136 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001137 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001138 }
1139}
1140
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001141/// ParseStatement:
1142/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001143/// ::= Label* Directive ...Operands... EndOfStatement
1144/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4a200922013-09-20 23:08:21 +00001145bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001146 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001147 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001148 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001149 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001150 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001151
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001152 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001153 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001154 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001155 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001156 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001157 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001158 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4a200922013-09-20 23:08:21 +00001159 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001160
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001161 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001162 if (Lexer.is(AsmToken::Integer)) {
1163 LocalLabelVal = getTok().getIntVal();
1164 if (LocalLabelVal < 0) {
1165 if (!TheCondState.Ignore)
1166 return TokError("unexpected token at start of statement");
1167 IDVal = "";
Eli Benderskyed5df012013-01-16 19:32:36 +00001168 } else {
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001169 IDVal = getTok().getString();
1170 Lex(); // Consume the integer token to be used as an identifier token.
1171 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001172 if (!TheCondState.Ignore)
1173 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001174 }
1175 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001176 } else if (Lexer.is(AsmToken::Dot)) {
1177 // Treat '.' as a valid identifier in this context.
1178 Lex();
1179 IDVal = ".";
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001180 } else if (parseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001181 if (!TheCondState.Ignore)
1182 return TokError("unexpected token at start of statement");
1183 IDVal = "";
1184 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001185
Chris Lattner7834fac2010-04-17 18:14:27 +00001186 // Handle conditional assembly here before checking for skipping. We
1187 // have to do this so that .endif isn't skipped in a ".if 0" block for
1188 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001189 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4a200922013-09-20 23:08:21 +00001190 DirectiveKindMap.find(IDVal);
1191 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1192 ? DK_NO_DIRECTIVE
1193 : DirKindIt->getValue();
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001194 switch (DirKind) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001195 default:
1196 break;
1197 case DK_IF:
1198 return parseDirectiveIf(IDLoc);
1199 case DK_IFB:
1200 return parseDirectiveIfb(IDLoc, true);
1201 case DK_IFNB:
1202 return parseDirectiveIfb(IDLoc, false);
1203 case DK_IFC:
1204 return parseDirectiveIfc(IDLoc, true);
1205 case DK_IFNC:
1206 return parseDirectiveIfc(IDLoc, false);
1207 case DK_IFDEF:
1208 return parseDirectiveIfdef(IDLoc, true);
1209 case DK_IFNDEF:
1210 case DK_IFNOTDEF:
1211 return parseDirectiveIfdef(IDLoc, false);
1212 case DK_ELSEIF:
1213 return parseDirectiveElseIf(IDLoc);
1214 case DK_ELSE:
1215 return parseDirectiveElse(IDLoc);
1216 case DK_ENDIF:
1217 return parseDirectiveEndIf(IDLoc);
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001218 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001219
Eli Benderskyed5df012013-01-16 19:32:36 +00001220 // Ignore the statement if in the middle of inactive conditional
1221 // (e.g. ".if 0").
Chad Rosier17feeec2012-10-20 00:47:08 +00001222 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001223 eatToEndOfStatement();
Chris Lattner7834fac2010-04-17 18:14:27 +00001224 return false;
1225 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001226
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001227 // FIXME: Recurse on local labels?
1228
1229 // See what kind of statement we have.
1230 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001231 case AsmToken::Colon: {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001232 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001233
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001234 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001235 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001236
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001237 // Diagnose attempt to use '.' as a label.
1238 if (IDVal == ".")
1239 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1240
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001241 // Diagnose attempt to use a variable as a label.
1242 //
1243 // FIXME: Diagnostics. Note the location of the definition as a label.
1244 // FIXME: This doesn't diagnose assignment to a symbol which has been
1245 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001246 MCSymbol *Sym;
1247 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001248 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001249 else
1250 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001251 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001252 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001253
Daniel Dunbar959fd882009-08-26 22:13:22 +00001254 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001255 if (!ParsingInlineAsm)
1256 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001257
Kevin Enderby94c2e852011-12-09 18:09:40 +00001258 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001259 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001260 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001261 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1262 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001263
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001264 // Consume any end of statement token, if present, to avoid spurious
1265 // AddBlankLine calls().
1266 if (Lexer.is(AsmToken::EndOfStatement)) {
1267 Lex();
1268 if (Lexer.is(AsmToken::Eof))
1269 return false;
1270 }
1271
Eli Friedman2128aae2012-10-22 23:58:19 +00001272 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001273 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001274
Daniel Dunbar3f872332009-07-28 16:08:33 +00001275 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001276 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001277 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001278
Jim Grosbach4a200922013-09-20 23:08:21 +00001279 return parseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001280
1281 default: // Normal instruction or directive.
1282 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001283 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001284
1285 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4a200922013-09-20 23:08:21 +00001286 if (areMacrosEnabled())
1287 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1288 return handleMacroEntry(M, IDLoc);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001289 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001290
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001291 // Otherwise, we have a normal instruction or directive.
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001292
Eli Bendersky6ee13082013-01-15 22:59:42 +00001293 // Directives start with "."
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001294 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky6ee13082013-01-15 22:59:42 +00001295 // There are several entities interested in parsing directives:
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001296 //
Eli Bendersky6ee13082013-01-15 22:59:42 +00001297 // 1. The target-specific assembly parser. Some directives are target
1298 // specific or may potentially behave differently on certain targets.
1299 // 2. Asm parser extensions. For example, platform-specific parsers
1300 // (like the ELF parser) register themselves as extensions.
1301 // 3. The generic directive parser implemented by this class. These are
1302 // all the directives that behave in a target and platform independent
1303 // manner, or at least have a default behavior that's shared between
1304 // all targets and platforms.
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001305
Eli Bendersky6ee13082013-01-15 22:59:42 +00001306 // First query the target-specific parser. It will return 'true' if it
1307 // isn't interested in this directive.
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001308 if (!getTargetParser().ParseDirective(ID))
1309 return false;
1310
Eli Bendersky6ee13082013-01-15 22:59:42 +00001311 // Next, check the extention directive map to see if any extension has
1312 // registered itself to parse this directive.
Jim Grosbach4a200922013-09-20 23:08:21 +00001313 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1314 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky6ee13082013-01-15 22:59:42 +00001315 if (Handler.first)
1316 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1317
1318 // Finally, if no one else is interested in this directive, it must be
1319 // generic and familiar to this class.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001320 switch (DirKind) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001321 default:
1322 break;
1323 case DK_SET:
1324 case DK_EQU:
1325 return parseDirectiveSet(IDVal, true);
1326 case DK_EQUIV:
1327 return parseDirectiveSet(IDVal, false);
1328 case DK_ASCII:
1329 return parseDirectiveAscii(IDVal, false);
1330 case DK_ASCIZ:
1331 case DK_STRING:
1332 return parseDirectiveAscii(IDVal, true);
1333 case DK_BYTE:
1334 return parseDirectiveValue(1);
1335 case DK_SHORT:
1336 case DK_VALUE:
1337 case DK_2BYTE:
1338 return parseDirectiveValue(2);
1339 case DK_LONG:
1340 case DK_INT:
1341 case DK_4BYTE:
1342 return parseDirectiveValue(4);
1343 case DK_QUAD:
1344 case DK_8BYTE:
1345 return parseDirectiveValue(8);
1346 case DK_SINGLE:
1347 case DK_FLOAT:
1348 return parseDirectiveRealValue(APFloat::IEEEsingle);
1349 case DK_DOUBLE:
1350 return parseDirectiveRealValue(APFloat::IEEEdouble);
1351 case DK_ALIGN: {
1352 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1353 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1354 }
1355 case DK_ALIGN32: {
1356 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1357 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1358 }
1359 case DK_BALIGN:
1360 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1361 case DK_BALIGNW:
1362 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1363 case DK_BALIGNL:
1364 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1365 case DK_P2ALIGN:
1366 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1367 case DK_P2ALIGNW:
1368 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1369 case DK_P2ALIGNL:
1370 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1371 case DK_ORG:
1372 return parseDirectiveOrg();
1373 case DK_FILL:
1374 return parseDirectiveFill();
1375 case DK_ZERO:
1376 return parseDirectiveZero();
1377 case DK_EXTERN:
1378 eatToEndOfStatement(); // .extern is the default, ignore it.
1379 return false;
1380 case DK_GLOBL:
1381 case DK_GLOBAL:
1382 return parseDirectiveSymbolAttribute(MCSA_Global);
1383 case DK_LAZY_REFERENCE:
1384 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1385 case DK_NO_DEAD_STRIP:
1386 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1387 case DK_SYMBOL_RESOLVER:
1388 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1389 case DK_PRIVATE_EXTERN:
1390 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1391 case DK_REFERENCE:
1392 return parseDirectiveSymbolAttribute(MCSA_Reference);
1393 case DK_WEAK_DEFINITION:
1394 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1395 case DK_WEAK_REFERENCE:
1396 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1397 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1398 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1399 case DK_COMM:
1400 case DK_COMMON:
1401 return parseDirectiveComm(/*IsLocal=*/false);
1402 case DK_LCOMM:
1403 return parseDirectiveComm(/*IsLocal=*/true);
1404 case DK_ABORT:
1405 return parseDirectiveAbort();
1406 case DK_INCLUDE:
1407 return parseDirectiveInclude();
1408 case DK_INCBIN:
1409 return parseDirectiveIncbin();
1410 case DK_CODE16:
1411 case DK_CODE16GCC:
1412 return TokError(Twine(IDVal) + " not supported yet");
1413 case DK_REPT:
1414 return parseDirectiveRept(IDLoc);
1415 case DK_IRP:
1416 return parseDirectiveIrp(IDLoc);
1417 case DK_IRPC:
1418 return parseDirectiveIrpc(IDLoc);
1419 case DK_ENDR:
1420 return parseDirectiveEndr(IDLoc);
1421 case DK_BUNDLE_ALIGN_MODE:
1422 return parseDirectiveBundleAlignMode();
1423 case DK_BUNDLE_LOCK:
1424 return parseDirectiveBundleLock();
1425 case DK_BUNDLE_UNLOCK:
1426 return parseDirectiveBundleUnlock();
1427 case DK_SLEB128:
1428 return parseDirectiveLEB128(true);
1429 case DK_ULEB128:
1430 return parseDirectiveLEB128(false);
1431 case DK_SPACE:
1432 case DK_SKIP:
1433 return parseDirectiveSpace(IDVal);
1434 case DK_FILE:
1435 return parseDirectiveFile(IDLoc);
1436 case DK_LINE:
1437 return parseDirectiveLine();
1438 case DK_LOC:
1439 return parseDirectiveLoc();
1440 case DK_STABS:
1441 return parseDirectiveStabs();
1442 case DK_CFI_SECTIONS:
1443 return parseDirectiveCFISections();
1444 case DK_CFI_STARTPROC:
1445 return parseDirectiveCFIStartProc();
1446 case DK_CFI_ENDPROC:
1447 return parseDirectiveCFIEndProc();
1448 case DK_CFI_DEF_CFA:
1449 return parseDirectiveCFIDefCfa(IDLoc);
1450 case DK_CFI_DEF_CFA_OFFSET:
1451 return parseDirectiveCFIDefCfaOffset();
1452 case DK_CFI_ADJUST_CFA_OFFSET:
1453 return parseDirectiveCFIAdjustCfaOffset();
1454 case DK_CFI_DEF_CFA_REGISTER:
1455 return parseDirectiveCFIDefCfaRegister(IDLoc);
1456 case DK_CFI_OFFSET:
1457 return parseDirectiveCFIOffset(IDLoc);
1458 case DK_CFI_REL_OFFSET:
1459 return parseDirectiveCFIRelOffset(IDLoc);
1460 case DK_CFI_PERSONALITY:
1461 return parseDirectiveCFIPersonalityOrLsda(true);
1462 case DK_CFI_LSDA:
1463 return parseDirectiveCFIPersonalityOrLsda(false);
1464 case DK_CFI_REMEMBER_STATE:
1465 return parseDirectiveCFIRememberState();
1466 case DK_CFI_RESTORE_STATE:
1467 return parseDirectiveCFIRestoreState();
1468 case DK_CFI_SAME_VALUE:
1469 return parseDirectiveCFISameValue(IDLoc);
1470 case DK_CFI_RESTORE:
1471 return parseDirectiveCFIRestore(IDLoc);
1472 case DK_CFI_ESCAPE:
1473 return parseDirectiveCFIEscape();
1474 case DK_CFI_SIGNAL_FRAME:
1475 return parseDirectiveCFISignalFrame();
1476 case DK_CFI_UNDEFINED:
1477 return parseDirectiveCFIUndefined(IDLoc);
1478 case DK_CFI_REGISTER:
1479 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju83ba58e2013-09-26 14:49:40 +00001480 case DK_CFI_WINDOW_SAVE:
1481 return parseDirectiveCFIWindowSave();
Jim Grosbach4a200922013-09-20 23:08:21 +00001482 case DK_MACROS_ON:
1483 case DK_MACROS_OFF:
1484 return parseDirectiveMacrosOnOff(IDVal);
1485 case DK_MACRO:
1486 return parseDirectiveMacro(IDLoc);
1487 case DK_ENDM:
1488 case DK_ENDMACRO:
1489 return parseDirectiveEndMacro(IDVal);
1490 case DK_PURGEM:
1491 return parseDirectivePurgeMacro(IDLoc);
Eli Friedman5d68ec22010-07-19 04:17:25 +00001492 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001493
Jim Grosbach686c0182012-05-01 18:38:27 +00001494 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001495 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001496
Chad Rosier469b1442013-02-12 21:33:51 +00001497 // __asm _emit or __asm __emit
1498 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1499 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4a200922013-09-20 23:08:21 +00001500 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosier469b1442013-02-12 21:33:51 +00001501
1502 // __asm align
1503 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4a200922013-09-20 23:08:21 +00001504 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman2128aae2012-10-22 23:58:19 +00001505
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001506 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001507
Chris Lattnera7f13542010-05-19 23:34:33 +00001508 // Canonicalize the opcode to lower case.
Eli Benderskyed5df012013-01-16 19:32:36 +00001509 std::string OpcodeStr = IDVal.lower();
Chad Rosier6a020a72012-10-25 20:41:34 +00001510 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4a200922013-09-20 23:08:21 +00001511 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001512 Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001513 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001514
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001515 // Dump the parsed representation, if requested.
1516 if (getShowParsedOperands()) {
1517 SmallString<256> Str;
1518 raw_svector_ostream OS(Str);
1519 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001520 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001521 if (i != 0)
1522 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001523 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001524 }
1525 OS << "]";
1526
Jim Grosbach4a200922013-09-20 23:08:21 +00001527 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001528 }
1529
Kevin Enderby613b7572011-11-01 22:27:22 +00001530 // If we are generating dwarf for assembly source files and the current
1531 // section is the initial text section then generate a .loc directive for
1532 // the instruction.
1533 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbournedf39be62013-04-17 21:18:16 +00001534 getContext().getGenDwarfSection() ==
Jim Grosbach4a200922013-09-20 23:08:21 +00001535 getStreamer().getCurrentSection().first) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001536
Eli Benderskyed5df012013-01-16 19:32:36 +00001537 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby938482f2012-11-01 17:31:35 +00001538
Eli Benderskyed5df012013-01-16 19:32:36 +00001539 // If we previously parsed a cpp hash file line comment then make sure the
1540 // current Dwarf File is for the CppHashFilename if not then emit the
1541 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001542 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4a200922013-09-20 23:08:21 +00001543 getContext().getMCDwarfFiles();
Eli Benderskyed5df012013-01-16 19:32:36 +00001544 if (CppHashFilename.size() != 0) {
1545 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby938482f2012-11-01 17:31:35 +00001546 CppHashFilename)
Eli Benderskyed5df012013-01-16 19:32:36 +00001547 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4a200922013-09-20 23:08:21 +00001548 getContext().nextGenDwarfFileNumber(), StringRef(),
1549 CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001550
Jim Grosbach4a200922013-09-20 23:08:21 +00001551 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1552 // cache with the different Loc from the call above we save the last
1553 // info we queried here with SrcMgr.FindLineNumber().
1554 unsigned CppHashLocLineNo;
1555 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1556 CppHashLocLineNo = LastQueryLine;
1557 else {
1558 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1559 LastQueryLine = CppHashLocLineNo;
1560 LastQueryIDLoc = CppHashLoc;
1561 LastQueryBuffer = CppHashBuf;
1562 }
1563 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Benderskyed5df012013-01-16 19:32:36 +00001564 }
Kevin Enderby938482f2012-11-01 17:31:35 +00001565
Jim Grosbach4a200922013-09-20 23:08:21 +00001566 getStreamer().EmitDwarfLocDirective(
1567 getContext().getGenDwarfFileNumber(), Line, 0,
1568 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1569 StringRef());
Kevin Enderby613b7572011-11-01 22:27:22 +00001570 }
1571
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001572 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001573 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001574 unsigned ErrorInfo;
Jim Grosbach4a200922013-09-20 23:08:21 +00001575 HadError = getTargetParser().MatchAndEmitInstruction(
1576 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1577 ParsingInlineAsm);
Chad Rosier84125ca2012-10-13 00:26:04 +00001578 }
Chris Lattner98986712010-01-14 22:21:20 +00001579
Chris Lattnercbf8a982010-09-11 16:18:25 +00001580 // Don't skip the rest of the line, the instruction parser is responsible for
1581 // that.
1582 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001583}
Chris Lattner9a023f72009-06-24 04:43:34 +00001584
Jim Grosbach4a200922013-09-20 23:08:21 +00001585/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001586/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4a200922013-09-20 23:08:21 +00001587void AsmParser::eatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001588 if (!Lexer.is(AsmToken::EndOfStatement))
1589 Lexer.LexUntilEndOfLine();
Jim Grosbach4a200922013-09-20 23:08:21 +00001590 // Eat EOL.
1591 Lex();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001592}
1593
Jim Grosbach4a200922013-09-20 23:08:21 +00001594/// parseCppHashLineFilenameComment as this:
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001595/// ::= # number "filename"
1596/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4a200922013-09-20 23:08:21 +00001597bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001598 Lex(); // Eat the hash token.
1599
1600 if (getLexer().isNot(AsmToken::Integer)) {
1601 // Consume the line since in cases it is not a well-formed line directive,
1602 // as if were simply a full line comment.
Jim Grosbach4a200922013-09-20 23:08:21 +00001603 eatToEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001604 return false;
1605 }
1606
1607 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001608 Lex();
1609
1610 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001611 eatToEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001612 return false;
1613 }
1614
1615 StringRef Filename = getTok().getString();
1616 // Get rid of the enclosing quotes.
Jim Grosbach4a200922013-09-20 23:08:21 +00001617 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001618
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001619 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1620 CppHashLoc = L;
1621 CppHashFilename = Filename;
1622 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001623 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001624
1625 // Ignore any trailing characters, they're just comment.
Jim Grosbach4a200922013-09-20 23:08:21 +00001626 eatToEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001627 return false;
1628}
1629
Jim Grosbach4a200922013-09-20 23:08:21 +00001630/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001631/// for the Filename and LineNo if any in the diagnostic.
1632void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001633 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001634 raw_ostream &OS = errs();
1635
1636 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1637 const SMLoc &DiagLoc = Diag.getLoc();
1638 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1639 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1640
Jim Grosbach4a200922013-09-20 23:08:21 +00001641 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001642 // before printing the message.
1643 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001644 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001645 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1646 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001647 }
1648
Eric Christopher2318ba12012-12-18 00:30:54 +00001649 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001650 // manager changed or buffer changed (like in a nested include) then just
1651 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4a200922013-09-20 23:08:21 +00001652 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001653 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001654 if (Parser->SavedDiagHandler)
1655 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1656 else
1657 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001658 return;
1659 }
1660
Eric Christopher2318ba12012-12-18 00:30:54 +00001661 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001662 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1663 // the diagnostic.
Jakub Staszakb06ea252013-09-16 22:03:38 +00001664 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001665
1666 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1667 int CppHashLocLineNo =
1668 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4a200922013-09-20 23:08:21 +00001669 int LineNo =
1670 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001671
Jim Grosbach4a200922013-09-20 23:08:21 +00001672 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1673 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001674 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001675
Benjamin Kramer04a04262011-10-16 10:48:29 +00001676 if (Parser->SavedDiagHandler)
1677 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1678 else
1679 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001680}
1681
Rafael Espindola799aacf2012-08-21 18:29:30 +00001682// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1683// difference being that that function accepts '@' as part of identifiers and
1684// we can't do that. AsmLexer.cpp should probably be changed to handle
1685// '@' as a special case when needed.
1686static bool isIdentifierChar(char c) {
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001687 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1688 c == '.';
Rafael Espindola799aacf2012-08-21 18:29:30 +00001689}
1690
Rafael Espindola761cb062012-06-03 23:57:14 +00001691bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001692 const MCAsmMacroParameters &Parameters,
Jim Grosbach4a200922013-09-20 23:08:21 +00001693 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001694 unsigned NParameters = Parameters.size();
1695 if (NParameters != 0 && NParameters != A.size())
1696 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001697
Preston Gurd7b6f2032012-09-19 20:36:12 +00001698 // A macro without parameters is handled differently on Darwin:
1699 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001700 while (!Body.empty()) {
1701 // Scan for the next substitution.
1702 std::size_t End = Body.size(), Pos = 0;
1703 for (; Pos != End; ++Pos) {
1704 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001705 if (!NParameters) {
1706 // This macro has no parameters, look for $0, $1, etc.
1707 if (Body[Pos] != '$' || Pos + 1 == End)
1708 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001709
Rafael Espindola65366442011-06-05 02:43:45 +00001710 char Next = Body[Pos + 1];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00001711 if (Next == '$' || Next == 'n' ||
1712 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola65366442011-06-05 02:43:45 +00001713 break;
1714 } else {
1715 // This macro has parameters, look for \foo, \bar, etc.
1716 if (Body[Pos] == '\\' && Pos + 1 != End)
1717 break;
1718 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001719 }
1720
1721 // Add the prefix.
1722 OS << Body.slice(0, Pos);
1723
1724 // Check if we reached the end.
1725 if (Pos == End)
1726 break;
1727
Rafael Espindola65366442011-06-05 02:43:45 +00001728 if (!NParameters) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001729 switch (Body[Pos + 1]) {
1730 // $$ => $
Rafael Espindola65366442011-06-05 02:43:45 +00001731 case '$':
1732 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001733 break;
1734
Jim Grosbach4a200922013-09-20 23:08:21 +00001735 // $n => number of arguments
Rafael Espindola65366442011-06-05 02:43:45 +00001736 case 'n':
1737 OS << A.size();
1738 break;
1739
Jim Grosbach4a200922013-09-20 23:08:21 +00001740 // $[0-9] => argument
Rafael Espindola65366442011-06-05 02:43:45 +00001741 default: {
1742 // Missing arguments are ignored.
Jim Grosbach4a200922013-09-20 23:08:21 +00001743 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola65366442011-06-05 02:43:45 +00001744 if (Index >= A.size())
1745 break;
1746
1747 // Otherwise substitute with the token values, with spaces eliminated.
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001748 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4a200922013-09-20 23:08:21 +00001749 ie = A[Index].end();
1750 it != ie; ++it)
Rafael Espindola65366442011-06-05 02:43:45 +00001751 OS << it->getString();
1752 break;
1753 }
1754 }
1755 Pos += 2;
1756 } else {
1757 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001758 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001759 ++I;
1760
Jim Grosbach4a200922013-09-20 23:08:21 +00001761 const char *Begin = Body.data() + Pos + 1;
1762 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola65366442011-06-05 02:43:45 +00001763 unsigned Index = 0;
1764 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001765 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001766 break;
1767
Preston Gurd7b6f2032012-09-19 20:36:12 +00001768 if (Index == NParameters) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001769 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1770 Pos += 3;
1771 else {
1772 OS << '\\' << Argument;
1773 Pos = I;
1774 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001775 } else {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001776 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4a200922013-09-20 23:08:21 +00001777 ie = A[Index].end();
1778 it != ie; ++it)
Preston Gurd7b6f2032012-09-19 20:36:12 +00001779 if (it->getKind() == AsmToken::String)
1780 OS << it->getStringContents();
1781 else
1782 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001783
Preston Gurd7b6f2032012-09-19 20:36:12 +00001784 Pos += 1 + Argument.size();
1785 }
Rafael Espindola65366442011-06-05 02:43:45 +00001786 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001787 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001788 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001789 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001790
Rafael Espindola65366442011-06-05 02:43:45 +00001791 return false;
1792}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001793
Jim Grosbach4a200922013-09-20 23:08:21 +00001794MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1795 SMLoc EL, MemoryBuffer *I)
1796 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1797 ExitLoc(EL) {}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001798
Jim Grosbach4a200922013-09-20 23:08:21 +00001799static bool isOperator(AsmToken::TokenKind kind) {
1800 switch (kind) {
1801 default:
1802 return false;
1803 case AsmToken::Plus:
1804 case AsmToken::Minus:
1805 case AsmToken::Tilde:
1806 case AsmToken::Slash:
1807 case AsmToken::Star:
1808 case AsmToken::Dot:
1809 case AsmToken::Equal:
1810 case AsmToken::EqualEqual:
1811 case AsmToken::Pipe:
1812 case AsmToken::PipePipe:
1813 case AsmToken::Caret:
1814 case AsmToken::Amp:
1815 case AsmToken::AmpAmp:
1816 case AsmToken::Exclaim:
1817 case AsmToken::ExclaimEqual:
1818 case AsmToken::Percent:
1819 case AsmToken::Less:
1820 case AsmToken::LessEqual:
1821 case AsmToken::LessLess:
1822 case AsmToken::LessGreater:
1823 case AsmToken::Greater:
1824 case AsmToken::GreaterEqual:
1825 case AsmToken::GreaterGreater:
1826 return true;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001827 }
1828}
1829
Jim Grosbach4a200922013-09-20 23:08:21 +00001830bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +00001831 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001832 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001833 unsigned AddTokens = 0;
1834
1835 // gas accepts arguments separated by whitespace, except on Darwin
1836 if (!IsDarwin)
1837 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001838
1839 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001840 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1841 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001842 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001843 }
1844
1845 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1846 // Spaces and commas cannot be mixed to delimit parameters
1847 if (ArgumentDelimiter == AsmToken::Eof)
1848 ArgumentDelimiter = AsmToken::Comma;
1849 else if (ArgumentDelimiter != AsmToken::Comma) {
1850 Lexer.setSkipSpace(true);
1851 return TokError("expected ' ' for macro argument separator");
1852 }
1853 break;
1854 }
1855
1856 if (Lexer.is(AsmToken::Space)) {
1857 Lex(); // Eat spaces
1858
1859 // Spaces can delimit parameters, but could also be part an expression.
1860 // If the token after a space is an operator, add the token and the next
1861 // one into this argument
1862 if (ArgumentDelimiter == AsmToken::Space ||
1863 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4a200922013-09-20 23:08:21 +00001864 if (isOperator(Lexer.getKind())) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001865 // Check to see whether the token is used as an operator,
1866 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001867 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001868 if (*NextChar == ' ')
1869 AddTokens = 2;
1870 }
1871
1872 if (!AddTokens && ParenLevel == 0) {
1873 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4a200922013-09-20 23:08:21 +00001874 !isOperator(Lexer.getKind()))
Preston Gurd7b6f2032012-09-19 20:36:12 +00001875 ArgumentDelimiter = AsmToken::Space;
1876 break;
1877 }
1878 }
1879 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001880
Jim Grosbach4a200922013-09-20 23:08:21 +00001881 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001882 // to be able to fill in the remaining default parameter values
1883 if (Lexer.is(AsmToken::EndOfStatement))
1884 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001885
1886 // Adjust the current parentheses level.
1887 if (Lexer.is(AsmToken::LParen))
1888 ++ParenLevel;
1889 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1890 --ParenLevel;
1891
1892 // Append the token to the current argument list.
1893 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001894 if (AddTokens)
1895 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001896 Lex();
1897 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001898
1899 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001900 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001901 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001902 return false;
1903}
1904
1905// Parse the macro instantiation arguments.
Jim Grosbach4a200922013-09-20 23:08:21 +00001906bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic7b0a7962013-08-20 13:33:18 +00001907 MCAsmMacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001908 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001909 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4a200922013-09-20 23:08:21 +00001910 // parseMacroArgument()
Preston Gurd7b6f2032012-09-19 20:36:12 +00001911 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001912
1913 // Parse two kinds of macro invocations:
1914 // - macros defined without any parameters accept an arbitrary number of them
1915 // - macros defined with parameters accept at most that many of them
1916 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1917 ++Parameter) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001918 MCAsmMacroArgument MA;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001919
Jim Grosbach4a200922013-09-20 23:08:21 +00001920 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001921 return true;
1922
Preston Gurd6c9176a2012-09-19 20:29:04 +00001923 if (!MA.empty() || !NParameters)
1924 A.push_back(MA);
1925 else if (NParameters) {
1926 if (!M->Parameters[Parameter].second.empty())
1927 A.push_back(M->Parameters[Parameter].second);
1928 }
Jim Grosbach97146442012-07-30 22:44:17 +00001929
Preston Gurd6c9176a2012-09-19 20:29:04 +00001930 // At the end of the statement, fill in remaining arguments that have
1931 // default values. If there aren't any, then the next argument is
1932 // required but missing
1933 if (Lexer.is(AsmToken::EndOfStatement)) {
1934 if (NParameters && Parameter < NParameters - 1) {
1935 if (M->Parameters[Parameter + 1].second.empty())
1936 return TokError("macro argument '" +
1937 Twine(M->Parameters[Parameter + 1].first) +
1938 "' is missing");
1939 else
1940 continue;
1941 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001942 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001943 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001944
1945 if (Lexer.is(AsmToken::Comma))
1946 Lex();
1947 }
1948 return TokError("Too many arguments");
1949}
1950
Jim Grosbach4a200922013-09-20 23:08:21 +00001951const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1952 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001953 return (I == MacroMap.end()) ? NULL : I->getValue();
1954}
1955
Jim Grosbach4a200922013-09-20 23:08:21 +00001956void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001957 MacroMap[Name] = new MCAsmMacro(Macro);
1958}
1959
Jim Grosbach4a200922013-09-20 23:08:21 +00001960void AsmParser::undefineMacro(StringRef Name) {
1961 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001962 if (I != MacroMap.end()) {
1963 delete I->getValue();
1964 MacroMap.erase(I);
1965 }
1966}
1967
Jim Grosbach4a200922013-09-20 23:08:21 +00001968bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001969 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1970 // this, although we should protect against infinite loops.
1971 if (ActiveMacros.size() == 20)
1972 return TokError("macros cannot be nested more than 20 levels deep");
1973
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001974 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00001975 if (parseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001976 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001977
Jim Grosbach97146442012-07-30 22:44:17 +00001978 // Remove any trailing empty arguments. Do this after-the-fact as we have
1979 // to keep empty arguments in the middle of the list or positionality
1980 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001981 while (!A.empty() && A.back().empty())
1982 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001983
Rafael Espindola65366442011-06-05 02:43:45 +00001984 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1985 // to hold the macro body with substitutions.
1986 SmallString<256> Buf;
1987 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001988 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001989
Rafael Espindola8a403d32012-08-08 14:51:03 +00001990 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001991 return true;
1992
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001993 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola761cb062012-06-03 23:57:14 +00001994 // instantiation.
1995 OS << ".endmacro\n";
1996
Rafael Espindola65366442011-06-05 02:43:45 +00001997 MemoryBuffer *Instantiation =
Jim Grosbach4a200922013-09-20 23:08:21 +00001998 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001999
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002000 // Create the macro instantiation object and add to the current macro
2001 // instantiation stack.
Jim Grosbach4a200922013-09-20 23:08:21 +00002002 MacroInstantiation *MI = new MacroInstantiation(
2003 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002004 ActiveMacros.push_back(MI);
2005
2006 // Jump to the macro instantiation and prime the lexer.
2007 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2008 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2009 Lex();
2010
2011 return false;
2012}
2013
Jim Grosbach4a200922013-09-20 23:08:21 +00002014void AsmParser::handleMacroExit() {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002015 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4a200922013-09-20 23:08:21 +00002016 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002017 Lex();
2018
2019 // Pop the instantiation entry.
2020 delete ActiveMacros.back();
2021 ActiveMacros.pop_back();
2022}
2023
Jim Grosbach4a200922013-09-20 23:08:21 +00002024static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002025 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00002026 case MCExpr::Binary: {
Jim Grosbach4a200922013-09-20 23:08:21 +00002027 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2028 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002029 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00002030 case MCExpr::Target:
2031 case MCExpr::Constant:
2032 return false;
2033 case MCExpr::SymbolRef: {
Jim Grosbach4a200922013-09-20 23:08:21 +00002034 const MCSymbol &S =
2035 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00002036 if (S.isVariable())
Jim Grosbach4a200922013-09-20 23:08:21 +00002037 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola8b01c822012-01-28 06:22:14 +00002038 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00002039 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002040 case MCExpr::Unary:
Jim Grosbach4a200922013-09-20 23:08:21 +00002041 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002042 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00002043
2044 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002045}
2046
Jim Grosbach4a200922013-09-20 23:08:21 +00002047bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002048 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00002049 // FIXME: Use better location, we should use proper tokens.
2050 SMLoc EqualLoc = Lexer.getLoc();
2051
Daniel Dunbar821e3332009-08-31 08:09:28 +00002052 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002053 if (parseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002054 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002055
Rafael Espindolae71cc862012-01-28 05:57:00 +00002056 // Note: we don't count b as used in "a = b". This is to allow
2057 // a = b
2058 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002059
Daniel Dunbar3f872332009-07-28 16:08:33 +00002060 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002061 return TokError("unexpected token in assignment");
2062
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00002063 // Error on assignment to '.'.
2064 if (Name == ".") {
2065 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2066 "(use '.space' or '.org').)"));
2067 }
2068
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002069 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00002070 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002071
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002072 // Validate that the LHS is allowed to be a variable (either it has not been
2073 // used as a symbol, or it is an absolute symbol).
2074 MCSymbol *Sym = getContext().LookupSymbol(Name);
2075 if (Sym) {
2076 // Diagnose assignment to a label.
2077 //
2078 // FIXME: Diagnostics. Note the location of the definition as a label.
2079 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4a200922013-09-20 23:08:21 +00002080 if (isUsedIn(Sym, Value))
Rafael Espindolae71cc862012-01-28 05:57:00 +00002081 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2082 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00002083 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00002084 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2085 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00002086 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002087 return Error(EqualLoc, "redefinition of '" + Name + "'");
2088 else if (!Sym->isVariable())
2089 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00002090 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002091 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4a200922013-09-20 23:08:21 +00002092 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00002093
2094 // Don't count these checks as uses.
2095 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002096 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002097 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00002098
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002099 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00002100
2101 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00002102 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002103 if (NoDeadStrip)
2104 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2105
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002106 return false;
2107}
2108
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002109/// parseIdentifier:
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002110/// ::= identifier
2111/// ::= string
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002112bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002113 // The assembler has relaxed rules for accepting identifiers, in particular we
2114 // allow things like '.globl $foo', which would normally be separate
2115 // tokens. At this level, we have already lexed so we cannot (currently)
2116 // handle this as a context dependent token, instead we detect adjacent tokens
2117 // and return the combined identifier.
2118 if (Lexer.is(AsmToken::Dollar)) {
2119 SMLoc DollarLoc = getLexer().getLoc();
2120
2121 // Consume the dollar sign, and check for a following identifier.
2122 Lex();
2123 if (Lexer.isNot(AsmToken::Identifier))
2124 return true;
2125
2126 // We have a '$' followed by an identifier, make sure they are adjacent.
2127 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2128 return true;
2129
2130 // Construct the joined identifier and consume the token.
Jim Grosbach4a200922013-09-20 23:08:21 +00002131 Res =
2132 StringRef(DollarLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002133 Lex();
2134 return false;
2135 }
2136
Jim Grosbach4a200922013-09-20 23:08:21 +00002137 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002138 return true;
2139
Sean Callanan18b83232010-01-19 21:44:56 +00002140 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002141
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002143
2144 return false;
2145}
2146
Jim Grosbach4a200922013-09-20 23:08:21 +00002147/// parseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002148/// ::= .equ identifier ',' expression
2149/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002150/// ::= .set identifier ',' expression
Jim Grosbach4a200922013-09-20 23:08:21 +00002151bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002152 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002153
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002154 if (parseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002155 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002156
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002157 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002158 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002159 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002160
Jim Grosbach4a200922013-09-20 23:08:21 +00002161 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002162}
2163
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002164bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002165 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002166
2167 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002168 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002169 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2170 if (Str[i] != '\\') {
2171 Data += Str[i];
2172 continue;
2173 }
2174
2175 // Recognize escaped characters. Note that this escape semantics currently
2176 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2177 ++i;
2178 if (i == e)
2179 return TokError("unexpected backslash at end of string");
2180
2181 // Recognize octal sequences.
Jim Grosbach4a200922013-09-20 23:08:21 +00002182 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002183 // Consume up to three octal characters.
2184 unsigned Value = Str[i] - '0';
2185
Jim Grosbach4a200922013-09-20 23:08:21 +00002186 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002187 ++i;
2188 Value = Value * 8 + (Str[i] - '0');
2189
Jim Grosbach4a200922013-09-20 23:08:21 +00002190 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002191 ++i;
2192 Value = Value * 8 + (Str[i] - '0');
2193 }
2194 }
2195
2196 if (Value > 255)
2197 return TokError("invalid octal escape sequence (out of range)");
2198
Jim Grosbach4a200922013-09-20 23:08:21 +00002199 Data += (unsigned char)Value;
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002200 continue;
2201 }
2202
2203 // Otherwise recognize individual escapes.
2204 switch (Str[i]) {
2205 default:
2206 // Just reject invalid escape sequences for now.
2207 return TokError("invalid escape sequence (unrecognized character)");
2208
2209 case 'b': Data += '\b'; break;
2210 case 'f': Data += '\f'; break;
2211 case 'n': Data += '\n'; break;
2212 case 'r': Data += '\r'; break;
2213 case 't': Data += '\t'; break;
2214 case '"': Data += '"'; break;
2215 case '\\': Data += '\\'; break;
2216 }
2217 }
2218
2219 return false;
2220}
2221
Jim Grosbach4a200922013-09-20 23:08:21 +00002222/// parseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002223/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002224bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002225 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002226 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002227
Daniel Dunbara0d14262009-06-24 23:30:00 +00002228 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002229 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002230 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002231
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002232 std::string Data;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002233 if (parseEscapedString(Data))
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002234 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002235
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002236 getStreamer().EmitBytes(Data);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002237 if (ZeroTerminated)
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002238 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002239
Sean Callanan79ed1a82010-01-19 20:22:31 +00002240 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002241
2242 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002243 break;
2244
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002245 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002246 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002247 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002248 }
2249 }
2250
Sean Callanan79ed1a82010-01-19 20:22:31 +00002251 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002252 return false;
2253}
2254
Jim Grosbach4a200922013-09-20 23:08:21 +00002255/// parseDirectiveValue
Daniel Dunbara0d14262009-06-24 23:30:00 +00002256/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002257bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002258 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002259 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002260
Daniel Dunbara0d14262009-06-24 23:30:00 +00002261 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002262 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002263 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002264 if (parseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002265 return true;
2266
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002267 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002268 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2269 assert(Size <= 8 && "Invalid size");
2270 uint64_t IntValue = MCE->getValue();
2271 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2272 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002273 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach254cf032011-06-29 16:05:14 +00002274 } else
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002275 getStreamer().EmitValue(Value, Size);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002276
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002277 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002278 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002279
Daniel Dunbara0d14262009-06-24 23:30:00 +00002280 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002281 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002282 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002283 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002284 }
2285 }
2286
Sean Callanan79ed1a82010-01-19 20:22:31 +00002287 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002288 return false;
2289}
2290
Jim Grosbach4a200922013-09-20 23:08:21 +00002291/// parseDirectiveRealValue
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002292/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002293bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002294 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002295 checkForValidSection();
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002296
2297 for (;;) {
2298 // We don't truly support arithmetic on floating point expressions, so we
2299 // have to manually parse unary prefixes.
2300 bool IsNeg = false;
2301 if (getLexer().is(AsmToken::Minus)) {
2302 Lex();
2303 IsNeg = true;
2304 } else if (getLexer().is(AsmToken::Plus))
2305 Lex();
2306
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002307 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002308 getLexer().isNot(AsmToken::Real) &&
2309 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002310 return TokError("unexpected token in directive");
2311
2312 // Convert to an APFloat.
2313 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002314 StringRef IDVal = getTok().getString();
2315 if (getLexer().is(AsmToken::Identifier)) {
2316 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2317 Value = APFloat::getInf(Semantics);
2318 else if (!IDVal.compare_lower("nan"))
2319 Value = APFloat::getNaN(Semantics, false, ~0);
2320 else
2321 return TokError("invalid floating point literal");
2322 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4a200922013-09-20 23:08:21 +00002323 APFloat::opInvalidOp)
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002324 return TokError("invalid floating point literal");
2325 if (IsNeg)
2326 Value.changeSign();
2327
2328 // Consume the numeric token.
2329 Lex();
2330
2331 // Emit the value as an integer.
2332 APInt AsInt = Value.bitcastToAPInt();
2333 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002334 AsInt.getBitWidth() / 8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002335
2336 if (getLexer().is(AsmToken::EndOfStatement))
2337 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002338
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002339 if (getLexer().isNot(AsmToken::Comma))
2340 return TokError("unexpected token in directive");
2341 Lex();
2342 }
2343 }
2344
2345 Lex();
2346 return false;
2347}
2348
Jim Grosbach4a200922013-09-20 23:08:21 +00002349/// parseDirectiveZero
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002350/// ::= .zero expression
Jim Grosbach4a200922013-09-20 23:08:21 +00002351bool AsmParser::parseDirectiveZero() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002352 checkForValidSection();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002353
2354 int64_t NumBytes;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002355 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002356 return true;
2357
Rafael Espindolae452b172010-10-05 19:42:57 +00002358 int64_t Val = 0;
2359 if (getLexer().is(AsmToken::Comma)) {
2360 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002361 if (parseAbsoluteExpression(Val))
Rafael Espindolae452b172010-10-05 19:42:57 +00002362 return true;
2363 }
2364
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002365 if (getLexer().isNot(AsmToken::EndOfStatement))
2366 return TokError("unexpected token in '.zero' directive");
2367
2368 Lex();
2369
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002370 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002371
2372 return false;
2373}
2374
Jim Grosbach4a200922013-09-20 23:08:21 +00002375/// parseDirectiveFill
Roman Divacky9c607102013-09-24 17:44:41 +00002376/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002377bool AsmParser::parseDirectiveFill() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002378 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002379
Daniel Dunbara0d14262009-06-24 23:30:00 +00002380 int64_t NumValues;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002381 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002382 return true;
2383
Roman Divacky9c607102013-09-24 17:44:41 +00002384 int64_t FillSize = 1;
2385 int64_t FillExpr = 0;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002386
Roman Divacky9c607102013-09-24 17:44:41 +00002387 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2388 if (getLexer().isNot(AsmToken::Comma))
2389 return TokError("unexpected token in '.fill' directive");
2390 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002391
Roman Divacky9c607102013-09-24 17:44:41 +00002392 if (parseAbsoluteExpression(FillSize))
2393 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002394
Roman Divacky9c607102013-09-24 17:44:41 +00002395 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2396 if (getLexer().isNot(AsmToken::Comma))
2397 return TokError("unexpected token in '.fill' directive");
2398 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002399
Roman Divacky9c607102013-09-24 17:44:41 +00002400 if (parseAbsoluteExpression(FillExpr))
2401 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002402
Roman Divacky9c607102013-09-24 17:44:41 +00002403 if (getLexer().isNot(AsmToken::EndOfStatement))
2404 return TokError("unexpected token in '.fill' directive");
2405
2406 Lex();
2407 }
2408 }
Daniel Dunbara0d14262009-06-24 23:30:00 +00002409
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002410 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2411 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002412
2413 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002414 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002415
2416 return false;
2417}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002418
Jim Grosbach4a200922013-09-20 23:08:21 +00002419/// parseDirectiveOrg
Daniel Dunbarc238b582009-06-25 22:44:51 +00002420/// ::= .org expression [ , expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00002421bool AsmParser::parseDirectiveOrg() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002422 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002423
Daniel Dunbar821e3332009-08-31 08:09:28 +00002424 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002425 SMLoc Loc = getTok().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002426 if (parseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002427 return true;
2428
2429 // Parse optional fill expression.
2430 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002431 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2432 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002433 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002434 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002435
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002436 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002437 return true;
2438
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002439 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002440 return TokError("unexpected token in '.org' directive");
2441 }
2442
Sean Callanan79ed1a82010-01-19 20:22:31 +00002443 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002444
Jim Grosbachebd4c052012-01-27 00:37:08 +00002445 // Only limited forms of relocatable expressions are accepted here, it
2446 // has to be relative to the current section. The streamer will return
2447 // 'true' if the expression wasn't evaluatable.
2448 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2449 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002450
2451 return false;
2452}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002453
Jim Grosbach4a200922013-09-20 23:08:21 +00002454/// parseDirectiveAlign
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002455/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4a200922013-09-20 23:08:21 +00002456bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002457 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002458
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002459 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002460 int64_t Alignment;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002461 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002462 return true;
2463
2464 SMLoc MaxBytesLoc;
2465 bool HasFillExpr = false;
2466 int64_t FillExpr = 0;
2467 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002468 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2469 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002470 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002471 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002472
2473 // The fill expression can be omitted while specifying a maximum number of
2474 // alignment bytes, e.g:
2475 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002476 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002477 HasFillExpr = true;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002478 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002479 return true;
2480 }
2481
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002482 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2483 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002484 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002485 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002486
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002487 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002488 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002489 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002490
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002491 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002492 return TokError("unexpected token in directive");
2493 }
2494 }
2495
Sean Callanan79ed1a82010-01-19 20:22:31 +00002496 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002497
Daniel Dunbar648ac512010-05-17 21:54:30 +00002498 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002499 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002500
2501 // Compute alignment in bytes.
2502 if (IsPow2) {
2503 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002504 if (Alignment >= 32) {
2505 Error(AlignmentLoc, "invalid alignment value");
2506 Alignment = 31;
2507 }
2508
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002509 Alignment = 1ULL << Alignment;
Benjamin Kramer8a89cf22013-02-16 15:00:16 +00002510 } else {
2511 // Reject alignments that aren't a power of two, for gas compatibility.
2512 if (!isPowerOf2_64(Alignment))
2513 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002514 }
2515
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002516 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002517 if (MaxBytesLoc.isValid()) {
2518 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002519 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4a200922013-09-20 23:08:21 +00002520 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002521 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002522 }
2523
2524 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002525 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4a200922013-09-20 23:08:21 +00002526 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002527 MaxBytesToFill = 0;
2528 }
2529 }
2530
Daniel Dunbar648ac512010-05-17 21:54:30 +00002531 // Check whether we should use optimal code alignment for this .align
2532 // directive.
Peter Collingbournedf39be62013-04-17 21:18:16 +00002533 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002534 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2535 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002536 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002537 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002538 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002539 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2540 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002541 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002542
2543 return false;
2544}
2545
Jim Grosbach4a200922013-09-20 23:08:21 +00002546/// parseDirectiveFile
Eli Bendersky6ee13082013-01-15 22:59:42 +00002547/// ::= .file [number] filename
2548/// ::= .file number directory filename
Jim Grosbach4a200922013-09-20 23:08:21 +00002549bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002550 // FIXME: I'm not sure what this is.
2551 int64_t FileNumber = -1;
2552 SMLoc FileNumberLoc = getLexer().getLoc();
2553 if (getLexer().is(AsmToken::Integer)) {
2554 FileNumber = getTok().getIntVal();
2555 Lex();
2556
2557 if (FileNumber < 1)
2558 return TokError("file number less than one");
2559 }
2560
2561 if (getLexer().isNot(AsmToken::String))
2562 return TokError("unexpected token in '.file' directive");
2563
2564 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gaoed119822013-09-05 19:14:26 +00002565 // Allow the strings to have escaped octal character sequence.
2566 std::string Path = getTok().getString();
2567 if (parseEscapedString(Path))
2568 return true;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002569 Lex();
2570
2571 StringRef Directory;
2572 StringRef Filename;
Yunzhong Gaoed119822013-09-05 19:14:26 +00002573 std::string FilenameData;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002574 if (getLexer().is(AsmToken::String)) {
2575 if (FileNumber == -1)
2576 return TokError("explicit path specified, but no file number");
Yunzhong Gaoed119822013-09-05 19:14:26 +00002577 if (parseEscapedString(FilenameData))
2578 return true;
2579 Filename = FilenameData;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002580 Directory = Path;
2581 Lex();
2582 } else {
2583 Filename = Path;
2584 }
2585
2586 if (getLexer().isNot(AsmToken::EndOfStatement))
2587 return TokError("unexpected token in '.file' directive");
2588
2589 if (FileNumber == -1)
2590 getStreamer().EmitFileDirective(Filename);
2591 else {
2592 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4a200922013-09-20 23:08:21 +00002593 Error(DirectiveLoc,
2594 "input can't have .file dwarf directives when -g is "
2595 "used to generate dwarf debug info for assembly code");
Eli Bendersky6ee13082013-01-15 22:59:42 +00002596
2597 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2598 Error(FileNumberLoc, "file number already allocated");
2599 }
2600
2601 return false;
2602}
2603
Jim Grosbach4a200922013-09-20 23:08:21 +00002604/// parseDirectiveLine
Eli Bendersky6ee13082013-01-15 22:59:42 +00002605/// ::= .line [number]
Jim Grosbach4a200922013-09-20 23:08:21 +00002606bool AsmParser::parseDirectiveLine() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002607 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2608 if (getLexer().isNot(AsmToken::Integer))
2609 return TokError("unexpected token in '.line' directive");
2610
2611 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4a200922013-09-20 23:08:21 +00002612 (void)LineNumber;
Eli Bendersky6ee13082013-01-15 22:59:42 +00002613 Lex();
2614
2615 // FIXME: Do something with the .line.
2616 }
2617
2618 if (getLexer().isNot(AsmToken::EndOfStatement))
2619 return TokError("unexpected token in '.line' directive");
2620
2621 return false;
2622}
2623
Jim Grosbach4a200922013-09-20 23:08:21 +00002624/// parseDirectiveLoc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002625/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2626/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2627/// The first number is a file number, must have been previously assigned with
2628/// a .file directive, the second number is the line number and optionally the
2629/// third number is a column position (zero if not specified). The remaining
2630/// optional items are .loc sub-directives.
Jim Grosbach4a200922013-09-20 23:08:21 +00002631bool AsmParser::parseDirectiveLoc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002632 if (getLexer().isNot(AsmToken::Integer))
2633 return TokError("unexpected token in '.loc' directive");
2634 int64_t FileNumber = getTok().getIntVal();
2635 if (FileNumber < 1)
2636 return TokError("file number less than one in '.loc' directive");
2637 if (!getContext().isValidDwarfFileNumber(FileNumber))
2638 return TokError("unassigned file number in '.loc' directive");
2639 Lex();
2640
2641 int64_t LineNumber = 0;
2642 if (getLexer().is(AsmToken::Integer)) {
2643 LineNumber = getTok().getIntVal();
Adrian Prantldeac1372013-09-26 23:37:11 +00002644 if (LineNumber < 0)
2645 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky6ee13082013-01-15 22:59:42 +00002646 Lex();
2647 }
2648
2649 int64_t ColumnPos = 0;
2650 if (getLexer().is(AsmToken::Integer)) {
2651 ColumnPos = getTok().getIntVal();
2652 if (ColumnPos < 0)
2653 return TokError("column position less than zero in '.loc' directive");
2654 Lex();
2655 }
2656
2657 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2658 unsigned Isa = 0;
2659 int64_t Discriminator = 0;
2660 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2661 for (;;) {
2662 if (getLexer().is(AsmToken::EndOfStatement))
2663 break;
2664
2665 StringRef Name;
2666 SMLoc Loc = getTok().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002667 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002668 return TokError("unexpected token in '.loc' directive");
2669
2670 if (Name == "basic_block")
2671 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2672 else if (Name == "prologue_end")
2673 Flags |= DWARF2_FLAG_PROLOGUE_END;
2674 else if (Name == "epilogue_begin")
2675 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2676 else if (Name == "is_stmt") {
2677 Loc = getTok().getLoc();
2678 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002679 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002680 return true;
2681 // The expression must be the constant 0 or 1.
2682 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2683 int Value = MCE->getValue();
2684 if (Value == 0)
2685 Flags &= ~DWARF2_FLAG_IS_STMT;
2686 else if (Value == 1)
2687 Flags |= DWARF2_FLAG_IS_STMT;
2688 else
2689 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperefa703d2013-04-22 04:22:40 +00002690 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002691 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2692 }
Craig Topperefa703d2013-04-22 04:22:40 +00002693 } else if (Name == "isa") {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002694 Loc = getTok().getLoc();
2695 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002696 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002697 return true;
2698 // The expression must be a constant greater or equal to 0.
2699 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2700 int Value = MCE->getValue();
2701 if (Value < 0)
2702 return Error(Loc, "isa number less than zero");
2703 Isa = Value;
Craig Topperefa703d2013-04-22 04:22:40 +00002704 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002705 return Error(Loc, "isa number not a constant value");
2706 }
Craig Topperefa703d2013-04-22 04:22:40 +00002707 } else if (Name == "discriminator") {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002708 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002709 return true;
Craig Topperefa703d2013-04-22 04:22:40 +00002710 } else {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002711 return Error(Loc, "unknown sub-directive in '.loc' directive");
2712 }
2713
2714 if (getLexer().is(AsmToken::EndOfStatement))
2715 break;
2716 }
2717 }
2718
2719 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2720 Isa, Discriminator, StringRef());
2721
2722 return false;
2723}
2724
Jim Grosbach4a200922013-09-20 23:08:21 +00002725/// parseDirectiveStabs
Eli Bendersky6ee13082013-01-15 22:59:42 +00002726/// ::= .stabs string, number, number, number
Jim Grosbach4a200922013-09-20 23:08:21 +00002727bool AsmParser::parseDirectiveStabs() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002728 return TokError("unsupported directive '.stabs'");
2729}
2730
Jim Grosbach4a200922013-09-20 23:08:21 +00002731/// parseDirectiveCFISections
Eli Bendersky6ee13082013-01-15 22:59:42 +00002732/// ::= .cfi_sections section [, section]
Jim Grosbach4a200922013-09-20 23:08:21 +00002733bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002734 StringRef Name;
2735 bool EH = false;
2736 bool Debug = false;
2737
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002738 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002739 return TokError("Expected an identifier");
2740
2741 if (Name == ".eh_frame")
2742 EH = true;
2743 else if (Name == ".debug_frame")
2744 Debug = true;
2745
2746 if (getLexer().is(AsmToken::Comma)) {
2747 Lex();
2748
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002749 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002750 return TokError("Expected an identifier");
2751
2752 if (Name == ".eh_frame")
2753 EH = true;
2754 else if (Name == ".debug_frame")
2755 Debug = true;
2756 }
2757
2758 getStreamer().EmitCFISections(EH, Debug);
2759 return false;
2760}
2761
Jim Grosbach4a200922013-09-20 23:08:21 +00002762/// parseDirectiveCFIStartProc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002763/// ::= .cfi_startproc
Jim Grosbach4a200922013-09-20 23:08:21 +00002764bool AsmParser::parseDirectiveCFIStartProc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002765 getStreamer().EmitCFIStartProc();
2766 return false;
2767}
2768
Jim Grosbach4a200922013-09-20 23:08:21 +00002769/// parseDirectiveCFIEndProc
Eli Bendersky6ee13082013-01-15 22:59:42 +00002770/// ::= .cfi_endproc
Jim Grosbach4a200922013-09-20 23:08:21 +00002771bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002772 getStreamer().EmitCFIEndProc();
2773 return false;
2774}
2775
Jim Grosbach4a200922013-09-20 23:08:21 +00002776/// \brief parse register name or number.
2777bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky6ee13082013-01-15 22:59:42 +00002778 SMLoc DirectiveLoc) {
2779 unsigned RegNo;
2780
2781 if (getLexer().isNot(AsmToken::Integer)) {
2782 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2783 return true;
Bill Wendling99cb6222013-06-18 07:20:20 +00002784 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky6ee13082013-01-15 22:59:42 +00002785 } else
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002786 return parseAbsoluteExpression(Register);
Eli Bendersky6ee13082013-01-15 22:59:42 +00002787
2788 return false;
2789}
2790
Jim Grosbach4a200922013-09-20 23:08:21 +00002791/// parseDirectiveCFIDefCfa
Eli Bendersky6ee13082013-01-15 22:59:42 +00002792/// ::= .cfi_def_cfa register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002793bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002794 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002795 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002796 return true;
2797
2798 if (getLexer().isNot(AsmToken::Comma))
2799 return TokError("unexpected token in directive");
2800 Lex();
2801
2802 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002803 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002804 return true;
2805
2806 getStreamer().EmitCFIDefCfa(Register, Offset);
2807 return false;
2808}
2809
Jim Grosbach4a200922013-09-20 23:08:21 +00002810/// parseDirectiveCFIDefCfaOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002811/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002812bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002813 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002814 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002815 return true;
2816
2817 getStreamer().EmitCFIDefCfaOffset(Offset);
2818 return false;
2819}
2820
Jim Grosbach4a200922013-09-20 23:08:21 +00002821/// parseDirectiveCFIRegister
Eli Bendersky6ee13082013-01-15 22:59:42 +00002822/// ::= .cfi_register register, register
Jim Grosbach4a200922013-09-20 23:08:21 +00002823bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002824 int64_t Register1 = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002825 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002826 return true;
2827
2828 if (getLexer().isNot(AsmToken::Comma))
2829 return TokError("unexpected token in directive");
2830 Lex();
2831
2832 int64_t Register2 = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002833 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002834 return true;
2835
2836 getStreamer().EmitCFIRegister(Register1, Register2);
2837 return false;
2838}
2839
Venkatraman Govindaraju83ba58e2013-09-26 14:49:40 +00002840/// parseDirectiveCFIWindowSave
2841/// ::= .cfi_window_save
2842bool AsmParser::parseDirectiveCFIWindowSave() {
2843 getStreamer().EmitCFIWindowSave();
2844 return false;
2845}
2846
Jim Grosbach4a200922013-09-20 23:08:21 +00002847/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002848/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4a200922013-09-20 23:08:21 +00002849bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002850 int64_t Adjustment = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002851 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002852 return true;
2853
2854 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2855 return false;
2856}
2857
Jim Grosbach4a200922013-09-20 23:08:21 +00002858/// parseDirectiveCFIDefCfaRegister
Eli Bendersky6ee13082013-01-15 22:59:42 +00002859/// ::= .cfi_def_cfa_register register
Jim Grosbach4a200922013-09-20 23:08:21 +00002860bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002861 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002862 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002863 return true;
2864
2865 getStreamer().EmitCFIDefCfaRegister(Register);
2866 return false;
2867}
2868
Jim Grosbach4a200922013-09-20 23:08:21 +00002869/// parseDirectiveCFIOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002870/// ::= .cfi_offset register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002871bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002872 int64_t Register = 0;
2873 int64_t Offset = 0;
2874
Jim Grosbach4a200922013-09-20 23:08:21 +00002875 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002876 return true;
2877
2878 if (getLexer().isNot(AsmToken::Comma))
2879 return TokError("unexpected token in directive");
2880 Lex();
2881
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002882 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002883 return true;
2884
2885 getStreamer().EmitCFIOffset(Register, Offset);
2886 return false;
2887}
2888
Jim Grosbach4a200922013-09-20 23:08:21 +00002889/// parseDirectiveCFIRelOffset
Eli Bendersky6ee13082013-01-15 22:59:42 +00002890/// ::= .cfi_rel_offset register, offset
Jim Grosbach4a200922013-09-20 23:08:21 +00002891bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002892 int64_t Register = 0;
2893
Jim Grosbach4a200922013-09-20 23:08:21 +00002894 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002895 return true;
2896
2897 if (getLexer().isNot(AsmToken::Comma))
2898 return TokError("unexpected token in directive");
2899 Lex();
2900
2901 int64_t Offset = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002902 if (parseAbsoluteExpression(Offset))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002903 return true;
2904
2905 getStreamer().EmitCFIRelOffset(Register, Offset);
2906 return false;
2907}
2908
2909static bool isValidEncoding(int64_t Encoding) {
2910 if (Encoding & ~0xff)
2911 return false;
2912
2913 if (Encoding == dwarf::DW_EH_PE_omit)
2914 return true;
2915
2916 const unsigned Format = Encoding & 0xf;
2917 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2918 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2919 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2920 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2921 return false;
2922
2923 const unsigned Application = Encoding & 0x70;
2924 if (Application != dwarf::DW_EH_PE_absptr &&
2925 Application != dwarf::DW_EH_PE_pcrel)
2926 return false;
2927
2928 return true;
2929}
2930
Jim Grosbach4a200922013-09-20 23:08:21 +00002931/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky6ee13082013-01-15 22:59:42 +00002932/// IsPersonality true for cfi_personality, false for cfi_lsda
2933/// ::= .cfi_personality encoding, [symbol_name]
2934/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4a200922013-09-20 23:08:21 +00002935bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002936 int64_t Encoding = 0;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002937 if (parseAbsoluteExpression(Encoding))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002938 return true;
2939 if (Encoding == dwarf::DW_EH_PE_omit)
2940 return false;
2941
2942 if (!isValidEncoding(Encoding))
2943 return TokError("unsupported encoding.");
2944
2945 if (getLexer().isNot(AsmToken::Comma))
2946 return TokError("unexpected token in directive");
2947 Lex();
2948
2949 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002950 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002951 return TokError("expected identifier in directive");
2952
2953 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2954
2955 if (IsPersonality)
2956 getStreamer().EmitCFIPersonality(Sym, Encoding);
2957 else
2958 getStreamer().EmitCFILsda(Sym, Encoding);
2959 return false;
2960}
2961
Jim Grosbach4a200922013-09-20 23:08:21 +00002962/// parseDirectiveCFIRememberState
Eli Bendersky6ee13082013-01-15 22:59:42 +00002963/// ::= .cfi_remember_state
Jim Grosbach4a200922013-09-20 23:08:21 +00002964bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002965 getStreamer().EmitCFIRememberState();
2966 return false;
2967}
2968
Jim Grosbach4a200922013-09-20 23:08:21 +00002969/// parseDirectiveCFIRestoreState
Eli Bendersky6ee13082013-01-15 22:59:42 +00002970/// ::= .cfi_remember_state
Jim Grosbach4a200922013-09-20 23:08:21 +00002971bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002972 getStreamer().EmitCFIRestoreState();
2973 return false;
2974}
2975
Jim Grosbach4a200922013-09-20 23:08:21 +00002976/// parseDirectiveCFISameValue
Eli Bendersky6ee13082013-01-15 22:59:42 +00002977/// ::= .cfi_same_value register
Jim Grosbach4a200922013-09-20 23:08:21 +00002978bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002979 int64_t Register = 0;
2980
Jim Grosbach4a200922013-09-20 23:08:21 +00002981 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002982 return true;
2983
2984 getStreamer().EmitCFISameValue(Register);
2985 return false;
2986}
2987
Jim Grosbach4a200922013-09-20 23:08:21 +00002988/// parseDirectiveCFIRestore
Eli Bendersky6ee13082013-01-15 22:59:42 +00002989/// ::= .cfi_restore register
Jim Grosbach4a200922013-09-20 23:08:21 +00002990bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00002991 int64_t Register = 0;
Jim Grosbach4a200922013-09-20 23:08:21 +00002992 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00002993 return true;
2994
2995 getStreamer().EmitCFIRestore(Register);
2996 return false;
2997}
2998
Jim Grosbach4a200922013-09-20 23:08:21 +00002999/// parseDirectiveCFIEscape
Eli Bendersky6ee13082013-01-15 22:59:42 +00003000/// ::= .cfi_escape expression[,...]
Jim Grosbach4a200922013-09-20 23:08:21 +00003001bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003002 std::string Values;
3003 int64_t CurrValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003004 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003005 return true;
3006
3007 Values.push_back((uint8_t)CurrValue);
3008
3009 while (getLexer().is(AsmToken::Comma)) {
3010 Lex();
3011
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003012 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003013 return true;
3014
3015 Values.push_back((uint8_t)CurrValue);
3016 }
3017
3018 getStreamer().EmitCFIEscape(Values);
3019 return false;
3020}
3021
Jim Grosbach4a200922013-09-20 23:08:21 +00003022/// parseDirectiveCFISignalFrame
Eli Bendersky6ee13082013-01-15 22:59:42 +00003023/// ::= .cfi_signal_frame
Jim Grosbach4a200922013-09-20 23:08:21 +00003024bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003025 if (getLexer().isNot(AsmToken::EndOfStatement))
3026 return Error(getLexer().getLoc(),
3027 "unexpected token in '.cfi_signal_frame'");
3028
3029 getStreamer().EmitCFISignalFrame();
3030 return false;
3031}
3032
Jim Grosbach4a200922013-09-20 23:08:21 +00003033/// parseDirectiveCFIUndefined
Eli Bendersky6ee13082013-01-15 22:59:42 +00003034/// ::= .cfi_undefined register
Jim Grosbach4a200922013-09-20 23:08:21 +00003035bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003036 int64_t Register = 0;
3037
Jim Grosbach4a200922013-09-20 23:08:21 +00003038 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003039 return true;
3040
3041 getStreamer().EmitCFIUndefined(Register);
3042 return false;
3043}
3044
Jim Grosbach4a200922013-09-20 23:08:21 +00003045/// parseDirectiveMacrosOnOff
Eli Bendersky6ee13082013-01-15 22:59:42 +00003046/// ::= .macros_on
3047/// ::= .macros_off
Jim Grosbach4a200922013-09-20 23:08:21 +00003048bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003049 if (getLexer().isNot(AsmToken::EndOfStatement))
3050 return Error(getLexer().getLoc(),
3051 "unexpected token in '" + Directive + "' directive");
3052
Jim Grosbach4a200922013-09-20 23:08:21 +00003053 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003054 return false;
3055}
3056
Jim Grosbach4a200922013-09-20 23:08:21 +00003057/// parseDirectiveMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003058/// ::= .macro name [parameters]
Jim Grosbach4a200922013-09-20 23:08:21 +00003059bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003060 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003061 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003062 return TokError("expected identifier in '.macro' directive");
3063
3064 MCAsmMacroParameters Parameters;
3065 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4a200922013-09-20 23:08:21 +00003066 // parseMacroArgument()
Eli Bendersky6ee13082013-01-15 22:59:42 +00003067 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3068 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3069 for (;;) {
3070 MCAsmMacroParameter Parameter;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003071 if (parseIdentifier(Parameter.first))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003072 return TokError("expected identifier in '.macro' directive");
3073
3074 if (getLexer().is(AsmToken::Equal)) {
3075 Lex();
Jim Grosbach4a200922013-09-20 23:08:21 +00003076 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003077 return true;
3078 }
3079
3080 Parameters.push_back(Parameter);
3081
3082 if (getLexer().is(AsmToken::Comma))
3083 Lex();
3084 else if (getLexer().is(AsmToken::EndOfStatement))
3085 break;
3086 }
3087 }
3088
3089 // Eat the end of statement.
3090 Lex();
3091
3092 AsmToken EndToken, StartToken = getTok();
3093
3094 // Lex the macro definition.
3095 for (;;) {
3096 // Check whether we have reached the end of the file.
3097 if (getLexer().is(AsmToken::Eof))
3098 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3099
3100 // Otherwise, check whether we have reach the .endmacro.
3101 if (getLexer().is(AsmToken::Identifier) &&
3102 (getTok().getIdentifier() == ".endm" ||
3103 getTok().getIdentifier() == ".endmacro")) {
3104 EndToken = getTok();
3105 Lex();
3106 if (getLexer().isNot(AsmToken::EndOfStatement))
3107 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3108 "' directive");
3109 break;
3110 }
3111
3112 // Otherwise, scan til the end of the statement.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003113 eatToEndOfStatement();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003114 }
3115
Jim Grosbach4a200922013-09-20 23:08:21 +00003116 if (lookupMacro(Name)) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003117 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3118 }
3119
3120 const char *BodyStart = StartToken.getLoc().getPointer();
3121 const char *BodyEnd = EndToken.getLoc().getPointer();
3122 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4a200922013-09-20 23:08:21 +00003123 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3124 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky6ee13082013-01-15 22:59:42 +00003125 return false;
3126}
3127
Jim Grosbach4a200922013-09-20 23:08:21 +00003128/// checkForBadMacro
Kevin Enderby221514e2013-01-22 21:44:53 +00003129///
3130/// With the support added for named parameters there may be code out there that
3131/// is transitioning from positional parameters. In versions of gas that did
3132/// not support named parameters they would be ignored on the macro defintion.
3133/// But to support both styles of parameters this is not possible so if a macro
3134/// defintion has named parameters but does not use them and has what appears
3135/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3136/// warning that the positional parameter found in body which have no effect.
3137/// Hoping the developer will either remove the named parameters from the macro
3138/// definiton so the positional parameters get used if that was what was
3139/// intended or change the macro to use the named parameters. It is possible
3140/// this warning will trigger when the none of the named parameters are used
3141/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4a200922013-09-20 23:08:21 +00003142void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby221514e2013-01-22 21:44:53 +00003143 StringRef Body,
3144 MCAsmMacroParameters Parameters) {
3145 // If this macro is not defined with named parameters the warning we are
3146 // checking for here doesn't apply.
3147 unsigned NParameters = Parameters.size();
3148 if (NParameters == 0)
3149 return;
3150
3151 bool NamedParametersFound = false;
3152 bool PositionalParametersFound = false;
3153
3154 // Look at the body of the macro for use of both the named parameters and what
3155 // are likely to be positional parameters. This is what expandMacro() is
3156 // doing when it finds the parameters in the body.
3157 while (!Body.empty()) {
3158 // Scan for the next possible parameter.
3159 std::size_t End = Body.size(), Pos = 0;
3160 for (; Pos != End; ++Pos) {
3161 // Check for a substitution or escape.
3162 // This macro is defined with parameters, look for \foo, \bar, etc.
3163 if (Body[Pos] == '\\' && Pos + 1 != End)
3164 break;
3165
3166 // This macro should have parameters, but look for $0, $1, ..., $n too.
3167 if (Body[Pos] != '$' || Pos + 1 == End)
3168 continue;
3169 char Next = Body[Pos + 1];
Guy Benyei87d0b9e2013-02-12 21:21:59 +00003170 if (Next == '$' || Next == 'n' ||
3171 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby221514e2013-01-22 21:44:53 +00003172 break;
3173 }
3174
3175 // Check if we reached the end.
3176 if (Pos == End)
3177 break;
3178
3179 if (Body[Pos] == '$') {
Jim Grosbach4a200922013-09-20 23:08:21 +00003180 switch (Body[Pos + 1]) {
3181 // $$ => $
Kevin Enderby221514e2013-01-22 21:44:53 +00003182 case '$':
3183 break;
3184
Jim Grosbach4a200922013-09-20 23:08:21 +00003185 // $n => number of arguments
Kevin Enderby221514e2013-01-22 21:44:53 +00003186 case 'n':
3187 PositionalParametersFound = true;
3188 break;
3189
Jim Grosbach4a200922013-09-20 23:08:21 +00003190 // $[0-9] => argument
Kevin Enderby221514e2013-01-22 21:44:53 +00003191 default: {
3192 PositionalParametersFound = true;
3193 break;
Jim Grosbach4a200922013-09-20 23:08:21 +00003194 }
Kevin Enderby221514e2013-01-22 21:44:53 +00003195 }
3196 Pos += 2;
3197 } else {
3198 unsigned I = Pos + 1;
3199 while (isIdentifierChar(Body[I]) && I + 1 != End)
3200 ++I;
3201
Jim Grosbach4a200922013-09-20 23:08:21 +00003202 const char *Begin = Body.data() + Pos + 1;
3203 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby221514e2013-01-22 21:44:53 +00003204 unsigned Index = 0;
3205 for (; Index < NParameters; ++Index)
3206 if (Parameters[Index].first == Argument)
3207 break;
3208
3209 if (Index == NParameters) {
Jim Grosbach4a200922013-09-20 23:08:21 +00003210 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3211 Pos += 3;
3212 else {
3213 Pos = I;
3214 }
Kevin Enderby221514e2013-01-22 21:44:53 +00003215 } else {
3216 NamedParametersFound = true;
3217 Pos += 1 + Argument.size();
3218 }
3219 }
3220 // Update the scan point.
3221 Body = Body.substr(Pos);
3222 }
3223
3224 if (!NamedParametersFound && PositionalParametersFound)
3225 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3226 "used in macro body, possible positional parameter "
3227 "found in body which will have no effect");
3228}
3229
Jim Grosbach4a200922013-09-20 23:08:21 +00003230/// parseDirectiveEndMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003231/// ::= .endm
3232/// ::= .endmacro
Jim Grosbach4a200922013-09-20 23:08:21 +00003233bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003234 if (getLexer().isNot(AsmToken::EndOfStatement))
3235 return TokError("unexpected token in '" + Directive + "' directive");
3236
3237 // If we are inside a macro instantiation, terminate the current
3238 // instantiation.
Jim Grosbach4a200922013-09-20 23:08:21 +00003239 if (isInsideMacroInstantiation()) {
3240 handleMacroExit();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003241 return false;
3242 }
3243
3244 // Otherwise, this .endmacro is a stray entry in the file; well formed
3245 // .endmacro directives are handled during the macro definition parsing.
3246 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4a200922013-09-20 23:08:21 +00003247 "no current macro definition");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003248}
3249
Jim Grosbach4a200922013-09-20 23:08:21 +00003250/// parseDirectivePurgeMacro
Eli Bendersky6ee13082013-01-15 22:59:42 +00003251/// ::= .purgem
Jim Grosbach4a200922013-09-20 23:08:21 +00003252bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky6ee13082013-01-15 22:59:42 +00003253 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003254 if (parseIdentifier(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003255 return TokError("expected identifier in '.purgem' directive");
3256
3257 if (getLexer().isNot(AsmToken::EndOfStatement))
3258 return TokError("unexpected token in '.purgem' directive");
3259
Jim Grosbach4a200922013-09-20 23:08:21 +00003260 if (!lookupMacro(Name))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003261 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3262
Jim Grosbach4a200922013-09-20 23:08:21 +00003263 undefineMacro(Name);
Eli Bendersky6ee13082013-01-15 22:59:42 +00003264 return false;
3265}
Eli Bendersky4766ef42012-12-20 19:05:53 +00003266
Jim Grosbach4a200922013-09-20 23:08:21 +00003267/// parseDirectiveBundleAlignMode
Eli Bendersky4766ef42012-12-20 19:05:53 +00003268/// ::= {.bundle_align_mode} expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003269bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003270 checkForValidSection();
Eli Bendersky4766ef42012-12-20 19:05:53 +00003271
3272 // Expect a single argument: an expression that evaluates to a constant
3273 // in the inclusive range 0-30.
3274 SMLoc ExprLoc = getLexer().getLoc();
3275 int64_t AlignSizePow2;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003276 if (parseAbsoluteExpression(AlignSizePow2))
Eli Bendersky4766ef42012-12-20 19:05:53 +00003277 return true;
3278 else if (getLexer().isNot(AsmToken::EndOfStatement))
3279 return TokError("unexpected token after expression in"
3280 " '.bundle_align_mode' directive");
3281 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3282 return Error(ExprLoc,
3283 "invalid bundle alignment size (expected between 0 and 30)");
3284
3285 Lex();
3286
3287 // Because of AlignSizePow2's verified range we can safely truncate it to
3288 // unsigned.
3289 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3290 return false;
3291}
3292
Jim Grosbach4a200922013-09-20 23:08:21 +00003293/// parseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003294/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4a200922013-09-20 23:08:21 +00003295bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003296 checkForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003297 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00003298
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003299 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3300 StringRef Option;
3301 SMLoc Loc = getTok().getLoc();
3302 const char *kInvalidOptionError =
Jim Grosbach4a200922013-09-20 23:08:21 +00003303 "invalid option for '.bundle_lock' directive";
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003304
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003305 if (parseIdentifier(Option))
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003306 return Error(Loc, kInvalidOptionError);
3307
3308 if (Option != "align_to_end")
3309 return Error(Loc, kInvalidOptionError);
3310 else if (getLexer().isNot(AsmToken::EndOfStatement))
3311 return Error(Loc,
3312 "unexpected token after '.bundle_lock' directive option");
3313 AlignToEnd = true;
3314 }
3315
Eli Bendersky4766ef42012-12-20 19:05:53 +00003316 Lex();
3317
Eli Bendersky6c1d4972013-01-07 21:51:08 +00003318 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00003319 return false;
3320}
3321
Jim Grosbach4a200922013-09-20 23:08:21 +00003322/// parseDirectiveBundleLock
Eli Bendersky4766ef42012-12-20 19:05:53 +00003323/// ::= {.bundle_lock}
Jim Grosbach4a200922013-09-20 23:08:21 +00003324bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003325 checkForValidSection();
Eli Bendersky4766ef42012-12-20 19:05:53 +00003326
3327 if (getLexer().isNot(AsmToken::EndOfStatement))
3328 return TokError("unexpected token in '.bundle_unlock' directive");
3329 Lex();
3330
3331 getStreamer().EmitBundleUnlock();
3332 return false;
3333}
3334
Jim Grosbach4a200922013-09-20 23:08:21 +00003335/// parseDirectiveSpace
Eli Bendersky6ee13082013-01-15 22:59:42 +00003336/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003337bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003338 checkForValidSection();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003339
3340 int64_t NumBytes;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003341 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003342 return true;
3343
3344 int64_t FillExpr = 0;
3345 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3346 if (getLexer().isNot(AsmToken::Comma))
3347 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3348 Lex();
3349
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003350 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003351 return true;
3352
3353 if (getLexer().isNot(AsmToken::EndOfStatement))
3354 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3355 }
3356
3357 Lex();
3358
3359 if (NumBytes <= 0)
Jim Grosbach4a200922013-09-20 23:08:21 +00003360 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3361 "' directive");
Eli Bendersky6ee13082013-01-15 22:59:42 +00003362
3363 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00003364 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky6ee13082013-01-15 22:59:42 +00003365
3366 return false;
3367}
3368
Jim Grosbach4a200922013-09-20 23:08:21 +00003369/// parseDirectiveLEB128
Eli Bendersky6ee13082013-01-15 22:59:42 +00003370/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003371bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003372 checkForValidSection();
Eli Bendersky6ee13082013-01-15 22:59:42 +00003373 const MCExpr *Value;
3374
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003375 if (parseExpression(Value))
Eli Bendersky6ee13082013-01-15 22:59:42 +00003376 return true;
3377
3378 if (getLexer().isNot(AsmToken::EndOfStatement))
3379 return TokError("unexpected token in directive");
3380
3381 if (Signed)
3382 getStreamer().EmitSLEB128Value(Value);
3383 else
3384 getStreamer().EmitULEB128Value(Value);
3385
3386 return false;
3387}
3388
Jim Grosbach4a200922013-09-20 23:08:21 +00003389/// parseDirectiveSymbolAttribute
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003390/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003391bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003392 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003393 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003394 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00003395 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003396
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003397 if (parseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00003398 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003399
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00003400 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003401
Jim Grosbach10ec6502011-09-15 17:56:49 +00003402 // Assembler local symbols don't make any sense here. Complain loudly.
3403 if (Sym->isTemporary())
3404 return Error(Loc, "non-local symbol required in directive");
3405
Saleem Abdulrasool1c9cd022013-08-09 01:52:03 +00003406 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3407 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003408
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003409 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003410 break;
3411
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003412 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003413 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003414 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003415 }
3416 }
3417
Sean Callanan79ed1a82010-01-19 20:22:31 +00003418 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00003419 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00003420}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003421
Jim Grosbach4a200922013-09-20 23:08:21 +00003422/// parseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00003423/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4a200922013-09-20 23:08:21 +00003424bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003425 checkForValidSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00003426
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003427 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00003428 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003429 if (parseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003430 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003431
Daniel Dunbar76c4d762009-07-31 21:55:09 +00003432 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00003433 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003434
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003435 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003436 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003437 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003438
3439 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003440 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003441 if (parseAbsoluteExpression(Size))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003442 return true;
3443
3444 int64_t Pow2Alignment = 0;
3445 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003446 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00003447 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003448 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003449 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003450 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003451
Benjamin Kramera9e37c52012-09-07 21:08:01 +00003452 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3453 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00003454 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3455
Chris Lattner258281d2010-01-19 06:22:22 +00003456 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00003457 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3458 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00003459 if (!isPowerOf2_64(Pow2Alignment))
3460 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3461 Pow2Alignment = Log2_64(Pow2Alignment);
3462 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003463 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003464
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003465 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00003466 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003467
Sean Callanan79ed1a82010-01-19 20:22:31 +00003468 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003469
Chris Lattner1fc3d752009-07-09 17:25:12 +00003470 // NOTE: a size of zero for a .comm should create a undefined symbol
3471 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003472 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00003473 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4a200922013-09-20 23:08:21 +00003474 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003475
Eric Christopherc260a3e2010-05-14 01:38:54 +00003476 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003477 // may internally end up wanting an alignment in bytes.
3478 // FIXME: Diagnose overflow.
3479 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00003480 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4a200922013-09-20 23:08:21 +00003481 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003482
Daniel Dunbar8906ff12009-08-22 07:22:36 +00003483 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003484 return Error(IDLoc, "invalid symbol redefinition");
3485
Chris Lattner1fc3d752009-07-09 17:25:12 +00003486 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00003487 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00003488 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00003489 return false;
3490 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003491
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003492 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00003493 return false;
3494}
Chris Lattner9be3fee2009-07-10 22:20:30 +00003495
Jim Grosbach4a200922013-09-20 23:08:21 +00003496/// parseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003497/// ::= .abort [... message ...]
Jim Grosbach4a200922013-09-20 23:08:21 +00003498bool AsmParser::parseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003499 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003500 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003501
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003502 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003503 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003504 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003505
Sean Callanan79ed1a82010-01-19 20:22:31 +00003506 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003507
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00003508 if (Str.empty())
3509 Error(Loc, ".abort detected. Assembly stopping.");
3510 else
3511 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00003512 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00003513
3514 return false;
3515}
Kevin Enderby71148242009-07-14 21:35:03 +00003516
Jim Grosbach4a200922013-09-20 23:08:21 +00003517/// parseDirectiveInclude
Kevin Enderby1f049b22009-07-14 23:21:55 +00003518/// ::= .include "filename"
Jim Grosbach4a200922013-09-20 23:08:21 +00003519bool AsmParser::parseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003520 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00003521 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003522
Yunzhong Gaoed119822013-09-05 19:14:26 +00003523 // Allow the strings to have escaped octal character sequence.
3524 std::string Filename;
3525 if (parseEscapedString(Filename))
3526 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003527 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00003528 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00003529
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003530 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00003531 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003532
Chris Lattner8e25e2d2009-07-16 06:14:39 +00003533 // Attempt to switch the lexer to the included file before consuming the end
3534 // of statement to avoid losing it when we switch.
Jim Grosbach4a200922013-09-20 23:08:21 +00003535 if (enterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00003536 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00003537 return true;
3538 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00003539
3540 return false;
3541}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00003542
Jim Grosbach4a200922013-09-20 23:08:21 +00003543/// parseDirectiveIncbin
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003544/// ::= .incbin "filename"
Jim Grosbach4a200922013-09-20 23:08:21 +00003545bool AsmParser::parseDirectiveIncbin() {
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003546 if (getLexer().isNot(AsmToken::String))
3547 return TokError("expected string in '.incbin' directive");
3548
Yunzhong Gaoed119822013-09-05 19:14:26 +00003549 // Allow the strings to have escaped octal character sequence.
3550 std::string Filename;
3551 if (parseEscapedString(Filename))
3552 return true;
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003553 SMLoc IncbinLoc = getLexer().getLoc();
3554 Lex();
3555
3556 if (getLexer().isNot(AsmToken::EndOfStatement))
3557 return TokError("unexpected token in '.incbin' directive");
3558
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003559 // Attempt to process the included file.
Jim Grosbach4a200922013-09-20 23:08:21 +00003560 if (processIncbinFile(Filename)) {
Kevin Enderbyc55acca2011-12-14 21:47:48 +00003561 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3562 return true;
3563 }
3564
3565 return false;
3566}
3567
Jim Grosbach4a200922013-09-20 23:08:21 +00003568/// parseDirectiveIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003569/// ::= .if expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003570bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003571 TheCondStack.push_back(TheCondState);
3572 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00003573 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003574 eatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00003575 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003576 int64_t ExprValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003577 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003578 return true;
3579
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003580 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003581 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003582
Sean Callanan79ed1a82010-01-19 20:22:31 +00003583 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003584
3585 TheCondState.CondMet = ExprValue;
3586 TheCondState.Ignore = !TheCondState.CondMet;
3587 }
3588
3589 return false;
3590}
3591
Jim Grosbach4a200922013-09-20 23:08:21 +00003592/// parseDirectiveIfb
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003593/// ::= .ifb string
Jim Grosbach4a200922013-09-20 23:08:21 +00003594bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003595 TheCondStack.push_back(TheCondState);
3596 TheCondState.TheCond = AsmCond::IfCond;
3597
Benjamin Kramer29739e72012-05-12 16:52:21 +00003598 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003599 eatToEndOfStatement();
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003600 } else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003601 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00003602
3603 if (getLexer().isNot(AsmToken::EndOfStatement))
3604 return TokError("unexpected token in '.ifb' directive");
3605
3606 Lex();
3607
3608 TheCondState.CondMet = ExpectBlank == Str.empty();
3609 TheCondState.Ignore = !TheCondState.CondMet;
3610 }
3611
3612 return false;
3613}
3614
Jim Grosbach4a200922013-09-20 23:08:21 +00003615/// parseDirectiveIfc
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003616/// ::= .ifc string1, string2
Jim Grosbach4a200922013-09-20 23:08:21 +00003617bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003618 TheCondStack.push_back(TheCondState);
3619 TheCondState.TheCond = AsmCond::IfCond;
3620
Benjamin Kramer29739e72012-05-12 16:52:21 +00003621 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003622 eatToEndOfStatement();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003623 } else {
Jim Grosbach4a200922013-09-20 23:08:21 +00003624 StringRef Str1 = parseStringToComma();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003625
3626 if (getLexer().isNot(AsmToken::Comma))
3627 return TokError("unexpected token in '.ifc' directive");
3628
3629 Lex();
3630
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003631 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003632
3633 if (getLexer().isNot(AsmToken::EndOfStatement))
3634 return TokError("unexpected token in '.ifc' directive");
3635
3636 Lex();
3637
3638 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3639 TheCondState.Ignore = !TheCondState.CondMet;
3640 }
3641
3642 return false;
3643}
3644
Jim Grosbach4a200922013-09-20 23:08:21 +00003645/// parseDirectiveIfdef
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00003646/// ::= .ifdef symbol
Jim Grosbach4a200922013-09-20 23:08:21 +00003647bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003648 StringRef Name;
3649 TheCondStack.push_back(TheCondState);
3650 TheCondState.TheCond = AsmCond::IfCond;
3651
3652 if (TheCondState.Ignore) {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003653 eatToEndOfStatement();
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003654 } else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003655 if (parseIdentifier(Name))
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00003656 return TokError("expected identifier after '.ifdef'");
3657
3658 Lex();
3659
3660 MCSymbol *Sym = getContext().LookupSymbol(Name);
3661
3662 if (expect_defined)
3663 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3664 else
3665 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3666 TheCondState.Ignore = !TheCondState.CondMet;
3667 }
3668
3669 return false;
3670}
3671
Jim Grosbach4a200922013-09-20 23:08:21 +00003672/// parseDirectiveElseIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003673/// ::= .elseif expression
Jim Grosbach4a200922013-09-20 23:08:21 +00003674bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003675 if (TheCondState.TheCond != AsmCond::IfCond &&
3676 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper955d1e92013-04-22 04:24:02 +00003677 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3678 " an .elseif");
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003679 TheCondState.TheCond = AsmCond::ElseIfCond;
3680
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003681 bool LastIgnoreState = false;
3682 if (!TheCondStack.empty())
Craig Topper955d1e92013-04-22 04:24:02 +00003683 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003684 if (LastIgnoreState || TheCondState.CondMet) {
3685 TheCondState.Ignore = true;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003686 eatToEndOfStatement();
Craig Topperefa703d2013-04-22 04:22:40 +00003687 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003688 int64_t ExprValue;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003689 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003690 return true;
3691
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003692 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003693 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003694
Sean Callanan79ed1a82010-01-19 20:22:31 +00003695 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003696 TheCondState.CondMet = ExprValue;
3697 TheCondState.Ignore = !TheCondState.CondMet;
3698 }
3699
3700 return false;
3701}
3702
Jim Grosbach4a200922013-09-20 23:08:21 +00003703/// parseDirectiveElse
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003704/// ::= .else
Jim Grosbach4a200922013-09-20 23:08:21 +00003705bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003706 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003707 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003708
Sean Callanan79ed1a82010-01-19 20:22:31 +00003709 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003710
3711 if (TheCondState.TheCond != AsmCond::IfCond &&
3712 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper955d1e92013-04-22 04:24:02 +00003713 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3714 ".elseif");
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003715 TheCondState.TheCond = AsmCond::ElseCond;
3716 bool LastIgnoreState = false;
3717 if (!TheCondStack.empty())
3718 LastIgnoreState = TheCondStack.back().Ignore;
3719 if (LastIgnoreState || TheCondState.CondMet)
3720 TheCondState.Ignore = true;
3721 else
3722 TheCondState.Ignore = false;
3723
3724 return false;
3725}
3726
Jim Grosbach4a200922013-09-20 23:08:21 +00003727/// parseDirectiveEndIf
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003728/// ::= .endif
Jim Grosbach4a200922013-09-20 23:08:21 +00003729bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00003730 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003731 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003732
Sean Callanan79ed1a82010-01-19 20:22:31 +00003733 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003734
Jim Grosbach4a200922013-09-20 23:08:21 +00003735 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyc114ed72009-08-07 22:46:00 +00003736 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3737 ".else");
3738 if (!TheCondStack.empty()) {
3739 TheCondState = TheCondStack.back();
3740 TheCondStack.pop_back();
3741 }
3742
3743 return false;
3744}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003745
Eli Bendersky6ee13082013-01-15 22:59:42 +00003746void AsmParser::initializeDirectiveKindMap() {
3747 DirectiveKindMap[".set"] = DK_SET;
3748 DirectiveKindMap[".equ"] = DK_EQU;
3749 DirectiveKindMap[".equiv"] = DK_EQUIV;
3750 DirectiveKindMap[".ascii"] = DK_ASCII;
3751 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3752 DirectiveKindMap[".string"] = DK_STRING;
3753 DirectiveKindMap[".byte"] = DK_BYTE;
3754 DirectiveKindMap[".short"] = DK_SHORT;
3755 DirectiveKindMap[".value"] = DK_VALUE;
3756 DirectiveKindMap[".2byte"] = DK_2BYTE;
3757 DirectiveKindMap[".long"] = DK_LONG;
3758 DirectiveKindMap[".int"] = DK_INT;
3759 DirectiveKindMap[".4byte"] = DK_4BYTE;
3760 DirectiveKindMap[".quad"] = DK_QUAD;
3761 DirectiveKindMap[".8byte"] = DK_8BYTE;
3762 DirectiveKindMap[".single"] = DK_SINGLE;
3763 DirectiveKindMap[".float"] = DK_FLOAT;
3764 DirectiveKindMap[".double"] = DK_DOUBLE;
3765 DirectiveKindMap[".align"] = DK_ALIGN;
3766 DirectiveKindMap[".align32"] = DK_ALIGN32;
3767 DirectiveKindMap[".balign"] = DK_BALIGN;
3768 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3769 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3770 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3771 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3772 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3773 DirectiveKindMap[".org"] = DK_ORG;
3774 DirectiveKindMap[".fill"] = DK_FILL;
3775 DirectiveKindMap[".zero"] = DK_ZERO;
3776 DirectiveKindMap[".extern"] = DK_EXTERN;
3777 DirectiveKindMap[".globl"] = DK_GLOBL;
3778 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky6ee13082013-01-15 22:59:42 +00003779 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3780 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3781 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3782 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3783 DirectiveKindMap[".reference"] = DK_REFERENCE;
3784 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3785 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3786 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3787 DirectiveKindMap[".comm"] = DK_COMM;
3788 DirectiveKindMap[".common"] = DK_COMMON;
3789 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3790 DirectiveKindMap[".abort"] = DK_ABORT;
3791 DirectiveKindMap[".include"] = DK_INCLUDE;
3792 DirectiveKindMap[".incbin"] = DK_INCBIN;
3793 DirectiveKindMap[".code16"] = DK_CODE16;
3794 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3795 DirectiveKindMap[".rept"] = DK_REPT;
3796 DirectiveKindMap[".irp"] = DK_IRP;
3797 DirectiveKindMap[".irpc"] = DK_IRPC;
3798 DirectiveKindMap[".endr"] = DK_ENDR;
3799 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3800 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3801 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3802 DirectiveKindMap[".if"] = DK_IF;
3803 DirectiveKindMap[".ifb"] = DK_IFB;
3804 DirectiveKindMap[".ifnb"] = DK_IFNB;
3805 DirectiveKindMap[".ifc"] = DK_IFC;
3806 DirectiveKindMap[".ifnc"] = DK_IFNC;
3807 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3808 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3809 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3810 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3811 DirectiveKindMap[".else"] = DK_ELSE;
3812 DirectiveKindMap[".endif"] = DK_ENDIF;
3813 DirectiveKindMap[".skip"] = DK_SKIP;
3814 DirectiveKindMap[".space"] = DK_SPACE;
3815 DirectiveKindMap[".file"] = DK_FILE;
3816 DirectiveKindMap[".line"] = DK_LINE;
3817 DirectiveKindMap[".loc"] = DK_LOC;
3818 DirectiveKindMap[".stabs"] = DK_STABS;
3819 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3820 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3821 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3822 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3823 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3824 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3825 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3826 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3827 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3828 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3829 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3830 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3831 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3832 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3833 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3834 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3835 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3836 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3837 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3838 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3839 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju83ba58e2013-09-26 14:49:40 +00003840 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky6ee13082013-01-15 22:59:42 +00003841 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3842 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3843 DirectiveKindMap[".macro"] = DK_MACRO;
3844 DirectiveKindMap[".endm"] = DK_ENDM;
3845 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3846 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00003847}
3848
Jim Grosbach4a200922013-09-20 23:08:21 +00003849MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003850 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003851
Rafael Espindola761cb062012-06-03 23:57:14 +00003852 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003853 for (;;) {
3854 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003855 if (getLexer().is(AsmToken::Eof)) {
3856 Error(DirectiveLoc, "no matching '.endr' in definition");
3857 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003858 }
3859
Rafael Espindola761cb062012-06-03 23:57:14 +00003860 if (Lexer.is(AsmToken::Identifier) &&
3861 (getTok().getIdentifier() == ".rept")) {
3862 ++NestLevel;
3863 }
3864
3865 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4a200922013-09-20 23:08:21 +00003866 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola761cb062012-06-03 23:57:14 +00003867 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003868 EndToken = getTok();
3869 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003870 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3871 TokError("unexpected token in '.endr' directive");
3872 return 0;
3873 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003874 break;
3875 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003876 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003877 }
3878
Rafael Espindola761cb062012-06-03 23:57:14 +00003879 // Otherwise, scan till the end of the statement.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003880 eatToEndOfStatement();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003881 }
3882
3883 const char *BodyStart = StartToken.getLoc().getPointer();
3884 const char *BodyEnd = EndToken.getLoc().getPointer();
3885 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3886
Rafael Espindola761cb062012-06-03 23:57:14 +00003887 // We Are Anonymous.
3888 StringRef Name;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003889 MCAsmMacroParameters Parameters;
Benjamin Kramera2b0c332013-08-04 09:06:29 +00003890 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3891 return &MacroLikeBodies.back();
Rafael Espindola761cb062012-06-03 23:57:14 +00003892}
3893
Jim Grosbach4a200922013-09-20 23:08:21 +00003894void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +00003895 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003896 OS << ".endr\n";
3897
3898 MemoryBuffer *Instantiation =
Jim Grosbach4a200922013-09-20 23:08:21 +00003899 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003900
Rafael Espindola761cb062012-06-03 23:57:14 +00003901 // Create the macro instantiation object and add to the current macro
3902 // instantiation stack.
Jim Grosbach4a200922013-09-20 23:08:21 +00003903 MacroInstantiation *MI = new MacroInstantiation(
3904 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola761cb062012-06-03 23:57:14 +00003905 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003906
Rafael Espindola761cb062012-06-03 23:57:14 +00003907 // Jump to the macro instantiation and prime the lexer.
3908 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3909 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3910 Lex();
3911}
3912
Jim Grosbach4a200922013-09-20 23:08:21 +00003913bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc) {
Rafael Espindola761cb062012-06-03 23:57:14 +00003914 int64_t Count;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003915 if (parseAbsoluteExpression(Count))
Rafael Espindola761cb062012-06-03 23:57:14 +00003916 return TokError("unexpected token in '.rept' directive");
3917
3918 if (Count < 0)
3919 return TokError("Count is negative");
3920
3921 if (Lexer.isNot(AsmToken::EndOfStatement))
3922 return TokError("unexpected token in '.rept' directive");
3923
3924 // Eat the end of statement.
3925 Lex();
3926
3927 // Lex the rept definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003928 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola761cb062012-06-03 23:57:14 +00003929 if (!M)
3930 return true;
3931
3932 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3933 // to hold the macro body with substitutions.
3934 SmallString<256> Buf;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003935 MCAsmMacroParameters Parameters;
3936 MCAsmMacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003937 raw_svector_ostream OS(Buf);
3938 while (Count--) {
3939 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3940 return true;
3941 }
Jim Grosbach4a200922013-09-20 23:08:21 +00003942 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003943
3944 return false;
3945}
3946
Jim Grosbach4a200922013-09-20 23:08:21 +00003947/// parseDirectiveIrp
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003948/// ::= .irp symbol,values
Jim Grosbach4a200922013-09-20 23:08:21 +00003949bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003950 MCAsmMacroParameters Parameters;
3951 MCAsmMacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003952
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003953 if (parseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003954 return TokError("expected identifier in '.irp' directive");
3955
3956 Parameters.push_back(Parameter);
3957
3958 if (Lexer.isNot(AsmToken::Comma))
3959 return TokError("expected comma in '.irp' directive");
3960
3961 Lex();
3962
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003963 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00003964 if (parseMacroArguments(0, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003965 return true;
3966
3967 // Eat the end of statement.
3968 Lex();
3969
3970 // Lex the irp definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00003971 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003972 if (!M)
3973 return true;
3974
3975 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3976 // to hold the macro body with substitutions.
3977 SmallString<256> Buf;
3978 raw_svector_ostream OS(Buf);
3979
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003980 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3981 MCAsmMacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003982 Args.push_back(*i);
3983
3984 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3985 return true;
3986 }
3987
Jim Grosbach4a200922013-09-20 23:08:21 +00003988 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003989
3990 return false;
3991}
3992
Jim Grosbach4a200922013-09-20 23:08:21 +00003993/// parseDirectiveIrpc
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003994/// ::= .irpc symbol,values
Jim Grosbach4a200922013-09-20 23:08:21 +00003995bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003996 MCAsmMacroParameters Parameters;
3997 MCAsmMacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003998
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00003999 if (parseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004000 return TokError("expected identifier in '.irpc' directive");
4001
4002 Parameters.push_back(Parameter);
4003
4004 if (Lexer.isNot(AsmToken::Comma))
4005 return TokError("expected comma in '.irpc' directive");
4006
4007 Lex();
4008
Eli Benderskyc0c67b02013-01-14 23:22:36 +00004009 MCAsmMacroArguments A;
Jim Grosbach4a200922013-09-20 23:08:21 +00004010 if (parseMacroArguments(0, A))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004011 return true;
4012
4013 if (A.size() != 1 || A.front().size() != 1)
4014 return TokError("unexpected token in '.irpc' directive");
4015
4016 // Eat the end of statement.
4017 Lex();
4018
4019 // Lex the irpc definition.
Jim Grosbach4a200922013-09-20 23:08:21 +00004020 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004021 if (!M)
4022 return true;
4023
4024 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4025 // to hold the macro body with substitutions.
4026 SmallString<256> Buf;
4027 raw_svector_ostream OS(Buf);
4028
4029 StringRef Values = A.front().front().getString();
4030 std::size_t I, End = Values.size();
4031 for (I = 0; I < End; ++I) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00004032 MCAsmMacroArgument Arg;
Jim Grosbach4a200922013-09-20 23:08:21 +00004033 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004034
Eli Benderskyc0c67b02013-01-14 23:22:36 +00004035 MCAsmMacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004036 Args.push_back(Arg);
4037
4038 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4039 return true;
4040 }
4041
Jim Grosbach4a200922013-09-20 23:08:21 +00004042 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00004043
4044 return false;
4045}
4046
Jim Grosbach4a200922013-09-20 23:08:21 +00004047bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola761cb062012-06-03 23:57:14 +00004048 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00004049 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004050
4051 // The only .repl that should get here are the ones created by
Jim Grosbach4a200922013-09-20 23:08:21 +00004052 // instantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004053 assert(getLexer().is(AsmToken::EndOfStatement));
4054
Jim Grosbach4a200922013-09-20 23:08:21 +00004055 handleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00004056 return false;
4057}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00004058
Jim Grosbach4a200922013-09-20 23:08:21 +00004059bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer75234372013-02-15 20:37:21 +00004060 size_t Len) {
Eli Friedman2128aae2012-10-22 23:58:19 +00004061 const MCExpr *Value;
4062 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00004063 if (parseExpression(Value))
Eli Friedman2128aae2012-10-22 23:58:19 +00004064 return true;
4065 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4066 if (!MCE)
4067 return Error(ExprLoc, "unexpected expression in _emit");
4068 uint64_t IntValue = MCE->getValue();
4069 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4070 return Error(ExprLoc, "literal value out of range for directive");
4071
Chad Rosier469b1442013-02-12 21:33:51 +00004072 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4073 return false;
4074}
4075
Jim Grosbach4a200922013-09-20 23:08:21 +00004076bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosier469b1442013-02-12 21:33:51 +00004077 const MCExpr *Value;
4078 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00004079 if (parseExpression(Value))
Chad Rosier469b1442013-02-12 21:33:51 +00004080 return true;
4081 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4082 if (!MCE)
4083 return Error(ExprLoc, "unexpected expression in align");
4084 uint64_t IntValue = MCE->getValue();
4085 if (!isPowerOf2_64(IntValue))
4086 return Error(ExprLoc, "literal value not a power of two greater then zero");
4087
Jim Grosbach4a200922013-09-20 23:08:21 +00004088 Info.AsmRewrites->push_back(
4089 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman2128aae2012-10-22 23:58:19 +00004090 return false;
4091}
4092
Chad Rosier19aa3e32013-02-13 21:27:17 +00004093// We are comparing pointers, but the pointers are relative to a single string.
4094// Thus, this should always be deterministic.
Benjamin Kramer0d293e42013-09-22 14:09:50 +00004095static int rewritesSort(const AsmRewrite *AsmRewriteA,
4096 const AsmRewrite *AsmRewriteB) {
Chad Rosierabde6752013-02-13 18:38:58 +00004097 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4098 return -1;
4099 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4100 return 1;
Chad Rosierb54562b2013-02-15 22:54:16 +00004101
Chad Rosier6b369ce2013-04-08 17:43:47 +00004102 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4103 // rewrite to the same location. Make sure the SizeDirective rewrite is
4104 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4105 // ensures the sort algorithm is stable.
Jim Grosbach4a200922013-09-20 23:08:21 +00004106 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4107 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosierb54562b2013-02-15 22:54:16 +00004108 return -1;
Chad Rosier6b369ce2013-04-08 17:43:47 +00004109
Jim Grosbach4a200922013-09-20 23:08:21 +00004110 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4111 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosierb54562b2013-02-15 22:54:16 +00004112 return 1;
Jim Grosbach4a200922013-09-20 23:08:21 +00004113 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb1953982013-02-13 01:03:13 +00004114}
4115
Jim Grosbach4a200922013-09-20 23:08:21 +00004116bool AsmParser::parseMSInlineAsm(
4117 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4118 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4119 SmallVectorImpl<std::string> &Constraints,
4120 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4121 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004122 SmallVector<void *, 4> InputDecls;
4123 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00004124 SmallVector<bool, 4> InputDeclsAddressOf;
4125 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004126 SmallVector<std::string, 4> InputConstraints;
4127 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer75234372013-02-15 20:37:21 +00004128 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004129
Benjamin Kramer75234372013-02-15 20:37:21 +00004130 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004131
4132 // Prime the lexer.
4133 Lex();
4134
4135 // While we have input, parse each statement.
4136 unsigned InputIdx = 0;
4137 unsigned OutputIdx = 0;
4138 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00004139 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4a200922013-09-20 23:08:21 +00004140 if (parseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00004141 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004142
Chad Rosier57498012012-12-12 22:45:52 +00004143 if (Info.ParseError)
4144 return true;
4145
Benjamin Kramer75234372013-02-15 20:37:21 +00004146 if (Info.Opcode == ~0U)
4147 continue;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004148
Benjamin Kramer75234372013-02-15 20:37:21 +00004149 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004150
Benjamin Kramer75234372013-02-15 20:37:21 +00004151 // Build the list of clobbers, outputs and inputs.
4152 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4153 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004154
Benjamin Kramer75234372013-02-15 20:37:21 +00004155 // Immediate.
Chad Rosier811ddf62013-03-19 21:58:18 +00004156 if (Operand->isImm())
Benjamin Kramer75234372013-02-15 20:37:21 +00004157 continue;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004158
Benjamin Kramer75234372013-02-15 20:37:21 +00004159 // Register operand.
4160 if (Operand->isReg() && !Operand->needAddressOf()) {
4161 unsigned NumDefs = Desc.getNumDefs();
4162 // Clobber.
4163 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4164 ClobberRegs.push_back(Operand->getReg());
4165 continue;
4166 }
4167
4168 // Expr/Input or Output.
Chad Rosierb976e402013-04-09 17:53:49 +00004169 StringRef SymName = Operand->getSymName();
4170 if (SymName.empty())
4171 continue;
4172
Chad Rosier087c3092013-04-22 22:12:12 +00004173 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer75234372013-02-15 20:37:21 +00004174 if (!OpDecl)
4175 continue;
4176
4177 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierb976e402013-04-09 17:53:49 +00004178 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer75234372013-02-15 20:37:21 +00004179 if (isOutput) {
4180 ++InputIdx;
4181 OutputDecls.push_back(OpDecl);
4182 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4183 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosierb976e402013-04-09 17:53:49 +00004184 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer75234372013-02-15 20:37:21 +00004185 } else {
4186 InputDecls.push_back(OpDecl);
4187 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4188 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosierb976e402013-04-09 17:53:49 +00004189 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00004190 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00004191 }
4192 }
4193
4194 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00004195 NumOutputs = OutputDecls.size();
4196 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00004197
4198 // Set the unique clobbers.
Benjamin Kramer75234372013-02-15 20:37:21 +00004199 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4200 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4201 ClobberRegs.end());
4202 Clobbers.assign(ClobberRegs.size(), std::string());
4203 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4204 raw_string_ostream OS(Clobbers[I]);
4205 IP->printRegName(OS, ClobberRegs[I]);
4206 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00004207
4208 // Merge the various outputs and inputs. Output are expected first.
4209 if (NumOutputs || NumInputs) {
4210 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00004211 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004212 Constraints.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004213 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00004214 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier1c99a7f2013-01-15 23:07:53 +00004215 Constraints[i] = OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004216 }
4217 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00004218 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier1c99a7f2013-01-15 23:07:53 +00004219 Constraints[j] = InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00004220 }
4221 }
4222
4223 // Build the IR assembly string.
4224 std::string AsmStringIR;
4225 raw_string_ostream OS(AsmStringIR);
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004226 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4227 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4a200922013-09-20 23:08:21 +00004228 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer75234372013-02-15 20:37:21 +00004229 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4230 E = AsmStrRewrites.end();
4231 I != E; ++I) {
Chad Rosierdda4b6b2013-04-12 16:26:42 +00004232 AsmRewriteKind Kind = (*I).Kind;
4233 if (Kind == AOK_Delete)
4234 continue;
4235
Chad Rosierb1f8c132012-10-18 15:49:34 +00004236 const char *Loc = (*I).Loc.getPointer();
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004237 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier96d58e62012-10-19 20:57:14 +00004238
Chad Rosier023c8802013-03-19 17:32:17 +00004239 // Emit everything up to the immediate/expression.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004240 unsigned Len = Loc - AsmStart;
Chad Rosierf06cc982013-04-11 21:49:30 +00004241 if (Len)
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004242 OS << StringRef(AsmStart, Len);
Chad Rosier96d58e62012-10-19 20:57:14 +00004243
Chad Rosier5a719fc2012-10-23 17:43:43 +00004244 // Skip the original expression.
4245 if (Kind == AOK_Skip) {
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004246 AsmStart = Loc + (*I).Len;
Chad Rosier5a719fc2012-10-23 17:43:43 +00004247 continue;
4248 }
4249
Chad Rosierdda4b6b2013-04-12 16:26:42 +00004250 unsigned AdditionalSkip = 0;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004251 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004252 switch (Kind) {
Jim Grosbach4a200922013-09-20 23:08:21 +00004253 default:
4254 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004255 case AOK_Imm:
Benjamin Kramer75234372013-02-15 20:37:21 +00004256 OS << "$$" << (*I).Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +00004257 break;
4258 case AOK_ImmPrefix:
Benjamin Kramer75234372013-02-15 20:37:21 +00004259 OS << "$$";
Chad Rosierb1f8c132012-10-18 15:49:34 +00004260 break;
4261 case AOK_Input:
Benjamin Kramer75234372013-02-15 20:37:21 +00004262 OS << '$' << InputIdx++;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004263 break;
4264 case AOK_Output:
Benjamin Kramer75234372013-02-15 20:37:21 +00004265 OS << '$' << OutputIdx++;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004266 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004267 case AOK_SizeDirective:
Benjamin Kramer75234372013-02-15 20:37:21 +00004268 switch ((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004269 default: break;
4270 case 8: OS << "byte ptr "; break;
4271 case 16: OS << "word ptr "; break;
4272 case 32: OS << "dword ptr "; break;
4273 case 64: OS << "qword ptr "; break;
4274 case 80: OS << "xword ptr "; break;
4275 case 128: OS << "xmmword ptr "; break;
4276 case 256: OS << "ymmword ptr "; break;
4277 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004278 break;
4279 case AOK_Emit:
4280 OS << ".byte";
4281 break;
Chad Rosier469b1442013-02-12 21:33:51 +00004282 case AOK_Align: {
4283 unsigned Val = (*I).Val;
4284 OS << ".align " << Val;
4285
4286 // Skip the original immediate.
Benjamin Kramer75234372013-02-15 20:37:21 +00004287 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosier469b1442013-02-12 21:33:51 +00004288 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4289 break;
4290 }
Chad Rosier6a020a72012-10-25 20:41:34 +00004291 case AOK_DotOperator:
4292 OS << (*I).Val;
4293 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004294 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004295
Chad Rosierb1f8c132012-10-18 15:49:34 +00004296 // Skip the original expression.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004297 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004298 }
4299
4300 // Emit the remainder of the asm string.
Chad Rosier0f7ccd22013-03-19 21:12:14 +00004301 if (AsmStart != AsmEnd)
4302 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosierb1f8c132012-10-18 15:49:34 +00004303
4304 AsmString = OS.str();
4305 return false;
4306}
4307
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004308/// \brief Create an MCAsmParser instance.
Jim Grosbach4a200922013-09-20 23:08:21 +00004309MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4310 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004311 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004312}