blob: fe3969a0a78977a17b3c3103f76990c1f31ef6c3 [file] [log] [blame]
Chris Lattnerb0133452009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2af16532010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Chad Rosiereb5c1682013-02-13 18:38:58 +000016#include "llvm/ADT/STLExtras.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000017#include "llvm/ADT/StringMap.h"
Daniel Dunbareb6bb322009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng11424442011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar115e4d62009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Chad Rosier8bce6642012-10-18 15:49:34 +000023#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrInfo.h"
Rafael Espindolae28610d2013-12-09 20:26:40 +000025#include "llvm/MC/MCObjectFileInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000026#include "llvm/MC/MCParser/AsmCond.h"
27#include "llvm/MC/MCParser/AsmLexer.h"
28#include "llvm/MC/MCParser/MCAsmParser.h"
29#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Cheng76792992011-07-20 05:58:47 +000030#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000031#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarca29e4d2009-06-23 22:01:43 +000032#include "llvm/MC/MCStreamer.h"
Daniel Dunbarae7ac012009-06-29 23:43:14 +000033#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000034#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000035#include "llvm/Support/CommandLine.h"
Benjamin Kramer4efe5062012-01-28 15:28:41 +000036#include "llvm/Support/ErrorHandling.h"
Jim Grosbach76346c32011-06-29 16:05:14 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye233dda2010-06-28 21:45:58 +000038#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000039#include "llvm/Support/SourceMgr.h"
Chris Lattner36e02122009-06-21 20:54:55 +000040#include "llvm/Support/raw_ostream.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000041#include <cctype>
Chad Rosier8bce6642012-10-18 15:49:34 +000042#include <set>
43#include <string>
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +000044#include <vector>
Chris Lattnerb0133452009-06-21 20:16:42 +000045using namespace llvm;
46
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +000047static cl::opt<bool>
48FatalAssemblerWarnings("fatal-assembler-warnings",
49 cl::desc("Consider warnings as error"));
50
Eric Christophera7c32732012-12-18 00:30:54 +000051MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewyckyac612272012-10-19 07:00:09 +000052
Daniel Dunbar86033402010-07-12 17:54:38 +000053namespace {
54
Eli Benderskya313ae62013-01-16 18:56:50 +000055/// \brief Helper types for tracking macro definitions.
56typedef std::vector<AsmToken> MCAsmMacroArgument;
57typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
58typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter;
59typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
60
61struct MCAsmMacro {
62 StringRef Name;
63 StringRef Body;
64 MCAsmMacroParameters Parameters;
65
66public:
67 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) :
68 Name(N), Body(B), Parameters(P) {}
69
70 MCAsmMacro(const MCAsmMacro& Other)
71 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {}
72};
73
Daniel Dunbar43235712010-07-18 18:54:11 +000074/// \brief Helper class for storing information about an active macro
75/// instantiation.
76struct MacroInstantiation {
77 /// The macro being instantiated.
Eli Bendersky38274122013-01-14 23:22:36 +000078 const MCAsmMacro *TheMacro;
Daniel Dunbar43235712010-07-18 18:54:11 +000079
80 /// The macro instantiation with substitutions.
81 MemoryBuffer *Instantiation;
82
83 /// The location of the instantiation.
84 SMLoc InstantiationLoc;
85
Daniel Dunbar40f1d852012-12-01 01:38:48 +000086 /// The buffer where parsing should resume upon instantiation completion.
87 int ExitBuffer;
88
Daniel Dunbar43235712010-07-18 18:54:11 +000089 /// The location where parsing should resume upon instantiation completion.
90 SMLoc ExitLoc;
91
92public:
Eli Bendersky38274122013-01-14 23:22:36 +000093 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola1134ab232011-06-05 02:43:45 +000094 MemoryBuffer *I);
Daniel Dunbar43235712010-07-18 18:54:11 +000095};
96
Eli Friedman0f4871d2012-10-22 23:58:19 +000097struct ParseStatementInfo {
Jim Grosbach4b905842013-09-20 23:08:21 +000098 /// \brief The parsed operands from the last parsed statement.
Eli Friedman0f4871d2012-10-22 23:58:19 +000099 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
100
Jim Grosbach4b905842013-09-20 23:08:21 +0000101 /// \brief The opcode from the last parsed instruction.
Eli Friedman0f4871d2012-10-22 23:58:19 +0000102 unsigned Opcode;
103
Jim Grosbach4b905842013-09-20 23:08:21 +0000104 /// \brief Was there an error parsing the inline assembly?
Chad Rosier149e8e02012-12-12 22:45:52 +0000105 bool ParseError;
106
Eli Friedman0f4871d2012-10-22 23:58:19 +0000107 SmallVectorImpl<AsmRewrite> *AsmRewrites;
108
Chad Rosier149e8e02012-12-12 22:45:52 +0000109 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000110 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier149e8e02012-12-12 22:45:52 +0000111 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman0f4871d2012-10-22 23:58:19 +0000112
113 ~ParseStatementInfo() {
114 // Free any parsed operands.
115 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
116 delete ParsedOperands[i];
117 ParsedOperands.clear();
118 }
119};
120
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000121/// \brief The concrete assembly parser instance.
122class AsmParser : public MCAsmParser {
Craig Topper2e6644c2012-09-15 16:23:52 +0000123 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
124 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000125private:
126 AsmLexer Lexer;
127 MCContext &Ctx;
128 MCStreamer &Out;
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000129 const MCAsmInfo &MAI;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000130 SourceMgr &SrcMgr;
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000131 SourceMgr::DiagHandlerTy SavedDiagHandler;
132 void *SavedDiagContext;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000133 MCAsmParserExtension *PlatformParser;
Rafael Espindola82065cb2011-04-11 21:49:50 +0000134
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000135 /// This is the current buffer index we're lexing from as managed by the
136 /// SourceMgr object.
137 int CurBuffer;
138
139 AsmCond TheCondState;
140 std::vector<AsmCond> TheCondStack;
141
Jim Grosbach4b905842013-09-20 23:08:21 +0000142 /// \brief maps directive names to handler methods in parser
Eli Bendersky17233942013-01-15 22:59:42 +0000143 /// extensions. Extensions register themselves in this map by calling
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000144 /// addDirectiveHandler.
Eli Bendersky17233942013-01-15 22:59:42 +0000145 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000146
Jim Grosbach4b905842013-09-20 23:08:21 +0000147 /// \brief Map of currently defined macros.
Eli Bendersky38274122013-01-14 23:22:36 +0000148 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbarc1f58ec2010-07-18 18:47:21 +0000149
Jim Grosbach4b905842013-09-20 23:08:21 +0000150 /// \brief Stack of active macro instantiations.
Daniel Dunbar43235712010-07-18 18:54:11 +0000151 std::vector<MacroInstantiation*> ActiveMacros;
152
Jim Grosbach4b905842013-09-20 23:08:21 +0000153 /// \brief List of bodies of anonymous macros.
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +0000154 std::deque<MCAsmMacro> MacroLikeBodies;
155
Daniel Dunbar828984f2010-07-18 18:38:02 +0000156 /// Boolean tracking whether macro substitution is enabled.
Eli Benderskyc2f6f922013-01-14 18:08:41 +0000157 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar828984f2010-07-18 18:38:02 +0000158
Daniel Dunbar43325c42010-09-09 22:42:56 +0000159 /// Flag tracking whether any errors have been encountered.
160 unsigned HadError : 1;
161
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000162 /// The values from the last parsed cpp hash file line comment if any.
163 StringRef CppHashFilename;
164 int64_t CppHashLineNumber;
165 SMLoc CppHashLoc;
Kevin Enderby27121c12012-11-05 21:55:41 +0000166 int CppHashBuf;
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000167 /// When generating dwarf for assembly source files we need to calculate the
168 /// logical line number based on the last parsed cpp hash file line comment
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000169 /// and current line. Since this is slow and messes up the SourceMgr's
Kevin Enderby0fd064c2013-06-21 20:51:39 +0000170 /// cache we save the last info we queried with SrcMgr.FindLineNumber().
171 SMLoc LastQueryIDLoc;
172 int LastQueryBuffer;
173 unsigned LastQueryLine;
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000174
Devang Patela173ee52012-01-31 18:14:05 +0000175 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
176 unsigned AssemblerDialect;
177
Jim Grosbach4b905842013-09-20 23:08:21 +0000178 /// \brief is Darwin compatibility enabled?
Preston Gurd05500642012-09-19 20:36:12 +0000179 bool IsDarwin;
180
Jim Grosbach4b905842013-09-20 23:08:21 +0000181 /// \brief Are we parsing ms-style inline assembly?
Chad Rosier49963552012-10-13 00:26:04 +0000182 bool ParsingInlineAsm;
183
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000184public:
Jim Grosbach345768c2011-08-16 18:33:49 +0000185 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000186 const MCAsmInfo &MAI);
Craig Topper5f96ca52012-08-29 05:48:09 +0000187 virtual ~AsmParser();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000188
189 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
190
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000191 virtual void addDirectiveHandler(StringRef Directive,
Eli Bendersky29b9f472013-01-16 00:50:52 +0000192 ExtensionDirectiveHandler Handler) {
193 ExtensionDirectiveMap[Directive] = Handler;
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000194 }
195
196public:
197 /// @name MCAsmParser Interface
198 /// {
199
200 virtual SourceMgr &getSourceManager() { return SrcMgr; }
201 virtual MCAsmLexer &getLexer() { return Lexer; }
202 virtual MCContext &getContext() { return Ctx; }
203 virtual MCStreamer &getStreamer() { return Out; }
Eric Christophera7c32732012-12-18 00:30:54 +0000204 virtual unsigned getAssemblerDialect() {
Devang Patela173ee52012-01-31 18:14:05 +0000205 if (AssemblerDialect == ~0U)
Eric Christophera7c32732012-12-18 00:30:54 +0000206 return MAI.getAssemblerDialect();
Devang Patela173ee52012-01-31 18:14:05 +0000207 else
208 return AssemblerDialect;
209 }
210 virtual void setAssemblerDialect(unsigned i) {
211 AssemblerDialect = i;
212 }
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000213
Chris Lattnera3a06812011-10-16 04:47:35 +0000214 virtual bool Warning(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000215 ArrayRef<SMRange> Ranges = None);
Chris Lattnera3a06812011-10-16 04:47:35 +0000216 virtual bool Error(SMLoc L, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000217 ArrayRef<SMRange> Ranges = None);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000218
Craig Topper5f96ca52012-08-29 05:48:09 +0000219 virtual const AsmToken &Lex();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000220
Chad Rosier49963552012-10-13 00:26:04 +0000221 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosiere4ad2a02012-10-16 20:16:20 +0000222 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosier8bce6642012-10-18 15:49:34 +0000223
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000224 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString,
Chad Rosier8bce6642012-10-18 15:49:34 +0000225 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier37e755c2012-10-23 17:43:43 +0000226 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosier8bce6642012-10-18 15:49:34 +0000227 SmallVectorImpl<std::string> &Constraints,
Chad Rosier8bce6642012-10-18 15:49:34 +0000228 SmallVectorImpl<std::string> &Clobbers,
229 const MCInstrInfo *MII,
230 const MCInstPrinter *IP,
231 MCAsmParserSemaCallback &SI);
Chad Rosier49963552012-10-13 00:26:04 +0000232
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000233 bool parseExpression(const MCExpr *&Res);
234 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc);
Chad Rosier1863f4f2013-04-10 17:35:30 +0000235 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000236 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
237 virtual bool parseAbsoluteExpression(int64_t &Res);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000238
Jim Grosbach4b905842013-09-20 23:08:21 +0000239 /// \brief Parse an identifier or string (as a quoted identifier)
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000240 /// and set \p Res to the identifier contents.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000241 virtual bool parseIdentifier(StringRef &Res);
242 virtual void eatToEndOfStatement();
Eli Bendersky0cf0cb92013-01-12 00:05:00 +0000243
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000244 virtual void checkForValidSection();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000245 /// }
246
247private:
Daniel Dunbare5444a82010-09-09 22:42:59 +0000248
Jim Grosbach4b905842013-09-20 23:08:21 +0000249 bool parseStatement(ParseStatementInfo &Info);
250 void eatToEndOfLine();
251 bool parseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000252
Jim Grosbach4b905842013-09-20 23:08:21 +0000253 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body,
Kevin Enderby81c944c2013-01-22 21:44:53 +0000254 MCAsmMacroParameters Parameters);
Rafael Espindola34b9c512012-06-03 23:57:14 +0000255 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +0000256 const MCAsmMacroParameters &Parameters,
257 const MCAsmMacroArguments &A,
Rafael Espindola1134ab232011-06-05 02:43:45 +0000258 const SMLoc &L);
Daniel Dunbar43235712010-07-18 18:54:11 +0000259
Eli Benderskya313ae62013-01-16 18:56:50 +0000260 /// \brief Are macros enabled in the parser?
Jim Grosbach4b905842013-09-20 23:08:21 +0000261 bool areMacrosEnabled() {return MacrosEnabledFlag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000262
263 /// \brief Control a flag in the parser that enables or disables macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000264 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
Eli Benderskya313ae62013-01-16 18:56:50 +0000265
266 /// \brief Lookup a previously defined macro.
267 /// \param Name Macro name.
268 /// \returns Pointer to macro. NULL if no such macro was defined.
Jim Grosbach4b905842013-09-20 23:08:21 +0000269 const MCAsmMacro* lookupMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000270
271 /// \brief Define a new macro with the given name and information.
Jim Grosbach4b905842013-09-20 23:08:21 +0000272 void defineMacro(StringRef Name, const MCAsmMacro& Macro);
Eli Benderskya313ae62013-01-16 18:56:50 +0000273
274 /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
Jim Grosbach4b905842013-09-20 23:08:21 +0000275 void undefineMacro(StringRef Name);
Eli Benderskya313ae62013-01-16 18:56:50 +0000276
277 /// \brief Are we inside a macro instantiation?
Jim Grosbach4b905842013-09-20 23:08:21 +0000278 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
Eli Benderskya313ae62013-01-16 18:56:50 +0000279
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000280 /// \brief Handle entry to macro instantiation.
Eli Benderskya313ae62013-01-16 18:56:50 +0000281 ///
282 /// \param M The macro.
283 /// \param NameLoc Instantiation location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000284 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
Eli Benderskya313ae62013-01-16 18:56:50 +0000285
286 /// \brief Handle exit from macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +0000287 void handleMacroExit();
Eli Benderskya313ae62013-01-16 18:56:50 +0000288
289 /// \brief Extract AsmTokens for a macro argument. If the argument delimiter
290 /// is initially unknown, set it to AsmToken::Eof. It will be set to the
291 /// correct delimiter by the method.
Jim Grosbach4b905842013-09-20 23:08:21 +0000292 bool parseMacroArgument(MCAsmMacroArgument &MA,
Eli Benderskya313ae62013-01-16 18:56:50 +0000293 AsmToken::TokenKind &ArgumentDelimiter);
294
295 /// \brief Parse all macro arguments for a given macro.
Jim Grosbach4b905842013-09-20 23:08:21 +0000296 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Eli Benderskya313ae62013-01-16 18:56:50 +0000297
Jim Grosbach4b905842013-09-20 23:08:21 +0000298 void printMacroInstantiations();
299 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000300 ArrayRef<SMRange> Ranges = None) const {
Chris Lattner72845262011-10-16 05:47:55 +0000301 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000302 }
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000303 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerc7583112010-09-27 17:42:11 +0000304
Jim Grosbach4b905842013-09-20 23:08:21 +0000305 /// \brief Enter the specified file. This returns true on failure.
306 bool enterIncludeFile(const std::string &Filename);
307
308 /// \brief Process the specified file for the .incbin directive.
Kevin Enderby109f25c2011-12-14 21:47:48 +0000309 /// This returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000310 bool processIncbinFile(const std::string &Filename);
Daniel Dunbar43235712010-07-18 18:54:11 +0000311
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000312 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbar43235712010-07-18 18:54:11 +0000313 /// current token is not set; clients should ensure Lex() is called
314 /// subsequently.
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000315 ///
316 /// \param InBuffer If not -1, should be the known buffer id that contains the
317 /// location.
Jim Grosbach4b905842013-09-20 23:08:21 +0000318 void jumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbar43235712010-07-18 18:54:11 +0000319
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000320 /// \brief Parse up to the end of statement and a return the contents from the
321 /// current token until the end of the statement; the current token on exit
322 /// will be either the EndOfStatement or EOF.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000323 virtual StringRef parseStringToEndOfStatement();
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000324
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000325 /// \brief Parse until the end of a statement or a comma is encountered,
326 /// return the contents from the current token up to the end or comma.
Jim Grosbach4b905842013-09-20 23:08:21 +0000327 StringRef parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000328
Jim Grosbach4b905842013-09-20 23:08:21 +0000329 bool parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +0000330 bool NoDeadStrip = false);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000331
Jim Grosbach4b905842013-09-20 23:08:21 +0000332 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
333 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
334 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000335
Jim Grosbach4b905842013-09-20 23:08:21 +0000336 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
Rafael Espindola63760ba2010-10-28 20:02:27 +0000337
Eli Bendersky17233942013-01-15 22:59:42 +0000338 // Generic (target and platform independent) directive parsing.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000339 enum DirectiveKind {
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000340 DK_NO_DIRECTIVE, // Placeholder
341 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
342 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
343 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky96522722013-01-11 22:55:28 +0000344 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000345 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
Kevin Enderby3aeada22013-08-28 17:50:59 +0000346 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL,
Eli Bendersky4d21fa02013-01-10 23:40:56 +0000347 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
348 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
349 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
350 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
351 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
Eli Bendersky17233942013-01-15 22:59:42 +0000352 DK_ELSEIF, DK_ELSE, DK_ENDIF,
353 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS,
354 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA,
355 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER,
356 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA,
357 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE,
358 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED,
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000359 DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE,
Eli Bendersky17233942013-01-15 22:59:42 +0000360 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM,
361 DK_SLEB128, DK_ULEB128
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000362 };
363
Jim Grosbach4b905842013-09-20 23:08:21 +0000364 /// \brief Maps directive name --> DirectiveKind enum, for
Eli Bendersky17233942013-01-15 22:59:42 +0000365 /// directives parsed by this class.
366 StringMap<DirectiveKind> DirectiveKindMap;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000367
368 // ".ascii", ".asciz", ".string"
Jim Grosbach4b905842013-09-20 23:08:21 +0000369 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
370 bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ...
371 bool parseDirectiveRealValue(const fltSemantics &); // ".single", ...
372 bool parseDirectiveFill(); // ".fill"
373 bool parseDirectiveZero(); // ".zero"
Eric Christophera7c32732012-12-18 00:30:54 +0000374 // ".set", ".equ", ".equiv"
Jim Grosbach4b905842013-09-20 23:08:21 +0000375 bool parseDirectiveSet(StringRef IDVal, bool allow_redef);
376 bool parseDirectiveOrg(); // ".org"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000377 // ".align{,32}", ".p2align{,w,l}"
Jim Grosbach4b905842013-09-20 23:08:21 +0000378 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000379
Eli Bendersky17233942013-01-15 22:59:42 +0000380 // ".file", ".line", ".loc", ".stabs"
Jim Grosbach4b905842013-09-20 23:08:21 +0000381 bool parseDirectiveFile(SMLoc DirectiveLoc);
382 bool parseDirectiveLine();
383 bool parseDirectiveLoc();
384 bool parseDirectiveStabs();
Eli Bendersky17233942013-01-15 22:59:42 +0000385
386 // .cfi directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000387 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000388 bool parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +0000389 bool parseDirectiveCFISections();
390 bool parseDirectiveCFIStartProc();
391 bool parseDirectiveCFIEndProc();
392 bool parseDirectiveCFIDefCfaOffset();
393 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
394 bool parseDirectiveCFIAdjustCfaOffset();
395 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
396 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
397 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
398 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
399 bool parseDirectiveCFIRememberState();
400 bool parseDirectiveCFIRestoreState();
401 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
402 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
403 bool parseDirectiveCFIEscape();
404 bool parseDirectiveCFISignalFrame();
405 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
Eli Bendersky17233942013-01-15 22:59:42 +0000406
407 // macro directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000408 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
409 bool parseDirectiveEndMacro(StringRef Directive);
410 bool parseDirectiveMacro(SMLoc DirectiveLoc);
411 bool parseDirectiveMacrosOnOff(StringRef Directive);
Eli Bendersky17233942013-01-15 22:59:42 +0000412
Eli Benderskyf483ff92012-12-20 19:05:53 +0000413 // ".bundle_align_mode"
Jim Grosbach4b905842013-09-20 23:08:21 +0000414 bool parseDirectiveBundleAlignMode();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000415 // ".bundle_lock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000416 bool parseDirectiveBundleLock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000417 // ".bundle_unlock"
Jim Grosbach4b905842013-09-20 23:08:21 +0000418 bool parseDirectiveBundleUnlock();
Eli Benderskyf483ff92012-12-20 19:05:53 +0000419
Eli Bendersky17233942013-01-15 22:59:42 +0000420 // ".space", ".skip"
Jim Grosbach4b905842013-09-20 23:08:21 +0000421 bool parseDirectiveSpace(StringRef IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +0000422
423 // .sleb128 (Signed=true) and .uleb128 (Signed=false)
Jim Grosbach4b905842013-09-20 23:08:21 +0000424 bool parseDirectiveLEB128(bool Signed);
Eli Bendersky17233942013-01-15 22:59:42 +0000425
Jim Grosbach4b905842013-09-20 23:08:21 +0000426 /// \brief Parse a directive like ".globl" which
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000427 /// accepts a single symbol (which should be a label or an external).
Jim Grosbach4b905842013-09-20 23:08:21 +0000428 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000429
Jim Grosbach4b905842013-09-20 23:08:21 +0000430 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000431
Jim Grosbach4b905842013-09-20 23:08:21 +0000432 bool parseDirectiveAbort(); // ".abort"
433 bool parseDirectiveInclude(); // ".include"
434 bool parseDirectiveIncbin(); // ".incbin"
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000435
Jim Grosbach4b905842013-09-20 23:08:21 +0000436 bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer62c18b02012-05-12 11:18:42 +0000437 // ".ifb" or ".ifnb", depending on ExpectBlank.
Jim Grosbach4b905842013-09-20 23:08:21 +0000438 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000439 // ".ifc" or ".ifnc", depending on ExpectEqual.
Jim Grosbach4b905842013-09-20 23:08:21 +0000440 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer7b7caf52011-02-08 22:29:56 +0000441 // ".ifdef" or ".ifndef", depending on expect_defined
Jim Grosbach4b905842013-09-20 23:08:21 +0000442 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
443 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
444 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else"
445 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000446 virtual bool parseEscapedString(std::string &Data);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000447
Jim Grosbach4b905842013-09-20 23:08:21 +0000448 const MCExpr *applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000449 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola47b7dac2012-05-12 16:31:10 +0000450
Rafael Espindola34b9c512012-06-03 23:57:14 +0000451 // Macro-like directives
Jim Grosbach4b905842013-09-20 23:08:21 +0000452 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
453 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +0000454 raw_svector_ostream &OS);
Jim Grosbach4b905842013-09-20 23:08:21 +0000455 bool parseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
456 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
457 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
458 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosier8bce6642012-10-18 15:49:34 +0000459
Chad Rosierc7f552c2013-02-12 21:33:51 +0000460 // "_emit" or "__emit"
Jim Grosbach4b905842013-09-20 23:08:21 +0000461 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
Chad Rosierc7f552c2013-02-12 21:33:51 +0000462 size_t Len);
463
464 // "align"
Jim Grosbach4b905842013-09-20 23:08:21 +0000465 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000466
Eli Bendersky17233942013-01-15 22:59:42 +0000467 void initializeDirectiveKindMap();
Daniel Dunbar2a2c6cf2010-07-18 18:31:38 +0000468};
Daniel Dunbar86033402010-07-12 17:54:38 +0000469}
470
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000471namespace llvm {
472
473extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000474extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencerc8dbdfd2010-10-09 11:01:07 +0000475extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000476
477}
478
Chris Lattnerc35681b2010-01-19 19:46:13 +0000479enum { DEFAULT_ADDRSPACE = 0 };
480
Jim Grosbach4b905842013-09-20 23:08:21 +0000481AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
482 const MCAsmInfo &_MAI)
483 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
484 PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true),
485 CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false),
486 ParsingInlineAsm(false) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +0000487 // Save the old handler.
488 SavedDiagHandler = SrcMgr.getDiagHandler();
489 SavedDiagContext = SrcMgr.getDiagContext();
490 // Set our own handler which calls the saved handler.
Kevin Enderbye7c0c492011-10-12 21:38:39 +0000491 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000492 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar86033402010-07-12 17:54:38 +0000493
Daniel Dunbarc5011082010-07-12 18:12:02 +0000494 // Initialize the platform / file format parser.
Rafael Espindolae28610d2013-12-09 20:26:40 +0000495 switch (_Ctx.getObjectFileInfo()->getObjectFileType()) {
496 case MCObjectFileInfo::IsCOFF:
497 PlatformParser = createCOFFAsmParser();
498 PlatformParser->Initialize(*this);
499 break;
500 case MCObjectFileInfo::IsMachO:
501 PlatformParser = createDarwinAsmParser();
502 PlatformParser->Initialize(*this);
503 IsDarwin = true;
504 break;
505 case MCObjectFileInfo::IsELF:
506 PlatformParser = createELFAsmParser();
507 PlatformParser->Initialize(*this);
508 break;
Daniel Dunbarc5011082010-07-12 18:12:02 +0000509 }
Eli Benderskyec9e3cf2013-01-10 22:44:57 +0000510
Eli Bendersky17233942013-01-15 22:59:42 +0000511 initializeDirectiveKindMap();
Chris Lattner351a7ef2009-09-27 21:16:52 +0000512}
513
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000514AsmParser::~AsmParser() {
Daniel Dunbarb759a132010-07-29 01:51:55 +0000515 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
516
517 // Destroy any macros.
Jim Grosbach4b905842013-09-20 23:08:21 +0000518 for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(),
519 ie = MacroMap.end();
520 it != ie; ++it)
Daniel Dunbarb759a132010-07-29 01:51:55 +0000521 delete it->getValue();
522
Daniel Dunbarc5011082010-07-12 18:12:02 +0000523 delete PlatformParser;
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000524}
525
Jim Grosbach4b905842013-09-20 23:08:21 +0000526void AsmParser::printMacroInstantiations() {
Daniel Dunbar43235712010-07-18 18:54:11 +0000527 // Print the active macro instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +0000528 for (std::vector<MacroInstantiation *>::const_reverse_iterator
529 it = ActiveMacros.rbegin(),
530 ie = ActiveMacros.rend();
531 it != ie; ++it)
532 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
Chris Lattner03b80a42011-10-16 05:43:57 +0000533 "while in macro instantiation");
Daniel Dunbar43235712010-07-18 18:54:11 +0000534}
535
Chris Lattnera3a06812011-10-16 04:47:35 +0000536bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000537 if (FatalAssemblerWarnings)
Chris Lattnera3a06812011-10-16 04:47:35 +0000538 return Error(L, Msg, Ranges);
Jim Grosbach4b905842013-09-20 23:08:21 +0000539 printMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
540 printMacroInstantiations();
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000541 return false;
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +0000542}
543
Chris Lattnera3a06812011-10-16 04:47:35 +0000544bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000545 HadError = true;
Jim Grosbach4b905842013-09-20 23:08:21 +0000546 printMessage(L, SourceMgr::DK_Error, Msg, Ranges);
547 printMacroInstantiations();
Chris Lattner2adc9e72009-06-21 21:22:11 +0000548 return true;
549}
550
Jim Grosbach4b905842013-09-20 23:08:21 +0000551bool AsmParser::enterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000552 std::string IncludedFile;
553 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000554 if (NewBuf == -1)
555 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000556
Sean Callanan7a77eae2010-01-21 00:19:58 +0000557 CurBuffer = NewBuf;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000558
Sean Callanan7a77eae2010-01-21 00:19:58 +0000559 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencer530ce852010-10-09 11:00:50 +0000560
Sean Callanan7a77eae2010-01-21 00:19:58 +0000561 return false;
562}
Daniel Dunbar43235712010-07-18 18:54:11 +0000563
Sylvestre Ledru149e2812013-05-14 23:36:24 +0000564/// Process the specified .incbin file by searching for it in the include paths
Benjamin Kramerbde91762012-06-02 10:20:22 +0000565/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderby109f25c2011-12-14 21:47:48 +0000566/// returns true on failure.
Jim Grosbach4b905842013-09-20 23:08:21 +0000567bool AsmParser::processIncbinFile(const std::string &Filename) {
Kevin Enderby109f25c2011-12-14 21:47:48 +0000568 std::string IncludedFile;
569 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
570 if (NewBuf == -1)
571 return true;
572
Kevin Enderbyad41ab52011-12-14 22:34:45 +0000573 // Pick up the bytes from the file and emit them.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000574 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
Kevin Enderby109f25c2011-12-14 21:47:48 +0000575 return false;
576}
577
Jim Grosbach4b905842013-09-20 23:08:21 +0000578void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) {
Daniel Dunbar40f1d852012-12-01 01:38:48 +0000579 if (InBuffer != -1) {
580 CurBuffer = InBuffer;
581 } else {
582 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
583 }
Daniel Dunbar43235712010-07-18 18:54:11 +0000584 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
585}
586
Sean Callanan7a77eae2010-01-21 00:19:58 +0000587const AsmToken &AsmParser::Lex() {
588 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000589
Sean Callanan7a77eae2010-01-21 00:19:58 +0000590 if (tok->is(AsmToken::Eof)) {
591 // If this is the end of an included file, pop the parent file off the
592 // include stack.
593 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
594 if (ParentIncludeLoc != SMLoc()) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000595 jumpToLoc(ParentIncludeLoc);
Sean Callanan7a77eae2010-01-21 00:19:58 +0000596 tok = &Lexer.Lex();
597 }
598 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000599
Sean Callanan7a77eae2010-01-21 00:19:58 +0000600 if (tok->is(AsmToken::Error))
Daniel Dunbard8a18452010-07-18 18:31:45 +0000601 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencer530ce852010-10-09 11:00:50 +0000602
Sean Callanan7a77eae2010-01-21 00:19:58 +0000603 return *tok;
Sean Callanan686ed8d2010-01-19 20:22:31 +0000604}
605
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000606bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar322fec62010-03-13 02:20:57 +0000607 // Create the initial section, if requested.
Daniel Dunbar322fec62010-03-13 02:20:57 +0000608 if (!NoInitialTextSection)
Rafael Espindolaf667d922010-09-15 21:48:40 +0000609 Out.InitSections();
Daniel Dunbar4d7b2e32009-08-26 22:49:51 +0000610
Chris Lattner36e02122009-06-21 20:54:55 +0000611 // Prime the lexer.
Sean Callanan686ed8d2010-01-19 20:22:31 +0000612 Lex();
Daniel Dunbar43325c42010-09-09 22:42:56 +0000613
614 HadError = false;
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000615 AsmCond StartingCondState = TheCondState;
616
Kevin Enderby6469fc22011-11-01 22:27:22 +0000617 // If we are generating dwarf for assembly source files save the initial text
618 // section and generate a .file directive.
619 if (getContext().getGenDwarfForAssembly()) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000620 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000621 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
622 getStreamer().EmitLabel(SectionStartSym);
623 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby6469fc22011-11-01 22:27:22 +0000624 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher906da232012-12-18 00:31:01 +0000625 StringRef(),
626 getContext().getMainFileName());
Kevin Enderby6469fc22011-11-01 22:27:22 +0000627 }
628
Chris Lattner73f36112009-07-02 21:53:43 +0000629 // While we have input, parse each statement.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000630 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +0000631 ParseStatementInfo Info;
Jim Grosbach4b905842013-09-20 23:08:21 +0000632 if (!parseStatement(Info))
633 continue;
Michael J. Spencer530ce852010-10-09 11:00:50 +0000634
Daniel Dunbar43325c42010-09-09 22:42:56 +0000635 // We had an error, validate that one was emitted and recover by skipping to
636 // the next line.
637 assert(HadError && "Parse statement returned an error, but none emitted!");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000638 eatToEndOfStatement();
Chris Lattner73f36112009-07-02 21:53:43 +0000639 }
Kevin Enderbyd9f95292009-08-07 22:46:00 +0000640
641 if (TheCondState.TheCond != StartingCondState.TheCond ||
642 TheCondState.Ignore != StartingCondState.Ignore)
643 return TokError("unmatched .ifs or .elses");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000644
645 // Check to see there are no empty DwarfFile slots.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000646 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +0000647 getContext().getMCDwarfFiles();
Kevin Enderbye5930f12010-07-28 20:55:35 +0000648 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar43325c42010-09-09 22:42:56 +0000649 if (!MCDwarfFiles[i])
Kevin Enderbye5930f12010-07-28 20:55:35 +0000650 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderbye5930f12010-07-28 20:55:35 +0000651 }
Michael J. Spencer530ce852010-10-09 11:00:50 +0000652
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000653 // Check to see that all assembler local symbols were actually defined.
654 // Targets that don't do subsections via symbols may not want this, though,
655 // so conservatively exclude them. Only do this if we're finalizing, though,
656 // as otherwise we won't necessarilly have seen everything yet.
657 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
658 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
659 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +0000660 e = Symbols.end();
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000661 i != e; ++i) {
662 MCSymbol *Sym = i->getValue();
663 // Variable symbols may not be marked as defined, so check those
664 // explicitly. If we know it's a variable, we have a definition for
665 // the purposes of this check.
666 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
667 // FIXME: We would really like to refer back to where the symbol was
668 // first referenced for a source location. We need to add something
669 // to track that. Currently, we just point to the end of the file.
Jim Grosbach4b905842013-09-20 23:08:21 +0000670 printMessage(
671 getLexer().getLoc(), SourceMgr::DK_Error,
672 "assembler local symbol '" + Sym->getName() + "' not defined");
Jim Grosbachc7e6b8f2011-06-15 18:33:28 +0000673 }
674 }
675
Chris Lattner3b21e4d2010-04-05 23:15:42 +0000676 // Finalize the output stream if there are no errors and if the client wants
677 // us to.
Jack Carter13d5f752013-10-04 22:52:31 +0000678 if (!HadError && !NoFinalize)
Daniel Dunbar9df5f332009-08-21 08:34:18 +0000679 Out.Finish();
680
Chris Lattner73f36112009-07-02 21:53:43 +0000681 return HadError;
Chris Lattner36e02122009-06-21 20:54:55 +0000682}
683
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000684void AsmParser::checkForValidSection() {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000685 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) {
Daniel Dunbare5444a82010-09-09 22:42:59 +0000686 TokError("expected section directive before assembly directive");
Eli Benderskycbb25142013-01-14 19:04:57 +0000687 Out.InitToTextSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +0000688 }
689}
690
Jim Grosbach4b905842013-09-20 23:08:21 +0000691/// \brief Throw away the rest of the line for testing purposes.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000692void AsmParser::eatToEndOfStatement() {
Jim Grosbach4b905842013-09-20 23:08:21 +0000693 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000694 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +0000695
Chris Lattnere5074c42009-06-22 01:29:09 +0000696 // Eat EOL.
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000697 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan686ed8d2010-01-19 20:22:31 +0000698 Lex();
Chris Lattnere5074c42009-06-22 01:29:09 +0000699}
700
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000701StringRef AsmParser::parseStringToEndOfStatement() {
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000702 const char *Start = getTok().getLoc().getPointer();
703
Jim Grosbach4b905842013-09-20 23:08:21 +0000704 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000705 Lex();
706
707 const char *End = getTok().getLoc().getPointer();
708 return StringRef(Start, End - Start);
709}
Chris Lattner78db3622009-06-22 05:51:26 +0000710
Jim Grosbach4b905842013-09-20 23:08:21 +0000711StringRef AsmParser::parseStringToComma() {
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000712 const char *Start = getTok().getLoc().getPointer();
713
714 while (Lexer.isNot(AsmToken::EndOfStatement) &&
Jim Grosbach4b905842013-09-20 23:08:21 +0000715 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof))
Benjamin Kramere297b9f2012-05-12 11:18:51 +0000716 Lex();
717
718 const char *End = getTok().getLoc().getPointer();
719 return StringRef(Start, End - Start);
720}
721
Jim Grosbach4b905842013-09-20 23:08:21 +0000722/// \brief Parse a paren expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000723/// NOTE: This assumes the leading '(' has already been consumed.
724///
725/// parenexpr ::= expr)
726///
Jim Grosbach4b905842013-09-20 23:08:21 +0000727bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
728 if (parseExpression(Res))
729 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000730 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner7fdbce72009-06-22 06:32:03 +0000731 return TokError("expected ')' in parentheses expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000732 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000733 Lex();
Chris Lattner7fdbce72009-06-22 06:32:03 +0000734 return false;
735}
Chris Lattner78db3622009-06-22 05:51:26 +0000736
Jim Grosbach4b905842013-09-20 23:08:21 +0000737/// \brief Parse a bracket expression and return it.
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000738/// NOTE: This assumes the leading '[' has already been consumed.
739///
740/// bracketexpr ::= expr]
741///
Jim Grosbach4b905842013-09-20 23:08:21 +0000742bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
743 if (parseExpression(Res))
744 return true;
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000745 if (Lexer.isNot(AsmToken::RBrac))
746 return TokError("expected ']' in brackets expression");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000747 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000748 Lex();
749 return false;
750}
751
Jim Grosbach4b905842013-09-20 23:08:21 +0000752/// \brief Parse a primary expression and return it.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000753/// primaryexpr ::= (parenexpr
754/// primaryexpr ::= symbol
755/// primaryexpr ::= number
Chris Lattner6b55cb92010-04-14 04:40:28 +0000756/// primaryexpr ::= '.'
Chris Lattner7fdbce72009-06-22 06:32:03 +0000757/// primaryexpr ::= ~,+,- primaryexpr
Jim Grosbach4b905842013-09-20 23:08:21 +0000758bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000759 SMLoc FirstTokenLoc = getLexer().getLoc();
760 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
761 switch (FirstTokenKind) {
Chris Lattner78db3622009-06-22 05:51:26 +0000762 default:
763 return TokError("unknown token in expression");
Eric Christopher104af062011-04-12 00:03:13 +0000764 // If we have an error assume that we've already handled it.
765 case AsmToken::Error:
766 return true;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000767 case AsmToken::Exclaim:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000768 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000769 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000770 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000771 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000772 return false;
Daniel Dunbar24764322010-08-24 19:13:42 +0000773 case AsmToken::Dollar:
Hans Wennborgce69d772013-10-18 20:46:28 +0000774 case AsmToken::At:
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +0000775 case AsmToken::String:
Daniel Dunbard20cda02009-10-16 01:34:54 +0000776 case AsmToken::Identifier: {
Daniel Dunbar24764322010-08-24 19:13:42 +0000777 StringRef Identifier;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000778 if (parseIdentifier(Identifier)) {
David Majnemer0c58bc62013-09-25 10:47:21 +0000779 if (FirstTokenKind == AsmToken::Dollar) {
780 if (Lexer.getMAI().getDollarIsPC()) {
781 // This is a '$' reference, which references the current PC. Emit a
782 // temporary label to the streamer and refer to it.
783 MCSymbol *Sym = Ctx.CreateTempSymbol();
784 Out.EmitLabel(Sym);
Jack Carter721726a2013-10-04 21:26:15 +0000785 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
786 getContext());
David Majnemer0c58bc62013-09-25 10:47:21 +0000787 EndLoc = FirstTokenLoc;
788 return false;
789 } else
790 return Error(FirstTokenLoc, "invalid token in expression");
791 return true;
792 }
Kevin Enderby0017d8a2013-01-22 21:09:20 +0000793 }
David Peixotto8ad70b32013-12-04 22:43:20 +0000794 // Parse symbol variant
795 std::pair<StringRef, StringRef> Split;
796 if (!MAI.useParensForSymbolVariant()) {
797 Split = Identifier.split('@');
798 } else if (Lexer.is(AsmToken::LParen)) {
799 Lexer.Lex(); // eat (
800 StringRef VName;
801 parseIdentifier(VName);
802 if (Lexer.isNot(AsmToken::RParen)) {
803 return Error(Lexer.getTok().getLoc(),
804 "unexpected token in variant, expected ')'");
805 }
806 Lexer.Lex(); // eat )
807 Split = std::make_pair(Identifier, VName);
808 }
Daniel Dunbar24764322010-08-24 19:13:42 +0000809
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000810 EndLoc = SMLoc::getFromPointer(Identifier.end());
811
Daniel Dunbard20cda02009-10-16 01:34:54 +0000812 // This is a symbol reference.
Hans Wennborgce69d772013-10-18 20:46:28 +0000813 StringRef SymbolName = Identifier;
814 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg69918bc2013-10-17 01:13:02 +0000815
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000816 // Lookup the symbol variant if used.
David Peixotto8ad70b32013-12-04 22:43:20 +0000817 if (Split.second.size()) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000818 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Hans Wennborgce69d772013-10-18 20:46:28 +0000819 if (Variant != MCSymbolRefExpr::VK_Invalid) {
820 SymbolName = Split.first;
David Peixotto8ad70b32013-12-04 22:43:20 +0000821 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000822 Variant = MCSymbolRefExpr::VK_None;
823 } else {
Daniel Dunbar55f16672010-09-17 02:47:07 +0000824 Variant = MCSymbolRefExpr::VK_None;
Hans Wennborg7ddcdc82013-10-18 02:14:40 +0000825 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000826 }
827 }
Daniel Dunbar55992562010-03-15 23:51:06 +0000828
Hans Wennborgce69d772013-10-18 20:46:28 +0000829 MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
830
Daniel Dunbard20cda02009-10-16 01:34:54 +0000831 // If this is an absolute variable reference, substitute it now to preserve
832 // semantics in the face of reassignment.
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000833 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar55992562010-03-15 23:51:06 +0000834 if (Variant)
Daniel Dunbar8a3c3f22010-11-08 17:53:02 +0000835 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar55992562010-03-15 23:51:06 +0000836
Daniel Dunbar7a989da2010-05-05 17:41:00 +0000837 Res = Sym->getVariableValue();
Daniel Dunbard20cda02009-10-16 01:34:54 +0000838 return false;
839 }
840
841 // Otherwise create a symbol ref.
Daniel Dunbar55992562010-03-15 23:51:06 +0000842 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattner78db3622009-06-22 05:51:26 +0000843 return false;
Daniel Dunbard20cda02009-10-16 01:34:54 +0000844 }
Kevin Enderby0510b482010-05-17 23:08:19 +0000845 case AsmToken::Integer: {
846 SMLoc Loc = getTok().getLoc();
847 int64_t IntVal = getTok().getIntVal();
848 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000849 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +0000850 Lex(); // Eat token.
Kevin Enderby0510b482010-05-17 23:08:19 +0000851 // Look for 'b' or 'f' following an Integer as a directional label
852 if (Lexer.getKind() == AsmToken::Identifier) {
853 StringRef IDVal = getTok().getString();
Ulrich Weigandd4120982013-06-20 16:24:17 +0000854 // Lookup the symbol variant if used.
855 std::pair<StringRef, StringRef> Split = IDVal.split('@');
856 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
857 if (Split.first.size() != IDVal.size()) {
858 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
859 if (Variant == MCSymbolRefExpr::VK_Invalid) {
860 Variant = MCSymbolRefExpr::VK_None;
861 return TokError("invalid variant '" + Split.second + "'");
862 }
Vladimir Medic9bad0d332013-08-20 13:33:18 +0000863 IDVal = Split.first;
Ulrich Weigandd4120982013-06-20 16:24:17 +0000864 }
Jim Grosbach4b905842013-09-20 23:08:21 +0000865 if (IDVal == "f" || IDVal == "b") {
866 MCSymbol *Sym =
867 Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0);
Ulrich Weigandd4120982013-06-20 16:24:17 +0000868 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +0000869 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderby0510b482010-05-17 23:08:19 +0000870 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000871 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderby0510b482010-05-17 23:08:19 +0000872 Lex(); // Eat identifier.
873 }
874 }
Chris Lattner78db3622009-06-22 05:51:26 +0000875 return false;
Kevin Enderby0510b482010-05-17 23:08:19 +0000876 }
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000877 case AsmToken::Real: {
878 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson813bdf62011-02-03 23:17:47 +0000879 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000880 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000881 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendlingcdbf17b2011-01-25 21:26:41 +0000882 Lex(); // Eat token.
883 return false;
884 }
Chris Lattner6b55cb92010-04-14 04:40:28 +0000885 case AsmToken::Dot: {
886 // This is a '.' reference, which references the current PC. Emit a
887 // temporary label to the streamer and refer to it.
888 MCSymbol *Sym = Ctx.CreateTempSymbol();
889 Out.EmitLabel(Sym);
890 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rosee8f1eae2013-01-07 19:00:49 +0000891 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattner6b55cb92010-04-14 04:40:28 +0000892 Lex(); // Eat identifier.
893 return false;
894 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000895 case AsmToken::LParen:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000896 Lex(); // Eat the '('.
Jim Grosbach4b905842013-09-20 23:08:21 +0000897 return parseParenExpr(Res, EndLoc);
Joerg Sonnenbergerafb36fa2011-02-24 21:59:22 +0000898 case AsmToken::LBrac:
899 if (!PlatformParser->HasBracketExpressions())
900 return TokError("brackets expression not supported on this target");
901 Lex(); // Eat the '['.
Jim Grosbach4b905842013-09-20 23:08:21 +0000902 return parseBracketExpr(Res, EndLoc);
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000903 case AsmToken::Minus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000904 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000905 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000906 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000907 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000908 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000909 case AsmToken::Plus:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000910 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000911 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000912 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000913 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000914 return false;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000915 case AsmToken::Tilde:
Sean Callanan686ed8d2010-01-19 20:22:31 +0000916 Lex(); // Eat the operator.
Jim Grosbach4b905842013-09-20 23:08:21 +0000917 if (parsePrimaryExpr(Res, EndLoc))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000918 return true;
Daniel Dunbar940cda22009-08-31 08:07:44 +0000919 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000920 return false;
Chris Lattner78db3622009-06-22 05:51:26 +0000921 }
922}
Chris Lattner7fdbce72009-06-22 06:32:03 +0000923
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000924bool AsmParser::parseExpression(const MCExpr *&Res) {
Chris Lattnere17df0b2010-01-15 19:39:23 +0000925 SMLoc EndLoc;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000926 return parseExpression(Res, EndLoc);
Chris Lattner528d00b2010-01-15 19:28:38 +0000927}
928
Daniel Dunbar55f16672010-09-17 02:47:07 +0000929const MCExpr *
Jim Grosbach4b905842013-09-20 23:08:21 +0000930AsmParser::applyModifierToExpr(const MCExpr *E,
Daniel Dunbar55f16672010-09-17 02:47:07 +0000931 MCSymbolRefExpr::VariantKind Variant) {
Joerg Sonnenbergerb822af42013-08-27 20:23:19 +0000932 // Ask the target implementation about this expression first.
933 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx);
934 if (NewE)
935 return NewE;
Daniel Dunbar55f16672010-09-17 02:47:07 +0000936 // Recurse over the given expression, rebuilding it to apply the given variant
937 // if there is exactly one symbol.
938 switch (E->getKind()) {
939 case MCExpr::Target:
940 case MCExpr::Constant:
941 return 0;
942
943 case MCExpr::SymbolRef: {
944 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
945
946 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
Jim Grosbach4b905842013-09-20 23:08:21 +0000947 TokError("invalid variant on expression '" + getTok().getIdentifier() +
948 "' (already modified)");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000949 return E;
950 }
951
952 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
953 }
954
955 case MCExpr::Unary: {
956 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000957 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000958 if (!Sub)
959 return 0;
960 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
961 }
962
963 case MCExpr::Binary: {
964 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
Jim Grosbach4b905842013-09-20 23:08:21 +0000965 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant);
966 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +0000967
968 if (!LHS && !RHS)
969 return 0;
970
Jim Grosbach4b905842013-09-20 23:08:21 +0000971 if (!LHS)
972 LHS = BE->getLHS();
973 if (!RHS)
974 RHS = BE->getRHS();
Daniel Dunbar55f16672010-09-17 02:47:07 +0000975
976 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
977 }
978 }
Daniel Dunbarbaad46c2010-09-17 16:34:24 +0000979
Craig Toppera2886c22012-02-07 05:05:23 +0000980 llvm_unreachable("Invalid expression kind!");
Daniel Dunbar55f16672010-09-17 02:47:07 +0000981}
982
Jim Grosbach4b905842013-09-20 23:08:21 +0000983/// \brief Parse an expression and return it.
Michael J. Spencer530ce852010-10-09 11:00:50 +0000984///
Jim Grosbachbd164242011-08-20 16:24:13 +0000985/// expr ::= expr &&,|| expr -> lowest.
986/// expr ::= expr |,^,&,! expr
987/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
988/// expr ::= expr <<,>> expr
989/// expr ::= expr +,- expr
990/// expr ::= expr *,/,% expr -> highest.
Chris Lattner7fdbce72009-06-22 06:32:03 +0000991/// expr ::= primaryexpr
992///
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000993bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbard0c6d362010-02-13 01:28:07 +0000994 // Parse the expression.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000995 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +0000996 if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc))
Daniel Dunbard0c6d362010-02-13 01:28:07 +0000997 return true;
998
Daniel Dunbar55f16672010-09-17 02:47:07 +0000999 // As a special case, we support 'a op b @ modifier' by rewriting the
1000 // expression to include the modifier. This is inefficient, but in general we
1001 // expect users to use 'a@modifier op b'.
1002 if (Lexer.getKind() == AsmToken::At) {
1003 Lex();
1004
1005 if (Lexer.isNot(AsmToken::Identifier))
1006 return TokError("unexpected symbol modifier following '@'");
1007
1008 MCSymbolRefExpr::VariantKind Variant =
Jim Grosbach4b905842013-09-20 23:08:21 +00001009 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
Daniel Dunbar55f16672010-09-17 02:47:07 +00001010 if (Variant == MCSymbolRefExpr::VK_Invalid)
1011 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
1012
Jim Grosbach4b905842013-09-20 23:08:21 +00001013 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
Daniel Dunbar55f16672010-09-17 02:47:07 +00001014 if (!ModifiedRes) {
1015 return TokError("invalid modifier '" + getTok().getIdentifier() +
1016 "' (no symbols present)");
Daniel Dunbar55f16672010-09-17 02:47:07 +00001017 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001018
Daniel Dunbar55f16672010-09-17 02:47:07 +00001019 Res = ModifiedRes;
1020 Lex();
1021 }
1022
Daniel Dunbard0c6d362010-02-13 01:28:07 +00001023 // Try to constant fold it up front, if possible.
1024 int64_t Value;
1025 if (Res->EvaluateAsAbsolute(Value))
1026 Res = MCConstantExpr::Create(Value, getContext());
1027
1028 return false;
Chris Lattner7fdbce72009-06-22 06:32:03 +00001029}
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001030
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001031bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner807a3bc2010-01-24 01:07:33 +00001032 Res = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00001033 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
Daniel Dunbar7c82d562009-08-31 08:08:17 +00001034}
1035
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001036bool AsmParser::parseAbsoluteExpression(int64_t &Res) {
Daniel Dunbarf3636452009-08-31 08:07:22 +00001037 const MCExpr *Expr;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001038
Daniel Dunbar75630b32009-06-30 02:10:03 +00001039 SMLoc StartLoc = Lexer.getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001040 if (parseExpression(Expr))
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001041 return true;
1042
Daniel Dunbarc3bd60e2009-10-16 01:57:52 +00001043 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbar75630b32009-06-30 02:10:03 +00001044 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001045
1046 return false;
1047}
1048
Michael J. Spencer530ce852010-10-09 11:00:50 +00001049static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001050 MCBinaryExpr::Opcode &Kind) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001051 switch (K) {
Daniel Dunbar940cda22009-08-31 08:07:44 +00001052 default:
Jim Grosbach4b905842013-09-20 23:08:21 +00001053 return 0; // not a binop.
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001054
Jim Grosbach4b905842013-09-20 23:08:21 +00001055 // Lowest Precedence: &&, ||
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001056 case AsmToken::AmpAmp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001057 Kind = MCBinaryExpr::LAnd;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001058 return 1;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001059 case AsmToken::PipePipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001060 Kind = MCBinaryExpr::LOr;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001061 return 1;
1062
Jim Grosbach4b905842013-09-20 23:08:21 +00001063 // Low Precedence: |, &, ^
1064 //
1065 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001066 case AsmToken::Pipe:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001067 Kind = MCBinaryExpr::Or;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001068 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001069 case AsmToken::Caret:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001070 Kind = MCBinaryExpr::Xor;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001071 return 2;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001072 case AsmToken::Amp:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001073 Kind = MCBinaryExpr::And;
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001074 return 2;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001075
Jim Grosbach4b905842013-09-20 23:08:21 +00001076 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattner2bb9504d2010-09-22 05:05:16 +00001077 case AsmToken::EqualEqual:
1078 Kind = MCBinaryExpr::EQ;
1079 return 3;
1080 case AsmToken::ExclaimEqual:
1081 case AsmToken::LessGreater:
1082 Kind = MCBinaryExpr::NE;
1083 return 3;
1084 case AsmToken::Less:
1085 Kind = MCBinaryExpr::LT;
1086 return 3;
1087 case AsmToken::LessEqual:
1088 Kind = MCBinaryExpr::LTE;
1089 return 3;
1090 case AsmToken::Greater:
1091 Kind = MCBinaryExpr::GT;
1092 return 3;
1093 case AsmToken::GreaterEqual:
1094 Kind = MCBinaryExpr::GTE;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001095 return 3;
1096
Jim Grosbach4b905842013-09-20 23:08:21 +00001097 // Intermediate Precedence: <<, >>
Jim Grosbachbd164242011-08-20 16:24:13 +00001098 case AsmToken::LessLess:
1099 Kind = MCBinaryExpr::Shl;
1100 return 4;
1101 case AsmToken::GreaterGreater:
1102 Kind = MCBinaryExpr::Shr;
1103 return 4;
1104
Jim Grosbach4b905842013-09-20 23:08:21 +00001105 // High Intermediate Precedence: +, -
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001106 case AsmToken::Plus:
1107 Kind = MCBinaryExpr::Add;
Jim Grosbachbd164242011-08-20 16:24:13 +00001108 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001109 case AsmToken::Minus:
1110 Kind = MCBinaryExpr::Sub;
Jim Grosbachbd164242011-08-20 16:24:13 +00001111 return 5;
Daniel Dunbarb3a48f32010-10-25 20:18:56 +00001112
Jim Grosbach4b905842013-09-20 23:08:21 +00001113 // Highest Precedence: *, /, %
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001114 case AsmToken::Star:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001115 Kind = MCBinaryExpr::Mul;
Jim Grosbachbd164242011-08-20 16:24:13 +00001116 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001117 case AsmToken::Slash:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001118 Kind = MCBinaryExpr::Div;
Jim Grosbachbd164242011-08-20 16:24:13 +00001119 return 6;
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001120 case AsmToken::Percent:
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001121 Kind = MCBinaryExpr::Mod;
Jim Grosbachbd164242011-08-20 16:24:13 +00001122 return 6;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001123 }
1124}
1125
Jim Grosbach4b905842013-09-20 23:08:21 +00001126/// \brief Parse all binary operators with precedence >= 'Precedence'.
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001127/// Res contains the LHS of the expression on input.
Jim Grosbach4b905842013-09-20 23:08:21 +00001128bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
Chris Lattner528d00b2010-01-15 19:28:38 +00001129 SMLoc &EndLoc) {
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001130 while (1) {
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001131 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001132 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001133
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001134 // If the next token is lower precedence than we are allowed to eat, return
1135 // successfully with what we ate already.
1136 if (TokPrec < Precedence)
1137 return false;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001138
Sean Callanan686ed8d2010-01-19 20:22:31 +00001139 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00001140
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001141 // Eat the next primary expression.
Daniel Dunbarf3636452009-08-31 08:07:22 +00001142 const MCExpr *RHS;
Jim Grosbach4b905842013-09-20 23:08:21 +00001143 if (parsePrimaryExpr(RHS, EndLoc))
1144 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00001145
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001146 // If BinOp binds less tightly with RHS than the operator after RHS, let
1147 // the pending operator take RHS as its LHS.
Daniel Dunbar115e4d62009-08-31 08:06:59 +00001148 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001149 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Jim Grosbach4b905842013-09-20 23:08:21 +00001150 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1151 return true;
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001152
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +00001153 // Merge LHS and RHS according to operator.
Daniel Dunbar940cda22009-08-31 08:07:44 +00001154 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattnerf97d8bb2009-06-23 05:57:07 +00001155 }
1156}
1157
Chris Lattner36e02122009-06-21 20:54:55 +00001158/// ParseStatement:
1159/// ::= EndOfStatement
Chris Lattnere5074c42009-06-22 01:29:09 +00001160/// ::= Label* Directive ...Operands... EndOfStatement
1161/// ::= Label* Identifier OperandList* EndOfStatement
Jim Grosbach4b905842013-09-20 23:08:21 +00001162bool AsmParser::parseStatement(ParseStatementInfo &Info) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001163 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001164 Out.AddBlankLine();
Sean Callanan686ed8d2010-01-19 20:22:31 +00001165 Lex();
Chris Lattner36e02122009-06-21 20:54:55 +00001166 return false;
Chris Lattner36e02122009-06-21 20:54:55 +00001167 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001168
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001169 // Statements always start with an identifier or are a full line comment.
Sean Callanan936b0d32010-01-19 21:44:56 +00001170 AsmToken ID = getTok();
Daniel Dunbaree4465c2009-07-28 16:38:40 +00001171 SMLoc IDLoc = ID.getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001172 StringRef IDVal;
Kevin Enderby0510b482010-05-17 23:08:19 +00001173 int64_t LocalLabelVal = -1;
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001174 // A full line comment is a '#' as the first token.
Kevin Enderby72553612011-09-13 23:45:18 +00001175 if (Lexer.is(AsmToken::Hash))
Jim Grosbach4b905842013-09-20 23:08:21 +00001176 return parseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar3f561042011-03-25 17:47:14 +00001177
Kevin Enderbyfa3c6f12010-12-24 00:12:02 +00001178 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderby0510b482010-05-17 23:08:19 +00001179 if (Lexer.is(AsmToken::Integer)) {
1180 LocalLabelVal = getTok().getIntVal();
1181 if (LocalLabelVal < 0) {
1182 if (!TheCondState.Ignore)
1183 return TokError("unexpected token at start of statement");
1184 IDVal = "";
Eli Bendersky88024712013-01-16 19:32:36 +00001185 } else {
Kevin Enderby0510b482010-05-17 23:08:19 +00001186 IDVal = getTok().getString();
1187 Lex(); // Consume the integer token to be used as an identifier token.
1188 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00001189 if (!TheCondState.Ignore)
1190 return TokError("unexpected token at start of statement");
Kevin Enderby0510b482010-05-17 23:08:19 +00001191 }
1192 }
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001193 } else if (Lexer.is(AsmToken::Dot)) {
1194 // Treat '.' as a valid identifier in this context.
1195 Lex();
1196 IDVal = ".";
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001197 } else if (parseIdentifier(IDVal)) {
Chris Lattner926885c2010-04-17 18:14:27 +00001198 if (!TheCondState.Ignore)
1199 return TokError("unexpected token at start of statement");
1200 IDVal = "";
1201 }
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001202
Chris Lattner926885c2010-04-17 18:14:27 +00001203 // Handle conditional assembly here before checking for skipping. We
1204 // have to do this so that .endif isn't skipped in a ".if 0" block for
1205 // example.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001206 StringMap<DirectiveKind>::const_iterator DirKindIt =
Jim Grosbach4b905842013-09-20 23:08:21 +00001207 DirectiveKindMap.find(IDVal);
1208 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1209 ? DK_NO_DIRECTIVE
1210 : DirKindIt->getValue();
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001211 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001212 default:
1213 break;
1214 case DK_IF:
1215 return parseDirectiveIf(IDLoc);
1216 case DK_IFB:
1217 return parseDirectiveIfb(IDLoc, true);
1218 case DK_IFNB:
1219 return parseDirectiveIfb(IDLoc, false);
1220 case DK_IFC:
1221 return parseDirectiveIfc(IDLoc, true);
1222 case DK_IFNC:
1223 return parseDirectiveIfc(IDLoc, false);
1224 case DK_IFDEF:
1225 return parseDirectiveIfdef(IDLoc, true);
1226 case DK_IFNDEF:
1227 case DK_IFNOTDEF:
1228 return parseDirectiveIfdef(IDLoc, false);
1229 case DK_ELSEIF:
1230 return parseDirectiveElseIf(IDLoc);
1231 case DK_ELSE:
1232 return parseDirectiveElse(IDLoc);
1233 case DK_ENDIF:
1234 return parseDirectiveEndIf(IDLoc);
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001235 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001236
Eli Bendersky88024712013-01-16 19:32:36 +00001237 // Ignore the statement if in the middle of inactive conditional
1238 // (e.g. ".if 0").
Chad Rosiereda70b32012-10-20 00:47:08 +00001239 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001240 eatToEndOfStatement();
Chris Lattner926885c2010-04-17 18:14:27 +00001241 return false;
1242 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00001243
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00001244 // FIXME: Recurse on local labels?
1245
1246 // See what kind of statement we have.
1247 switch (Lexer.getKind()) {
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001248 case AsmToken::Colon: {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001249 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001250
Chris Lattner36e02122009-06-21 20:54:55 +00001251 // identifier ':' -> Label.
Sean Callanan686ed8d2010-01-19 20:22:31 +00001252 Lex();
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001253
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001254 // Diagnose attempt to use '.' as a label.
1255 if (IDVal == ".")
1256 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1257
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001258 // Diagnose attempt to use a variable as a label.
1259 //
1260 // FIXME: Diagnostics. Note the location of the definition as a label.
1261 // FIXME: This doesn't diagnose assignment to a symbol which has been
1262 // implicitly marked as external.
Kevin Enderby0510b482010-05-17 23:08:19 +00001263 MCSymbol *Sym;
1264 if (LocalLabelVal == -1)
Daniel Dunbar101c14c2010-07-12 19:52:10 +00001265 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderby0510b482010-05-17 23:08:19 +00001266 else
1267 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbardeb7ba92010-05-05 19:01:00 +00001268 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001269 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencer530ce852010-10-09 11:00:50 +00001270
Daniel Dunbare73b2672009-08-26 22:13:22 +00001271 // Emit the label.
Chad Rosierf3feab32013-01-07 20:34:12 +00001272 if (!ParsingInlineAsm)
1273 Out.EmitLabel(Sym);
Michael J. Spencer530ce852010-10-09 11:00:50 +00001274
Kevin Enderbye7739d42011-12-09 18:09:40 +00001275 // If we are generating dwarf for assembly source files then gather the
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001276 // info to make a dwarf label entry for this label if needed.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001277 if (getContext().getGenDwarfForAssembly())
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001278 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1279 IDLoc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001280
Tim Northover1744d0a2013-10-25 12:49:50 +00001281 getTargetParser().onLabelParsed(Sym);
1282
Daniel Dunbar8271d1bb2010-05-23 18:36:34 +00001283 // Consume any end of statement token, if present, to avoid spurious
1284 // AddBlankLine calls().
1285 if (Lexer.is(AsmToken::EndOfStatement)) {
1286 Lex();
1287 if (Lexer.is(AsmToken::Eof))
1288 return false;
1289 }
1290
Eli Friedman0f4871d2012-10-22 23:58:19 +00001291 return false;
Daniel Dunbarae7ac012009-06-29 23:43:14 +00001292 }
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001293
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00001294 case AsmToken::Equal:
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001295 // identifier '=' ... -> assignment statement
Sean Callanan686ed8d2010-01-19 20:22:31 +00001296 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001297
Jim Grosbach4b905842013-09-20 23:08:21 +00001298 return parseAssignment(IDVal, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00001299
1300 default: // Normal instruction or directive.
1301 break;
Chris Lattner36e02122009-06-21 20:54:55 +00001302 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001303
1304 // If macros are enabled, check to see if this is a macro instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00001305 if (areMacrosEnabled())
1306 if (const MCAsmMacro *M = lookupMacro(IDVal)) {
1307 return handleMacroEntry(M, IDLoc);
Eli Bendersky38274122013-01-14 23:22:36 +00001308 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001309
Michael J. Spencer530ce852010-10-09 11:00:50 +00001310 // Otherwise, we have a normal instruction or directive.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001311
Eli Bendersky17233942013-01-15 22:59:42 +00001312 // Directives start with "."
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00001313 if (IDVal[0] == '.' && IDVal != ".") {
Eli Bendersky17233942013-01-15 22:59:42 +00001314 // There are several entities interested in parsing directives:
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001315 //
Eli Bendersky17233942013-01-15 22:59:42 +00001316 // 1. The target-specific assembly parser. Some directives are target
1317 // specific or may potentially behave differently on certain targets.
1318 // 2. Asm parser extensions. For example, platform-specific parsers
1319 // (like the ELF parser) register themselves as extensions.
1320 // 3. The generic directive parser implemented by this class. These are
1321 // all the directives that behave in a target and platform independent
1322 // manner, or at least have a default behavior that's shared between
1323 // all targets and platforms.
Akira Hatanakad3590752012-07-05 19:09:33 +00001324
Eli Bendersky17233942013-01-15 22:59:42 +00001325 // First query the target-specific parser. It will return 'true' if it
1326 // isn't interested in this directive.
Akira Hatanakad3590752012-07-05 19:09:33 +00001327 if (!getTargetParser().ParseDirective(ID))
1328 return false;
1329
Eli Bendersky17233942013-01-15 22:59:42 +00001330 // Next, check the extention directive map to see if any extension has
1331 // registered itself to parse this directive.
Jim Grosbach4b905842013-09-20 23:08:21 +00001332 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
1333 ExtensionDirectiveMap.lookup(IDVal);
Eli Bendersky17233942013-01-15 22:59:42 +00001334 if (Handler.first)
1335 return (*Handler.second)(Handler.first, IDVal, IDLoc);
1336
1337 // Finally, if no one else is interested in this directive, it must be
1338 // generic and familiar to this class.
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00001339 switch (DirKind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001340 default:
1341 break;
1342 case DK_SET:
1343 case DK_EQU:
1344 return parseDirectiveSet(IDVal, true);
1345 case DK_EQUIV:
1346 return parseDirectiveSet(IDVal, false);
1347 case DK_ASCII:
1348 return parseDirectiveAscii(IDVal, false);
1349 case DK_ASCIZ:
1350 case DK_STRING:
1351 return parseDirectiveAscii(IDVal, true);
1352 case DK_BYTE:
1353 return parseDirectiveValue(1);
1354 case DK_SHORT:
1355 case DK_VALUE:
1356 case DK_2BYTE:
1357 return parseDirectiveValue(2);
1358 case DK_LONG:
1359 case DK_INT:
1360 case DK_4BYTE:
1361 return parseDirectiveValue(4);
1362 case DK_QUAD:
1363 case DK_8BYTE:
1364 return parseDirectiveValue(8);
1365 case DK_SINGLE:
1366 case DK_FLOAT:
1367 return parseDirectiveRealValue(APFloat::IEEEsingle);
1368 case DK_DOUBLE:
1369 return parseDirectiveRealValue(APFloat::IEEEdouble);
1370 case DK_ALIGN: {
1371 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1372 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1373 }
1374 case DK_ALIGN32: {
1375 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes();
1376 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1377 }
1378 case DK_BALIGN:
1379 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
1380 case DK_BALIGNW:
1381 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
1382 case DK_BALIGNL:
1383 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
1384 case DK_P2ALIGN:
1385 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
1386 case DK_P2ALIGNW:
1387 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
1388 case DK_P2ALIGNL:
1389 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1390 case DK_ORG:
1391 return parseDirectiveOrg();
1392 case DK_FILL:
1393 return parseDirectiveFill();
1394 case DK_ZERO:
1395 return parseDirectiveZero();
1396 case DK_EXTERN:
1397 eatToEndOfStatement(); // .extern is the default, ignore it.
1398 return false;
1399 case DK_GLOBL:
1400 case DK_GLOBAL:
1401 return parseDirectiveSymbolAttribute(MCSA_Global);
1402 case DK_LAZY_REFERENCE:
1403 return parseDirectiveSymbolAttribute(MCSA_LazyReference);
1404 case DK_NO_DEAD_STRIP:
1405 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1406 case DK_SYMBOL_RESOLVER:
1407 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver);
1408 case DK_PRIVATE_EXTERN:
1409 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1410 case DK_REFERENCE:
1411 return parseDirectiveSymbolAttribute(MCSA_Reference);
1412 case DK_WEAK_DEFINITION:
1413 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1414 case DK_WEAK_REFERENCE:
1415 return parseDirectiveSymbolAttribute(MCSA_WeakReference);
1416 case DK_WEAK_DEF_CAN_BE_HIDDEN:
1417 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1418 case DK_COMM:
1419 case DK_COMMON:
1420 return parseDirectiveComm(/*IsLocal=*/false);
1421 case DK_LCOMM:
1422 return parseDirectiveComm(/*IsLocal=*/true);
1423 case DK_ABORT:
1424 return parseDirectiveAbort();
1425 case DK_INCLUDE:
1426 return parseDirectiveInclude();
1427 case DK_INCBIN:
1428 return parseDirectiveIncbin();
1429 case DK_CODE16:
1430 case DK_CODE16GCC:
1431 return TokError(Twine(IDVal) + " not supported yet");
1432 case DK_REPT:
1433 return parseDirectiveRept(IDLoc);
1434 case DK_IRP:
1435 return parseDirectiveIrp(IDLoc);
1436 case DK_IRPC:
1437 return parseDirectiveIrpc(IDLoc);
1438 case DK_ENDR:
1439 return parseDirectiveEndr(IDLoc);
1440 case DK_BUNDLE_ALIGN_MODE:
1441 return parseDirectiveBundleAlignMode();
1442 case DK_BUNDLE_LOCK:
1443 return parseDirectiveBundleLock();
1444 case DK_BUNDLE_UNLOCK:
1445 return parseDirectiveBundleUnlock();
1446 case DK_SLEB128:
1447 return parseDirectiveLEB128(true);
1448 case DK_ULEB128:
1449 return parseDirectiveLEB128(false);
1450 case DK_SPACE:
1451 case DK_SKIP:
1452 return parseDirectiveSpace(IDVal);
1453 case DK_FILE:
1454 return parseDirectiveFile(IDLoc);
1455 case DK_LINE:
1456 return parseDirectiveLine();
1457 case DK_LOC:
1458 return parseDirectiveLoc();
1459 case DK_STABS:
1460 return parseDirectiveStabs();
1461 case DK_CFI_SECTIONS:
1462 return parseDirectiveCFISections();
1463 case DK_CFI_STARTPROC:
1464 return parseDirectiveCFIStartProc();
1465 case DK_CFI_ENDPROC:
1466 return parseDirectiveCFIEndProc();
1467 case DK_CFI_DEF_CFA:
1468 return parseDirectiveCFIDefCfa(IDLoc);
1469 case DK_CFI_DEF_CFA_OFFSET:
1470 return parseDirectiveCFIDefCfaOffset();
1471 case DK_CFI_ADJUST_CFA_OFFSET:
1472 return parseDirectiveCFIAdjustCfaOffset();
1473 case DK_CFI_DEF_CFA_REGISTER:
1474 return parseDirectiveCFIDefCfaRegister(IDLoc);
1475 case DK_CFI_OFFSET:
1476 return parseDirectiveCFIOffset(IDLoc);
1477 case DK_CFI_REL_OFFSET:
1478 return parseDirectiveCFIRelOffset(IDLoc);
1479 case DK_CFI_PERSONALITY:
1480 return parseDirectiveCFIPersonalityOrLsda(true);
1481 case DK_CFI_LSDA:
1482 return parseDirectiveCFIPersonalityOrLsda(false);
1483 case DK_CFI_REMEMBER_STATE:
1484 return parseDirectiveCFIRememberState();
1485 case DK_CFI_RESTORE_STATE:
1486 return parseDirectiveCFIRestoreState();
1487 case DK_CFI_SAME_VALUE:
1488 return parseDirectiveCFISameValue(IDLoc);
1489 case DK_CFI_RESTORE:
1490 return parseDirectiveCFIRestore(IDLoc);
1491 case DK_CFI_ESCAPE:
1492 return parseDirectiveCFIEscape();
1493 case DK_CFI_SIGNAL_FRAME:
1494 return parseDirectiveCFISignalFrame();
1495 case DK_CFI_UNDEFINED:
1496 return parseDirectiveCFIUndefined(IDLoc);
1497 case DK_CFI_REGISTER:
1498 return parseDirectiveCFIRegister(IDLoc);
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001499 case DK_CFI_WINDOW_SAVE:
1500 return parseDirectiveCFIWindowSave();
Jim Grosbach4b905842013-09-20 23:08:21 +00001501 case DK_MACROS_ON:
1502 case DK_MACROS_OFF:
1503 return parseDirectiveMacrosOnOff(IDVal);
1504 case DK_MACRO:
1505 return parseDirectiveMacro(IDLoc);
1506 case DK_ENDM:
1507 case DK_ENDMACRO:
1508 return parseDirectiveEndMacro(IDVal);
1509 case DK_PURGEM:
1510 return parseDirectivePurgeMacro(IDLoc);
Eli Friedman20b02642010-07-19 04:17:25 +00001511 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00001512
Jim Grosbach758e0cc2012-05-01 18:38:27 +00001513 return Error(IDLoc, "unknown directive");
Chris Lattnere5074c42009-06-22 01:29:09 +00001514 }
Chris Lattner36e02122009-06-21 20:54:55 +00001515
Chad Rosierc7f552c2013-02-12 21:33:51 +00001516 // __asm _emit or __asm __emit
1517 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
1518 IDVal == "_EMIT" || IDVal == "__EMIT"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001519 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
Chad Rosierc7f552c2013-02-12 21:33:51 +00001520
1521 // __asm align
1522 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
Jim Grosbach4b905842013-09-20 23:08:21 +00001523 return parseDirectiveMSAlign(IDLoc, Info);
Eli Friedman0f4871d2012-10-22 23:58:19 +00001524
Jim Grosbachd2037eb2013-02-20 22:21:35 +00001525 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00001526
Chris Lattner7cbfa442010-05-19 23:34:33 +00001527 // Canonicalize the opcode to lower case.
Eli Bendersky88024712013-01-16 19:32:36 +00001528 std::string OpcodeStr = IDVal.lower();
Chad Rosierf0e87202012-10-25 20:41:34 +00001529 ParseInstructionInfo IInfo(Info.AsmRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00001530 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001531 Info.ParsedOperands);
Chad Rosier149e8e02012-12-12 22:45:52 +00001532 Info.ParseError = HadError;
Chris Lattnere5074c42009-06-22 01:29:09 +00001533
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001534 // Dump the parsed representation, if requested.
1535 if (getShowParsedOperands()) {
1536 SmallString<256> Str;
1537 raw_svector_ostream OS(Str);
1538 OS << "parsed instruction: [";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001539 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001540 if (i != 0)
1541 OS << ", ";
Eli Friedman0f4871d2012-10-22 23:58:19 +00001542 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001543 }
1544 OS << "]";
1545
Jim Grosbach4b905842013-09-20 23:08:21 +00001546 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar2eca0252010-08-11 06:37:09 +00001547 }
1548
Kevin Enderby6469fc22011-11-01 22:27:22 +00001549 // If we are generating dwarf for assembly source files and the current
1550 // section is the initial text section then generate a .loc directive for
1551 // the instruction.
1552 if (!HadError && getContext().getGenDwarfForAssembly() &&
Peter Collingbourne2f495b92013-04-17 21:18:16 +00001553 getContext().getGenDwarfSection() ==
Jim Grosbach4b905842013-09-20 23:08:21 +00001554 getStreamer().getCurrentSection().first) {
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001555
Eli Bendersky88024712013-01-16 19:32:36 +00001556 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001557
Eli Bendersky88024712013-01-16 19:32:36 +00001558 // If we previously parsed a cpp hash file line comment then make sure the
1559 // current Dwarf File is for the CppHashFilename if not then emit the
1560 // Dwarf File table for it and adjust the line number for the .loc.
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001561 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Jim Grosbach4b905842013-09-20 23:08:21 +00001562 getContext().getMCDwarfFiles();
Eli Bendersky88024712013-01-16 19:32:36 +00001563 if (CppHashFilename.size() != 0) {
1564 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001565 CppHashFilename)
Eli Bendersky88024712013-01-16 19:32:36 +00001566 getStreamer().EmitDwarfFileDirective(
Jim Grosbach4b905842013-09-20 23:08:21 +00001567 getContext().nextGenDwarfFileNumber(), StringRef(),
1568 CppHashFilename);
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001569
Jim Grosbach4b905842013-09-20 23:08:21 +00001570 // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's
1571 // cache with the different Loc from the call above we save the last
1572 // info we queried here with SrcMgr.FindLineNumber().
1573 unsigned CppHashLocLineNo;
1574 if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf)
1575 CppHashLocLineNo = LastQueryLine;
1576 else {
1577 CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf);
1578 LastQueryLine = CppHashLocLineNo;
1579 LastQueryIDLoc = CppHashLoc;
1580 LastQueryBuffer = CppHashBuf;
1581 }
1582 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
Eli Bendersky88024712013-01-16 19:32:36 +00001583 }
Kevin Enderby4eaf8ef2012-11-01 17:31:35 +00001584
Jim Grosbach4b905842013-09-20 23:08:21 +00001585 getStreamer().EmitDwarfLocDirective(
1586 getContext().getGenDwarfFileNumber(), Line, 0,
1587 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0,
1588 StringRef());
Kevin Enderby6469fc22011-11-01 22:27:22 +00001589 }
1590
Daniel Dunbarce0c1e12010-05-04 00:33:07 +00001591 // If parsing succeeded, match the instruction.
Chad Rosier49963552012-10-13 00:26:04 +00001592 if (!HadError) {
Chad Rosier49963552012-10-13 00:26:04 +00001593 unsigned ErrorInfo;
Jim Grosbach4b905842013-09-20 23:08:21 +00001594 HadError = getTargetParser().MatchAndEmitInstruction(
1595 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
1596 ParsingInlineAsm);
Chad Rosier49963552012-10-13 00:26:04 +00001597 }
Chris Lattnerf29c0b62010-01-14 22:21:20 +00001598
Chris Lattnera2a9d162010-09-11 16:18:25 +00001599 // Don't skip the rest of the line, the instruction parser is responsible for
1600 // that.
1601 return false;
Chris Lattnerb0133452009-06-21 20:16:42 +00001602}
Chris Lattnerbedf6c22009-06-24 04:43:34 +00001603
Jim Grosbach4b905842013-09-20 23:08:21 +00001604/// eatToEndOfLine uses the Lexer to eat the characters to the end of the line
Kevin Enderby72553612011-09-13 23:45:18 +00001605/// since they may not be able to be tokenized to get to the end of line token.
Jim Grosbach4b905842013-09-20 23:08:21 +00001606void AsmParser::eatToEndOfLine() {
Rafael Espindolae0d09082011-10-19 18:48:52 +00001607 if (!Lexer.is(AsmToken::EndOfStatement))
1608 Lexer.LexUntilEndOfLine();
Jim Grosbach4b905842013-09-20 23:08:21 +00001609 // Eat EOL.
1610 Lex();
Kevin Enderby72553612011-09-13 23:45:18 +00001611}
1612
Jim Grosbach4b905842013-09-20 23:08:21 +00001613/// parseCppHashLineFilenameComment as this:
Kevin Enderby72553612011-09-13 23:45:18 +00001614/// ::= # number "filename"
1615/// or just as a full line comment if it doesn't have a number and a string.
Jim Grosbach4b905842013-09-20 23:08:21 +00001616bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) {
Kevin Enderby72553612011-09-13 23:45:18 +00001617 Lex(); // Eat the hash token.
1618
1619 if (getLexer().isNot(AsmToken::Integer)) {
1620 // Consume the line since in cases it is not a well-formed line directive,
1621 // as if were simply a full line comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001622 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001623 return false;
1624 }
1625
1626 int64_t LineNumber = getTok().getIntVal();
Kevin Enderby72553612011-09-13 23:45:18 +00001627 Lex();
1628
1629 if (getLexer().isNot(AsmToken::String)) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001630 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001631 return false;
1632 }
1633
1634 StringRef Filename = getTok().getString();
1635 // Get rid of the enclosing quotes.
Jim Grosbach4b905842013-09-20 23:08:21 +00001636 Filename = Filename.substr(1, Filename.size() - 2);
Kevin Enderby72553612011-09-13 23:45:18 +00001637
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001638 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1639 CppHashLoc = L;
1640 CppHashFilename = Filename;
1641 CppHashLineNumber = LineNumber;
Kevin Enderby27121c12012-11-05 21:55:41 +00001642 CppHashBuf = CurBuffer;
Kevin Enderby72553612011-09-13 23:45:18 +00001643
1644 // Ignore any trailing characters, they're just comment.
Jim Grosbach4b905842013-09-20 23:08:21 +00001645 eatToEndOfLine();
Kevin Enderby72553612011-09-13 23:45:18 +00001646 return false;
1647}
1648
Jim Grosbach4b905842013-09-20 23:08:21 +00001649/// \brief will use the last parsed cpp hash line filename comment
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001650/// for the Filename and LineNo if any in the diagnostic.
1651void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001652 const AsmParser *Parser = static_cast<const AsmParser *>(Context);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001653 raw_ostream &OS = errs();
1654
1655 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1656 const SMLoc &DiagLoc = Diag.getLoc();
1657 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1658 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1659
Jim Grosbach4b905842013-09-20 23:08:21 +00001660 // Like SourceMgr::printMessage() we need to print the include stack if any
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001661 // before printing the message.
1662 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001663 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001664 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1665 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001666 }
1667
Eric Christophera7c32732012-12-18 00:30:54 +00001668 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001669 // manager changed or buffer changed (like in a nested include) then just
1670 // print the normal diagnostic using its Filename and LineNo.
Jim Grosbach4b905842013-09-20 23:08:21 +00001671 if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001672 DiagBuf != CppHashBuf) {
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001673 if (Parser->SavedDiagHandler)
1674 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1675 else
1676 Diag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001677 return;
1678 }
1679
Eric Christophera7c32732012-12-18 00:30:54 +00001680 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001681 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1682 // the diagnostic.
Jakub Staszakec2ffa92013-09-16 22:03:38 +00001683 const std::string &Filename = Parser->CppHashFilename;
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001684
1685 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1686 int CppHashLocLineNo =
1687 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
Jim Grosbach4b905842013-09-20 23:08:21 +00001688 int LineNo =
1689 Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001690
Jim Grosbach4b905842013-09-20 23:08:21 +00001691 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
1692 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
Chris Lattner72845262011-10-16 05:47:55 +00001693 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001694
Benjamin Kramer47f5e302011-10-16 10:48:29 +00001695 if (Parser->SavedDiagHandler)
1696 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1697 else
1698 NewDiag.print(0, OS);
Kevin Enderbye7c0c492011-10-12 21:38:39 +00001699}
1700
Rafael Espindola2c064482012-08-21 18:29:30 +00001701// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1702// difference being that that function accepts '@' as part of identifiers and
1703// we can't do that. AsmLexer.cpp should probably be changed to handle
1704// '@' as a special case when needed.
1705static bool isIdentifierChar(char c) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001706 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' ||
1707 c == '.';
Rafael Espindola2c064482012-08-21 18:29:30 +00001708}
1709
Rafael Espindola34b9c512012-06-03 23:57:14 +00001710bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Bendersky38274122013-01-14 23:22:36 +00001711 const MCAsmMacroParameters &Parameters,
Jim Grosbach4b905842013-09-20 23:08:21 +00001712 const MCAsmMacroArguments &A, const SMLoc &L) {
Rafael Espindola1134ab232011-06-05 02:43:45 +00001713 unsigned NParameters = Parameters.size();
1714 if (NParameters != 0 && NParameters != A.size())
1715 return Error(L, "Wrong number of arguments");
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001716
Preston Gurd05500642012-09-19 20:36:12 +00001717 // A macro without parameters is handled differently on Darwin:
1718 // gas accepts no arguments and does no substitutions
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001719 while (!Body.empty()) {
1720 // Scan for the next substitution.
1721 std::size_t End = Body.size(), Pos = 0;
1722 for (; Pos != End; ++Pos) {
1723 // Check for a substitution or escape.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001724 if (!NParameters) {
1725 // This macro has no parameters, look for $0, $1, etc.
1726 if (Body[Pos] != '$' || Pos + 1 == End)
1727 continue;
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001728
Rafael Espindola1134ab232011-06-05 02:43:45 +00001729 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00001730 if (Next == '$' || Next == 'n' ||
1731 isdigit(static_cast<unsigned char>(Next)))
Rafael Espindola1134ab232011-06-05 02:43:45 +00001732 break;
1733 } else {
1734 // This macro has parameters, look for \foo, \bar, etc.
1735 if (Body[Pos] == '\\' && Pos + 1 != End)
1736 break;
1737 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001738 }
1739
1740 // Add the prefix.
1741 OS << Body.slice(0, Pos);
1742
1743 // Check if we reached the end.
1744 if (Pos == End)
1745 break;
1746
Rafael Espindola1134ab232011-06-05 02:43:45 +00001747 if (!NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001748 switch (Body[Pos + 1]) {
1749 // $$ => $
Rafael Espindola1134ab232011-06-05 02:43:45 +00001750 case '$':
1751 OS << '$';
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001752 break;
1753
Jim Grosbach4b905842013-09-20 23:08:21 +00001754 // $n => number of arguments
Rafael Espindola1134ab232011-06-05 02:43:45 +00001755 case 'n':
1756 OS << A.size();
1757 break;
1758
Jim Grosbach4b905842013-09-20 23:08:21 +00001759 // $[0-9] => argument
Rafael Espindola1134ab232011-06-05 02:43:45 +00001760 default: {
1761 // Missing arguments are ignored.
Jim Grosbach4b905842013-09-20 23:08:21 +00001762 unsigned Index = Body[Pos + 1] - '0';
Rafael Espindola1134ab232011-06-05 02:43:45 +00001763 if (Index >= A.size())
1764 break;
1765
1766 // Otherwise substitute with the token values, with spaces eliminated.
Eli Benderskya7b905e2013-01-14 19:00:26 +00001767 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001768 ie = A[Index].end();
1769 it != ie; ++it)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001770 OS << it->getString();
1771 break;
1772 }
1773 }
1774 Pos += 2;
1775 } else {
1776 unsigned I = Pos + 1;
Rafael Espindola2c064482012-08-21 18:29:30 +00001777 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001778 ++I;
1779
Jim Grosbach4b905842013-09-20 23:08:21 +00001780 const char *Begin = Body.data() + Pos + 1;
1781 StringRef Argument(Begin, I - (Pos + 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +00001782 unsigned Index = 0;
1783 for (; Index < NParameters; ++Index)
Preston Gurd242ed3152012-09-19 20:29:04 +00001784 if (Parameters[Index].first == Argument)
Rafael Espindola1134ab232011-06-05 02:43:45 +00001785 break;
1786
Preston Gurd05500642012-09-19 20:36:12 +00001787 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001788 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
1789 Pos += 3;
1790 else {
1791 OS << '\\' << Argument;
1792 Pos = I;
1793 }
Preston Gurd05500642012-09-19 20:36:12 +00001794 } else {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001795 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Jim Grosbach4b905842013-09-20 23:08:21 +00001796 ie = A[Index].end();
1797 it != ie; ++it)
Preston Gurd05500642012-09-19 20:36:12 +00001798 if (it->getKind() == AsmToken::String)
1799 OS << it->getStringContents();
1800 else
1801 OS << it->getString();
Rafael Espindola1134ab232011-06-05 02:43:45 +00001802
Preston Gurd05500642012-09-19 20:36:12 +00001803 Pos += 1 + Argument.size();
1804 }
Rafael Espindola1134ab232011-06-05 02:43:45 +00001805 }
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001806 // Update the scan point.
Rafael Espindola1134ab232011-06-05 02:43:45 +00001807 Body = Body.substr(Pos);
Daniel Dunbar6fb1c3a2010-07-18 19:00:10 +00001808 }
Daniel Dunbar43235712010-07-18 18:54:11 +00001809
Rafael Espindola1134ab232011-06-05 02:43:45 +00001810 return false;
1811}
Daniel Dunbar43235712010-07-18 18:54:11 +00001812
Jim Grosbach4b905842013-09-20 23:08:21 +00001813MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB,
1814 SMLoc EL, MemoryBuffer *I)
1815 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1816 ExitLoc(EL) {}
Daniel Dunbar43235712010-07-18 18:54:11 +00001817
Jim Grosbach4b905842013-09-20 23:08:21 +00001818static bool isOperator(AsmToken::TokenKind kind) {
1819 switch (kind) {
1820 default:
1821 return false;
1822 case AsmToken::Plus:
1823 case AsmToken::Minus:
1824 case AsmToken::Tilde:
1825 case AsmToken::Slash:
1826 case AsmToken::Star:
1827 case AsmToken::Dot:
1828 case AsmToken::Equal:
1829 case AsmToken::EqualEqual:
1830 case AsmToken::Pipe:
1831 case AsmToken::PipePipe:
1832 case AsmToken::Caret:
1833 case AsmToken::Amp:
1834 case AsmToken::AmpAmp:
1835 case AsmToken::Exclaim:
1836 case AsmToken::ExclaimEqual:
1837 case AsmToken::Percent:
1838 case AsmToken::Less:
1839 case AsmToken::LessEqual:
1840 case AsmToken::LessLess:
1841 case AsmToken::LessGreater:
1842 case AsmToken::Greater:
1843 case AsmToken::GreaterEqual:
1844 case AsmToken::GreaterGreater:
1845 return true;
Preston Gurd05500642012-09-19 20:36:12 +00001846 }
1847}
1848
Jim Grosbach4b905842013-09-20 23:08:21 +00001849bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd05500642012-09-19 20:36:12 +00001850 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001851 unsigned ParenLevel = 0;
Preston Gurd05500642012-09-19 20:36:12 +00001852 unsigned AddTokens = 0;
1853
1854 // gas accepts arguments separated by whitespace, except on Darwin
1855 if (!IsDarwin)
1856 Lexer.setSkipSpace(false);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001857
1858 for (;;) {
Preston Gurd05500642012-09-19 20:36:12 +00001859 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1860 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001861 return TokError("unexpected token in macro instantiation");
Preston Gurd05500642012-09-19 20:36:12 +00001862 }
1863
1864 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1865 // Spaces and commas cannot be mixed to delimit parameters
1866 if (ArgumentDelimiter == AsmToken::Eof)
1867 ArgumentDelimiter = AsmToken::Comma;
1868 else if (ArgumentDelimiter != AsmToken::Comma) {
1869 Lexer.setSkipSpace(true);
1870 return TokError("expected ' ' for macro argument separator");
1871 }
1872 break;
1873 }
1874
1875 if (Lexer.is(AsmToken::Space)) {
1876 Lex(); // Eat spaces
1877
1878 // Spaces can delimit parameters, but could also be part an expression.
1879 // If the token after a space is an operator, add the token and the next
1880 // one into this argument
1881 if (ArgumentDelimiter == AsmToken::Space ||
1882 ArgumentDelimiter == AsmToken::Eof) {
Jim Grosbach4b905842013-09-20 23:08:21 +00001883 if (isOperator(Lexer.getKind())) {
Preston Gurd05500642012-09-19 20:36:12 +00001884 // Check to see whether the token is used as an operator,
1885 // or part of an identifier
Jordan Rosee8f1eae2013-01-07 19:00:49 +00001886 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd05500642012-09-19 20:36:12 +00001887 if (*NextChar == ' ')
1888 AddTokens = 2;
1889 }
1890
1891 if (!AddTokens && ParenLevel == 0) {
1892 if (ArgumentDelimiter == AsmToken::Eof &&
Jim Grosbach4b905842013-09-20 23:08:21 +00001893 !isOperator(Lexer.getKind()))
Preston Gurd05500642012-09-19 20:36:12 +00001894 ArgumentDelimiter = AsmToken::Space;
1895 break;
1896 }
1897 }
1898 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001899
Jim Grosbach4b905842013-09-20 23:08:21 +00001900 // handleMacroEntry relies on not advancing the lexer here
Rafael Espindola768b41c2012-06-15 14:02:34 +00001901 // to be able to fill in the remaining default parameter values
1902 if (Lexer.is(AsmToken::EndOfStatement))
1903 break;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001904
1905 // Adjust the current parentheses level.
1906 if (Lexer.is(AsmToken::LParen))
1907 ++ParenLevel;
1908 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1909 --ParenLevel;
1910
1911 // Append the token to the current argument list.
1912 MA.push_back(getTok());
Preston Gurd05500642012-09-19 20:36:12 +00001913 if (AddTokens)
1914 AddTokens--;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001915 Lex();
1916 }
Preston Gurd05500642012-09-19 20:36:12 +00001917
1918 Lexer.setSkipSpace(true);
Rafael Espindola768b41c2012-06-15 14:02:34 +00001919 if (ParenLevel != 0)
Rafael Espindola3e5eb422012-08-21 15:55:04 +00001920 return TokError("unbalanced parentheses in macro argument");
Rafael Espindola768b41c2012-06-15 14:02:34 +00001921 return false;
1922}
1923
1924// Parse the macro instantiation arguments.
Jim Grosbach4b905842013-09-20 23:08:21 +00001925bool AsmParser::parseMacroArguments(const MCAsmMacro *M,
Vladimir Medic9bad0d332013-08-20 13:33:18 +00001926 MCAsmMacroArguments &A) {
Rafael Espindola768b41c2012-06-15 14:02:34 +00001927 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd05500642012-09-19 20:36:12 +00001928 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00001929 // parseMacroArgument()
Preston Gurd05500642012-09-19 20:36:12 +00001930 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001931
1932 // Parse two kinds of macro invocations:
1933 // - macros defined without any parameters accept an arbitrary number of them
1934 // - macros defined with parameters accept at most that many of them
1935 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1936 ++Parameter) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00001937 MCAsmMacroArgument MA;
Rafael Espindola768b41c2012-06-15 14:02:34 +00001938
Jim Grosbach4b905842013-09-20 23:08:21 +00001939 if (parseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001940 return true;
1941
Preston Gurd242ed3152012-09-19 20:29:04 +00001942 if (!MA.empty() || !NParameters)
1943 A.push_back(MA);
1944 else if (NParameters) {
1945 if (!M->Parameters[Parameter].second.empty())
1946 A.push_back(M->Parameters[Parameter].second);
1947 }
Jim Grosbach206661622012-07-30 22:44:17 +00001948
Preston Gurd242ed3152012-09-19 20:29:04 +00001949 // At the end of the statement, fill in remaining arguments that have
1950 // default values. If there aren't any, then the next argument is
1951 // required but missing
1952 if (Lexer.is(AsmToken::EndOfStatement)) {
1953 if (NParameters && Parameter < NParameters - 1) {
1954 if (M->Parameters[Parameter + 1].second.empty())
1955 return TokError("macro argument '" +
1956 Twine(M->Parameters[Parameter + 1].first) +
1957 "' is missing");
1958 else
1959 continue;
1960 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001961 return false;
Preston Gurd242ed3152012-09-19 20:29:04 +00001962 }
Rafael Espindola768b41c2012-06-15 14:02:34 +00001963
1964 if (Lexer.is(AsmToken::Comma))
1965 Lex();
1966 }
1967 return TokError("Too many arguments");
1968}
1969
Jim Grosbach4b905842013-09-20 23:08:21 +00001970const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
1971 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001972 return (I == MacroMap.end()) ? NULL : I->getValue();
1973}
1974
Jim Grosbach4b905842013-09-20 23:08:21 +00001975void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) {
Eli Bendersky38274122013-01-14 23:22:36 +00001976 MacroMap[Name] = new MCAsmMacro(Macro);
1977}
1978
Jim Grosbach4b905842013-09-20 23:08:21 +00001979void AsmParser::undefineMacro(StringRef Name) {
1980 StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name);
Eli Bendersky38274122013-01-14 23:22:36 +00001981 if (I != MacroMap.end()) {
1982 delete I->getValue();
1983 MacroMap.erase(I);
1984 }
1985}
1986
Jim Grosbach4b905842013-09-20 23:08:21 +00001987bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbar43235712010-07-18 18:54:11 +00001988 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1989 // this, although we should protect against infinite loops.
1990 if (ActiveMacros.size() == 20)
1991 return TokError("macros cannot be nested more than 20 levels deep");
1992
Eli Bendersky38274122013-01-14 23:22:36 +00001993 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00001994 if (parseMacroArguments(M, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00001995 return true;
Daniel Dunbar43235712010-07-18 18:54:11 +00001996
Jim Grosbach206661622012-07-30 22:44:17 +00001997 // Remove any trailing empty arguments. Do this after-the-fact as we have
1998 // to keep empty arguments in the middle of the list or positionality
1999 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002000 while (!A.empty() && A.back().empty())
2001 A.pop_back();
Jim Grosbach206661622012-07-30 22:44:17 +00002002
Rafael Espindola1134ab232011-06-05 02:43:45 +00002003 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2004 // to hold the macro body with substitutions.
2005 SmallString<256> Buf;
2006 StringRef Body = M->Body;
Rafael Espindola34b9c512012-06-03 23:57:14 +00002007 raw_svector_ostream OS(Buf);
Rafael Espindola1134ab232011-06-05 02:43:45 +00002008
Rafael Espindolacb7eadf2012-08-08 14:51:03 +00002009 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola1134ab232011-06-05 02:43:45 +00002010 return true;
2011
Eli Bendersky38274122013-01-14 23:22:36 +00002012 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola34b9c512012-06-03 23:57:14 +00002013 // instantiation.
2014 OS << ".endmacro\n";
2015
Rafael Espindola1134ab232011-06-05 02:43:45 +00002016 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00002017 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola1134ab232011-06-05 02:43:45 +00002018
Daniel Dunbar43235712010-07-18 18:54:11 +00002019 // Create the macro instantiation object and add to the current macro
2020 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00002021 MacroInstantiation *MI = new MacroInstantiation(
2022 M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation);
Daniel Dunbar43235712010-07-18 18:54:11 +00002023 ActiveMacros.push_back(MI);
2024
2025 // Jump to the macro instantiation and prime the lexer.
2026 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
2027 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
2028 Lex();
2029
2030 return false;
2031}
2032
Jim Grosbach4b905842013-09-20 23:08:21 +00002033void AsmParser::handleMacroExit() {
Daniel Dunbar43235712010-07-18 18:54:11 +00002034 // Jump to the EndOfStatement we should return to, and consume it.
Jim Grosbach4b905842013-09-20 23:08:21 +00002035 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbar43235712010-07-18 18:54:11 +00002036 Lex();
2037
2038 // Pop the instantiation entry.
2039 delete ActiveMacros.back();
2040 ActiveMacros.pop_back();
2041}
2042
Jim Grosbach4b905842013-09-20 23:08:21 +00002043static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002044 switch (Value->getKind()) {
Rafael Espindola72f5f172012-01-28 05:57:00 +00002045 case MCExpr::Binary: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002046 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
2047 return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002048 }
Rafael Espindola72f5f172012-01-28 05:57:00 +00002049 case MCExpr::Target:
2050 case MCExpr::Constant:
2051 return false;
2052 case MCExpr::SymbolRef: {
Jim Grosbach4b905842013-09-20 23:08:21 +00002053 const MCSymbol &S =
2054 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
Rafael Espindola00472582012-01-28 06:22:14 +00002055 if (S.isVariable())
Jim Grosbach4b905842013-09-20 23:08:21 +00002056 return isUsedIn(Sym, S.getVariableValue());
Rafael Espindola00472582012-01-28 06:22:14 +00002057 return &S == Sym;
Rafael Espindola72f5f172012-01-28 05:57:00 +00002058 }
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002059 case MCExpr::Unary:
Jim Grosbach4b905842013-09-20 23:08:21 +00002060 return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002061 }
Benjamin Kramer4efe5062012-01-28 15:28:41 +00002062
2063 llvm_unreachable("Unknown expr kind!");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002064}
2065
Jim Grosbach4b905842013-09-20 23:08:21 +00002066bool AsmParser::parseAssignment(StringRef Name, bool allow_redef,
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002067 bool NoDeadStrip) {
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002068 // FIXME: Use better location, we should use proper tokens.
2069 SMLoc EqualLoc = Lexer.getLoc();
2070
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002071 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002072 if (parseExpression(Value))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002073 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002074
Rafael Espindola72f5f172012-01-28 05:57:00 +00002075 // Note: we don't count b as used in "a = b". This is to allow
2076 // a = b
2077 // b = c
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002078
Daniel Dunbarf2dcd772009-07-28 16:08:33 +00002079 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002080 return TokError("unexpected token in assignment");
2081
Daniel Dunbar6f4c9422011-03-25 17:47:17 +00002082 // Error on assignment to '.'.
2083 if (Name == ".") {
2084 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
2085 "(use '.space' or '.org').)"));
2086 }
2087
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002088 // Eat the end of statement marker.
Sean Callanan686ed8d2010-01-19 20:22:31 +00002089 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002090
Daniel Dunbar5f339242009-10-16 01:57:39 +00002091 // Validate that the LHS is allowed to be a variable (either it has not been
2092 // used as a symbol, or it is an absolute symbol).
2093 MCSymbol *Sym = getContext().LookupSymbol(Name);
2094 if (Sym) {
2095 // Diagnose assignment to a label.
2096 //
2097 // FIXME: Diagnostics. Note the location of the definition as a label.
2098 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Jim Grosbach4b905842013-09-20 23:08:21 +00002099 if (isUsedIn(Sym, Value))
Rafael Espindola72f5f172012-01-28 05:57:00 +00002100 return Error(EqualLoc, "Recursive use of '" + Name + "'");
2101 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar9b4a8242010-05-17 17:46:23 +00002102 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach12833172012-03-20 21:33:21 +00002103 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
2104 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar1bf128e2011-04-29 17:53:11 +00002105 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002106 return Error(EqualLoc, "redefinition of '" + Name + "'");
2107 else if (!Sym->isVariable())
2108 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar7a989da2010-05-05 17:41:00 +00002109 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar5f339242009-10-16 01:57:39 +00002110 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
Jim Grosbach4b905842013-09-20 23:08:21 +00002111 Name + "'");
Rafael Espindola46c79ef2010-11-15 14:40:36 +00002112
2113 // Don't count these checks as uses.
2114 Sym->setUsed(false);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002115 } else
Daniel Dunbar101c14c2010-07-12 19:52:10 +00002116 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar5f339242009-10-16 01:57:39 +00002117
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002118 // FIXME: Handle '.'.
Daniel Dunbarae7ac012009-06-29 23:43:14 +00002119
2120 // Do the assignment.
Daniel Dunbarb7b20972009-08-31 08:09:09 +00002121 Out.EmitAssignment(Sym, Value);
Jim Grosbachb7b750d2012-09-13 23:11:31 +00002122 if (NoDeadStrip)
2123 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2124
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002125 return false;
2126}
2127
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002128/// parseIdentifier:
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002129/// ::= identifier
2130/// ::= string
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002131bool AsmParser::parseIdentifier(StringRef &Res) {
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002132 // The assembler has relaxed rules for accepting identifiers, in particular we
Hans Wennborgce69d772013-10-18 20:46:28 +00002133 // allow things like '.globl $foo' and '.def @feat.00', which would normally be
2134 // separate tokens. At this level, we have already lexed so we cannot (currently)
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002135 // handle this as a context dependent token, instead we detect adjacent tokens
2136 // and return the combined identifier.
Hans Wennborgce69d772013-10-18 20:46:28 +00002137 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2138 SMLoc PrefixLoc = getLexer().getLoc();
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002139
Hans Wennborgce69d772013-10-18 20:46:28 +00002140 // Consume the prefix character, and check for a following identifier.
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002141 Lex();
2142 if (Lexer.isNot(AsmToken::Identifier))
2143 return true;
2144
Hans Wennborgce69d772013-10-18 20:46:28 +00002145 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2146 if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer())
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002147 return true;
2148
2149 // Construct the joined identifier and consume the token.
Jim Grosbach4b905842013-09-20 23:08:21 +00002150 Res =
Hans Wennborgce69d772013-10-18 20:46:28 +00002151 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
Daniel Dunbar3b96ffd2010-08-24 18:12:12 +00002152 Lex();
2153 return false;
2154 }
2155
Jim Grosbach4b905842013-09-20 23:08:21 +00002156 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002157 return true;
2158
Sean Callanan936b0d32010-01-19 21:44:56 +00002159 Res = getTok().getIdentifier();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002160
Sean Callanan686ed8d2010-01-19 20:22:31 +00002161 Lex(); // Consume the identifier token.
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002162
2163 return false;
2164}
2165
Jim Grosbach4b905842013-09-20 23:08:21 +00002166/// parseDirectiveSet:
Nico Weber4ada0d92011-01-28 03:04:41 +00002167/// ::= .equ identifier ',' expression
2168/// ::= .equiv identifier ',' expression
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002169/// ::= .set identifier ',' expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002170bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00002171 StringRef Name;
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002172
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002173 if (parseIdentifier(Name))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002174 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencer530ce852010-10-09 11:00:50 +00002175
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002176 if (getLexer().isNot(AsmToken::Comma))
Roman Divacky41e6ceb2010-10-28 16:57:58 +00002177 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002178 Lex();
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002179
Jim Grosbach4b905842013-09-20 23:08:21 +00002180 return parseAssignment(Name, allow_redef, true);
Daniel Dunbar2d2ee152009-06-25 21:56:11 +00002181}
2182
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002183bool AsmParser::parseEscapedString(std::string &Data) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002184 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbaref668c12009-08-14 18:19:52 +00002185
2186 Data = "";
Sean Callanan936b0d32010-01-19 21:44:56 +00002187 StringRef Str = getTok().getStringContents();
Daniel Dunbaref668c12009-08-14 18:19:52 +00002188 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2189 if (Str[i] != '\\') {
2190 Data += Str[i];
2191 continue;
2192 }
2193
2194 // Recognize escaped characters. Note that this escape semantics currently
2195 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2196 ++i;
2197 if (i == e)
2198 return TokError("unexpected backslash at end of string");
2199
2200 // Recognize octal sequences.
Jim Grosbach4b905842013-09-20 23:08:21 +00002201 if ((unsigned)(Str[i] - '0') <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002202 // Consume up to three octal characters.
2203 unsigned Value = Str[i] - '0';
2204
Jim Grosbach4b905842013-09-20 23:08:21 +00002205 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002206 ++i;
2207 Value = Value * 8 + (Str[i] - '0');
2208
Jim Grosbach4b905842013-09-20 23:08:21 +00002209 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) {
Daniel Dunbaref668c12009-08-14 18:19:52 +00002210 ++i;
2211 Value = Value * 8 + (Str[i] - '0');
2212 }
2213 }
2214
2215 if (Value > 255)
2216 return TokError("invalid octal escape sequence (out of range)");
2217
Jim Grosbach4b905842013-09-20 23:08:21 +00002218 Data += (unsigned char)Value;
Daniel Dunbaref668c12009-08-14 18:19:52 +00002219 continue;
2220 }
2221
2222 // Otherwise recognize individual escapes.
2223 switch (Str[i]) {
2224 default:
2225 // Just reject invalid escape sequences for now.
2226 return TokError("invalid escape sequence (unrecognized character)");
2227
2228 case 'b': Data += '\b'; break;
2229 case 'f': Data += '\f'; break;
2230 case 'n': Data += '\n'; break;
2231 case 'r': Data += '\r'; break;
2232 case 't': Data += '\t'; break;
2233 case '"': Data += '"'; break;
2234 case '\\': Data += '\\'; break;
2235 }
2236 }
2237
2238 return false;
2239}
2240
Jim Grosbach4b905842013-09-20 23:08:21 +00002241/// parseDirectiveAscii:
Rafael Espindola63760ba2010-10-28 20:02:27 +00002242/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002243bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002244 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002245 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002246
Daniel Dunbara10e5192009-06-24 23:30:00 +00002247 for (;;) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002248 if (getLexer().isNot(AsmToken::String))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002249 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002250
Daniel Dunbaref668c12009-08-14 18:19:52 +00002251 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002252 if (parseEscapedString(Data))
Daniel Dunbaref668c12009-08-14 18:19:52 +00002253 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002254
Rafael Espindola64e1af82013-07-02 15:49:13 +00002255 getStreamer().EmitBytes(Data);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002256 if (ZeroTerminated)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002257 getStreamer().EmitBytes(StringRef("\0", 1));
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002258
Sean Callanan686ed8d2010-01-19 20:22:31 +00002259 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002260
2261 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002262 break;
2263
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002264 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola63760ba2010-10-28 20:02:27 +00002265 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002266 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002267 }
2268 }
2269
Sean Callanan686ed8d2010-01-19 20:22:31 +00002270 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002271 return false;
2272}
2273
Jim Grosbach4b905842013-09-20 23:08:21 +00002274/// parseDirectiveValue
Daniel Dunbara10e5192009-06-24 23:30:00 +00002275/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002276bool AsmParser::parseDirectiveValue(unsigned Size) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002277 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002278 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002279
Daniel Dunbara10e5192009-06-24 23:30:00 +00002280 for (;;) {
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002281 const MCExpr *Value;
Jim Grosbach76346c32011-06-29 16:05:14 +00002282 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002283 if (parseExpression(Value))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002284 return true;
2285
Daniel Dunbar6738a2e2010-05-23 18:36:38 +00002286 // Special case constant expressions to match code generator.
Jim Grosbach76346c32011-06-29 16:05:14 +00002287 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2288 assert(Size <= 8 && "Invalid size");
2289 uint64_t IntValue = MCE->getValue();
2290 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2291 return Error(ExprLoc, "literal value out of range for directive");
Rafael Espindola64e1af82013-07-02 15:49:13 +00002292 getStreamer().EmitIntValue(IntValue, Size);
Jim Grosbach76346c32011-06-29 16:05:14 +00002293 } else
Rafael Espindola64e1af82013-07-02 15:49:13 +00002294 getStreamer().EmitValue(Value, Size);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002295
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002296 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002297 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002298
Daniel Dunbara10e5192009-06-24 23:30:00 +00002299 // FIXME: Improve diagnostic.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002300 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002301 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002302 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002303 }
2304 }
2305
Sean Callanan686ed8d2010-01-19 20:22:31 +00002306 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002307 return false;
2308}
2309
Jim Grosbach4b905842013-09-20 23:08:21 +00002310/// parseDirectiveRealValue
Daniel Dunbar2af16532010-09-24 01:59:56 +00002311/// ::= (.single | .double) [ expression (, expression)* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002312bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
Daniel Dunbar2af16532010-09-24 01:59:56 +00002313 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002314 checkForValidSection();
Daniel Dunbar2af16532010-09-24 01:59:56 +00002315
2316 for (;;) {
2317 // We don't truly support arithmetic on floating point expressions, so we
2318 // have to manually parse unary prefixes.
2319 bool IsNeg = false;
2320 if (getLexer().is(AsmToken::Minus)) {
2321 Lex();
2322 IsNeg = true;
2323 } else if (getLexer().is(AsmToken::Plus))
2324 Lex();
2325
Michael J. Spencer530ce852010-10-09 11:00:50 +00002326 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002327 getLexer().isNot(AsmToken::Real) &&
2328 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbar2af16532010-09-24 01:59:56 +00002329 return TokError("unexpected token in directive");
2330
2331 // Convert to an APFloat.
2332 APFloat Value(Semantics);
Kevin Enderby5bbe9572011-03-29 21:11:52 +00002333 StringRef IDVal = getTok().getString();
2334 if (getLexer().is(AsmToken::Identifier)) {
2335 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2336 Value = APFloat::getInf(Semantics);
2337 else if (!IDVal.compare_lower("nan"))
2338 Value = APFloat::getNaN(Semantics, false, ~0);
2339 else
2340 return TokError("invalid floating point literal");
2341 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Jim Grosbach4b905842013-09-20 23:08:21 +00002342 APFloat::opInvalidOp)
Daniel Dunbar2af16532010-09-24 01:59:56 +00002343 return TokError("invalid floating point literal");
2344 if (IsNeg)
2345 Value.changeSign();
2346
2347 // Consume the numeric token.
2348 Lex();
2349
2350 // Emit the value as an integer.
2351 APInt AsInt = Value.bitcastToAPInt();
2352 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
Rafael Espindola64e1af82013-07-02 15:49:13 +00002353 AsInt.getBitWidth() / 8);
Daniel Dunbar2af16532010-09-24 01:59:56 +00002354
2355 if (getLexer().is(AsmToken::EndOfStatement))
2356 break;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002357
Daniel Dunbar2af16532010-09-24 01:59:56 +00002358 if (getLexer().isNot(AsmToken::Comma))
2359 return TokError("unexpected token in directive");
2360 Lex();
2361 }
2362 }
2363
2364 Lex();
2365 return false;
2366}
2367
Jim Grosbach4b905842013-09-20 23:08:21 +00002368/// parseDirectiveZero
Rafael Espindola922e3f42010-09-16 15:03:59 +00002369/// ::= .zero expression
Jim Grosbach4b905842013-09-20 23:08:21 +00002370bool AsmParser::parseDirectiveZero() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002371 checkForValidSection();
Rafael Espindola922e3f42010-09-16 15:03:59 +00002372
2373 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002374 if (parseAbsoluteExpression(NumBytes))
Rafael Espindola922e3f42010-09-16 15:03:59 +00002375 return true;
2376
Rafael Espindolab91bac62010-10-05 19:42:57 +00002377 int64_t Val = 0;
2378 if (getLexer().is(AsmToken::Comma)) {
2379 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002380 if (parseAbsoluteExpression(Val))
Rafael Espindolab91bac62010-10-05 19:42:57 +00002381 return true;
2382 }
2383
Rafael Espindola922e3f42010-09-16 15:03:59 +00002384 if (getLexer().isNot(AsmToken::EndOfStatement))
2385 return TokError("unexpected token in '.zero' directive");
2386
2387 Lex();
2388
Rafael Espindola64e1af82013-07-02 15:49:13 +00002389 getStreamer().EmitFill(NumBytes, Val);
Rafael Espindola922e3f42010-09-16 15:03:59 +00002390
2391 return false;
2392}
2393
Jim Grosbach4b905842013-09-20 23:08:21 +00002394/// parseDirectiveFill
Roman Divackye33098f2013-09-24 17:44:41 +00002395/// ::= .fill expression [ , expression [ , expression ] ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002396bool AsmParser::parseDirectiveFill() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002397 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002398
Daniel Dunbara10e5192009-06-24 23:30:00 +00002399 int64_t NumValues;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002400 if (parseAbsoluteExpression(NumValues))
Daniel Dunbara10e5192009-06-24 23:30:00 +00002401 return true;
2402
Roman Divackye33098f2013-09-24 17:44:41 +00002403 int64_t FillSize = 1;
2404 int64_t FillExpr = 0;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002405
Roman Divackye33098f2013-09-24 17:44:41 +00002406 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2407 if (getLexer().isNot(AsmToken::Comma))
2408 return TokError("unexpected token in '.fill' directive");
2409 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002410
Roman Divackye33098f2013-09-24 17:44:41 +00002411 if (parseAbsoluteExpression(FillSize))
2412 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002413
Roman Divackye33098f2013-09-24 17:44:41 +00002414 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2415 if (getLexer().isNot(AsmToken::Comma))
2416 return TokError("unexpected token in '.fill' directive");
2417 Lex();
Daniel Dunbara10e5192009-06-24 23:30:00 +00002418
Roman Divackye33098f2013-09-24 17:44:41 +00002419 if (parseAbsoluteExpression(FillExpr))
2420 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002421
Roman Divackye33098f2013-09-24 17:44:41 +00002422 if (getLexer().isNot(AsmToken::EndOfStatement))
2423 return TokError("unexpected token in '.fill' directive");
2424
2425 Lex();
2426 }
2427 }
Daniel Dunbara10e5192009-06-24 23:30:00 +00002428
Daniel Dunbar8e5edd82009-08-21 15:43:35 +00002429 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2430 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara10e5192009-06-24 23:30:00 +00002431
2432 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Rafael Espindola64e1af82013-07-02 15:49:13 +00002433 getStreamer().EmitIntValue(FillExpr, FillSize);
Daniel Dunbara10e5192009-06-24 23:30:00 +00002434
2435 return false;
2436}
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002437
Jim Grosbach4b905842013-09-20 23:08:21 +00002438/// parseDirectiveOrg
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002439/// ::= .org expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00002440bool AsmParser::parseDirectiveOrg() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002441 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002442
Daniel Dunbar897ffad2009-08-31 08:09:28 +00002443 const MCExpr *Offset;
Jim Grosbachb5912772012-01-27 00:37:08 +00002444 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002445 if (parseExpression(Offset))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002446 return true;
2447
2448 // Parse optional fill expression.
2449 int64_t FillExpr = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002450 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2451 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002452 return TokError("unexpected token in '.org' directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002453 Lex();
Michael J. Spencer530ce852010-10-09 11:00:50 +00002454
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002455 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002456 return true;
2457
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002458 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002459 return TokError("unexpected token in '.org' directive");
2460 }
2461
Sean Callanan686ed8d2010-01-19 20:22:31 +00002462 Lex();
Daniel Dunbar75630b32009-06-30 02:10:03 +00002463
Jim Grosbachb5912772012-01-27 00:37:08 +00002464 // Only limited forms of relocatable expressions are accepted here, it
2465 // has to be relative to the current section. The streamer will return
2466 // 'true' if the expression wasn't evaluatable.
2467 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2468 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbar4a5a5612009-06-25 22:44:51 +00002469
2470 return false;
2471}
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002472
Jim Grosbach4b905842013-09-20 23:08:21 +00002473/// parseDirectiveAlign
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002474/// ::= {.align, ...} expression [ , expression [ , expression ]]
Jim Grosbach4b905842013-09-20 23:08:21 +00002475bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002476 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00002477
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002478 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002479 int64_t Alignment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002480 if (parseAbsoluteExpression(Alignment))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002481 return true;
2482
2483 SMLoc MaxBytesLoc;
2484 bool HasFillExpr = false;
2485 int64_t FillExpr = 0;
2486 int64_t MaxBytesToFill = 0;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002487 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2488 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002489 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002490 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002491
2492 // The fill expression can be omitted while specifying a maximum number of
2493 // alignment bytes, e.g:
2494 // .align 3,,4
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002495 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002496 HasFillExpr = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002497 if (parseAbsoluteExpression(FillExpr))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002498 return true;
2499 }
2500
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002501 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2502 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002503 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00002504 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002505
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002506 MaxBytesLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002507 if (parseAbsoluteExpression(MaxBytesToFill))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002508 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00002509
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002510 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002511 return TokError("unexpected token in directive");
2512 }
2513 }
2514
Sean Callanan686ed8d2010-01-19 20:22:31 +00002515 Lex();
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002516
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002517 if (!HasFillExpr)
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002518 FillExpr = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002519
2520 // Compute alignment in bytes.
2521 if (IsPow2) {
2522 // FIXME: Diagnose overflow.
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002523 if (Alignment >= 32) {
2524 Error(AlignmentLoc, "invalid alignment value");
2525 Alignment = 31;
2526 }
2527
Benjamin Kramer63951ad2009-09-06 09:35:10 +00002528 Alignment = 1ULL << Alignment;
Benjamin Kramer64bf7802013-02-16 15:00:16 +00002529 } else {
2530 // Reject alignments that aren't a power of two, for gas compatibility.
2531 if (!isPowerOf2_64(Alignment))
2532 Error(AlignmentLoc, "alignment must be a power of 2");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002533 }
2534
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002535 // Diagnose non-sensical max bytes to align.
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002536 if (MaxBytesLoc.isValid()) {
2537 if (MaxBytesToFill < 1) {
Daniel Dunbar18f3c9b2009-08-26 09:16:34 +00002538 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
Jim Grosbach4b905842013-09-20 23:08:21 +00002539 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar4abcccb2009-08-21 23:01:53 +00002540 MaxBytesToFill = 0;
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002541 }
2542
2543 if (MaxBytesToFill >= Alignment) {
Daniel Dunbarc9dc78a2009-06-30 00:49:23 +00002544 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
Jim Grosbach4b905842013-09-20 23:08:21 +00002545 "has no effect");
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002546 MaxBytesToFill = 0;
2547 }
2548 }
2549
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002550 // Check whether we should use optimal code alignment for this .align
2551 // directive.
Peter Collingbourne2f495b92013-04-17 21:18:16 +00002552 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign();
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002553 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2554 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00002555 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002556 } else {
Kevin Enderby7f993022010-02-25 18:46:04 +00002557 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnerc2b36752010-07-15 21:19:31 +00002558 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2559 MaxBytesToFill);
Daniel Dunbarbb166be2010-05-17 21:54:30 +00002560 }
Daniel Dunbarcc566a712009-06-29 23:46:59 +00002561
2562 return false;
2563}
2564
Jim Grosbach4b905842013-09-20 23:08:21 +00002565/// parseDirectiveFile
Eli Bendersky17233942013-01-15 22:59:42 +00002566/// ::= .file [number] filename
2567/// ::= .file number directory filename
Jim Grosbach4b905842013-09-20 23:08:21 +00002568bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002569 // FIXME: I'm not sure what this is.
2570 int64_t FileNumber = -1;
2571 SMLoc FileNumberLoc = getLexer().getLoc();
2572 if (getLexer().is(AsmToken::Integer)) {
2573 FileNumber = getTok().getIntVal();
2574 Lex();
2575
2576 if (FileNumber < 1)
2577 return TokError("file number less than one");
2578 }
2579
2580 if (getLexer().isNot(AsmToken::String))
2581 return TokError("unexpected token in '.file' directive");
2582
2583 // Usually the directory and filename together, otherwise just the directory.
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002584 // Allow the strings to have escaped octal character sequence.
2585 std::string Path = getTok().getString();
2586 if (parseEscapedString(Path))
2587 return true;
Eli Bendersky17233942013-01-15 22:59:42 +00002588 Lex();
2589
2590 StringRef Directory;
2591 StringRef Filename;
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002592 std::string FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002593 if (getLexer().is(AsmToken::String)) {
2594 if (FileNumber == -1)
2595 return TokError("explicit path specified, but no file number");
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00002596 if (parseEscapedString(FilenameData))
2597 return true;
2598 Filename = FilenameData;
Eli Bendersky17233942013-01-15 22:59:42 +00002599 Directory = Path;
2600 Lex();
2601 } else {
2602 Filename = Path;
2603 }
2604
2605 if (getLexer().isNot(AsmToken::EndOfStatement))
2606 return TokError("unexpected token in '.file' directive");
2607
2608 if (FileNumber == -1)
2609 getStreamer().EmitFileDirective(Filename);
2610 else {
2611 if (getContext().getGenDwarfForAssembly() == true)
Jim Grosbach4b905842013-09-20 23:08:21 +00002612 Error(DirectiveLoc,
2613 "input can't have .file dwarf directives when -g is "
2614 "used to generate dwarf debug info for assembly code");
Eli Bendersky17233942013-01-15 22:59:42 +00002615
2616 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
2617 Error(FileNumberLoc, "file number already allocated");
2618 }
2619
2620 return false;
2621}
2622
Jim Grosbach4b905842013-09-20 23:08:21 +00002623/// parseDirectiveLine
Eli Bendersky17233942013-01-15 22:59:42 +00002624/// ::= .line [number]
Jim Grosbach4b905842013-09-20 23:08:21 +00002625bool AsmParser::parseDirectiveLine() {
Eli Bendersky17233942013-01-15 22:59:42 +00002626 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2627 if (getLexer().isNot(AsmToken::Integer))
2628 return TokError("unexpected token in '.line' directive");
2629
2630 int64_t LineNumber = getTok().getIntVal();
Jim Grosbach4b905842013-09-20 23:08:21 +00002631 (void)LineNumber;
Eli Bendersky17233942013-01-15 22:59:42 +00002632 Lex();
2633
2634 // FIXME: Do something with the .line.
2635 }
2636
2637 if (getLexer().isNot(AsmToken::EndOfStatement))
2638 return TokError("unexpected token in '.line' directive");
2639
2640 return false;
2641}
2642
Jim Grosbach4b905842013-09-20 23:08:21 +00002643/// parseDirectiveLoc
Eli Bendersky17233942013-01-15 22:59:42 +00002644/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
2645/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2646/// The first number is a file number, must have been previously assigned with
2647/// a .file directive, the second number is the line number and optionally the
2648/// third number is a column position (zero if not specified). The remaining
2649/// optional items are .loc sub-directives.
Jim Grosbach4b905842013-09-20 23:08:21 +00002650bool AsmParser::parseDirectiveLoc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002651 if (getLexer().isNot(AsmToken::Integer))
2652 return TokError("unexpected token in '.loc' directive");
2653 int64_t FileNumber = getTok().getIntVal();
2654 if (FileNumber < 1)
2655 return TokError("file number less than one in '.loc' directive");
2656 if (!getContext().isValidDwarfFileNumber(FileNumber))
2657 return TokError("unassigned file number in '.loc' directive");
2658 Lex();
2659
2660 int64_t LineNumber = 0;
2661 if (getLexer().is(AsmToken::Integer)) {
2662 LineNumber = getTok().getIntVal();
Adrian Prantl6ac40032013-09-26 23:37:11 +00002663 if (LineNumber < 0)
2664 return TokError("line number less than zero in '.loc' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00002665 Lex();
2666 }
2667
2668 int64_t ColumnPos = 0;
2669 if (getLexer().is(AsmToken::Integer)) {
2670 ColumnPos = getTok().getIntVal();
2671 if (ColumnPos < 0)
2672 return TokError("column position less than zero in '.loc' directive");
2673 Lex();
2674 }
2675
2676 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
2677 unsigned Isa = 0;
2678 int64_t Discriminator = 0;
2679 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2680 for (;;) {
2681 if (getLexer().is(AsmToken::EndOfStatement))
2682 break;
2683
2684 StringRef Name;
2685 SMLoc Loc = getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002686 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002687 return TokError("unexpected token in '.loc' directive");
2688
2689 if (Name == "basic_block")
2690 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2691 else if (Name == "prologue_end")
2692 Flags |= DWARF2_FLAG_PROLOGUE_END;
2693 else if (Name == "epilogue_begin")
2694 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2695 else if (Name == "is_stmt") {
2696 Loc = getTok().getLoc();
2697 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002698 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002699 return true;
2700 // The expression must be the constant 0 or 1.
2701 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2702 int Value = MCE->getValue();
2703 if (Value == 0)
2704 Flags &= ~DWARF2_FLAG_IS_STMT;
2705 else if (Value == 1)
2706 Flags |= DWARF2_FLAG_IS_STMT;
2707 else
2708 return Error(Loc, "is_stmt value not 0 or 1");
Craig Topperf15655b2013-04-22 04:22:40 +00002709 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002710 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2711 }
Craig Topperf15655b2013-04-22 04:22:40 +00002712 } else if (Name == "isa") {
Eli Bendersky17233942013-01-15 22:59:42 +00002713 Loc = getTok().getLoc();
2714 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002715 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00002716 return true;
2717 // The expression must be a constant greater or equal to 0.
2718 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2719 int Value = MCE->getValue();
2720 if (Value < 0)
2721 return Error(Loc, "isa number less than zero");
2722 Isa = Value;
Craig Topperf15655b2013-04-22 04:22:40 +00002723 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002724 return Error(Loc, "isa number not a constant value");
2725 }
Craig Topperf15655b2013-04-22 04:22:40 +00002726 } else if (Name == "discriminator") {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002727 if (parseAbsoluteExpression(Discriminator))
Eli Bendersky17233942013-01-15 22:59:42 +00002728 return true;
Craig Topperf15655b2013-04-22 04:22:40 +00002729 } else {
Eli Bendersky17233942013-01-15 22:59:42 +00002730 return Error(Loc, "unknown sub-directive in '.loc' directive");
2731 }
2732
2733 if (getLexer().is(AsmToken::EndOfStatement))
2734 break;
2735 }
2736 }
2737
2738 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2739 Isa, Discriminator, StringRef());
2740
2741 return false;
2742}
2743
Jim Grosbach4b905842013-09-20 23:08:21 +00002744/// parseDirectiveStabs
Eli Bendersky17233942013-01-15 22:59:42 +00002745/// ::= .stabs string, number, number, number
Jim Grosbach4b905842013-09-20 23:08:21 +00002746bool AsmParser::parseDirectiveStabs() {
Eli Bendersky17233942013-01-15 22:59:42 +00002747 return TokError("unsupported directive '.stabs'");
2748}
2749
Jim Grosbach4b905842013-09-20 23:08:21 +00002750/// parseDirectiveCFISections
Eli Bendersky17233942013-01-15 22:59:42 +00002751/// ::= .cfi_sections section [, section]
Jim Grosbach4b905842013-09-20 23:08:21 +00002752bool AsmParser::parseDirectiveCFISections() {
Eli Bendersky17233942013-01-15 22:59:42 +00002753 StringRef Name;
2754 bool EH = false;
2755 bool Debug = false;
2756
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002757 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002758 return TokError("Expected an identifier");
2759
2760 if (Name == ".eh_frame")
2761 EH = true;
2762 else if (Name == ".debug_frame")
2763 Debug = true;
2764
2765 if (getLexer().is(AsmToken::Comma)) {
2766 Lex();
2767
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002768 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002769 return TokError("Expected an identifier");
2770
2771 if (Name == ".eh_frame")
2772 EH = true;
2773 else if (Name == ".debug_frame")
2774 Debug = true;
2775 }
2776
2777 getStreamer().EmitCFISections(EH, Debug);
2778 return false;
2779}
2780
Jim Grosbach4b905842013-09-20 23:08:21 +00002781/// parseDirectiveCFIStartProc
Eli Bendersky17233942013-01-15 22:59:42 +00002782/// ::= .cfi_startproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002783bool AsmParser::parseDirectiveCFIStartProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002784 getStreamer().EmitCFIStartProc();
2785 return false;
2786}
2787
Jim Grosbach4b905842013-09-20 23:08:21 +00002788/// parseDirectiveCFIEndProc
Eli Bendersky17233942013-01-15 22:59:42 +00002789/// ::= .cfi_endproc
Jim Grosbach4b905842013-09-20 23:08:21 +00002790bool AsmParser::parseDirectiveCFIEndProc() {
Eli Bendersky17233942013-01-15 22:59:42 +00002791 getStreamer().EmitCFIEndProc();
2792 return false;
2793}
2794
Jim Grosbach4b905842013-09-20 23:08:21 +00002795/// \brief parse register name or number.
2796bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register,
Eli Bendersky17233942013-01-15 22:59:42 +00002797 SMLoc DirectiveLoc) {
2798 unsigned RegNo;
2799
2800 if (getLexer().isNot(AsmToken::Integer)) {
2801 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc))
2802 return true;
Bill Wendlingbc07a892013-06-18 07:20:20 +00002803 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true);
Eli Bendersky17233942013-01-15 22:59:42 +00002804 } else
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002805 return parseAbsoluteExpression(Register);
Eli Bendersky17233942013-01-15 22:59:42 +00002806
2807 return false;
2808}
2809
Jim Grosbach4b905842013-09-20 23:08:21 +00002810/// parseDirectiveCFIDefCfa
Eli Bendersky17233942013-01-15 22:59:42 +00002811/// ::= .cfi_def_cfa register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002812bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002813 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002814 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002815 return true;
2816
2817 if (getLexer().isNot(AsmToken::Comma))
2818 return TokError("unexpected token in directive");
2819 Lex();
2820
2821 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002822 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002823 return true;
2824
2825 getStreamer().EmitCFIDefCfa(Register, Offset);
2826 return false;
2827}
2828
Jim Grosbach4b905842013-09-20 23:08:21 +00002829/// parseDirectiveCFIDefCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002830/// ::= .cfi_def_cfa_offset offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002831bool AsmParser::parseDirectiveCFIDefCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002832 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002833 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002834 return true;
2835
2836 getStreamer().EmitCFIDefCfaOffset(Offset);
2837 return false;
2838}
2839
Jim Grosbach4b905842013-09-20 23:08:21 +00002840/// parseDirectiveCFIRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002841/// ::= .cfi_register register, register
Jim Grosbach4b905842013-09-20 23:08:21 +00002842bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002843 int64_t Register1 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002844 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002845 return true;
2846
2847 if (getLexer().isNot(AsmToken::Comma))
2848 return TokError("unexpected token in directive");
2849 Lex();
2850
2851 int64_t Register2 = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002852 if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002853 return true;
2854
2855 getStreamer().EmitCFIRegister(Register1, Register2);
2856 return false;
2857}
2858
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00002859/// parseDirectiveCFIWindowSave
2860/// ::= .cfi_window_save
2861bool AsmParser::parseDirectiveCFIWindowSave() {
2862 getStreamer().EmitCFIWindowSave();
2863 return false;
2864}
2865
Jim Grosbach4b905842013-09-20 23:08:21 +00002866/// parseDirectiveCFIAdjustCfaOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002867/// ::= .cfi_adjust_cfa_offset adjustment
Jim Grosbach4b905842013-09-20 23:08:21 +00002868bool AsmParser::parseDirectiveCFIAdjustCfaOffset() {
Eli Bendersky17233942013-01-15 22:59:42 +00002869 int64_t Adjustment = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002870 if (parseAbsoluteExpression(Adjustment))
Eli Bendersky17233942013-01-15 22:59:42 +00002871 return true;
2872
2873 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2874 return false;
2875}
2876
Jim Grosbach4b905842013-09-20 23:08:21 +00002877/// parseDirectiveCFIDefCfaRegister
Eli Bendersky17233942013-01-15 22:59:42 +00002878/// ::= .cfi_def_cfa_register register
Jim Grosbach4b905842013-09-20 23:08:21 +00002879bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002880 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00002881 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002882 return true;
2883
2884 getStreamer().EmitCFIDefCfaRegister(Register);
2885 return false;
2886}
2887
Jim Grosbach4b905842013-09-20 23:08:21 +00002888/// parseDirectiveCFIOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002889/// ::= .cfi_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002890bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002891 int64_t Register = 0;
2892 int64_t Offset = 0;
2893
Jim Grosbach4b905842013-09-20 23:08:21 +00002894 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-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
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002901 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002902 return true;
2903
2904 getStreamer().EmitCFIOffset(Register, Offset);
2905 return false;
2906}
2907
Jim Grosbach4b905842013-09-20 23:08:21 +00002908/// parseDirectiveCFIRelOffset
Eli Bendersky17233942013-01-15 22:59:42 +00002909/// ::= .cfi_rel_offset register, offset
Jim Grosbach4b905842013-09-20 23:08:21 +00002910bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002911 int64_t Register = 0;
2912
Jim Grosbach4b905842013-09-20 23:08:21 +00002913 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00002914 return true;
2915
2916 if (getLexer().isNot(AsmToken::Comma))
2917 return TokError("unexpected token in directive");
2918 Lex();
2919
2920 int64_t Offset = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002921 if (parseAbsoluteExpression(Offset))
Eli Bendersky17233942013-01-15 22:59:42 +00002922 return true;
2923
2924 getStreamer().EmitCFIRelOffset(Register, Offset);
2925 return false;
2926}
2927
2928static bool isValidEncoding(int64_t Encoding) {
2929 if (Encoding & ~0xff)
2930 return false;
2931
2932 if (Encoding == dwarf::DW_EH_PE_omit)
2933 return true;
2934
2935 const unsigned Format = Encoding & 0xf;
2936 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2937 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2938 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2939 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2940 return false;
2941
2942 const unsigned Application = Encoding & 0x70;
2943 if (Application != dwarf::DW_EH_PE_absptr &&
2944 Application != dwarf::DW_EH_PE_pcrel)
2945 return false;
2946
2947 return true;
2948}
2949
Jim Grosbach4b905842013-09-20 23:08:21 +00002950/// parseDirectiveCFIPersonalityOrLsda
Eli Bendersky17233942013-01-15 22:59:42 +00002951/// IsPersonality true for cfi_personality, false for cfi_lsda
2952/// ::= .cfi_personality encoding, [symbol_name]
2953/// ::= .cfi_lsda encoding, [symbol_name]
Jim Grosbach4b905842013-09-20 23:08:21 +00002954bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) {
Eli Bendersky17233942013-01-15 22:59:42 +00002955 int64_t Encoding = 0;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002956 if (parseAbsoluteExpression(Encoding))
Eli Bendersky17233942013-01-15 22:59:42 +00002957 return true;
2958 if (Encoding == dwarf::DW_EH_PE_omit)
2959 return false;
2960
2961 if (!isValidEncoding(Encoding))
2962 return TokError("unsupported encoding.");
2963
2964 if (getLexer().isNot(AsmToken::Comma))
2965 return TokError("unexpected token in directive");
2966 Lex();
2967
2968 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00002969 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00002970 return TokError("expected identifier in directive");
2971
2972 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2973
2974 if (IsPersonality)
2975 getStreamer().EmitCFIPersonality(Sym, Encoding);
2976 else
2977 getStreamer().EmitCFILsda(Sym, Encoding);
2978 return false;
2979}
2980
Jim Grosbach4b905842013-09-20 23:08:21 +00002981/// parseDirectiveCFIRememberState
Eli Bendersky17233942013-01-15 22:59:42 +00002982/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00002983bool AsmParser::parseDirectiveCFIRememberState() {
Eli Bendersky17233942013-01-15 22:59:42 +00002984 getStreamer().EmitCFIRememberState();
2985 return false;
2986}
2987
Jim Grosbach4b905842013-09-20 23:08:21 +00002988/// parseDirectiveCFIRestoreState
Eli Bendersky17233942013-01-15 22:59:42 +00002989/// ::= .cfi_remember_state
Jim Grosbach4b905842013-09-20 23:08:21 +00002990bool AsmParser::parseDirectiveCFIRestoreState() {
Eli Bendersky17233942013-01-15 22:59:42 +00002991 getStreamer().EmitCFIRestoreState();
2992 return false;
2993}
2994
Jim Grosbach4b905842013-09-20 23:08:21 +00002995/// parseDirectiveCFISameValue
Eli Bendersky17233942013-01-15 22:59:42 +00002996/// ::= .cfi_same_value register
Jim Grosbach4b905842013-09-20 23:08:21 +00002997bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00002998 int64_t Register = 0;
2999
Jim Grosbach4b905842013-09-20 23:08:21 +00003000 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003001 return true;
3002
3003 getStreamer().EmitCFISameValue(Register);
3004 return false;
3005}
3006
Jim Grosbach4b905842013-09-20 23:08:21 +00003007/// parseDirectiveCFIRestore
Eli Bendersky17233942013-01-15 22:59:42 +00003008/// ::= .cfi_restore register
Jim Grosbach4b905842013-09-20 23:08:21 +00003009bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003010 int64_t Register = 0;
Jim Grosbach4b905842013-09-20 23:08:21 +00003011 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003012 return true;
3013
3014 getStreamer().EmitCFIRestore(Register);
3015 return false;
3016}
3017
Jim Grosbach4b905842013-09-20 23:08:21 +00003018/// parseDirectiveCFIEscape
Eli Bendersky17233942013-01-15 22:59:42 +00003019/// ::= .cfi_escape expression[,...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003020bool AsmParser::parseDirectiveCFIEscape() {
Eli Bendersky17233942013-01-15 22:59:42 +00003021 std::string Values;
3022 int64_t CurrValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003023 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003024 return true;
3025
3026 Values.push_back((uint8_t)CurrValue);
3027
3028 while (getLexer().is(AsmToken::Comma)) {
3029 Lex();
3030
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003031 if (parseAbsoluteExpression(CurrValue))
Eli Bendersky17233942013-01-15 22:59:42 +00003032 return true;
3033
3034 Values.push_back((uint8_t)CurrValue);
3035 }
3036
3037 getStreamer().EmitCFIEscape(Values);
3038 return false;
3039}
3040
Jim Grosbach4b905842013-09-20 23:08:21 +00003041/// parseDirectiveCFISignalFrame
Eli Bendersky17233942013-01-15 22:59:42 +00003042/// ::= .cfi_signal_frame
Jim Grosbach4b905842013-09-20 23:08:21 +00003043bool AsmParser::parseDirectiveCFISignalFrame() {
Eli Bendersky17233942013-01-15 22:59:42 +00003044 if (getLexer().isNot(AsmToken::EndOfStatement))
3045 return Error(getLexer().getLoc(),
3046 "unexpected token in '.cfi_signal_frame'");
3047
3048 getStreamer().EmitCFISignalFrame();
3049 return false;
3050}
3051
Jim Grosbach4b905842013-09-20 23:08:21 +00003052/// parseDirectiveCFIUndefined
Eli Bendersky17233942013-01-15 22:59:42 +00003053/// ::= .cfi_undefined register
Jim Grosbach4b905842013-09-20 23:08:21 +00003054bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003055 int64_t Register = 0;
3056
Jim Grosbach4b905842013-09-20 23:08:21 +00003057 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc))
Eli Bendersky17233942013-01-15 22:59:42 +00003058 return true;
3059
3060 getStreamer().EmitCFIUndefined(Register);
3061 return false;
3062}
3063
Jim Grosbach4b905842013-09-20 23:08:21 +00003064/// parseDirectiveMacrosOnOff
Eli Bendersky17233942013-01-15 22:59:42 +00003065/// ::= .macros_on
3066/// ::= .macros_off
Jim Grosbach4b905842013-09-20 23:08:21 +00003067bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003068 if (getLexer().isNot(AsmToken::EndOfStatement))
3069 return Error(getLexer().getLoc(),
3070 "unexpected token in '" + Directive + "' directive");
3071
Jim Grosbach4b905842013-09-20 23:08:21 +00003072 setMacrosEnabled(Directive == ".macros_on");
Eli Bendersky17233942013-01-15 22:59:42 +00003073 return false;
3074}
3075
Jim Grosbach4b905842013-09-20 23:08:21 +00003076/// parseDirectiveMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003077/// ::= .macro name [parameters]
Jim Grosbach4b905842013-09-20 23:08:21 +00003078bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003079 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003080 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003081 return TokError("expected identifier in '.macro' directive");
3082
3083 MCAsmMacroParameters Parameters;
3084 // Argument delimiter is initially unknown. It will be set by
Jim Grosbach4b905842013-09-20 23:08:21 +00003085 // parseMacroArgument()
Eli Bendersky17233942013-01-15 22:59:42 +00003086 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
3087 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3088 for (;;) {
3089 MCAsmMacroParameter Parameter;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003090 if (parseIdentifier(Parameter.first))
Eli Bendersky17233942013-01-15 22:59:42 +00003091 return TokError("expected identifier in '.macro' directive");
3092
3093 if (getLexer().is(AsmToken::Equal)) {
3094 Lex();
Jim Grosbach4b905842013-09-20 23:08:21 +00003095 if (parseMacroArgument(Parameter.second, ArgumentDelimiter))
Eli Bendersky17233942013-01-15 22:59:42 +00003096 return true;
3097 }
3098
3099 Parameters.push_back(Parameter);
3100
3101 if (getLexer().is(AsmToken::Comma))
3102 Lex();
3103 else if (getLexer().is(AsmToken::EndOfStatement))
3104 break;
3105 }
3106 }
3107
3108 // Eat the end of statement.
3109 Lex();
3110
3111 AsmToken EndToken, StartToken = getTok();
3112
3113 // Lex the macro definition.
3114 for (;;) {
3115 // Check whether we have reached the end of the file.
3116 if (getLexer().is(AsmToken::Eof))
3117 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3118
3119 // Otherwise, check whether we have reach the .endmacro.
3120 if (getLexer().is(AsmToken::Identifier) &&
3121 (getTok().getIdentifier() == ".endm" ||
3122 getTok().getIdentifier() == ".endmacro")) {
3123 EndToken = getTok();
3124 Lex();
3125 if (getLexer().isNot(AsmToken::EndOfStatement))
3126 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3127 "' directive");
3128 break;
3129 }
3130
3131 // Otherwise, scan til the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003132 eatToEndOfStatement();
Eli Bendersky17233942013-01-15 22:59:42 +00003133 }
3134
Jim Grosbach4b905842013-09-20 23:08:21 +00003135 if (lookupMacro(Name)) {
Eli Bendersky17233942013-01-15 22:59:42 +00003136 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3137 }
3138
3139 const char *BodyStart = StartToken.getLoc().getPointer();
3140 const char *BodyEnd = EndToken.getLoc().getPointer();
3141 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Jim Grosbach4b905842013-09-20 23:08:21 +00003142 checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
3143 defineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Eli Bendersky17233942013-01-15 22:59:42 +00003144 return false;
3145}
3146
Jim Grosbach4b905842013-09-20 23:08:21 +00003147/// checkForBadMacro
Kevin Enderby81c944c2013-01-22 21:44:53 +00003148///
3149/// With the support added for named parameters there may be code out there that
3150/// is transitioning from positional parameters. In versions of gas that did
3151/// not support named parameters they would be ignored on the macro defintion.
3152/// But to support both styles of parameters this is not possible so if a macro
3153/// defintion has named parameters but does not use them and has what appears
3154/// to be positional parameters, strings like $1, $2, ... and $n, then issue a
3155/// warning that the positional parameter found in body which have no effect.
3156/// Hoping the developer will either remove the named parameters from the macro
3157/// definiton so the positional parameters get used if that was what was
3158/// intended or change the macro to use the named parameters. It is possible
3159/// this warning will trigger when the none of the named parameters are used
3160/// and the strings like $1 are infact to simply to be passed trough unchanged.
Jim Grosbach4b905842013-09-20 23:08:21 +00003161void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name,
Kevin Enderby81c944c2013-01-22 21:44:53 +00003162 StringRef Body,
3163 MCAsmMacroParameters Parameters) {
3164 // If this macro is not defined with named parameters the warning we are
3165 // checking for here doesn't apply.
3166 unsigned NParameters = Parameters.size();
3167 if (NParameters == 0)
3168 return;
3169
3170 bool NamedParametersFound = false;
3171 bool PositionalParametersFound = false;
3172
3173 // Look at the body of the macro for use of both the named parameters and what
3174 // are likely to be positional parameters. This is what expandMacro() is
3175 // doing when it finds the parameters in the body.
3176 while (!Body.empty()) {
3177 // Scan for the next possible parameter.
3178 std::size_t End = Body.size(), Pos = 0;
3179 for (; Pos != End; ++Pos) {
3180 // Check for a substitution or escape.
3181 // This macro is defined with parameters, look for \foo, \bar, etc.
3182 if (Body[Pos] == '\\' && Pos + 1 != End)
3183 break;
3184
3185 // This macro should have parameters, but look for $0, $1, ..., $n too.
3186 if (Body[Pos] != '$' || Pos + 1 == End)
3187 continue;
3188 char Next = Body[Pos + 1];
Guy Benyei83c74e92013-02-12 21:21:59 +00003189 if (Next == '$' || Next == 'n' ||
3190 isdigit(static_cast<unsigned char>(Next)))
Kevin Enderby81c944c2013-01-22 21:44:53 +00003191 break;
3192 }
3193
3194 // Check if we reached the end.
3195 if (Pos == End)
3196 break;
3197
3198 if (Body[Pos] == '$') {
Jim Grosbach4b905842013-09-20 23:08:21 +00003199 switch (Body[Pos + 1]) {
3200 // $$ => $
Kevin Enderby81c944c2013-01-22 21:44:53 +00003201 case '$':
3202 break;
3203
Jim Grosbach4b905842013-09-20 23:08:21 +00003204 // $n => number of arguments
Kevin Enderby81c944c2013-01-22 21:44:53 +00003205 case 'n':
3206 PositionalParametersFound = true;
3207 break;
3208
Jim Grosbach4b905842013-09-20 23:08:21 +00003209 // $[0-9] => argument
Kevin Enderby81c944c2013-01-22 21:44:53 +00003210 default: {
3211 PositionalParametersFound = true;
3212 break;
Jim Grosbach4b905842013-09-20 23:08:21 +00003213 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003214 }
3215 Pos += 2;
3216 } else {
3217 unsigned I = Pos + 1;
3218 while (isIdentifierChar(Body[I]) && I + 1 != End)
3219 ++I;
3220
Jim Grosbach4b905842013-09-20 23:08:21 +00003221 const char *Begin = Body.data() + Pos + 1;
3222 StringRef Argument(Begin, I - (Pos + 1));
Kevin Enderby81c944c2013-01-22 21:44:53 +00003223 unsigned Index = 0;
3224 for (; Index < NParameters; ++Index)
3225 if (Parameters[Index].first == Argument)
3226 break;
3227
3228 if (Index == NParameters) {
Jim Grosbach4b905842013-09-20 23:08:21 +00003229 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')')
3230 Pos += 3;
3231 else {
3232 Pos = I;
3233 }
Kevin Enderby81c944c2013-01-22 21:44:53 +00003234 } else {
3235 NamedParametersFound = true;
3236 Pos += 1 + Argument.size();
3237 }
3238 }
3239 // Update the scan point.
3240 Body = Body.substr(Pos);
3241 }
3242
3243 if (!NamedParametersFound && PositionalParametersFound)
3244 Warning(DirectiveLoc, "macro defined with named parameters which are not "
3245 "used in macro body, possible positional parameter "
3246 "found in body which will have no effect");
3247}
3248
Jim Grosbach4b905842013-09-20 23:08:21 +00003249/// parseDirectiveEndMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003250/// ::= .endm
3251/// ::= .endmacro
Jim Grosbach4b905842013-09-20 23:08:21 +00003252bool AsmParser::parseDirectiveEndMacro(StringRef Directive) {
Eli Bendersky17233942013-01-15 22:59:42 +00003253 if (getLexer().isNot(AsmToken::EndOfStatement))
3254 return TokError("unexpected token in '" + Directive + "' directive");
3255
3256 // If we are inside a macro instantiation, terminate the current
3257 // instantiation.
Jim Grosbach4b905842013-09-20 23:08:21 +00003258 if (isInsideMacroInstantiation()) {
3259 handleMacroExit();
Eli Bendersky17233942013-01-15 22:59:42 +00003260 return false;
3261 }
3262
3263 // Otherwise, this .endmacro is a stray entry in the file; well formed
3264 // .endmacro directives are handled during the macro definition parsing.
3265 return TokError("unexpected '" + Directive + "' in file, "
Jim Grosbach4b905842013-09-20 23:08:21 +00003266 "no current macro definition");
Eli Bendersky17233942013-01-15 22:59:42 +00003267}
3268
Jim Grosbach4b905842013-09-20 23:08:21 +00003269/// parseDirectivePurgeMacro
Eli Bendersky17233942013-01-15 22:59:42 +00003270/// ::= .purgem
Jim Grosbach4b905842013-09-20 23:08:21 +00003271bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
Eli Bendersky17233942013-01-15 22:59:42 +00003272 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003273 if (parseIdentifier(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003274 return TokError("expected identifier in '.purgem' directive");
3275
3276 if (getLexer().isNot(AsmToken::EndOfStatement))
3277 return TokError("unexpected token in '.purgem' directive");
3278
Jim Grosbach4b905842013-09-20 23:08:21 +00003279 if (!lookupMacro(Name))
Eli Bendersky17233942013-01-15 22:59:42 +00003280 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3281
Jim Grosbach4b905842013-09-20 23:08:21 +00003282 undefineMacro(Name);
Eli Bendersky17233942013-01-15 22:59:42 +00003283 return false;
3284}
Eli Benderskyf483ff92012-12-20 19:05:53 +00003285
Jim Grosbach4b905842013-09-20 23:08:21 +00003286/// parseDirectiveBundleAlignMode
Eli Benderskyf483ff92012-12-20 19:05:53 +00003287/// ::= {.bundle_align_mode} expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003288bool AsmParser::parseDirectiveBundleAlignMode() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003289 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003290
3291 // Expect a single argument: an expression that evaluates to a constant
3292 // in the inclusive range 0-30.
3293 SMLoc ExprLoc = getLexer().getLoc();
3294 int64_t AlignSizePow2;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003295 if (parseAbsoluteExpression(AlignSizePow2))
Eli Benderskyf483ff92012-12-20 19:05:53 +00003296 return true;
3297 else if (getLexer().isNot(AsmToken::EndOfStatement))
3298 return TokError("unexpected token after expression in"
3299 " '.bundle_align_mode' directive");
3300 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
3301 return Error(ExprLoc,
3302 "invalid bundle alignment size (expected between 0 and 30)");
3303
3304 Lex();
3305
3306 // Because of AlignSizePow2's verified range we can safely truncate it to
3307 // unsigned.
3308 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
3309 return false;
3310}
3311
Jim Grosbach4b905842013-09-20 23:08:21 +00003312/// parseDirectiveBundleLock
Eli Bendersky802b6282013-01-07 21:51:08 +00003313/// ::= {.bundle_lock} [align_to_end]
Jim Grosbach4b905842013-09-20 23:08:21 +00003314bool AsmParser::parseDirectiveBundleLock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003315 checkForValidSection();
Eli Bendersky802b6282013-01-07 21:51:08 +00003316 bool AlignToEnd = false;
Eli Benderskyf483ff92012-12-20 19:05:53 +00003317
Eli Bendersky802b6282013-01-07 21:51:08 +00003318 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3319 StringRef Option;
3320 SMLoc Loc = getTok().getLoc();
3321 const char *kInvalidOptionError =
Jim Grosbach4b905842013-09-20 23:08:21 +00003322 "invalid option for '.bundle_lock' directive";
Eli Bendersky802b6282013-01-07 21:51:08 +00003323
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003324 if (parseIdentifier(Option))
Eli Bendersky802b6282013-01-07 21:51:08 +00003325 return Error(Loc, kInvalidOptionError);
3326
3327 if (Option != "align_to_end")
3328 return Error(Loc, kInvalidOptionError);
3329 else if (getLexer().isNot(AsmToken::EndOfStatement))
3330 return Error(Loc,
3331 "unexpected token after '.bundle_lock' directive option");
3332 AlignToEnd = true;
3333 }
3334
Eli Benderskyf483ff92012-12-20 19:05:53 +00003335 Lex();
3336
Eli Bendersky802b6282013-01-07 21:51:08 +00003337 getStreamer().EmitBundleLock(AlignToEnd);
Eli Benderskyf483ff92012-12-20 19:05:53 +00003338 return false;
3339}
3340
Jim Grosbach4b905842013-09-20 23:08:21 +00003341/// parseDirectiveBundleLock
Eli Benderskyf483ff92012-12-20 19:05:53 +00003342/// ::= {.bundle_lock}
Jim Grosbach4b905842013-09-20 23:08:21 +00003343bool AsmParser::parseDirectiveBundleUnlock() {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003344 checkForValidSection();
Eli Benderskyf483ff92012-12-20 19:05:53 +00003345
3346 if (getLexer().isNot(AsmToken::EndOfStatement))
3347 return TokError("unexpected token in '.bundle_unlock' directive");
3348 Lex();
3349
3350 getStreamer().EmitBundleUnlock();
3351 return false;
3352}
3353
Jim Grosbach4b905842013-09-20 23:08:21 +00003354/// parseDirectiveSpace
Eli Bendersky17233942013-01-15 22:59:42 +00003355/// ::= (.skip | .space) expression [ , expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003356bool AsmParser::parseDirectiveSpace(StringRef IDVal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003357 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003358
3359 int64_t NumBytes;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003360 if (parseAbsoluteExpression(NumBytes))
Eli Bendersky17233942013-01-15 22:59:42 +00003361 return true;
3362
3363 int64_t FillExpr = 0;
3364 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3365 if (getLexer().isNot(AsmToken::Comma))
3366 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3367 Lex();
3368
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003369 if (parseAbsoluteExpression(FillExpr))
Eli Bendersky17233942013-01-15 22:59:42 +00003370 return true;
3371
3372 if (getLexer().isNot(AsmToken::EndOfStatement))
3373 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
3374 }
3375
3376 Lex();
3377
3378 if (NumBytes <= 0)
Jim Grosbach4b905842013-09-20 23:08:21 +00003379 return TokError("invalid number of bytes in '" + Twine(IDVal) +
3380 "' directive");
Eli Bendersky17233942013-01-15 22:59:42 +00003381
3382 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Rafael Espindola64e1af82013-07-02 15:49:13 +00003383 getStreamer().EmitFill(NumBytes, FillExpr);
Eli Bendersky17233942013-01-15 22:59:42 +00003384
3385 return false;
3386}
3387
Jim Grosbach4b905842013-09-20 23:08:21 +00003388/// parseDirectiveLEB128
Eli Bendersky17233942013-01-15 22:59:42 +00003389/// ::= (.sleb128 | .uleb128) expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003390bool AsmParser::parseDirectiveLEB128(bool Signed) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003391 checkForValidSection();
Eli Bendersky17233942013-01-15 22:59:42 +00003392 const MCExpr *Value;
3393
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003394 if (parseExpression(Value))
Eli Bendersky17233942013-01-15 22:59:42 +00003395 return true;
3396
3397 if (getLexer().isNot(AsmToken::EndOfStatement))
3398 return TokError("unexpected token in directive");
3399
3400 if (Signed)
3401 getStreamer().EmitSLEB128Value(Value);
3402 else
3403 getStreamer().EmitULEB128Value(Value);
3404
3405 return false;
3406}
3407
Jim Grosbach4b905842013-09-20 23:08:21 +00003408/// parseDirectiveSymbolAttribute
Daniel Dunbara5508c82009-06-30 00:33:19 +00003409/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003410bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003411 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara5508c82009-06-30 00:33:19 +00003412 for (;;) {
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003413 StringRef Name;
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003414 SMLoc Loc = getTok().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003415
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003416 if (parseIdentifier(Name))
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003417 return Error(Loc, "expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003418
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003419 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbara5508c82009-06-30 00:33:19 +00003420
Jim Grosbachebdf32f2011-09-15 17:56:49 +00003421 // Assembler local symbols don't make any sense here. Complain loudly.
3422 if (Sym->isTemporary())
3423 return Error(Loc, "non-local symbol required in directive");
3424
Saleem Abdulrasool4208b612013-08-09 01:52:03 +00003425 if (!getStreamer().EmitSymbolAttribute(Sym, Attr))
3426 return Error(Loc, "unable to emit symbol attribute");
Daniel Dunbara5508c82009-06-30 00:33:19 +00003427
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003428 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003429 break;
3430
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003431 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara5508c82009-06-30 00:33:19 +00003432 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003433 Lex();
Daniel Dunbara5508c82009-06-30 00:33:19 +00003434 }
3435 }
3436
Sean Callanan686ed8d2010-01-19 20:22:31 +00003437 Lex();
Jan Wen Voungc7682872010-09-30 01:09:20 +00003438 return false;
Daniel Dunbara5508c82009-06-30 00:33:19 +00003439}
Chris Lattnera1e11f52009-07-07 20:30:46 +00003440
Jim Grosbach4b905842013-09-20 23:08:21 +00003441/// parseDirectiveComm
Chris Lattner28ad7542009-07-09 17:25:12 +00003442/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
Jim Grosbach4b905842013-09-20 23:08:21 +00003443bool AsmParser::parseDirectiveComm(bool IsLocal) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003444 checkForValidSection();
Daniel Dunbare5444a82010-09-09 22:42:59 +00003445
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003446 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbarc54ecb32009-08-01 00:48:30 +00003447 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003448 if (parseIdentifier(Name))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003449 return TokError("expected identifier in directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003450
Daniel Dunbar9ee33ca2009-07-31 21:55:09 +00003451 // Handle the identifier as the key symbol.
Daniel Dunbar101c14c2010-07-12 19:52:10 +00003452 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003453
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003454 if (getLexer().isNot(AsmToken::Comma))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003455 return TokError("unexpected token in directive");
Sean Callanan686ed8d2010-01-19 20:22:31 +00003456 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003457
3458 int64_t Size;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003459 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003460 if (parseAbsoluteExpression(Size))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003461 return true;
3462
3463 int64_t Pow2Alignment = 0;
3464 SMLoc Pow2AlignmentLoc;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003465 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan686ed8d2010-01-19 20:22:31 +00003466 Lex();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003467 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003468 if (parseAbsoluteExpression(Pow2Alignment))
Chris Lattnera1e11f52009-07-07 20:30:46 +00003469 return true;
Michael J. Spencer530ce852010-10-09 11:00:50 +00003470
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003471 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
3472 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003473 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
3474
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003475 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramer68b9f052012-09-07 21:08:01 +00003476 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
3477 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattnerab9cd3e2010-01-19 06:22:22 +00003478 if (!isPowerOf2_64(Pow2Alignment))
3479 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
3480 Pow2Alignment = Log2_64(Pow2Alignment);
3481 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003482 }
Michael J. Spencer530ce852010-10-09 11:00:50 +00003483
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003484 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner28ad7542009-07-09 17:25:12 +00003485 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003486
Sean Callanan686ed8d2010-01-19 20:22:31 +00003487 Lex();
Chris Lattnera1e11f52009-07-07 20:30:46 +00003488
Chris Lattner28ad7542009-07-09 17:25:12 +00003489 // NOTE: a size of zero for a .comm should create a undefined symbol
3490 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattnera1e11f52009-07-07 20:30:46 +00003491 if (Size < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003492 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
Jim Grosbach4b905842013-09-20 23:08:21 +00003493 "be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003494
Eric Christopherbc818852010-05-14 01:38:54 +00003495 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattnera1e11f52009-07-07 20:30:46 +00003496 // may internally end up wanting an alignment in bytes.
3497 // FIXME: Diagnose overflow.
3498 if (Pow2Alignment < 0)
Chris Lattner28ad7542009-07-09 17:25:12 +00003499 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
Jim Grosbach4b905842013-09-20 23:08:21 +00003500 "alignment, can't be less than zero");
Chris Lattnera1e11f52009-07-07 20:30:46 +00003501
Daniel Dunbar6860ac72009-08-22 07:22:36 +00003502 if (!Sym->isUndefined())
Chris Lattnera1e11f52009-07-07 20:30:46 +00003503 return Error(IDLoc, "invalid symbol redefinition");
3504
Chris Lattner28ad7542009-07-09 17:25:12 +00003505 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003506 if (IsLocal) {
Benjamin Kramer47f9ec92012-09-07 17:25:13 +00003507 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar6a715dc2009-08-30 06:17:16 +00003508 return false;
3509 }
Chris Lattnera1e11f52009-07-07 20:30:46 +00003510
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003511 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattnera1e11f52009-07-07 20:30:46 +00003512 return false;
3513}
Chris Lattner07cadaf2009-07-10 22:20:30 +00003514
Jim Grosbach4b905842013-09-20 23:08:21 +00003515/// parseDirectiveAbort
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003516/// ::= .abort [... message ...]
Jim Grosbach4b905842013-09-20 23:08:21 +00003517bool AsmParser::parseDirectiveAbort() {
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003518 // FIXME: Use loc from directive.
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003519 SMLoc Loc = getLexer().getLoc();
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003520
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003521 StringRef Str = parseStringToEndOfStatement();
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003522 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby56523ce2009-07-13 23:15:14 +00003523 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003524
Sean Callanan686ed8d2010-01-19 20:22:31 +00003525 Lex();
Kevin Enderby56523ce2009-07-13 23:15:14 +00003526
Daniel Dunbareb6bb322009-07-27 23:20:52 +00003527 if (Str.empty())
3528 Error(Loc, ".abort detected. Assembly stopping.");
3529 else
3530 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar40a564f2010-07-18 20:15:59 +00003531 // FIXME: Actually abort assembly here.
Kevin Enderby56523ce2009-07-13 23:15:14 +00003532
3533 return false;
3534}
Kevin Enderbycbe475d2009-07-14 21:35:03 +00003535
Jim Grosbach4b905842013-09-20 23:08:21 +00003536/// parseDirectiveInclude
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003537/// ::= .include "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003538bool AsmParser::parseDirectiveInclude() {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003539 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003540 return TokError("expected string in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003541
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003542 // Allow the strings to have escaped octal character sequence.
3543 std::string Filename;
3544 if (parseEscapedString(Filename))
3545 return true;
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003546 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan686ed8d2010-01-19 20:22:31 +00003547 Lex();
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003548
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003549 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003550 return TokError("unexpected token in '.include' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003551
Chris Lattner693fbb82009-07-16 06:14:39 +00003552 // Attempt to switch the lexer to the included file before consuming the end
3553 // of statement to avoid losing it when we switch.
Jim Grosbach4b905842013-09-20 23:08:21 +00003554 if (enterIncludeFile(Filename)) {
Daniel Dunbard8a18452010-07-18 18:31:45 +00003555 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner693fbb82009-07-16 06:14:39 +00003556 return true;
3557 }
Kevin Enderbyd1ea5392009-07-14 23:21:55 +00003558
3559 return false;
3560}
Kevin Enderby09ea5702009-07-15 15:30:11 +00003561
Jim Grosbach4b905842013-09-20 23:08:21 +00003562/// parseDirectiveIncbin
Kevin Enderby109f25c2011-12-14 21:47:48 +00003563/// ::= .incbin "filename"
Jim Grosbach4b905842013-09-20 23:08:21 +00003564bool AsmParser::parseDirectiveIncbin() {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003565 if (getLexer().isNot(AsmToken::String))
3566 return TokError("expected string in '.incbin' directive");
3567
Yunzhong Gao8c0f5062013-09-05 19:14:26 +00003568 // Allow the strings to have escaped octal character sequence.
3569 std::string Filename;
3570 if (parseEscapedString(Filename))
3571 return true;
Kevin Enderby109f25c2011-12-14 21:47:48 +00003572 SMLoc IncbinLoc = getLexer().getLoc();
3573 Lex();
3574
3575 if (getLexer().isNot(AsmToken::EndOfStatement))
3576 return TokError("unexpected token in '.incbin' directive");
3577
Kevin Enderby109f25c2011-12-14 21:47:48 +00003578 // Attempt to process the included file.
Jim Grosbach4b905842013-09-20 23:08:21 +00003579 if (processIncbinFile(Filename)) {
Kevin Enderby109f25c2011-12-14 21:47:48 +00003580 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
3581 return true;
3582 }
3583
3584 return false;
3585}
3586
Jim Grosbach4b905842013-09-20 23:08:21 +00003587/// parseDirectiveIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003588/// ::= .if expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003589bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003590 TheCondStack.push_back(TheCondState);
3591 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003592 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003593 eatToEndOfStatement();
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003594 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003595 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003596 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003597 return true;
3598
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003599 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003600 return TokError("unexpected token in '.if' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003601
Sean Callanan686ed8d2010-01-19 20:22:31 +00003602 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003603
3604 TheCondState.CondMet = ExprValue;
3605 TheCondState.Ignore = !TheCondState.CondMet;
3606 }
3607
3608 return false;
3609}
3610
Jim Grosbach4b905842013-09-20 23:08:21 +00003611/// parseDirectiveIfb
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003612/// ::= .ifb string
Jim Grosbach4b905842013-09-20 23:08:21 +00003613bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003614 TheCondStack.push_back(TheCondState);
3615 TheCondState.TheCond = AsmCond::IfCond;
3616
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003617 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003618 eatToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003619 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003620 StringRef Str = parseStringToEndOfStatement();
Benjamin Kramer62c18b02012-05-12 11:18:42 +00003621
3622 if (getLexer().isNot(AsmToken::EndOfStatement))
3623 return TokError("unexpected token in '.ifb' directive");
3624
3625 Lex();
3626
3627 TheCondState.CondMet = ExpectBlank == Str.empty();
3628 TheCondState.Ignore = !TheCondState.CondMet;
3629 }
3630
3631 return false;
3632}
3633
Jim Grosbach4b905842013-09-20 23:08:21 +00003634/// parseDirectiveIfc
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003635/// ::= .ifc string1, string2
Jim Grosbach4b905842013-09-20 23:08:21 +00003636bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003637 TheCondStack.push_back(TheCondState);
3638 TheCondState.TheCond = AsmCond::IfCond;
3639
Benjamin Kramerc7eda3e2012-05-12 16:52:21 +00003640 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003641 eatToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003642 } else {
Jim Grosbach4b905842013-09-20 23:08:21 +00003643 StringRef Str1 = parseStringToComma();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003644
3645 if (getLexer().isNot(AsmToken::Comma))
3646 return TokError("unexpected token in '.ifc' directive");
3647
3648 Lex();
3649
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003650 StringRef Str2 = parseStringToEndOfStatement();
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003651
3652 if (getLexer().isNot(AsmToken::EndOfStatement))
3653 return TokError("unexpected token in '.ifc' directive");
3654
3655 Lex();
3656
3657 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
3658 TheCondState.Ignore = !TheCondState.CondMet;
3659 }
3660
3661 return false;
3662}
3663
Jim Grosbach4b905842013-09-20 23:08:21 +00003664/// parseDirectiveIfdef
Benjamin Kramere297b9f2012-05-12 11:18:51 +00003665/// ::= .ifdef symbol
Jim Grosbach4b905842013-09-20 23:08:21 +00003666bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003667 StringRef Name;
3668 TheCondStack.push_back(TheCondState);
3669 TheCondState.TheCond = AsmCond::IfCond;
3670
3671 if (TheCondState.Ignore) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003672 eatToEndOfStatement();
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003673 } else {
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003674 if (parseIdentifier(Name))
Benjamin Kramer7b7caf52011-02-08 22:29:56 +00003675 return TokError("expected identifier after '.ifdef'");
3676
3677 Lex();
3678
3679 MCSymbol *Sym = getContext().LookupSymbol(Name);
3680
3681 if (expect_defined)
3682 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
3683 else
3684 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
3685 TheCondState.Ignore = !TheCondState.CondMet;
3686 }
3687
3688 return false;
3689}
3690
Jim Grosbach4b905842013-09-20 23:08:21 +00003691/// parseDirectiveElseIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003692/// ::= .elseif expression
Jim Grosbach4b905842013-09-20 23:08:21 +00003693bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003694 if (TheCondState.TheCond != AsmCond::IfCond &&
3695 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003696 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
3697 " an .elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003698 TheCondState.TheCond = AsmCond::ElseIfCond;
3699
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003700 bool LastIgnoreState = false;
3701 if (!TheCondStack.empty())
Craig Topper2172ad62013-04-22 04:24:02 +00003702 LastIgnoreState = TheCondStack.back().Ignore;
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003703 if (LastIgnoreState || TheCondState.CondMet) {
3704 TheCondState.Ignore = true;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003705 eatToEndOfStatement();
Craig Topperf15655b2013-04-22 04:22:40 +00003706 } else {
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003707 int64_t ExprValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003708 if (parseAbsoluteExpression(ExprValue))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003709 return true;
3710
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003711 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003712 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003713
Sean Callanan686ed8d2010-01-19 20:22:31 +00003714 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003715 TheCondState.CondMet = ExprValue;
3716 TheCondState.Ignore = !TheCondState.CondMet;
3717 }
3718
3719 return false;
3720}
3721
Jim Grosbach4b905842013-09-20 23:08:21 +00003722/// parseDirectiveElse
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003723/// ::= .else
Jim Grosbach4b905842013-09-20 23:08:21 +00003724bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003725 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003726 return TokError("unexpected token in '.else' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003727
Sean Callanan686ed8d2010-01-19 20:22:31 +00003728 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003729
3730 if (TheCondState.TheCond != AsmCond::IfCond &&
3731 TheCondState.TheCond != AsmCond::ElseIfCond)
Craig Topper2172ad62013-04-22 04:24:02 +00003732 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
3733 ".elseif");
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003734 TheCondState.TheCond = AsmCond::ElseCond;
3735 bool LastIgnoreState = false;
3736 if (!TheCondStack.empty())
3737 LastIgnoreState = TheCondStack.back().Ignore;
3738 if (LastIgnoreState || TheCondState.CondMet)
3739 TheCondState.Ignore = true;
3740 else
3741 TheCondState.Ignore = false;
3742
3743 return false;
3744}
3745
Jim Grosbach4b905842013-09-20 23:08:21 +00003746/// parseDirectiveEndIf
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003747/// ::= .endif
Jim Grosbach4b905842013-09-20 23:08:21 +00003748bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbardd41dcf2010-07-12 18:03:11 +00003749 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003750 return TokError("unexpected token in '.endif' directive");
Michael J. Spencer530ce852010-10-09 11:00:50 +00003751
Sean Callanan686ed8d2010-01-19 20:22:31 +00003752 Lex();
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003753
Jim Grosbach4b905842013-09-20 23:08:21 +00003754 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
Kevin Enderbyd9f95292009-08-07 22:46:00 +00003755 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
3756 ".else");
3757 if (!TheCondStack.empty()) {
3758 TheCondState = TheCondStack.back();
3759 TheCondStack.pop_back();
3760 }
3761
3762 return false;
3763}
Daniel Dunbara4b069c2009-08-11 04:24:50 +00003764
Eli Bendersky17233942013-01-15 22:59:42 +00003765void AsmParser::initializeDirectiveKindMap() {
3766 DirectiveKindMap[".set"] = DK_SET;
3767 DirectiveKindMap[".equ"] = DK_EQU;
3768 DirectiveKindMap[".equiv"] = DK_EQUIV;
3769 DirectiveKindMap[".ascii"] = DK_ASCII;
3770 DirectiveKindMap[".asciz"] = DK_ASCIZ;
3771 DirectiveKindMap[".string"] = DK_STRING;
3772 DirectiveKindMap[".byte"] = DK_BYTE;
3773 DirectiveKindMap[".short"] = DK_SHORT;
3774 DirectiveKindMap[".value"] = DK_VALUE;
3775 DirectiveKindMap[".2byte"] = DK_2BYTE;
3776 DirectiveKindMap[".long"] = DK_LONG;
3777 DirectiveKindMap[".int"] = DK_INT;
3778 DirectiveKindMap[".4byte"] = DK_4BYTE;
3779 DirectiveKindMap[".quad"] = DK_QUAD;
3780 DirectiveKindMap[".8byte"] = DK_8BYTE;
3781 DirectiveKindMap[".single"] = DK_SINGLE;
3782 DirectiveKindMap[".float"] = DK_FLOAT;
3783 DirectiveKindMap[".double"] = DK_DOUBLE;
3784 DirectiveKindMap[".align"] = DK_ALIGN;
3785 DirectiveKindMap[".align32"] = DK_ALIGN32;
3786 DirectiveKindMap[".balign"] = DK_BALIGN;
3787 DirectiveKindMap[".balignw"] = DK_BALIGNW;
3788 DirectiveKindMap[".balignl"] = DK_BALIGNL;
3789 DirectiveKindMap[".p2align"] = DK_P2ALIGN;
3790 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW;
3791 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL;
3792 DirectiveKindMap[".org"] = DK_ORG;
3793 DirectiveKindMap[".fill"] = DK_FILL;
3794 DirectiveKindMap[".zero"] = DK_ZERO;
3795 DirectiveKindMap[".extern"] = DK_EXTERN;
3796 DirectiveKindMap[".globl"] = DK_GLOBL;
3797 DirectiveKindMap[".global"] = DK_GLOBAL;
Eli Bendersky17233942013-01-15 22:59:42 +00003798 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE;
3799 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP;
3800 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
3801 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN;
3802 DirectiveKindMap[".reference"] = DK_REFERENCE;
3803 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION;
3804 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE;
3805 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
3806 DirectiveKindMap[".comm"] = DK_COMM;
3807 DirectiveKindMap[".common"] = DK_COMMON;
3808 DirectiveKindMap[".lcomm"] = DK_LCOMM;
3809 DirectiveKindMap[".abort"] = DK_ABORT;
3810 DirectiveKindMap[".include"] = DK_INCLUDE;
3811 DirectiveKindMap[".incbin"] = DK_INCBIN;
3812 DirectiveKindMap[".code16"] = DK_CODE16;
3813 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC;
3814 DirectiveKindMap[".rept"] = DK_REPT;
3815 DirectiveKindMap[".irp"] = DK_IRP;
3816 DirectiveKindMap[".irpc"] = DK_IRPC;
3817 DirectiveKindMap[".endr"] = DK_ENDR;
3818 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
3819 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK;
3820 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
3821 DirectiveKindMap[".if"] = DK_IF;
3822 DirectiveKindMap[".ifb"] = DK_IFB;
3823 DirectiveKindMap[".ifnb"] = DK_IFNB;
3824 DirectiveKindMap[".ifc"] = DK_IFC;
3825 DirectiveKindMap[".ifnc"] = DK_IFNC;
3826 DirectiveKindMap[".ifdef"] = DK_IFDEF;
3827 DirectiveKindMap[".ifndef"] = DK_IFNDEF;
3828 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF;
3829 DirectiveKindMap[".elseif"] = DK_ELSEIF;
3830 DirectiveKindMap[".else"] = DK_ELSE;
3831 DirectiveKindMap[".endif"] = DK_ENDIF;
3832 DirectiveKindMap[".skip"] = DK_SKIP;
3833 DirectiveKindMap[".space"] = DK_SPACE;
3834 DirectiveKindMap[".file"] = DK_FILE;
3835 DirectiveKindMap[".line"] = DK_LINE;
3836 DirectiveKindMap[".loc"] = DK_LOC;
3837 DirectiveKindMap[".stabs"] = DK_STABS;
3838 DirectiveKindMap[".sleb128"] = DK_SLEB128;
3839 DirectiveKindMap[".uleb128"] = DK_ULEB128;
3840 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
3841 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
3842 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
3843 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
3844 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
3845 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
3846 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
3847 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
3848 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
3849 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
3850 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
3851 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
3852 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
3853 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
3854 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
3855 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
3856 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
3857 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
3858 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00003859 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
Eli Bendersky17233942013-01-15 22:59:42 +00003860 DirectiveKindMap[".macros_on"] = DK_MACROS_ON;
3861 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF;
3862 DirectiveKindMap[".macro"] = DK_MACRO;
3863 DirectiveKindMap[".endm"] = DK_ENDM;
3864 DirectiveKindMap[".endmacro"] = DK_ENDMACRO;
3865 DirectiveKindMap[".purgem"] = DK_PURGEM;
Eli Benderskyec9e3cf2013-01-10 22:44:57 +00003866}
3867
Jim Grosbach4b905842013-09-20 23:08:21 +00003868MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003869 AsmToken EndToken, StartToken = getTok();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003870
Rafael Espindola34b9c512012-06-03 23:57:14 +00003871 unsigned NestLevel = 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003872 for (;;) {
3873 // Check whether we have reached the end of the file.
Rafael Espindola34b9c512012-06-03 23:57:14 +00003874 if (getLexer().is(AsmToken::Eof)) {
3875 Error(DirectiveLoc, "no matching '.endr' in definition");
3876 return 0;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003877 }
3878
Rafael Espindola34b9c512012-06-03 23:57:14 +00003879 if (Lexer.is(AsmToken::Identifier) &&
3880 (getTok().getIdentifier() == ".rept")) {
3881 ++NestLevel;
3882 }
3883
3884 // Otherwise, check whether we have reached the .endr.
Jim Grosbach4b905842013-09-20 23:08:21 +00003885 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003886 if (NestLevel == 0) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003887 EndToken = getTok();
3888 Lex();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003889 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3890 TokError("unexpected token in '.endr' directive");
3891 return 0;
3892 }
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003893 break;
3894 }
Rafael Espindola34b9c512012-06-03 23:57:14 +00003895 --NestLevel;
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003896 }
3897
Rafael Espindola34b9c512012-06-03 23:57:14 +00003898 // Otherwise, scan till the end of the statement.
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003899 eatToEndOfStatement();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003900 }
3901
3902 const char *BodyStart = StartToken.getLoc().getPointer();
3903 const char *BodyEnd = EndToken.getLoc().getPointer();
3904 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3905
Rafael Espindola34b9c512012-06-03 23:57:14 +00003906 // We Are Anonymous.
3907 StringRef Name;
Eli Bendersky38274122013-01-14 23:22:36 +00003908 MCAsmMacroParameters Parameters;
Benjamin Kramer1df3a1f2013-08-04 09:06:29 +00003909 MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters));
3910 return &MacroLikeBodies.back();
Rafael Espindola34b9c512012-06-03 23:57:14 +00003911}
3912
Jim Grosbach4b905842013-09-20 23:08:21 +00003913void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola34b9c512012-06-03 23:57:14 +00003914 raw_svector_ostream &OS) {
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003915 OS << ".endr\n";
3916
3917 MemoryBuffer *Instantiation =
Jim Grosbach4b905842013-09-20 23:08:21 +00003918 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003919
Rafael Espindola34b9c512012-06-03 23:57:14 +00003920 // Create the macro instantiation object and add to the current macro
3921 // instantiation stack.
Jim Grosbach4b905842013-09-20 23:08:21 +00003922 MacroInstantiation *MI = new MacroInstantiation(
3923 M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003924 ActiveMacros.push_back(MI);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003925
Rafael Espindola34b9c512012-06-03 23:57:14 +00003926 // Jump to the macro instantiation and prime the lexer.
3927 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3928 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3929 Lex();
3930}
3931
Jim Grosbach4b905842013-09-20 23:08:21 +00003932bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00003933 int64_t Count;
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003934 if (parseAbsoluteExpression(Count))
Rafael Espindola34b9c512012-06-03 23:57:14 +00003935 return TokError("unexpected token in '.rept' directive");
3936
3937 if (Count < 0)
3938 return TokError("Count is negative");
3939
3940 if (Lexer.isNot(AsmToken::EndOfStatement))
3941 return TokError("unexpected token in '.rept' directive");
3942
3943 // Eat the end of statement.
3944 Lex();
3945
3946 // Lex the rept definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003947 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola34b9c512012-06-03 23:57:14 +00003948 if (!M)
3949 return true;
3950
3951 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3952 // to hold the macro body with substitutions.
3953 SmallString<256> Buf;
Eli Bendersky38274122013-01-14 23:22:36 +00003954 MCAsmMacroParameters Parameters;
3955 MCAsmMacroArguments A;
Rafael Espindola34b9c512012-06-03 23:57:14 +00003956 raw_svector_ostream OS(Buf);
3957 while (Count--) {
3958 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3959 return true;
3960 }
Jim Grosbach4b905842013-09-20 23:08:21 +00003961 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola47b7dac2012-05-12 16:31:10 +00003962
3963 return false;
3964}
3965
Jim Grosbach4b905842013-09-20 23:08:21 +00003966/// parseDirectiveIrp
Rafael Espindola768b41c2012-06-15 14:02:34 +00003967/// ::= .irp symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00003968bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00003969 MCAsmMacroParameters Parameters;
3970 MCAsmMacroParameter Parameter;
Rafael Espindola768b41c2012-06-15 14:02:34 +00003971
Jim Grosbachd2037eb2013-02-20 22:21:35 +00003972 if (parseIdentifier(Parameter.first))
Rafael Espindola768b41c2012-06-15 14:02:34 +00003973 return TokError("expected identifier in '.irp' directive");
3974
3975 Parameters.push_back(Parameter);
3976
3977 if (Lexer.isNot(AsmToken::Comma))
3978 return TokError("expected comma in '.irp' directive");
3979
3980 Lex();
3981
Eli Bendersky38274122013-01-14 23:22:36 +00003982 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00003983 if (parseMacroArguments(0, A))
Rafael Espindola768b41c2012-06-15 14:02:34 +00003984 return true;
3985
3986 // Eat the end of statement.
3987 Lex();
3988
3989 // Lex the irp definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00003990 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindola768b41c2012-06-15 14:02:34 +00003991 if (!M)
3992 return true;
3993
3994 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3995 // to hold the macro body with substitutions.
3996 SmallString<256> Buf;
3997 raw_svector_ostream OS(Buf);
3998
Eli Bendersky38274122013-01-14 23:22:36 +00003999 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
4000 MCAsmMacroArguments Args;
Rafael Espindola768b41c2012-06-15 14:02:34 +00004001 Args.push_back(*i);
4002
4003 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4004 return true;
4005 }
4006
Jim Grosbach4b905842013-09-20 23:08:21 +00004007 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola768b41c2012-06-15 14:02:34 +00004008
4009 return false;
4010}
4011
Jim Grosbach4b905842013-09-20 23:08:21 +00004012/// parseDirectiveIrpc
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004013/// ::= .irpc symbol,values
Jim Grosbach4b905842013-09-20 23:08:21 +00004014bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Bendersky38274122013-01-14 23:22:36 +00004015 MCAsmMacroParameters Parameters;
4016 MCAsmMacroParameter Parameter;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004017
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004018 if (parseIdentifier(Parameter.first))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004019 return TokError("expected identifier in '.irpc' directive");
4020
4021 Parameters.push_back(Parameter);
4022
4023 if (Lexer.isNot(AsmToken::Comma))
4024 return TokError("expected comma in '.irpc' directive");
4025
4026 Lex();
4027
Eli Bendersky38274122013-01-14 23:22:36 +00004028 MCAsmMacroArguments A;
Jim Grosbach4b905842013-09-20 23:08:21 +00004029 if (parseMacroArguments(0, A))
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004030 return true;
4031
4032 if (A.size() != 1 || A.front().size() != 1)
4033 return TokError("unexpected token in '.irpc' directive");
4034
4035 // Eat the end of statement.
4036 Lex();
4037
4038 // Lex the irpc definition.
Jim Grosbach4b905842013-09-20 23:08:21 +00004039 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004040 if (!M)
4041 return true;
4042
4043 // Macro instantiation is lexical, unfortunately. We construct a new buffer
4044 // to hold the macro body with substitutions.
4045 SmallString<256> Buf;
4046 raw_svector_ostream OS(Buf);
4047
4048 StringRef Values = A.front().front().getString();
4049 std::size_t I, End = Values.size();
4050 for (I = 0; I < End; ++I) {
Eli Benderskya7b905e2013-01-14 19:00:26 +00004051 MCAsmMacroArgument Arg;
Jim Grosbach4b905842013-09-20 23:08:21 +00004052 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1)));
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004053
Eli Bendersky38274122013-01-14 23:22:36 +00004054 MCAsmMacroArguments Args;
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004055 Args.push_back(Arg);
4056
4057 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
4058 return true;
4059 }
4060
Jim Grosbach4b905842013-09-20 23:08:21 +00004061 instantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindolaf70bea92012-06-16 18:03:25 +00004062
4063 return false;
4064}
4065
Jim Grosbach4b905842013-09-20 23:08:21 +00004066bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) {
Rafael Espindola34b9c512012-06-03 23:57:14 +00004067 if (ActiveMacros.empty())
Preston Gurdeb3ebf12012-09-19 20:23:43 +00004068 return TokError("unmatched '.endr' directive");
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004069
4070 // The only .repl that should get here are the ones created by
Jim Grosbach4b905842013-09-20 23:08:21 +00004071 // instantiateMacroLikeBody.
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004072 assert(getLexer().is(AsmToken::EndOfStatement));
4073
Jim Grosbach4b905842013-09-20 23:08:21 +00004074 handleMacroExit();
Rafael Espindola47b7dac2012-05-12 16:31:10 +00004075 return false;
4076}
Rafael Espindola12d73d12010-09-11 16:45:15 +00004077
Jim Grosbach4b905842013-09-20 23:08:21 +00004078bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
Benjamin Kramer1a136112013-02-15 20:37:21 +00004079 size_t Len) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004080 const MCExpr *Value;
4081 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004082 if (parseExpression(Value))
Eli Friedman0f4871d2012-10-22 23:58:19 +00004083 return true;
4084 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4085 if (!MCE)
4086 return Error(ExprLoc, "unexpected expression in _emit");
4087 uint64_t IntValue = MCE->getValue();
4088 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
4089 return Error(ExprLoc, "literal value out of range for directive");
4090
Chad Rosierc7f552c2013-02-12 21:33:51 +00004091 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len));
4092 return false;
4093}
4094
Jim Grosbach4b905842013-09-20 23:08:21 +00004095bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
Chad Rosierc7f552c2013-02-12 21:33:51 +00004096 const MCExpr *Value;
4097 SMLoc ExprLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +00004098 if (parseExpression(Value))
Chad Rosierc7f552c2013-02-12 21:33:51 +00004099 return true;
4100 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
4101 if (!MCE)
4102 return Error(ExprLoc, "unexpected expression in align");
4103 uint64_t IntValue = MCE->getValue();
4104 if (!isPowerOf2_64(IntValue))
4105 return Error(ExprLoc, "literal value not a power of two greater then zero");
4106
Jim Grosbach4b905842013-09-20 23:08:21 +00004107 Info.AsmRewrites->push_back(
4108 AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue)));
Eli Friedman0f4871d2012-10-22 23:58:19 +00004109 return false;
4110}
4111
Chad Rosierf43fcf52013-02-13 21:27:17 +00004112// We are comparing pointers, but the pointers are relative to a single string.
4113// Thus, this should always be deterministic.
Benjamin Kramer8817cca2013-09-22 14:09:50 +00004114static int rewritesSort(const AsmRewrite *AsmRewriteA,
4115 const AsmRewrite *AsmRewriteB) {
Chad Rosiereb5c1682013-02-13 18:38:58 +00004116 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
4117 return -1;
4118 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
4119 return 1;
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004120
Chad Rosierfce4fab2013-04-08 17:43:47 +00004121 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
4122 // rewrite to the same location. Make sure the SizeDirective rewrite is
4123 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
4124 // ensures the sort algorithm is stable.
Jim Grosbach4b905842013-09-20 23:08:21 +00004125 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
4126 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004127 return -1;
Chad Rosierfce4fab2013-04-08 17:43:47 +00004128
Jim Grosbach4b905842013-09-20 23:08:21 +00004129 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
4130 AsmRewritePrecedence[AsmRewriteB->Kind])
Chad Rosier42d4e2e2013-02-15 22:54:16 +00004131 return 1;
Jim Grosbach4b905842013-09-20 23:08:21 +00004132 llvm_unreachable("Unstable rewrite sort.");
Chad Rosierb2144ce2013-02-13 01:03:13 +00004133}
4134
Jim Grosbach4b905842013-09-20 23:08:21 +00004135bool AsmParser::parseMSInlineAsm(
4136 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs,
4137 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
4138 SmallVectorImpl<std::string> &Constraints,
4139 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
4140 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
Chad Rosier37e755c2012-10-23 17:43:43 +00004141 SmallVector<void *, 4> InputDecls;
4142 SmallVector<void *, 4> OutputDecls;
Chad Rosiera4bc9432013-01-10 22:10:27 +00004143 SmallVector<bool, 4> InputDeclsAddressOf;
4144 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosier8bce6642012-10-18 15:49:34 +00004145 SmallVector<std::string, 4> InputConstraints;
4146 SmallVector<std::string, 4> OutputConstraints;
Benjamin Kramer1a136112013-02-15 20:37:21 +00004147 SmallVector<unsigned, 4> ClobberRegs;
Chad Rosier8bce6642012-10-18 15:49:34 +00004148
Benjamin Kramer1a136112013-02-15 20:37:21 +00004149 SmallVector<AsmRewrite, 4> AsmStrRewrites;
Chad Rosier8bce6642012-10-18 15:49:34 +00004150
4151 // Prime the lexer.
4152 Lex();
4153
4154 // While we have input, parse each statement.
4155 unsigned InputIdx = 0;
4156 unsigned OutputIdx = 0;
4157 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman0f4871d2012-10-22 23:58:19 +00004158 ParseStatementInfo Info(&AsmStrRewrites);
Jim Grosbach4b905842013-09-20 23:08:21 +00004159 if (parseStatement(Info))
Chad Rosierf1f6a722012-10-19 22:57:33 +00004160 return true;
Chad Rosier8bce6642012-10-18 15:49:34 +00004161
Chad Rosier149e8e02012-12-12 22:45:52 +00004162 if (Info.ParseError)
4163 return true;
4164
Benjamin Kramer1a136112013-02-15 20:37:21 +00004165 if (Info.Opcode == ~0U)
4166 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004167
Benjamin Kramer1a136112013-02-15 20:37:21 +00004168 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosier8bce6642012-10-18 15:49:34 +00004169
Benjamin Kramer1a136112013-02-15 20:37:21 +00004170 // Build the list of clobbers, outputs and inputs.
4171 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
4172 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004173
Benjamin Kramer1a136112013-02-15 20:37:21 +00004174 // Immediate.
Chad Rosierf3c04f62013-03-19 21:58:18 +00004175 if (Operand->isImm())
Benjamin Kramer1a136112013-02-15 20:37:21 +00004176 continue;
Chad Rosier8bce6642012-10-18 15:49:34 +00004177
Benjamin Kramer1a136112013-02-15 20:37:21 +00004178 // Register operand.
4179 if (Operand->isReg() && !Operand->needAddressOf()) {
4180 unsigned NumDefs = Desc.getNumDefs();
4181 // Clobber.
4182 if (NumDefs && Operand->getMCOperandNum() < NumDefs)
4183 ClobberRegs.push_back(Operand->getReg());
4184 continue;
4185 }
4186
4187 // Expr/Input or Output.
Chad Rosiere81309b2013-04-09 17:53:49 +00004188 StringRef SymName = Operand->getSymName();
4189 if (SymName.empty())
4190 continue;
4191
Chad Rosierdba3fe52013-04-22 22:12:12 +00004192 void *OpDecl = Operand->getOpDecl();
Benjamin Kramer1a136112013-02-15 20:37:21 +00004193 if (!OpDecl)
4194 continue;
4195
4196 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosiere81309b2013-04-09 17:53:49 +00004197 SMLoc Start = SMLoc::getFromPointer(SymName.data());
Benjamin Kramer1a136112013-02-15 20:37:21 +00004198 if (isOutput) {
4199 ++InputIdx;
4200 OutputDecls.push_back(OpDecl);
4201 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
4202 OutputConstraints.push_back('=' + Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004203 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size()));
Benjamin Kramer1a136112013-02-15 20:37:21 +00004204 } else {
4205 InputDecls.push_back(OpDecl);
4206 InputDeclsAddressOf.push_back(Operand->needAddressOf());
4207 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosiere81309b2013-04-09 17:53:49 +00004208 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size()));
Chad Rosier8bce6642012-10-18 15:49:34 +00004209 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004210 }
4211 }
4212
4213 // Set the number of Outputs and Inputs.
Chad Rosierf641baa2012-10-18 19:39:30 +00004214 NumOutputs = OutputDecls.size();
4215 NumInputs = InputDecls.size();
Chad Rosier8bce6642012-10-18 15:49:34 +00004216
4217 // Set the unique clobbers.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004218 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
4219 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()),
4220 ClobberRegs.end());
4221 Clobbers.assign(ClobberRegs.size(), std::string());
4222 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
4223 raw_string_ostream OS(Clobbers[I]);
4224 IP->printRegName(OS, ClobberRegs[I]);
4225 }
Chad Rosier8bce6642012-10-18 15:49:34 +00004226
4227 // Merge the various outputs and inputs. Output are expected first.
4228 if (NumOutputs || NumInputs) {
4229 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierf641baa2012-10-18 19:39:30 +00004230 OpDecls.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004231 Constraints.resize(NumExprs);
Chad Rosier8bce6642012-10-18 15:49:34 +00004232 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004233 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004234 Constraints[i] = OutputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004235 }
4236 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosiera4bc9432013-01-10 22:10:27 +00004237 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
Chad Rosier72450332013-01-15 23:07:53 +00004238 Constraints[j] = InputConstraints[i];
Chad Rosier8bce6642012-10-18 15:49:34 +00004239 }
4240 }
4241
4242 // Build the IR assembly string.
4243 std::string AsmStringIR;
4244 raw_string_ostream OS(AsmStringIR);
Chad Rosier17d37992013-03-19 21:12:14 +00004245 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart();
4246 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
Jim Grosbach4b905842013-09-20 23:08:21 +00004247 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
Benjamin Kramer1a136112013-02-15 20:37:21 +00004248 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(),
4249 E = AsmStrRewrites.end();
4250 I != E; ++I) {
Chad Rosierff10ed12013-04-12 16:26:42 +00004251 AsmRewriteKind Kind = (*I).Kind;
4252 if (Kind == AOK_Delete)
4253 continue;
4254
Chad Rosier8bce6642012-10-18 15:49:34 +00004255 const char *Loc = (*I).Loc.getPointer();
Chad Rosier17d37992013-03-19 21:12:14 +00004256 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
Chad Rosier0f48c552012-10-19 20:57:14 +00004257
Chad Rosier120eefd2013-03-19 17:32:17 +00004258 // Emit everything up to the immediate/expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004259 unsigned Len = Loc - AsmStart;
Chad Rosier8fb83302013-04-11 21:49:30 +00004260 if (Len)
Chad Rosier17d37992013-03-19 21:12:14 +00004261 OS << StringRef(AsmStart, Len);
Chad Rosier0f48c552012-10-19 20:57:14 +00004262
Chad Rosier37e755c2012-10-23 17:43:43 +00004263 // Skip the original expression.
4264 if (Kind == AOK_Skip) {
Chad Rosier17d37992013-03-19 21:12:14 +00004265 AsmStart = Loc + (*I).Len;
Chad Rosier37e755c2012-10-23 17:43:43 +00004266 continue;
4267 }
4268
Chad Rosierff10ed12013-04-12 16:26:42 +00004269 unsigned AdditionalSkip = 0;
Chad Rosier8bce6642012-10-18 15:49:34 +00004270 // Rewrite expressions in $N notation.
Chad Rosier0f48c552012-10-19 20:57:14 +00004271 switch (Kind) {
Jim Grosbach4b905842013-09-20 23:08:21 +00004272 default:
4273 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004274 case AOK_Imm:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004275 OS << "$$" << (*I).Val;
Chad Rosier11c42f22012-10-26 18:04:20 +00004276 break;
4277 case AOK_ImmPrefix:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004278 OS << "$$";
Chad Rosier8bce6642012-10-18 15:49:34 +00004279 break;
4280 case AOK_Input:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004281 OS << '$' << InputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004282 break;
4283 case AOK_Output:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004284 OS << '$' << OutputIdx++;
Chad Rosier8bce6642012-10-18 15:49:34 +00004285 break;
Chad Rosier0f48c552012-10-19 20:57:14 +00004286 case AOK_SizeDirective:
Benjamin Kramer1a136112013-02-15 20:37:21 +00004287 switch ((*I).Val) {
Chad Rosier0f48c552012-10-19 20:57:14 +00004288 default: break;
4289 case 8: OS << "byte ptr "; break;
4290 case 16: OS << "word ptr "; break;
4291 case 32: OS << "dword ptr "; break;
4292 case 64: OS << "qword ptr "; break;
4293 case 80: OS << "xword ptr "; break;
4294 case 128: OS << "xmmword ptr "; break;
4295 case 256: OS << "ymmword ptr "; break;
4296 }
Eli Friedman0f4871d2012-10-22 23:58:19 +00004297 break;
4298 case AOK_Emit:
4299 OS << ".byte";
4300 break;
Chad Rosierc7f552c2013-02-12 21:33:51 +00004301 case AOK_Align: {
4302 unsigned Val = (*I).Val;
4303 OS << ".align " << Val;
4304
4305 // Skip the original immediate.
Benjamin Kramer1a136112013-02-15 20:37:21 +00004306 assert(Val < 10 && "Expected alignment less then 2^10.");
Chad Rosierc7f552c2013-02-12 21:33:51 +00004307 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
4308 break;
4309 }
Chad Rosierf0e87202012-10-25 20:41:34 +00004310 case AOK_DotOperator:
4311 OS << (*I).Val;
4312 break;
Chad Rosier8bce6642012-10-18 15:49:34 +00004313 }
Chad Rosier0f48c552012-10-19 20:57:14 +00004314
Chad Rosier8bce6642012-10-18 15:49:34 +00004315 // Skip the original expression.
Chad Rosier17d37992013-03-19 21:12:14 +00004316 AsmStart = Loc + (*I).Len + AdditionalSkip;
Chad Rosier8bce6642012-10-18 15:49:34 +00004317 }
4318
4319 // Emit the remainder of the asm string.
Chad Rosier17d37992013-03-19 21:12:14 +00004320 if (AsmStart != AsmEnd)
4321 OS << StringRef(AsmStart, AsmEnd - AsmStart);
Chad Rosier8bce6642012-10-18 15:49:34 +00004322
4323 AsmString = OS.str();
4324 return false;
4325}
4326
Daniel Dunbar01e36072010-07-17 02:26:10 +00004327/// \brief Create an MCAsmParser instance.
Jim Grosbach4b905842013-09-20 23:08:21 +00004328MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C,
4329 MCStreamer &Out, const MCAsmInfo &MAI) {
Jim Grosbach345768c2011-08-16 18:33:49 +00004330 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbar01e36072010-07-17 02:26:10 +00004331}