blob: 45aaa2ec754037038f4054079730b05405045f35 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000020#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Chad Rosierb1f8c132012-10-18 15:49:34 +000022#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000024#include "llvm/MC/MCParser/AsmCond.h"
25#include "llvm/MC/MCParser/AsmLexer.h"
26#include "llvm/MC/MCParser/MCAsmParser.h"
27#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000028#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000029#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000030#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000032#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000033#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000034#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000035#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000036#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000037#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000038#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000039#include <cctype>
Chad Rosierb1f8c132012-10-18 15:49:34 +000040#include <set>
41#include <string>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000042#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000043using namespace llvm;
44
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000045static cl::opt<bool>
46FatalAssemblerWarnings("fatal-assembler-warnings",
47 cl::desc("Consider warnings as error"));
48
Eric Christopher2318ba12012-12-18 00:30:54 +000049MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewycky0d7d11d2012-10-19 07:00:09 +000050
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000051namespace {
52
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053/// \brief Helper class for tracking macro definitions.
Eli Bendersky9bac6b22013-01-14 19:00:26 +000054typedef std::vector<MCAsmMacroArgument> MacroArguments;
55typedef std::pair<StringRef, MCAsmMacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000056typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000057
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000058struct Macro {
59 StringRef Name;
60 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000061 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000062
63public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000064 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000065 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000066};
67
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000068/// \brief Helper class for storing information about an active macro
69/// instantiation.
70struct MacroInstantiation {
71 /// The macro being instantiated.
72 const Macro *TheMacro;
73
74 /// The macro instantiation with substitutions.
75 MemoryBuffer *Instantiation;
76
77 /// The location of the instantiation.
78 SMLoc InstantiationLoc;
79
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000080 /// The buffer where parsing should resume upon instantiation completion.
81 int ExitBuffer;
82
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000083 /// The location where parsing should resume upon instantiation completion.
84 SMLoc ExitLoc;
85
86public:
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000087 MacroInstantiation(const Macro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000088 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000089};
90
Chad Rosier6a020a72012-10-25 20:41:34 +000091//struct AsmRewrite;
Eli Friedman2128aae2012-10-22 23:58:19 +000092struct ParseStatementInfo {
93 /// ParsedOperands - The parsed operands from the last parsed statement.
94 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
95
96 /// Opcode - The opcode from the last parsed instruction.
97 unsigned Opcode;
98
Chad Rosier57498012012-12-12 22:45:52 +000099 /// Error - Was there an error parsing the inline assembly?
100 bool ParseError;
101
Eli Friedman2128aae2012-10-22 23:58:19 +0000102 SmallVectorImpl<AsmRewrite> *AsmRewrites;
103
Chad Rosier57498012012-12-12 22:45:52 +0000104 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000105 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier57498012012-12-12 22:45:52 +0000106 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000107
108 ~ParseStatementInfo() {
109 // Free any parsed operands.
110 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
111 delete ParsedOperands[i];
112 ParsedOperands.clear();
113 }
114};
115
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000116/// \brief The concrete assembly parser instance.
117class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000118 friend class GenericAsmParser;
119
Craig Topper85aadc02012-09-15 16:23:52 +0000120 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
121 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000122private:
123 AsmLexer Lexer;
124 MCContext &Ctx;
125 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000126 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000127 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000128 SourceMgr::DiagHandlerTy SavedDiagHandler;
129 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000130 MCAsmParserExtension *GenericParser;
131 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000132
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000133 /// This is the current buffer index we're lexing from as managed by the
134 /// SourceMgr object.
135 int CurBuffer;
136
137 AsmCond TheCondState;
138 std::vector<AsmCond> TheCondStack;
139
140 /// DirectiveMap - This is a table handlers for directives. Each handler is
141 /// invoked after the directive identifier is read and is responsible for
142 /// parsing and validating the rest of the directive. The handler is passed
143 /// in the directive name and the location of the directive keyword.
144 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000145
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000146 /// MacroMap - Map of currently defined macros.
147 StringMap<Macro*> MacroMap;
148
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000149 /// ActiveMacros - Stack of active macro instantiations.
150 std::vector<MacroInstantiation*> ActiveMacros;
151
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000152 /// Boolean tracking whether macro substitution is enabled.
Eli Bendersky733c3362013-01-14 18:08:41 +0000153 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000154
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000155 /// Flag tracking whether any errors have been encountered.
156 unsigned HadError : 1;
157
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000158 /// The values from the last parsed cpp hash file line comment if any.
159 StringRef CppHashFilename;
160 int64_t CppHashLineNumber;
161 SMLoc CppHashLoc;
Kevin Enderby32c1a822012-11-05 21:55:41 +0000162 int CppHashBuf;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000163
Devang Patel0db58bf2012-01-31 18:14:05 +0000164 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
165 unsigned AssemblerDialect;
166
Preston Gurd7b6f2032012-09-19 20:36:12 +0000167 /// IsDarwin - is Darwin compatibility enabled?
168 bool IsDarwin;
169
Chad Rosier8f138d12012-10-15 17:19:13 +0000170 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000171 bool ParsingInlineAsm;
172
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000173public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000174 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000175 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000176 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177
178 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
179
Craig Topper345d16d2012-08-29 05:48:09 +0000180 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
181 StringRef Directive,
182 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183 DirectiveMap[Directive] = std::make_pair(Object, Handler);
184 }
185
186public:
187 /// @name MCAsmParser Interface
188 /// {
189
190 virtual SourceMgr &getSourceManager() { return SrcMgr; }
191 virtual MCAsmLexer &getLexer() { return Lexer; }
192 virtual MCContext &getContext() { return Ctx; }
193 virtual MCStreamer &getStreamer() { return Out; }
Eric Christopher2318ba12012-12-18 00:30:54 +0000194 virtual unsigned getAssemblerDialect() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000195 if (AssemblerDialect == ~0U)
Eric Christopher2318ba12012-12-18 00:30:54 +0000196 return MAI.getAssemblerDialect();
Devang Patel0db58bf2012-01-31 18:14:05 +0000197 else
198 return AssemblerDialect;
199 }
200 virtual void setAssemblerDialect(unsigned i) {
201 AssemblerDialect = i;
202 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000203
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000204 virtual bool Warning(SMLoc L, const Twine &Msg,
205 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
206 virtual bool Error(SMLoc L, const Twine &Msg,
207 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208
Craig Topper345d16d2012-08-29 05:48:09 +0000209 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210
Chad Rosier84125ca2012-10-13 00:26:04 +0000211 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000212 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000213
214 bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
215 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000216 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000217 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000218 SmallVectorImpl<std::string> &Clobbers,
219 const MCInstrInfo *MII,
220 const MCInstPrinter *IP,
221 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000222
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000223 bool ParseExpression(const MCExpr *&Res);
224 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
225 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
226 virtual bool ParseAbsoluteExpression(int64_t &Res);
227
Eli Benderskybf706b32013-01-12 00:05:00 +0000228 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
229 /// and set \p Res to the identifier contents.
230 virtual bool ParseIdentifier(StringRef &Res);
Eli Benderskyb2f0b592013-01-12 00:23:24 +0000231 virtual void EatToEndOfStatement();
Eli Benderskybf706b32013-01-12 00:05:00 +0000232
Eli Bendersky733c3362013-01-14 18:08:41 +0000233 virtual bool MacrosEnabled() {return MacrosEnabledFlag;}
234 virtual void SetMacrosEnabled(bool flag) {MacrosEnabledFlag = flag;}
235
Eli Bendersky318cad32013-01-14 19:15:01 +0000236 virtual void CheckForValidSection();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000237 /// }
238
239private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000240
Eli Friedman2128aae2012-10-22 23:58:19 +0000241 bool ParseStatement(ParseStatementInfo &Info);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000242 void EatToEndOfLine();
243 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000245 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000246 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000247 const MacroParameters &Parameters,
248 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000249 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000250 void HandleMacroExit();
251
252 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000253 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000254 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
255 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000256 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000257 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000258
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000259 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
260 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000261 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
262 /// This returns true on failure.
263 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000264
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000265 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000266 /// current token is not set; clients should ensure Lex() is called
267 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000268 ///
269 /// \param InBuffer If not -1, should be the known buffer id that contains the
270 /// location.
271 void JumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000272
Eli Bendersky9bac6b22013-01-14 19:00:26 +0000273 bool ParseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +0000274 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000275 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000276
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000277 /// \brief Parse up to the end of statement and a return the contents from the
278 /// current token until the end of the statement; the current token on exit
279 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000280 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000281
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000282 /// \brief Parse until the end of a statement or a comma is encountered,
283 /// return the contents from the current token up to the end or comma.
284 StringRef ParseStringToComma();
285
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000286 bool ParseAssignment(StringRef Name, bool allow_redef,
287 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000288
289 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
290 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
291 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000292 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000293
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000294 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000295
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000296 enum DirectiveKind {
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000297 DK_NO_DIRECTIVE, // Placeholder
298 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
299 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
300 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000301 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000302 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
303 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL,
304 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
305 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
306 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
307 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
308 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
309 DK_ELSEIF, DK_ELSE, DK_ENDIF
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000310 };
311
312 StringMap<DirectiveKind> DirectiveKindMapping;
313
314 // ".ascii", ".asciz", ".string"
Rafael Espindola787c3372010-10-28 20:02:27 +0000315 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000316 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000317 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000318 bool ParseDirectiveFill(); // ".fill"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000319 bool ParseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000320 // ".set", ".equ", ".equiv"
321 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000322 bool ParseDirectiveOrg(); // ".org"
323 // ".align{,32}", ".p2align{,w,l}"
324 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
325
Eli Bendersky4766ef42012-12-20 19:05:53 +0000326 // ".bundle_align_mode"
327 bool ParseDirectiveBundleAlignMode();
328 // ".bundle_lock"
329 bool ParseDirectiveBundleLock();
330 // ".bundle_unlock"
331 bool ParseDirectiveBundleUnlock();
332
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000333 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
334 /// accepts a single symbol (which should be a label or an external).
335 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000336
337 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
338
339 bool ParseDirectiveAbort(); // ".abort"
340 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000341 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000342
343 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000344 // ".ifb" or ".ifnb", depending on ExpectBlank.
345 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000346 // ".ifc" or ".ifnc", depending on ExpectEqual.
347 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000348 // ".ifdef" or ".ifndef", depending on expect_defined
349 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000350 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
351 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
352 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
353
354 /// ParseEscapedString - Parse the current token as a string which may include
355 /// escaped characters and return the string contents.
356 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000357
358 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
359 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000360
Rafael Espindola761cb062012-06-03 23:57:14 +0000361 // Macro-like directives
362 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
363 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
364 raw_svector_ostream &OS);
365 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000366 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000367 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000368 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000369
Eli Friedman2128aae2012-10-22 23:58:19 +0000370 // "_emit"
371 bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000372
373 void initializeDirectiveKindMapping();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000374};
375
Eli Bendersky63e6f482013-01-10 23:32:57 +0000376/// \brief Generic implementation of directive handling, etc. which is shared
377/// (or the default, at least) for all assembler parsers.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000378class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000379 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
380 void AddDirectiveHandler(StringRef Directive) {
381 getParser().AddDirectiveHandler(this, Directive,
382 HandleDirective<GenericAsmParser, Handler>);
383 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000384public:
385 GenericAsmParser() {}
386
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000387 AsmParser &getParser() {
388 return (AsmParser&) this->MCAsmParserExtension::getParser();
389 }
390
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000391 virtual void Initialize(MCAsmParser &Parser) {
392 // Call the base implementation.
393 this->MCAsmParserExtension::Initialize(Parser);
394
395 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000396 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
397 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
398 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000399 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000400
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000401 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".space");
402 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".skip");
403
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000404 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000405 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
406 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000407 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
408 ".cfi_startproc");
409 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
410 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000411 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
412 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000413 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
414 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000415 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
416 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000417 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
418 ".cfi_def_cfa_register");
419 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
420 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000421 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
422 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000423 AddDirectiveHandler<
424 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
425 AddDirectiveHandler<
426 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000427 AddDirectiveHandler<
428 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
429 AddDirectiveHandler<
430 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000431 AddDirectiveHandler<
432 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000433 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000434 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
435 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000436 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000437 AddDirectiveHandler<
438 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000439 AddDirectiveHandler<
440 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000441 AddDirectiveHandler<
442 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000443
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000444 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000445 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
446 ".macros_on");
447 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
448 ".macros_off");
449 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
450 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
451 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000452 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000453
454 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
455 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000456 }
457
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000458 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
459
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000460 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
461 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
462 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000463 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000464 bool ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000465 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000466 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
467 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000468 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000469 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000470 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000471 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
472 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000473 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000474 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000475 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
476 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000477 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000478 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000479 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000480 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000481 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000482 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000483
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000484 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000485 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
486 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000487 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000488
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000489 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000490};
491
492}
493
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000494namespace llvm {
495
496extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000497extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000498extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000499
500}
501
Chris Lattneraaec2052010-01-19 19:46:13 +0000502enum { DEFAULT_ADDRSPACE = 0 };
503
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000504AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000505 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000506 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000507 GenericParser(new GenericAsmParser), PlatformParser(0),
Eli Bendersky733c3362013-01-14 18:08:41 +0000508 CurBuffer(0), MacrosEnabledFlag(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000509 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000510 // Save the old handler.
511 SavedDiagHandler = SrcMgr.getDiagHandler();
512 SavedDiagContext = SrcMgr.getDiagContext();
513 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000514 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000515 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000516
517 // Initialize the generic parser.
518 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000519
520 // Initialize the platform / file format parser.
521 //
522 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
523 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000524 if (_MAI.hasMicrosoftFastStdCallMangling()) {
525 PlatformParser = createCOFFAsmParser();
526 PlatformParser->Initialize(*this);
527 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000528 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000529 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000530 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000531 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000532 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000533 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000534 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000535
536 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000537}
538
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000539AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000540 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
541
542 // Destroy any macros.
543 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
544 ie = MacroMap.end(); it != ie; ++it)
545 delete it->getValue();
546
Daniel Dunbare4749702010-07-12 18:12:02 +0000547 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000548 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000549}
550
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000551void AsmParser::PrintMacroInstantiations() {
552 // Print the active macro instantiation stack.
553 for (std::vector<MacroInstantiation*>::const_reverse_iterator
554 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000555 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
556 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000557}
558
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000559bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000560 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000561 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000562 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000563 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000564 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000565}
566
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000567bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000568 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000569 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000570 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000571 return true;
572}
573
Sean Callananfd0b0282010-01-21 00:19:58 +0000574bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000575 std::string IncludedFile;
576 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000577 if (NewBuf == -1)
578 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000579
Sean Callananfd0b0282010-01-21 00:19:58 +0000580 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000581
Sean Callananfd0b0282010-01-21 00:19:58 +0000582 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000583
Sean Callananfd0b0282010-01-21 00:19:58 +0000584 return false;
585}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000586
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000587/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000588/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000589/// returns true on failure.
590bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
591 std::string IncludedFile;
592 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
593 if (NewBuf == -1)
594 return true;
595
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000596 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000597 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
598 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000599 return false;
600}
601
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000602void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
603 if (InBuffer != -1) {
604 CurBuffer = InBuffer;
605 } else {
606 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
607 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000608 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
609}
610
Sean Callananfd0b0282010-01-21 00:19:58 +0000611const AsmToken &AsmParser::Lex() {
612 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000613
Sean Callananfd0b0282010-01-21 00:19:58 +0000614 if (tok->is(AsmToken::Eof)) {
615 // If this is the end of an included file, pop the parent file off the
616 // include stack.
617 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
618 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000619 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000620 tok = &Lexer.Lex();
621 }
622 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000623
Sean Callananfd0b0282010-01-21 00:19:58 +0000624 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000625 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000626
Sean Callananfd0b0282010-01-21 00:19:58 +0000627 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000628}
629
Chris Lattner79180e22010-04-05 23:15:42 +0000630bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000631 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000632 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000633 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000634
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000635 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000636 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000637
638 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000639 AsmCond StartingCondState = TheCondState;
640
Kevin Enderby613b7572011-11-01 22:27:22 +0000641 // If we are generating dwarf for assembly source files save the initial text
642 // section and generate a .file directive.
643 if (getContext().getGenDwarfForAssembly()) {
644 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000645 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
646 getStreamer().EmitLabel(SectionStartSym);
647 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000648 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000649 StringRef(),
650 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000651 }
652
Chris Lattnerb717fb02009-07-02 21:53:43 +0000653 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000654 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000655 ParseStatementInfo Info;
656 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000657
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000658 // We had an error, validate that one was emitted and recover by skipping to
659 // the next line.
660 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000661 EatToEndOfStatement();
662 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000663
664 if (TheCondState.TheCond != StartingCondState.TheCond ||
665 TheCondState.Ignore != StartingCondState.Ignore)
666 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000667
668 // Check to see there are no empty DwarfFile slots.
669 const std::vector<MCDwarfFile *> &MCDwarfFiles =
670 getContext().getMCDwarfFiles();
671 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000672 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000673 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000674 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000675
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000676 // Check to see that all assembler local symbols were actually defined.
677 // Targets that don't do subsections via symbols may not want this, though,
678 // so conservatively exclude them. Only do this if we're finalizing, though,
679 // as otherwise we won't necessarilly have seen everything yet.
680 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
681 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
682 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
683 e = Symbols.end();
684 i != e; ++i) {
685 MCSymbol *Sym = i->getValue();
686 // Variable symbols may not be marked as defined, so check those
687 // explicitly. If we know it's a variable, we have a definition for
688 // the purposes of this check.
689 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
690 // FIXME: We would really like to refer back to where the symbol was
691 // first referenced for a source location. We need to add something
692 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000693 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
694 "assembler local symbol '" + Sym->getName() +
695 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000696 }
697 }
698
699
Chris Lattner79180e22010-04-05 23:15:42 +0000700 // Finalize the output stream if there are no errors and if the client wants
701 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000702 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000703 Out.Finish();
704
Chris Lattnerb717fb02009-07-02 21:53:43 +0000705 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000706}
707
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000708void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000709 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000710 TokError("expected section directive before assembly directive");
Eli Bendersky030f63a2013-01-14 19:04:57 +0000711 Out.InitToTextSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000712 }
713}
714
Chris Lattner2cf5f142009-06-22 01:29:09 +0000715/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
716void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000717 while (Lexer.isNot(AsmToken::EndOfStatement) &&
718 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000719 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000720
Chris Lattner2cf5f142009-06-22 01:29:09 +0000721 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000722 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000723 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000724}
725
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000726StringRef AsmParser::ParseStringToEndOfStatement() {
727 const char *Start = getTok().getLoc().getPointer();
728
729 while (Lexer.isNot(AsmToken::EndOfStatement) &&
730 Lexer.isNot(AsmToken::Eof))
731 Lex();
732
733 const char *End = getTok().getLoc().getPointer();
734 return StringRef(Start, End - Start);
735}
Chris Lattnerc4193832009-06-22 05:51:26 +0000736
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000737StringRef AsmParser::ParseStringToComma() {
738 const char *Start = getTok().getLoc().getPointer();
739
740 while (Lexer.isNot(AsmToken::EndOfStatement) &&
741 Lexer.isNot(AsmToken::Comma) &&
742 Lexer.isNot(AsmToken::Eof))
743 Lex();
744
745 const char *End = getTok().getLoc().getPointer();
746 return StringRef(Start, End - Start);
747}
748
Chris Lattner74ec1a32009-06-22 06:32:03 +0000749/// ParseParenExpr - Parse a paren expression and return it.
750/// NOTE: This assumes the leading '(' has already been consumed.
751///
752/// parenexpr ::= expr)
753///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000754bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000755 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000756 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000757 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000758 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000759 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000760 return false;
761}
Chris Lattnerc4193832009-06-22 05:51:26 +0000762
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000763/// ParseBracketExpr - Parse a bracket expression and return it.
764/// NOTE: This assumes the leading '[' has already been consumed.
765///
766/// bracketexpr ::= expr]
767///
768bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
769 if (ParseExpression(Res)) return true;
770 if (Lexer.isNot(AsmToken::RBrac))
771 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000772 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000773 Lex();
774 return false;
775}
776
Chris Lattner74ec1a32009-06-22 06:32:03 +0000777/// ParsePrimaryExpr - Parse a primary expression and return it.
778/// primaryexpr ::= (parenexpr
779/// primaryexpr ::= symbol
780/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000781/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000782/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000783bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000784 switch (Lexer.getKind()) {
785 default:
786 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000787 // If we have an error assume that we've already handled it.
788 case AsmToken::Error:
789 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000791 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000792 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000793 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000794 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000795 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000796 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000797 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000798 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000799 StringRef Identifier;
800 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000801 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000802
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000803 EndLoc = SMLoc::getFromPointer(Identifier.end());
804
Daniel Dunbarfffff912009-10-16 01:34:54 +0000805 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000806 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000807 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000808
809 // Lookup the symbol variant if used.
810 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000811 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000812 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000813 if (Variant == MCSymbolRefExpr::VK_Invalid) {
814 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000815 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000816 }
817 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000818
Daniel Dunbarfffff912009-10-16 01:34:54 +0000819 // If this is an absolute variable reference, substitute it now to preserve
820 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000821 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000822 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000823 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000824
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000825 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000826 return false;
827 }
828
829 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000830 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000831 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000832 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000833 case AsmToken::Integer: {
834 SMLoc Loc = getTok().getLoc();
835 int64_t IntVal = getTok().getIntVal();
836 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000837 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000838 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000839 // Look for 'b' or 'f' following an Integer as a directional label
840 if (Lexer.getKind() == AsmToken::Identifier) {
841 StringRef IDVal = getTok().getString();
842 if (IDVal == "f" || IDVal == "b"){
843 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
844 IDVal == "f" ? 1 : 0);
845 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
846 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000847 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000848 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000849 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000850 Lex(); // Eat identifier.
851 }
852 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000853 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000854 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000855 case AsmToken::Real: {
856 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000857 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000858 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000859 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000860 Lex(); // Eat token.
861 return false;
862 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000863 case AsmToken::Dot: {
864 // This is a '.' reference, which references the current PC. Emit a
865 // temporary label to the streamer and refer to it.
866 MCSymbol *Sym = Ctx.CreateTempSymbol();
867 Out.EmitLabel(Sym);
868 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000869 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000870 Lex(); // Eat identifier.
871 return false;
872 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000873 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000874 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000875 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000876 case AsmToken::LBrac:
877 if (!PlatformParser->HasBracketExpressions())
878 return TokError("brackets expression not supported on this target");
879 Lex(); // Eat the '['.
880 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000881 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000882 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000883 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000884 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000885 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000886 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000887 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000888 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000889 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000890 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000891 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000892 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000893 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000894 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000895 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000896 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000897 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000898 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000899 }
900}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000901
Chris Lattnerb4307b32010-01-15 19:28:38 +0000902bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000903 SMLoc EndLoc;
904 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000905}
906
Daniel Dunbarcceba832010-09-17 02:47:07 +0000907const MCExpr *
908AsmParser::ApplyModifierToExpr(const MCExpr *E,
909 MCSymbolRefExpr::VariantKind Variant) {
910 // Recurse over the given expression, rebuilding it to apply the given variant
911 // if there is exactly one symbol.
912 switch (E->getKind()) {
913 case MCExpr::Target:
914 case MCExpr::Constant:
915 return 0;
916
917 case MCExpr::SymbolRef: {
918 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
919
920 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
921 TokError("invalid variant on expression '" +
922 getTok().getIdentifier() + "' (already modified)");
923 return E;
924 }
925
926 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
927 }
928
929 case MCExpr::Unary: {
930 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
931 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
932 if (!Sub)
933 return 0;
934 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
935 }
936
937 case MCExpr::Binary: {
938 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
939 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
940 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
941
942 if (!LHS && !RHS)
943 return 0;
944
945 if (!LHS) LHS = BE->getLHS();
946 if (!RHS) RHS = BE->getRHS();
947
948 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
949 }
950 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000951
Craig Topper85814382012-02-07 05:05:23 +0000952 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000953}
954
Chris Lattner74ec1a32009-06-22 06:32:03 +0000955/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000956///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000957/// expr ::= expr &&,|| expr -> lowest.
958/// expr ::= expr |,^,&,! expr
959/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
960/// expr ::= expr <<,>> expr
961/// expr ::= expr +,- expr
962/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000963/// expr ::= primaryexpr
964///
Chris Lattner54482b42010-01-15 19:39:23 +0000965bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000966 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000967 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000968 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
969 return true;
970
Daniel Dunbarcceba832010-09-17 02:47:07 +0000971 // As a special case, we support 'a op b @ modifier' by rewriting the
972 // expression to include the modifier. This is inefficient, but in general we
973 // expect users to use 'a@modifier op b'.
974 if (Lexer.getKind() == AsmToken::At) {
975 Lex();
976
977 if (Lexer.isNot(AsmToken::Identifier))
978 return TokError("unexpected symbol modifier following '@'");
979
980 MCSymbolRefExpr::VariantKind Variant =
981 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
982 if (Variant == MCSymbolRefExpr::VK_Invalid)
983 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
984
985 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
986 if (!ModifiedRes) {
987 return TokError("invalid modifier '" + getTok().getIdentifier() +
988 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000989 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000990
Daniel Dunbarcceba832010-09-17 02:47:07 +0000991 Res = ModifiedRes;
992 Lex();
993 }
994
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000995 // Try to constant fold it up front, if possible.
996 int64_t Value;
997 if (Res->EvaluateAsAbsolute(Value))
998 Res = MCConstantExpr::Create(Value, getContext());
999
1000 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +00001001}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001002
Chris Lattnerb4307b32010-01-15 19:28:38 +00001003bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +00001004 Res = 0;
1005 return ParseParenExpr(Res, EndLoc) ||
1006 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +00001007}
1008
Daniel Dunbar475839e2009-06-29 20:37:27 +00001009bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001010 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001011
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001012 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +00001013 if (ParseExpression(Expr))
1014 return true;
1015
Daniel Dunbare00b0112009-10-16 01:57:52 +00001016 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001017 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001018
1019 return false;
1020}
1021
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001022static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001023 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001024 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001025 default:
1026 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001027
Jim Grosbachfbe16812011-08-20 16:24:13 +00001028 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001029 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001030 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001031 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001032 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001033 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001034 return 1;
1035
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001036
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001037 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +00001038 //
1039 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001040 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001041 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001042 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001043 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001044 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001045 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001046 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001047 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001048 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001049
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001050 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001051 case AsmToken::EqualEqual:
1052 Kind = MCBinaryExpr::EQ;
1053 return 3;
1054 case AsmToken::ExclaimEqual:
1055 case AsmToken::LessGreater:
1056 Kind = MCBinaryExpr::NE;
1057 return 3;
1058 case AsmToken::Less:
1059 Kind = MCBinaryExpr::LT;
1060 return 3;
1061 case AsmToken::LessEqual:
1062 Kind = MCBinaryExpr::LTE;
1063 return 3;
1064 case AsmToken::Greater:
1065 Kind = MCBinaryExpr::GT;
1066 return 3;
1067 case AsmToken::GreaterEqual:
1068 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001069 return 3;
1070
Jim Grosbachfbe16812011-08-20 16:24:13 +00001071 // Intermediate Precedence: <<, >>
1072 case AsmToken::LessLess:
1073 Kind = MCBinaryExpr::Shl;
1074 return 4;
1075 case AsmToken::GreaterGreater:
1076 Kind = MCBinaryExpr::Shr;
1077 return 4;
1078
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001079 // High Intermediate Precedence: +, -
1080 case AsmToken::Plus:
1081 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001082 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001083 case AsmToken::Minus:
1084 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001085 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001086
Jim Grosbachfbe16812011-08-20 16:24:13 +00001087 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001088 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001089 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001090 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001091 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001092 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001093 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001094 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001095 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001096 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001097 }
1098}
1099
1100
1101/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1102/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001103bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1104 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001105 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001106 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001107 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001108
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001109 // If the next token is lower precedence than we are allowed to eat, return
1110 // successfully with what we ate already.
1111 if (TokPrec < Precedence)
1112 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001113
Sean Callanan79ed1a82010-01-19 20:22:31 +00001114 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001115
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001116 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001117 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001118 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001119
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001120 // If BinOp binds less tightly with RHS than the operator after RHS, let
1121 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001122 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001123 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001124 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001125 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001126 }
1127
Daniel Dunbar475839e2009-06-29 20:37:27 +00001128 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001129 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001130 }
1131}
1132
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001133/// ParseStatement:
1134/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001135/// ::= Label* Directive ...Operands... EndOfStatement
1136/// ::= Label* Identifier OperandList* EndOfStatement
Eli Friedman2128aae2012-10-22 23:58:19 +00001137bool AsmParser::ParseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001138 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001139 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001140 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001141 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001142 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001143
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001144 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001145 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001146 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001147 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001148 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001149 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001150 if (Lexer.is(AsmToken::Hash))
1151 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001152
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001153 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001154 if (Lexer.is(AsmToken::Integer)) {
1155 LocalLabelVal = getTok().getIntVal();
1156 if (LocalLabelVal < 0) {
1157 if (!TheCondState.Ignore)
1158 return TokError("unexpected token at start of statement");
1159 IDVal = "";
1160 }
1161 else {
1162 IDVal = getTok().getString();
1163 Lex(); // Consume the integer token to be used as an identifier token.
1164 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001165 if (!TheCondState.Ignore)
1166 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001167 }
1168 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001169
1170 } else if (Lexer.is(AsmToken::Dot)) {
1171 // Treat '.' as a valid identifier in this context.
1172 Lex();
1173 IDVal = ".";
1174
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001175 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001176 if (!TheCondState.Ignore)
1177 return TokError("unexpected token at start of statement");
1178 IDVal = "";
1179 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001180
Chris Lattner7834fac2010-04-17 18:14:27 +00001181 // Handle conditional assembly here before checking for skipping. We
1182 // have to do this so that .endif isn't skipped in a ".if 0" block for
1183 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001184 StringMap<DirectiveKind>::const_iterator DirKindIt =
1185 DirectiveKindMapping.find(IDVal);
1186 DirectiveKind DirKind =
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001187 (DirKindIt == DirectiveKindMapping.end()) ? DK_NO_DIRECTIVE :
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001188 DirKindIt->getValue();
1189 switch (DirKind) {
1190 default:
1191 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001192 case DK_IF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001193 return ParseDirectiveIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001194 case DK_IFB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001195 return ParseDirectiveIfb(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001196 case DK_IFNB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001197 return ParseDirectiveIfb(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001198 case DK_IFC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001199 return ParseDirectiveIfc(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001200 case DK_IFNC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001201 return ParseDirectiveIfc(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001202 case DK_IFDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001203 return ParseDirectiveIfdef(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001204 case DK_IFNDEF:
1205 case DK_IFNOTDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001206 return ParseDirectiveIfdef(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001207 case DK_ELSEIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001208 return ParseDirectiveElseIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001209 case DK_ELSE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001210 return ParseDirectiveElse(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001211 case DK_ENDIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001212 return ParseDirectiveEndIf(IDLoc);
1213 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001214
Chris Lattner7834fac2010-04-17 18:14:27 +00001215 // If we are in a ".if 0" block, ignore this statement.
Chad Rosier17feeec2012-10-20 00:47:08 +00001216 if (TheCondState.Ignore) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001217 EatToEndOfStatement();
1218 return false;
1219 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001220
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001221 // FIXME: Recurse on local labels?
1222
1223 // See what kind of statement we have.
1224 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001225 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001226 CheckForValidSection();
1227
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001228 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001229 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001230
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001231 // Diagnose attempt to use '.' as a label.
1232 if (IDVal == ".")
1233 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1234
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001235 // Diagnose attempt to use a variable as a label.
1236 //
1237 // FIXME: Diagnostics. Note the location of the definition as a label.
1238 // FIXME: This doesn't diagnose assignment to a symbol which has been
1239 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001240 MCSymbol *Sym;
1241 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001242 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001243 else
1244 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001245 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001246 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001247
Daniel Dunbar959fd882009-08-26 22:13:22 +00001248 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001249 if (!ParsingInlineAsm)
1250 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001251
Kevin Enderby94c2e852011-12-09 18:09:40 +00001252 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001253 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001254 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001255 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1256 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001257
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001258 // Consume any end of statement token, if present, to avoid spurious
1259 // AddBlankLine calls().
1260 if (Lexer.is(AsmToken::EndOfStatement)) {
1261 Lex();
1262 if (Lexer.is(AsmToken::Eof))
1263 return false;
1264 }
1265
Eli Friedman2128aae2012-10-22 23:58:19 +00001266 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001267 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001268
Daniel Dunbar3f872332009-07-28 16:08:33 +00001269 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001270 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001271 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001272
Nico Weber4c4c7322011-01-28 03:04:41 +00001273 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001274
1275 default: // Normal instruction or directive.
1276 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001277 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001278
1279 // If macros are enabled, check to see if this is a macro instantiation.
Eli Bendersky733c3362013-01-14 18:08:41 +00001280 if (MacrosEnabled())
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001281 if (const Macro *M = MacroMap.lookup(IDVal))
1282 return HandleMacroEntry(IDVal, IDLoc, M);
1283
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001284 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001285 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001286
1287 // Target hook for parsing target specific directives.
1288 if (!getTargetParser().ParseDirective(ID))
1289 return false;
1290
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001291 switch (DirKind) {
1292 default:
1293 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001294 case DK_SET:
1295 case DK_EQU:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001296 return ParseDirectiveSet(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001297 case DK_EQUIV:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001298 return ParseDirectiveSet(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001299 case DK_ASCII:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001300 return ParseDirectiveAscii(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001301 case DK_ASCIZ:
1302 case DK_STRING:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001303 return ParseDirectiveAscii(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001304 case DK_BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001305 return ParseDirectiveValue(1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001306 case DK_SHORT:
1307 case DK_VALUE:
1308 case DK_2BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001309 return ParseDirectiveValue(2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001310 case DK_LONG:
1311 case DK_INT:
1312 case DK_4BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001313 return ParseDirectiveValue(4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001314 case DK_QUAD:
1315 case DK_8BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001316 return ParseDirectiveValue(8);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001317 case DK_SINGLE:
1318 case DK_FLOAT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001319 return ParseDirectiveRealValue(APFloat::IEEEsingle);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001320 case DK_DOUBLE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001321 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001322 case DK_ALIGN: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001323 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1324 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1325 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001326 case DK_ALIGN32: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001327 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1328 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1329 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001330 case DK_BALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001331 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001332 case DK_BALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001333 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001334 case DK_BALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001335 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001336 case DK_P2ALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001337 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001338 case DK_P2ALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001339 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001340 case DK_P2ALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001341 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001342 case DK_ORG:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001343 return ParseDirectiveOrg();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001344 case DK_FILL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001345 return ParseDirectiveFill();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001346 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001347 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001348 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001349 EatToEndOfStatement(); // .extern is the default, ignore it.
1350 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001351 case DK_GLOBL:
1352 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001353 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001354 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001355 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001356 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001357 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001358 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001359 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001360 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001361 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001362 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001363 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001364 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001365 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001366 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001367 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001368 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001369 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001370 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001371 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001372 case DK_COMM:
1373 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001374 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001375 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001376 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001377 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001378 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001379 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001380 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001381 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001382 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001383 case DK_CODE16:
1384 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001385 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001386 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001387 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001388 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001389 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001390 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001391 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001392 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001393 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001394 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001395 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001396 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001397 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001398 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001399 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001400 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001401
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001402 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001403 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1404 DirectiveMap.lookup(IDVal);
1405 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001406 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001407
Jim Grosbach686c0182012-05-01 18:38:27 +00001408 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001409 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001410
Eli Friedman2128aae2012-10-22 23:58:19 +00001411 // _emit
1412 if (ParsingInlineAsm && IDVal == "_emit")
1413 return ParseDirectiveEmit(IDLoc, Info);
1414
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001415 CheckForValidSection();
1416
Chris Lattnera7f13542010-05-19 23:34:33 +00001417 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001418 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001419 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001420 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001421
Chad Rosier6a020a72012-10-25 20:41:34 +00001422 ParseInstructionInfo IInfo(Info.AsmRewrites);
1423 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1424 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001425 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001426
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001427 // Dump the parsed representation, if requested.
1428 if (getShowParsedOperands()) {
1429 SmallString<256> Str;
1430 raw_svector_ostream OS(Str);
1431 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001432 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001433 if (i != 0)
1434 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001435 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001436 }
1437 OS << "]";
1438
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001439 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001440 }
1441
Kevin Enderby613b7572011-11-01 22:27:22 +00001442 // If we are generating dwarf for assembly source files and the current
1443 // section is the initial text section then generate a .loc directive for
1444 // the instruction.
1445 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001446 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001447
1448 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1449
1450 // If we previously parsed a cpp hash file line comment then make sure the
1451 // current Dwarf File is for the CppHashFilename if not then emit the
1452 // Dwarf File table for it and adjust the line number for the .loc.
1453 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1454 getContext().getMCDwarfFiles();
1455 if (CppHashFilename.size() != 0) {
1456 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1457 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001458 getStreamer().EmitDwarfFileDirective(
1459 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001460
Kevin Enderby32c1a822012-11-05 21:55:41 +00001461 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001462 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1463 }
1464
Kevin Enderby613b7572011-11-01 22:27:22 +00001465 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001466 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001467 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001468 StringRef());
1469 }
1470
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001471 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001472 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001473 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001474 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1475 Info.ParsedOperands,
1476 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001477 ParsingInlineAsm);
1478 }
Chris Lattner98986712010-01-14 22:21:20 +00001479
Chris Lattnercbf8a982010-09-11 16:18:25 +00001480 // Don't skip the rest of the line, the instruction parser is responsible for
1481 // that.
1482 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001483}
Chris Lattner9a023f72009-06-24 04:43:34 +00001484
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001485/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1486/// since they may not be able to be tokenized to get to the end of line token.
1487void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001488 if (!Lexer.is(AsmToken::EndOfStatement))
1489 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001490 // Eat EOL.
1491 Lex();
1492}
1493
1494/// ParseCppHashLineFilenameComment as this:
1495/// ::= # number "filename"
1496/// or just as a full line comment if it doesn't have a number and a string.
1497bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1498 Lex(); // Eat the hash token.
1499
1500 if (getLexer().isNot(AsmToken::Integer)) {
1501 // Consume the line since in cases it is not a well-formed line directive,
1502 // as if were simply a full line comment.
1503 EatToEndOfLine();
1504 return false;
1505 }
1506
1507 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001508 Lex();
1509
1510 if (getLexer().isNot(AsmToken::String)) {
1511 EatToEndOfLine();
1512 return false;
1513 }
1514
1515 StringRef Filename = getTok().getString();
1516 // Get rid of the enclosing quotes.
1517 Filename = Filename.substr(1, Filename.size()-2);
1518
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001519 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1520 CppHashLoc = L;
1521 CppHashFilename = Filename;
1522 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001523 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001524
1525 // Ignore any trailing characters, they're just comment.
1526 EatToEndOfLine();
1527 return false;
1528}
1529
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001530/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001531/// for the Filename and LineNo if any in the diagnostic.
1532void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1533 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1534 raw_ostream &OS = errs();
1535
1536 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1537 const SMLoc &DiagLoc = Diag.getLoc();
1538 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1539 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1540
1541 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1542 // before printing the message.
1543 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001544 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001545 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1546 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1547 }
1548
Eric Christopher2318ba12012-12-18 00:30:54 +00001549 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001550 // manager changed or buffer changed (like in a nested include) then just
1551 // print the normal diagnostic using its Filename and LineNo.
1552 if (!Parser->CppHashLineNumber ||
1553 &DiagSrcMgr != &Parser->SrcMgr ||
1554 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001555 if (Parser->SavedDiagHandler)
1556 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1557 else
1558 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001559 return;
1560 }
1561
Eric Christopher2318ba12012-12-18 00:30:54 +00001562 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001563 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1564 // the diagnostic.
1565 const std::string Filename = Parser->CppHashFilename;
1566
1567 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1568 int CppHashLocLineNo =
1569 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1570 int LineNo = Parser->CppHashLineNumber - 1 +
1571 (DiagLocLineNo - CppHashLocLineNo);
1572
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001573 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1574 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001575 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001576 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001577
Benjamin Kramer04a04262011-10-16 10:48:29 +00001578 if (Parser->SavedDiagHandler)
1579 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1580 else
1581 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001582}
1583
Rafael Espindola799aacf2012-08-21 18:29:30 +00001584// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1585// difference being that that function accepts '@' as part of identifiers and
1586// we can't do that. AsmLexer.cpp should probably be changed to handle
1587// '@' as a special case when needed.
1588static bool isIdentifierChar(char c) {
1589 return isalnum(c) || c == '_' || c == '$' || c == '.';
1590}
1591
Rafael Espindola761cb062012-06-03 23:57:14 +00001592bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001593 const MacroParameters &Parameters,
1594 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001595 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001596 unsigned NParameters = Parameters.size();
1597 if (NParameters != 0 && NParameters != A.size())
1598 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001599
Preston Gurd7b6f2032012-09-19 20:36:12 +00001600 // A macro without parameters is handled differently on Darwin:
1601 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001602 while (!Body.empty()) {
1603 // Scan for the next substitution.
1604 std::size_t End = Body.size(), Pos = 0;
1605 for (; Pos != End; ++Pos) {
1606 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001607 if (!NParameters) {
1608 // This macro has no parameters, look for $0, $1, etc.
1609 if (Body[Pos] != '$' || Pos + 1 == End)
1610 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001611
Rafael Espindola65366442011-06-05 02:43:45 +00001612 char Next = Body[Pos + 1];
1613 if (Next == '$' || Next == 'n' || isdigit(Next))
1614 break;
1615 } else {
1616 // This macro has parameters, look for \foo, \bar, etc.
1617 if (Body[Pos] == '\\' && Pos + 1 != End)
1618 break;
1619 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001620 }
1621
1622 // Add the prefix.
1623 OS << Body.slice(0, Pos);
1624
1625 // Check if we reached the end.
1626 if (Pos == End)
1627 break;
1628
Rafael Espindola65366442011-06-05 02:43:45 +00001629 if (!NParameters) {
1630 switch (Body[Pos+1]) {
1631 // $$ => $
1632 case '$':
1633 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001634 break;
1635
Rafael Espindola65366442011-06-05 02:43:45 +00001636 // $n => number of arguments
1637 case 'n':
1638 OS << A.size();
1639 break;
1640
1641 // $[0-9] => argument
1642 default: {
1643 // Missing arguments are ignored.
1644 unsigned Index = Body[Pos+1] - '0';
1645 if (Index >= A.size())
1646 break;
1647
1648 // Otherwise substitute with the token values, with spaces eliminated.
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001649 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001650 ie = A[Index].end(); it != ie; ++it)
1651 OS << it->getString();
1652 break;
1653 }
1654 }
1655 Pos += 2;
1656 } else {
1657 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001658 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001659 ++I;
1660
1661 const char *Begin = Body.data() + Pos +1;
1662 StringRef Argument(Begin, I - (Pos +1));
1663 unsigned Index = 0;
1664 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001665 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001666 break;
1667
Preston Gurd7b6f2032012-09-19 20:36:12 +00001668 if (Index == NParameters) {
1669 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1670 Pos += 3;
1671 else {
1672 OS << '\\' << Argument;
1673 Pos = I;
1674 }
1675 } else {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001676 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Preston Gurd7b6f2032012-09-19 20:36:12 +00001677 ie = A[Index].end(); it != ie; ++it)
1678 if (it->getKind() == AsmToken::String)
1679 OS << it->getStringContents();
1680 else
1681 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001682
Preston Gurd7b6f2032012-09-19 20:36:12 +00001683 Pos += 1 + Argument.size();
1684 }
Rafael Espindola65366442011-06-05 02:43:45 +00001685 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001686 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001687 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001688 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001689
Rafael Espindola65366442011-06-05 02:43:45 +00001690 return false;
1691}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001692
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001693MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL,
1694 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001695 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001696 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1697 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001698{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001699}
1700
Preston Gurd7b6f2032012-09-19 20:36:12 +00001701static bool IsOperator(AsmToken::TokenKind kind)
1702{
1703 switch (kind)
1704 {
1705 default:
1706 return false;
1707 case AsmToken::Plus:
1708 case AsmToken::Minus:
1709 case AsmToken::Tilde:
1710 case AsmToken::Slash:
1711 case AsmToken::Star:
1712 case AsmToken::Dot:
1713 case AsmToken::Equal:
1714 case AsmToken::EqualEqual:
1715 case AsmToken::Pipe:
1716 case AsmToken::PipePipe:
1717 case AsmToken::Caret:
1718 case AsmToken::Amp:
1719 case AsmToken::AmpAmp:
1720 case AsmToken::Exclaim:
1721 case AsmToken::ExclaimEqual:
1722 case AsmToken::Percent:
1723 case AsmToken::Less:
1724 case AsmToken::LessEqual:
1725 case AsmToken::LessLess:
1726 case AsmToken::LessGreater:
1727 case AsmToken::Greater:
1728 case AsmToken::GreaterEqual:
1729 case AsmToken::GreaterGreater:
1730 return true;
1731 }
1732}
1733
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001734bool AsmParser::ParseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +00001735 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001736 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001737 unsigned AddTokens = 0;
1738
1739 // gas accepts arguments separated by whitespace, except on Darwin
1740 if (!IsDarwin)
1741 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001742
1743 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001744 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1745 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001746 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001747 }
1748
1749 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1750 // Spaces and commas cannot be mixed to delimit parameters
1751 if (ArgumentDelimiter == AsmToken::Eof)
1752 ArgumentDelimiter = AsmToken::Comma;
1753 else if (ArgumentDelimiter != AsmToken::Comma) {
1754 Lexer.setSkipSpace(true);
1755 return TokError("expected ' ' for macro argument separator");
1756 }
1757 break;
1758 }
1759
1760 if (Lexer.is(AsmToken::Space)) {
1761 Lex(); // Eat spaces
1762
1763 // Spaces can delimit parameters, but could also be part an expression.
1764 // If the token after a space is an operator, add the token and the next
1765 // one into this argument
1766 if (ArgumentDelimiter == AsmToken::Space ||
1767 ArgumentDelimiter == AsmToken::Eof) {
1768 if (IsOperator(Lexer.getKind())) {
1769 // Check to see whether the token is used as an operator,
1770 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001771 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001772 if (*NextChar == ' ')
1773 AddTokens = 2;
1774 }
1775
1776 if (!AddTokens && ParenLevel == 0) {
1777 if (ArgumentDelimiter == AsmToken::Eof &&
1778 !IsOperator(Lexer.getKind()))
1779 ArgumentDelimiter = AsmToken::Space;
1780 break;
1781 }
1782 }
1783 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001784
1785 // HandleMacroEntry relies on not advancing the lexer here
1786 // to be able to fill in the remaining default parameter values
1787 if (Lexer.is(AsmToken::EndOfStatement))
1788 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001789
1790 // Adjust the current parentheses level.
1791 if (Lexer.is(AsmToken::LParen))
1792 ++ParenLevel;
1793 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1794 --ParenLevel;
1795
1796 // Append the token to the current argument list.
1797 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001798 if (AddTokens)
1799 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001800 Lex();
1801 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001802
1803 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001804 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001805 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001806 return false;
1807}
1808
1809// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001810bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001811 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001812 // Argument delimiter is initially unknown. It will be set by
1813 // ParseMacroArgument()
1814 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001815
1816 // Parse two kinds of macro invocations:
1817 // - macros defined without any parameters accept an arbitrary number of them
1818 // - macros defined with parameters accept at most that many of them
1819 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1820 ++Parameter) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001821 MCAsmMacroArgument MA;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001822
Preston Gurd7b6f2032012-09-19 20:36:12 +00001823 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001824 return true;
1825
Preston Gurd6c9176a2012-09-19 20:29:04 +00001826 if (!MA.empty() || !NParameters)
1827 A.push_back(MA);
1828 else if (NParameters) {
1829 if (!M->Parameters[Parameter].second.empty())
1830 A.push_back(M->Parameters[Parameter].second);
1831 }
Jim Grosbach97146442012-07-30 22:44:17 +00001832
Preston Gurd6c9176a2012-09-19 20:29:04 +00001833 // At the end of the statement, fill in remaining arguments that have
1834 // default values. If there aren't any, then the next argument is
1835 // required but missing
1836 if (Lexer.is(AsmToken::EndOfStatement)) {
1837 if (NParameters && Parameter < NParameters - 1) {
1838 if (M->Parameters[Parameter + 1].second.empty())
1839 return TokError("macro argument '" +
1840 Twine(M->Parameters[Parameter + 1].first) +
1841 "' is missing");
1842 else
1843 continue;
1844 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001845 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001846 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001847
1848 if (Lexer.is(AsmToken::Comma))
1849 Lex();
1850 }
1851 return TokError("Too many arguments");
1852}
1853
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001854bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1855 const Macro *M) {
1856 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1857 // this, although we should protect against infinite loops.
1858 if (ActiveMacros.size() == 20)
1859 return TokError("macros cannot be nested more than 20 levels deep");
1860
Rafael Espindola8a403d32012-08-08 14:51:03 +00001861 MacroArguments A;
1862 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001863 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001864
Jim Grosbach97146442012-07-30 22:44:17 +00001865 // Remove any trailing empty arguments. Do this after-the-fact as we have
1866 // to keep empty arguments in the middle of the list or positionality
1867 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001868 while (!A.empty() && A.back().empty())
1869 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001870
Rafael Espindola65366442011-06-05 02:43:45 +00001871 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1872 // to hold the macro body with substitutions.
1873 SmallString<256> Buf;
1874 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001875 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001876
Rafael Espindola8a403d32012-08-08 14:51:03 +00001877 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001878 return true;
1879
Rafael Espindola761cb062012-06-03 23:57:14 +00001880 // We include the .endmacro in the buffer as our queue to exit the macro
1881 // instantiation.
1882 OS << ".endmacro\n";
1883
Rafael Espindola65366442011-06-05 02:43:45 +00001884 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001885 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001886
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001887 // Create the macro instantiation object and add to the current macro
1888 // instantiation stack.
1889 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001890 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001891 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001892 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001893 ActiveMacros.push_back(MI);
1894
1895 // Jump to the macro instantiation and prime the lexer.
1896 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1897 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1898 Lex();
1899
1900 return false;
1901}
1902
1903void AsmParser::HandleMacroExit() {
1904 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001905 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001906 Lex();
1907
1908 // Pop the instantiation entry.
1909 delete ActiveMacros.back();
1910 ActiveMacros.pop_back();
1911}
1912
Rafael Espindolae71cc862012-01-28 05:57:00 +00001913static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001914 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001915 case MCExpr::Binary: {
1916 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1917 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001918 break;
1919 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001920 case MCExpr::Target:
1921 case MCExpr::Constant:
1922 return false;
1923 case MCExpr::SymbolRef: {
1924 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001925 if (S.isVariable())
1926 return IsUsedIn(Sym, S.getVariableValue());
1927 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001928 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001929 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001930 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001931 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001932
1933 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001934}
1935
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001936bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1937 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001938 // FIXME: Use better location, we should use proper tokens.
1939 SMLoc EqualLoc = Lexer.getLoc();
1940
Daniel Dunbar821e3332009-08-31 08:09:28 +00001941 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001942 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001943 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001944
Rafael Espindolae71cc862012-01-28 05:57:00 +00001945 // Note: we don't count b as used in "a = b". This is to allow
1946 // a = b
1947 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001948
Daniel Dunbar3f872332009-07-28 16:08:33 +00001949 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001950 return TokError("unexpected token in assignment");
1951
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001952 // Error on assignment to '.'.
1953 if (Name == ".") {
1954 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1955 "(use '.space' or '.org').)"));
1956 }
1957
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001958 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001959 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001960
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001961 // Validate that the LHS is allowed to be a variable (either it has not been
1962 // used as a symbol, or it is an absolute symbol).
1963 MCSymbol *Sym = getContext().LookupSymbol(Name);
1964 if (Sym) {
1965 // Diagnose assignment to a label.
1966 //
1967 // FIXME: Diagnostics. Note the location of the definition as a label.
1968 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001969 if (IsUsedIn(Sym, Value))
1970 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1971 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001972 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001973 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1974 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001975 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001976 return Error(EqualLoc, "redefinition of '" + Name + "'");
1977 else if (!Sym->isVariable())
1978 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001979 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001980 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1981 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001982
1983 // Don't count these checks as uses.
1984 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001985 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001986 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001987
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001988 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001989
1990 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001991 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001992 if (NoDeadStrip)
1993 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1994
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001995
1996 return false;
1997}
1998
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001999/// ParseIdentifier:
2000/// ::= identifier
2001/// ::= string
2002bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002003 // The assembler has relaxed rules for accepting identifiers, in particular we
2004 // allow things like '.globl $foo', which would normally be separate
2005 // tokens. At this level, we have already lexed so we cannot (currently)
2006 // handle this as a context dependent token, instead we detect adjacent tokens
2007 // and return the combined identifier.
2008 if (Lexer.is(AsmToken::Dollar)) {
2009 SMLoc DollarLoc = getLexer().getLoc();
2010
2011 // Consume the dollar sign, and check for a following identifier.
2012 Lex();
2013 if (Lexer.isNot(AsmToken::Identifier))
2014 return true;
2015
2016 // We have a '$' followed by an identifier, make sure they are adjacent.
2017 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2018 return true;
2019
2020 // Construct the joined identifier and consume the token.
2021 Res = StringRef(DollarLoc.getPointer(),
2022 getTok().getIdentifier().size() + 1);
2023 Lex();
2024 return false;
2025 }
2026
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002027 if (Lexer.isNot(AsmToken::Identifier) &&
2028 Lexer.isNot(AsmToken::String))
2029 return true;
2030
Sean Callanan18b83232010-01-19 21:44:56 +00002031 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002032
Sean Callanan79ed1a82010-01-19 20:22:31 +00002033 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002034
2035 return false;
2036}
2037
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002038/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002039/// ::= .equ identifier ',' expression
2040/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002041/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002042bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002043 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002044
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002045 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002046 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002047
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002048 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002049 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002050 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002051
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002052 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002053}
2054
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002055bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002056 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002057
2058 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002059 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002060 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2061 if (Str[i] != '\\') {
2062 Data += Str[i];
2063 continue;
2064 }
2065
2066 // Recognize escaped characters. Note that this escape semantics currently
2067 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2068 ++i;
2069 if (i == e)
2070 return TokError("unexpected backslash at end of string");
2071
2072 // Recognize octal sequences.
2073 if ((unsigned) (Str[i] - '0') <= 7) {
2074 // Consume up to three octal characters.
2075 unsigned Value = Str[i] - '0';
2076
2077 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2078 ++i;
2079 Value = Value * 8 + (Str[i] - '0');
2080
2081 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2082 ++i;
2083 Value = Value * 8 + (Str[i] - '0');
2084 }
2085 }
2086
2087 if (Value > 255)
2088 return TokError("invalid octal escape sequence (out of range)");
2089
2090 Data += (unsigned char) Value;
2091 continue;
2092 }
2093
2094 // Otherwise recognize individual escapes.
2095 switch (Str[i]) {
2096 default:
2097 // Just reject invalid escape sequences for now.
2098 return TokError("invalid escape sequence (unrecognized character)");
2099
2100 case 'b': Data += '\b'; break;
2101 case 'f': Data += '\f'; break;
2102 case 'n': Data += '\n'; break;
2103 case 'r': Data += '\r'; break;
2104 case 't': Data += '\t'; break;
2105 case '"': Data += '"'; break;
2106 case '\\': Data += '\\'; break;
2107 }
2108 }
2109
2110 return false;
2111}
2112
Daniel Dunbara0d14262009-06-24 23:30:00 +00002113/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002114/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2115bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002116 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002117 CheckForValidSection();
2118
Daniel Dunbara0d14262009-06-24 23:30:00 +00002119 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002120 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002121 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002122
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002123 std::string Data;
2124 if (ParseEscapedString(Data))
2125 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002126
2127 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002128 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002129 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2130
Sean Callanan79ed1a82010-01-19 20:22:31 +00002131 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002132
2133 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002134 break;
2135
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002137 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002138 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002139 }
2140 }
2141
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002143 return false;
2144}
2145
2146/// ParseDirectiveValue
2147/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2148bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002149 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002150 CheckForValidSection();
2151
Daniel Dunbara0d14262009-06-24 23:30:00 +00002152 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002153 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002154 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002155 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002156 return true;
2157
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002158 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002159 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2160 assert(Size <= 8 && "Invalid size");
2161 uint64_t IntValue = MCE->getValue();
2162 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2163 return Error(ExprLoc, "literal value out of range for directive");
2164 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2165 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002166 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002167
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002168 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002169 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002170
Daniel Dunbara0d14262009-06-24 23:30:00 +00002171 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002172 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002173 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002174 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002175 }
2176 }
2177
Sean Callanan79ed1a82010-01-19 20:22:31 +00002178 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002179 return false;
2180}
2181
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002182/// ParseDirectiveRealValue
2183/// ::= (.single | .double) [ expression (, expression)* ]
2184bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2185 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2186 CheckForValidSection();
2187
2188 for (;;) {
2189 // We don't truly support arithmetic on floating point expressions, so we
2190 // have to manually parse unary prefixes.
2191 bool IsNeg = false;
2192 if (getLexer().is(AsmToken::Minus)) {
2193 Lex();
2194 IsNeg = true;
2195 } else if (getLexer().is(AsmToken::Plus))
2196 Lex();
2197
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002198 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002199 getLexer().isNot(AsmToken::Real) &&
2200 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002201 return TokError("unexpected token in directive");
2202
2203 // Convert to an APFloat.
2204 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002205 StringRef IDVal = getTok().getString();
2206 if (getLexer().is(AsmToken::Identifier)) {
2207 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2208 Value = APFloat::getInf(Semantics);
2209 else if (!IDVal.compare_lower("nan"))
2210 Value = APFloat::getNaN(Semantics, false, ~0);
2211 else
2212 return TokError("invalid floating point literal");
2213 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002214 APFloat::opInvalidOp)
2215 return TokError("invalid floating point literal");
2216 if (IsNeg)
2217 Value.changeSign();
2218
2219 // Consume the numeric token.
2220 Lex();
2221
2222 // Emit the value as an integer.
2223 APInt AsInt = Value.bitcastToAPInt();
2224 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2225 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2226
2227 if (getLexer().is(AsmToken::EndOfStatement))
2228 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002229
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002230 if (getLexer().isNot(AsmToken::Comma))
2231 return TokError("unexpected token in directive");
2232 Lex();
2233 }
2234 }
2235
2236 Lex();
2237 return false;
2238}
2239
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002240/// ParseDirectiveZero
2241/// ::= .zero expression
2242bool AsmParser::ParseDirectiveZero() {
2243 CheckForValidSection();
2244
2245 int64_t NumBytes;
2246 if (ParseAbsoluteExpression(NumBytes))
2247 return true;
2248
Rafael Espindolae452b172010-10-05 19:42:57 +00002249 int64_t Val = 0;
2250 if (getLexer().is(AsmToken::Comma)) {
2251 Lex();
2252 if (ParseAbsoluteExpression(Val))
2253 return true;
2254 }
2255
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002256 if (getLexer().isNot(AsmToken::EndOfStatement))
2257 return TokError("unexpected token in '.zero' directive");
2258
2259 Lex();
2260
Rafael Espindolae452b172010-10-05 19:42:57 +00002261 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002262
2263 return false;
2264}
2265
Daniel Dunbara0d14262009-06-24 23:30:00 +00002266/// ParseDirectiveFill
2267/// ::= .fill expression , expression , expression
2268bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002269 CheckForValidSection();
2270
Daniel Dunbara0d14262009-06-24 23:30:00 +00002271 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002272 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002273 return true;
2274
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002275 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002276 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002277 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002278
Daniel Dunbara0d14262009-06-24 23:30:00 +00002279 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002280 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002281 return true;
2282
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002283 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002284 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002285 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002286
Daniel Dunbara0d14262009-06-24 23:30:00 +00002287 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002288 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002289 return true;
2290
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002291 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002292 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002293
Sean Callanan79ed1a82010-01-19 20:22:31 +00002294 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002295
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002296 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2297 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002298
2299 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002300 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002301
2302 return false;
2303}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002304
2305/// ParseDirectiveOrg
2306/// ::= .org expression [ , expression ]
2307bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002308 CheckForValidSection();
2309
Daniel Dunbar821e3332009-08-31 08:09:28 +00002310 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002311 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002312 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002313 return true;
2314
2315 // Parse optional fill expression.
2316 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002317 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2318 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002319 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002320 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002321
Daniel Dunbar475839e2009-06-29 20:37:27 +00002322 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002323 return true;
2324
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002325 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002326 return TokError("unexpected token in '.org' directive");
2327 }
2328
Sean Callanan79ed1a82010-01-19 20:22:31 +00002329 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002330
Jim Grosbachebd4c052012-01-27 00:37:08 +00002331 // Only limited forms of relocatable expressions are accepted here, it
2332 // has to be relative to the current section. The streamer will return
2333 // 'true' if the expression wasn't evaluatable.
2334 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2335 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002336
2337 return false;
2338}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002339
2340/// ParseDirectiveAlign
2341/// ::= {.align, ...} expression [ , expression [ , expression ]]
2342bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002343 CheckForValidSection();
2344
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002345 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002346 int64_t Alignment;
2347 if (ParseAbsoluteExpression(Alignment))
2348 return true;
2349
2350 SMLoc MaxBytesLoc;
2351 bool HasFillExpr = false;
2352 int64_t FillExpr = 0;
2353 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002354 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2355 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002356 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002357 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002358
2359 // The fill expression can be omitted while specifying a maximum number of
2360 // alignment bytes, e.g:
2361 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002362 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002363 HasFillExpr = true;
2364 if (ParseAbsoluteExpression(FillExpr))
2365 return true;
2366 }
2367
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002368 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2369 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002370 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002371 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002372
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002373 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002374 if (ParseAbsoluteExpression(MaxBytesToFill))
2375 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002376
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002377 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002378 return TokError("unexpected token in directive");
2379 }
2380 }
2381
Sean Callanan79ed1a82010-01-19 20:22:31 +00002382 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002383
Daniel Dunbar648ac512010-05-17 21:54:30 +00002384 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002385 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002386
2387 // Compute alignment in bytes.
2388 if (IsPow2) {
2389 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002390 if (Alignment >= 32) {
2391 Error(AlignmentLoc, "invalid alignment value");
2392 Alignment = 31;
2393 }
2394
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002395 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002396 }
2397
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002398 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002399 if (MaxBytesLoc.isValid()) {
2400 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002401 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2402 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002403 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002404 }
2405
2406 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002407 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2408 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002409 MaxBytesToFill = 0;
2410 }
2411 }
2412
Daniel Dunbar648ac512010-05-17 21:54:30 +00002413 // Check whether we should use optimal code alignment for this .align
2414 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002415 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002416 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2417 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002418 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002419 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002420 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002421 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2422 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002423 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002424
2425 return false;
2426}
2427
Eli Bendersky4766ef42012-12-20 19:05:53 +00002428
2429/// ParseDirectiveBundleAlignMode
2430/// ::= {.bundle_align_mode} expression
2431bool AsmParser::ParseDirectiveBundleAlignMode() {
2432 CheckForValidSection();
2433
2434 // Expect a single argument: an expression that evaluates to a constant
2435 // in the inclusive range 0-30.
2436 SMLoc ExprLoc = getLexer().getLoc();
2437 int64_t AlignSizePow2;
2438 if (ParseAbsoluteExpression(AlignSizePow2))
2439 return true;
2440 else if (getLexer().isNot(AsmToken::EndOfStatement))
2441 return TokError("unexpected token after expression in"
2442 " '.bundle_align_mode' directive");
2443 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2444 return Error(ExprLoc,
2445 "invalid bundle alignment size (expected between 0 and 30)");
2446
2447 Lex();
2448
2449 // Because of AlignSizePow2's verified range we can safely truncate it to
2450 // unsigned.
2451 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2452 return false;
2453}
2454
2455/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002456/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002457bool AsmParser::ParseDirectiveBundleLock() {
2458 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002459 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002460
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002461 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2462 StringRef Option;
2463 SMLoc Loc = getTok().getLoc();
2464 const char *kInvalidOptionError =
2465 "invalid option for '.bundle_lock' directive";
2466
2467 if (ParseIdentifier(Option))
2468 return Error(Loc, kInvalidOptionError);
2469
2470 if (Option != "align_to_end")
2471 return Error(Loc, kInvalidOptionError);
2472 else if (getLexer().isNot(AsmToken::EndOfStatement))
2473 return Error(Loc,
2474 "unexpected token after '.bundle_lock' directive option");
2475 AlignToEnd = true;
2476 }
2477
Eli Bendersky4766ef42012-12-20 19:05:53 +00002478 Lex();
2479
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002480 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002481 return false;
2482}
2483
2484/// ParseDirectiveBundleLock
2485/// ::= {.bundle_lock}
2486bool AsmParser::ParseDirectiveBundleUnlock() {
2487 CheckForValidSection();
2488
2489 if (getLexer().isNot(AsmToken::EndOfStatement))
2490 return TokError("unexpected token in '.bundle_unlock' directive");
2491 Lex();
2492
2493 getStreamer().EmitBundleUnlock();
2494 return false;
2495}
2496
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002497/// ParseDirectiveSymbolAttribute
2498/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002499bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002500 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002501 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002502 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002503 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002504
2505 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002506 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002507
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002508 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002509
Jim Grosbach10ec6502011-09-15 17:56:49 +00002510 // Assembler local symbols don't make any sense here. Complain loudly.
2511 if (Sym->isTemporary())
2512 return Error(Loc, "non-local symbol required in directive");
2513
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002514 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002515
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002516 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002517 break;
2518
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002519 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002520 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002521 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002522 }
2523 }
2524
Sean Callanan79ed1a82010-01-19 20:22:31 +00002525 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002526 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002527}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002528
2529/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002530/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2531bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002532 CheckForValidSection();
2533
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002534 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002535 StringRef Name;
2536 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002537 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002538
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002539 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002540 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002541
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002542 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002543 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002544 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002545
2546 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002547 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002548 if (ParseAbsoluteExpression(Size))
2549 return true;
2550
2551 int64_t Pow2Alignment = 0;
2552 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002553 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002554 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002555 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002556 if (ParseAbsoluteExpression(Pow2Alignment))
2557 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002558
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002559 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2560 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002561 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2562
Chris Lattner258281d2010-01-19 06:22:22 +00002563 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002564 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2565 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002566 if (!isPowerOf2_64(Pow2Alignment))
2567 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2568 Pow2Alignment = Log2_64(Pow2Alignment);
2569 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002570 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002571
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002572 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002573 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002574
Sean Callanan79ed1a82010-01-19 20:22:31 +00002575 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002576
Chris Lattner1fc3d752009-07-09 17:25:12 +00002577 // NOTE: a size of zero for a .comm should create a undefined symbol
2578 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002579 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002580 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2581 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002582
Eric Christopherc260a3e2010-05-14 01:38:54 +00002583 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002584 // may internally end up wanting an alignment in bytes.
2585 // FIXME: Diagnose overflow.
2586 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002587 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2588 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002589
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002590 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002591 return Error(IDLoc, "invalid symbol redefinition");
2592
Chris Lattner1fc3d752009-07-09 17:25:12 +00002593 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002594 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002595 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002596 return false;
2597 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002598
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002599 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002600 return false;
2601}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002602
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002603/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002604/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002605bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002606 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002607 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002608
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002609 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002610 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002611 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002612
Sean Callanan79ed1a82010-01-19 20:22:31 +00002613 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002614
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002615 if (Str.empty())
2616 Error(Loc, ".abort detected. Assembly stopping.");
2617 else
2618 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002619 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002620
2621 return false;
2622}
Kevin Enderby71148242009-07-14 21:35:03 +00002623
Kevin Enderby1f049b22009-07-14 23:21:55 +00002624/// ParseDirectiveInclude
2625/// ::= .include "filename"
2626bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002627 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002628 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002629
Sean Callanan18b83232010-01-19 21:44:56 +00002630 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002631 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002632 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002633
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002634 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002635 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002636
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002637 // Strip the quotes.
2638 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002639
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002640 // Attempt to switch the lexer to the included file before consuming the end
2641 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002642 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002643 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002644 return true;
2645 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002646
2647 return false;
2648}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002649
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002650/// ParseDirectiveIncbin
2651/// ::= .incbin "filename"
2652bool AsmParser::ParseDirectiveIncbin() {
2653 if (getLexer().isNot(AsmToken::String))
2654 return TokError("expected string in '.incbin' directive");
2655
2656 std::string Filename = getTok().getString();
2657 SMLoc IncbinLoc = getLexer().getLoc();
2658 Lex();
2659
2660 if (getLexer().isNot(AsmToken::EndOfStatement))
2661 return TokError("unexpected token in '.incbin' directive");
2662
2663 // Strip the quotes.
2664 Filename = Filename.substr(1, Filename.size()-2);
2665
2666 // Attempt to process the included file.
2667 if (ProcessIncbinFile(Filename)) {
2668 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2669 return true;
2670 }
2671
2672 return false;
2673}
2674
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002675/// ParseDirectiveIf
2676/// ::= .if expression
2677bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002678 TheCondStack.push_back(TheCondState);
2679 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002680 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002681 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002682 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002683 int64_t ExprValue;
2684 if (ParseAbsoluteExpression(ExprValue))
2685 return true;
2686
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002687 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002688 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002689
Sean Callanan79ed1a82010-01-19 20:22:31 +00002690 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002691
2692 TheCondState.CondMet = ExprValue;
2693 TheCondState.Ignore = !TheCondState.CondMet;
2694 }
2695
2696 return false;
2697}
2698
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002699/// ParseDirectiveIfb
2700/// ::= .ifb string
2701bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2702 TheCondStack.push_back(TheCondState);
2703 TheCondState.TheCond = AsmCond::IfCond;
2704
Benjamin Kramer29739e72012-05-12 16:52:21 +00002705 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002706 EatToEndOfStatement();
2707 } else {
2708 StringRef Str = ParseStringToEndOfStatement();
2709
2710 if (getLexer().isNot(AsmToken::EndOfStatement))
2711 return TokError("unexpected token in '.ifb' directive");
2712
2713 Lex();
2714
2715 TheCondState.CondMet = ExpectBlank == Str.empty();
2716 TheCondState.Ignore = !TheCondState.CondMet;
2717 }
2718
2719 return false;
2720}
2721
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002722/// ParseDirectiveIfc
2723/// ::= .ifc string1, string2
2724bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2725 TheCondStack.push_back(TheCondState);
2726 TheCondState.TheCond = AsmCond::IfCond;
2727
Benjamin Kramer29739e72012-05-12 16:52:21 +00002728 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002729 EatToEndOfStatement();
2730 } else {
2731 StringRef Str1 = ParseStringToComma();
2732
2733 if (getLexer().isNot(AsmToken::Comma))
2734 return TokError("unexpected token in '.ifc' directive");
2735
2736 Lex();
2737
2738 StringRef Str2 = ParseStringToEndOfStatement();
2739
2740 if (getLexer().isNot(AsmToken::EndOfStatement))
2741 return TokError("unexpected token in '.ifc' directive");
2742
2743 Lex();
2744
2745 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2746 TheCondState.Ignore = !TheCondState.CondMet;
2747 }
2748
2749 return false;
2750}
2751
2752/// ParseDirectiveIfdef
2753/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002754bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2755 StringRef Name;
2756 TheCondStack.push_back(TheCondState);
2757 TheCondState.TheCond = AsmCond::IfCond;
2758
2759 if (TheCondState.Ignore) {
2760 EatToEndOfStatement();
2761 } else {
2762 if (ParseIdentifier(Name))
2763 return TokError("expected identifier after '.ifdef'");
2764
2765 Lex();
2766
2767 MCSymbol *Sym = getContext().LookupSymbol(Name);
2768
2769 if (expect_defined)
2770 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2771 else
2772 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2773 TheCondState.Ignore = !TheCondState.CondMet;
2774 }
2775
2776 return false;
2777}
2778
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002779/// ParseDirectiveElseIf
2780/// ::= .elseif expression
2781bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2782 if (TheCondState.TheCond != AsmCond::IfCond &&
2783 TheCondState.TheCond != AsmCond::ElseIfCond)
2784 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2785 " an .elseif");
2786 TheCondState.TheCond = AsmCond::ElseIfCond;
2787
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002788 bool LastIgnoreState = false;
2789 if (!TheCondStack.empty())
2790 LastIgnoreState = TheCondStack.back().Ignore;
2791 if (LastIgnoreState || TheCondState.CondMet) {
2792 TheCondState.Ignore = true;
2793 EatToEndOfStatement();
2794 }
2795 else {
2796 int64_t ExprValue;
2797 if (ParseAbsoluteExpression(ExprValue))
2798 return true;
2799
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002800 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002801 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002802
Sean Callanan79ed1a82010-01-19 20:22:31 +00002803 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002804 TheCondState.CondMet = ExprValue;
2805 TheCondState.Ignore = !TheCondState.CondMet;
2806 }
2807
2808 return false;
2809}
2810
2811/// ParseDirectiveElse
2812/// ::= .else
2813bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002814 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002815 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002816
Sean Callanan79ed1a82010-01-19 20:22:31 +00002817 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002818
2819 if (TheCondState.TheCond != AsmCond::IfCond &&
2820 TheCondState.TheCond != AsmCond::ElseIfCond)
2821 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2822 ".elseif");
2823 TheCondState.TheCond = AsmCond::ElseCond;
2824 bool LastIgnoreState = false;
2825 if (!TheCondStack.empty())
2826 LastIgnoreState = TheCondStack.back().Ignore;
2827 if (LastIgnoreState || TheCondState.CondMet)
2828 TheCondState.Ignore = true;
2829 else
2830 TheCondState.Ignore = false;
2831
2832 return false;
2833}
2834
2835/// ParseDirectiveEndIf
2836/// ::= .endif
2837bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002838 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002839 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002840
Sean Callanan79ed1a82010-01-19 20:22:31 +00002841 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002842
2843 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2844 TheCondStack.empty())
2845 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2846 ".else");
2847 if (!TheCondStack.empty()) {
2848 TheCondState = TheCondStack.back();
2849 TheCondStack.pop_back();
2850 }
2851
2852 return false;
2853}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002854
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002855void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002856 DirectiveKindMapping[".set"] = DK_SET;
2857 DirectiveKindMapping[".equ"] = DK_EQU;
2858 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2859 DirectiveKindMapping[".ascii"] = DK_ASCII;
2860 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2861 DirectiveKindMapping[".string"] = DK_STRING;
2862 DirectiveKindMapping[".byte"] = DK_BYTE;
2863 DirectiveKindMapping[".short"] = DK_SHORT;
2864 DirectiveKindMapping[".value"] = DK_VALUE;
2865 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2866 DirectiveKindMapping[".long"] = DK_LONG;
2867 DirectiveKindMapping[".int"] = DK_INT;
2868 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2869 DirectiveKindMapping[".quad"] = DK_QUAD;
2870 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2871 DirectiveKindMapping[".single"] = DK_SINGLE;
2872 DirectiveKindMapping[".float"] = DK_FLOAT;
2873 DirectiveKindMapping[".double"] = DK_DOUBLE;
2874 DirectiveKindMapping[".align"] = DK_ALIGN;
2875 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2876 DirectiveKindMapping[".balign"] = DK_BALIGN;
2877 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2878 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2879 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2880 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2881 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2882 DirectiveKindMapping[".org"] = DK_ORG;
2883 DirectiveKindMapping[".fill"] = DK_FILL;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002884 DirectiveKindMapping[".zero"] = DK_ZERO;
2885 DirectiveKindMapping[".extern"] = DK_EXTERN;
2886 DirectiveKindMapping[".globl"] = DK_GLOBL;
2887 DirectiveKindMapping[".global"] = DK_GLOBAL;
2888 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2889 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2890 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2891 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2892 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2893 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2894 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2895 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2896 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2897 DirectiveKindMapping[".comm"] = DK_COMM;
2898 DirectiveKindMapping[".common"] = DK_COMMON;
2899 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2900 DirectiveKindMapping[".abort"] = DK_ABORT;
2901 DirectiveKindMapping[".include"] = DK_INCLUDE;
2902 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2903 DirectiveKindMapping[".code16"] = DK_CODE16;
2904 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2905 DirectiveKindMapping[".rept"] = DK_REPT;
2906 DirectiveKindMapping[".irp"] = DK_IRP;
2907 DirectiveKindMapping[".irpc"] = DK_IRPC;
2908 DirectiveKindMapping[".endr"] = DK_ENDR;
2909 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2910 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2911 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2912 DirectiveKindMapping[".if"] = DK_IF;
2913 DirectiveKindMapping[".ifb"] = DK_IFB;
2914 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2915 DirectiveKindMapping[".ifc"] = DK_IFC;
2916 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2917 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2918 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2919 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2920 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2921 DirectiveKindMapping[".else"] = DK_ELSE;
2922 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002923}
2924
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002925/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002926/// ::= .file [number] filename
2927/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002928bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002929 // FIXME: I'm not sure what this is.
2930 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002931 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002932 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002933 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002934 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002935
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002936 if (FileNumber < 1)
2937 return TokError("file number less than one");
2938 }
2939
Daniel Dunbareceec052010-07-12 17:45:27 +00002940 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002941 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002942
Nick Lewycky44d798d2011-10-17 23:05:28 +00002943 // Usually the directory and filename together, otherwise just the directory.
2944 StringRef Path = getTok().getString();
2945 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002946 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002947
Nick Lewycky44d798d2011-10-17 23:05:28 +00002948 StringRef Directory;
2949 StringRef Filename;
2950 if (getLexer().is(AsmToken::String)) {
2951 if (FileNumber == -1)
2952 return TokError("explicit path specified, but no file number");
2953 Filename = getTok().getString();
2954 Filename = Filename.substr(1, Filename.size()-2);
2955 Directory = Path;
2956 Lex();
2957 } else {
2958 Filename = Path;
2959 }
2960
Daniel Dunbareceec052010-07-12 17:45:27 +00002961 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002962 return TokError("unexpected token in '.file' directive");
2963
Chris Lattnerd32e8032010-01-25 19:02:58 +00002964 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002965 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002966 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002967 if (getContext().getGenDwarfForAssembly() == true)
2968 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2969 "used to generate dwarf debug info for assembly code");
2970
Nick Lewycky44d798d2011-10-17 23:05:28 +00002971 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002972 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002973 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002974
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002975 return false;
2976}
2977
2978/// ParseDirectiveLine
2979/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002980bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002981 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2982 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002983 return TokError("unexpected token in '.line' directive");
2984
Sean Callanan18b83232010-01-19 21:44:56 +00002985 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002986 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002987 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002988
2989 // FIXME: Do something with the .line.
2990 }
2991
Daniel Dunbareceec052010-07-12 17:45:27 +00002992 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002993 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002994
2995 return false;
2996}
2997
2998
2999/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003000/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003001/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3002/// The first number is a file number, must have been previously assigned with
3003/// a .file directive, the second number is the line number and optionally the
3004/// third number is a column position (zero if not specified). The remaining
3005/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003006bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003007
Daniel Dunbareceec052010-07-12 17:45:27 +00003008 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003009 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003010 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003011 if (FileNumber < 1)
3012 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003013 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003014 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003015 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003016
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003017 int64_t LineNumber = 0;
3018 if (getLexer().is(AsmToken::Integer)) {
3019 LineNumber = getTok().getIntVal();
3020 if (LineNumber < 1)
3021 return TokError("line number less than one in '.loc' directive");
3022 Lex();
3023 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003024
3025 int64_t ColumnPos = 0;
3026 if (getLexer().is(AsmToken::Integer)) {
3027 ColumnPos = getTok().getIntVal();
3028 if (ColumnPos < 0)
3029 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003030 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003031 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003032
Kevin Enderbyc0957932010-09-30 16:52:03 +00003033 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003034 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003035 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003036 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3037 for (;;) {
3038 if (getLexer().is(AsmToken::EndOfStatement))
3039 break;
3040
3041 StringRef Name;
3042 SMLoc Loc = getTok().getLoc();
3043 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003044 return TokError("unexpected token in '.loc' directive");
3045
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003046 if (Name == "basic_block")
3047 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3048 else if (Name == "prologue_end")
3049 Flags |= DWARF2_FLAG_PROLOGUE_END;
3050 else if (Name == "epilogue_begin")
3051 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3052 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003053 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003054 const MCExpr *Value;
3055 if (getParser().ParseExpression(Value))
3056 return true;
3057 // The expression must be the constant 0 or 1.
3058 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3059 int Value = MCE->getValue();
3060 if (Value == 0)
3061 Flags &= ~DWARF2_FLAG_IS_STMT;
3062 else if (Value == 1)
3063 Flags |= DWARF2_FLAG_IS_STMT;
3064 else
3065 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003066 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003067 else {
3068 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3069 }
3070 }
3071 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003072 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003073 const MCExpr *Value;
3074 if (getParser().ParseExpression(Value))
3075 return true;
3076 // The expression must be a constant greater or equal to 0.
3077 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3078 int Value = MCE->getValue();
3079 if (Value < 0)
3080 return Error(Loc, "isa number less than zero");
3081 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003082 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003083 else {
3084 return Error(Loc, "isa number not a constant value");
3085 }
3086 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003087 else if (Name == "discriminator") {
3088 if (getParser().ParseAbsoluteExpression(Discriminator))
3089 return true;
3090 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003091 else {
3092 return Error(Loc, "unknown sub-directive in '.loc' directive");
3093 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003094
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003095 if (getLexer().is(AsmToken::EndOfStatement))
3096 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003097 }
3098 }
3099
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003100 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003101 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003102
3103 return false;
3104}
3105
Daniel Dunbar138abae2010-10-16 04:56:42 +00003106/// ParseDirectiveStabs
3107/// ::= .stabs string, number, number, number
3108bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3109 SMLoc DirectiveLoc) {
3110 return TokError("unsupported directive '" + Directive + "'");
3111}
3112
Eli Bendersky9b1bb052013-01-11 22:55:28 +00003113/// ParseDirectiveSpace
3114/// ::= .space expression [ , expression ]
3115bool GenericAsmParser::ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc) {
3116 getParser().CheckForValidSection();
3117
3118 int64_t NumBytes;
3119 if (getParser().ParseAbsoluteExpression(NumBytes))
3120 return true;
3121
3122 int64_t FillExpr = 0;
3123 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3124 if (getLexer().isNot(AsmToken::Comma))
3125 return TokError("unexpected token in '.space' directive");
3126 Lex();
3127
3128 if (getParser().ParseAbsoluteExpression(FillExpr))
3129 return true;
3130
3131 if (getLexer().isNot(AsmToken::EndOfStatement))
3132 return TokError("unexpected token in '.space' directive");
3133 }
3134
3135 Lex();
3136
3137 if (NumBytes <= 0)
3138 return TokError("invalid number of bytes in '.space' directive");
3139
3140 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3141 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
3142
3143 return false;
3144}
3145
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003146/// ParseDirectiveCFISections
3147/// ::= .cfi_sections section [, section]
3148bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3149 SMLoc DirectiveLoc) {
3150 StringRef Name;
3151 bool EH = false;
3152 bool Debug = false;
3153
3154 if (getParser().ParseIdentifier(Name))
3155 return TokError("Expected an identifier");
3156
3157 if (Name == ".eh_frame")
3158 EH = true;
3159 else if (Name == ".debug_frame")
3160 Debug = true;
3161
3162 if (getLexer().is(AsmToken::Comma)) {
3163 Lex();
3164
3165 if (getParser().ParseIdentifier(Name))
3166 return TokError("Expected an identifier");
3167
3168 if (Name == ".eh_frame")
3169 EH = true;
3170 else if (Name == ".debug_frame")
3171 Debug = true;
3172 }
3173
3174 getStreamer().EmitCFISections(EH, Debug);
3175
3176 return false;
3177}
3178
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003179/// ParseDirectiveCFIStartProc
3180/// ::= .cfi_startproc
3181bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3182 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003183 getStreamer().EmitCFIStartProc();
3184 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003185}
3186
3187/// ParseDirectiveCFIEndProc
3188/// ::= .cfi_endproc
3189bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003190 getStreamer().EmitCFIEndProc();
3191 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003192}
3193
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003194/// ParseRegisterOrRegisterNumber - parse register name or number.
3195bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3196 SMLoc DirectiveLoc) {
3197 unsigned RegNo;
3198
Jim Grosbach6f888a82011-06-02 17:14:04 +00003199 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003200 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3201 DirectiveLoc))
3202 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003203 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003204 } else
3205 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003206
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003207 return false;
3208}
3209
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003210/// ParseDirectiveCFIDefCfa
3211/// ::= .cfi_def_cfa register, offset
3212bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3213 SMLoc DirectiveLoc) {
3214 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003215 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003216 return true;
3217
3218 if (getLexer().isNot(AsmToken::Comma))
3219 return TokError("unexpected token in directive");
3220 Lex();
3221
3222 int64_t Offset = 0;
3223 if (getParser().ParseAbsoluteExpression(Offset))
3224 return true;
3225
Rafael Espindola066c2f42011-04-12 23:59:07 +00003226 getStreamer().EmitCFIDefCfa(Register, Offset);
3227 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003228}
3229
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003230/// ParseDirectiveCFIDefCfaOffset
3231/// ::= .cfi_def_cfa_offset offset
3232bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3233 SMLoc DirectiveLoc) {
3234 int64_t Offset = 0;
3235 if (getParser().ParseAbsoluteExpression(Offset))
3236 return true;
3237
Rafael Espindola066c2f42011-04-12 23:59:07 +00003238 getStreamer().EmitCFIDefCfaOffset(Offset);
3239 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003240}
3241
3242/// ParseDirectiveCFIAdjustCfaOffset
3243/// ::= .cfi_adjust_cfa_offset adjustment
3244bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3245 SMLoc DirectiveLoc) {
3246 int64_t Adjustment = 0;
3247 if (getParser().ParseAbsoluteExpression(Adjustment))
3248 return true;
3249
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003250 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3251 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003252}
3253
3254/// ParseDirectiveCFIDefCfaRegister
3255/// ::= .cfi_def_cfa_register register
3256bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3257 SMLoc DirectiveLoc) {
3258 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003259 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003260 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003261
Rafael Espindola066c2f42011-04-12 23:59:07 +00003262 getStreamer().EmitCFIDefCfaRegister(Register);
3263 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003264}
3265
3266/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003267/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003268bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3269 int64_t Register = 0;
3270 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003271
3272 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003273 return true;
3274
3275 if (getLexer().isNot(AsmToken::Comma))
3276 return TokError("unexpected token in directive");
3277 Lex();
3278
3279 if (getParser().ParseAbsoluteExpression(Offset))
3280 return true;
3281
Rafael Espindola066c2f42011-04-12 23:59:07 +00003282 getStreamer().EmitCFIOffset(Register, Offset);
3283 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003284}
3285
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003286/// ParseDirectiveCFIRelOffset
3287/// ::= .cfi_rel_offset register, offset
3288bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3289 SMLoc DirectiveLoc) {
3290 int64_t Register = 0;
3291
3292 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3293 return true;
3294
3295 if (getLexer().isNot(AsmToken::Comma))
3296 return TokError("unexpected token in directive");
3297 Lex();
3298
3299 int64_t Offset = 0;
3300 if (getParser().ParseAbsoluteExpression(Offset))
3301 return true;
3302
Rafael Espindola25f492e2011-04-12 16:12:03 +00003303 getStreamer().EmitCFIRelOffset(Register, Offset);
3304 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003305}
3306
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003307static bool isValidEncoding(int64_t Encoding) {
3308 if (Encoding & ~0xff)
3309 return false;
3310
3311 if (Encoding == dwarf::DW_EH_PE_omit)
3312 return true;
3313
3314 const unsigned Format = Encoding & 0xf;
3315 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3316 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3317 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3318 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3319 return false;
3320
Rafael Espindolacaf11582010-12-29 04:31:26 +00003321 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003322 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003323 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003324 return false;
3325
3326 return true;
3327}
3328
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003329/// ParseDirectiveCFIPersonalityOrLsda
3330/// ::= .cfi_personality encoding, [symbol_name]
3331/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003332bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003333 SMLoc DirectiveLoc) {
3334 int64_t Encoding = 0;
3335 if (getParser().ParseAbsoluteExpression(Encoding))
3336 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003337 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003338 return false;
3339
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003340 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003341 return TokError("unsupported encoding.");
3342
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003343 if (getLexer().isNot(AsmToken::Comma))
3344 return TokError("unexpected token in directive");
3345 Lex();
3346
3347 StringRef Name;
3348 if (getParser().ParseIdentifier(Name))
3349 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003350
3351 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3352
3353 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003354 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003355 else {
3356 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003357 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003358 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003359 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003360}
3361
Rafael Espindolafe024d02010-12-28 18:36:23 +00003362/// ParseDirectiveCFIRememberState
3363/// ::= .cfi_remember_state
3364bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3365 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003366 getStreamer().EmitCFIRememberState();
3367 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003368}
3369
3370/// ParseDirectiveCFIRestoreState
3371/// ::= .cfi_remember_state
3372bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3373 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003374 getStreamer().EmitCFIRestoreState();
3375 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003376}
3377
Rafael Espindolac5754392011-04-12 15:31:05 +00003378/// ParseDirectiveCFISameValue
3379/// ::= .cfi_same_value register
3380bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3381 SMLoc DirectiveLoc) {
3382 int64_t Register = 0;
3383
3384 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3385 return true;
3386
3387 getStreamer().EmitCFISameValue(Register);
3388
3389 return false;
3390}
3391
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003392/// ParseDirectiveCFIRestore
3393/// ::= .cfi_restore register
3394bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003395 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003396 int64_t Register = 0;
3397 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3398 return true;
3399
3400 getStreamer().EmitCFIRestore(Register);
3401
3402 return false;
3403}
3404
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003405/// ParseDirectiveCFIEscape
3406/// ::= .cfi_escape expression[,...]
3407bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003408 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003409 std::string Values;
3410 int64_t CurrValue;
3411 if (getParser().ParseAbsoluteExpression(CurrValue))
3412 return true;
3413
3414 Values.push_back((uint8_t)CurrValue);
3415
3416 while (getLexer().is(AsmToken::Comma)) {
3417 Lex();
3418
3419 if (getParser().ParseAbsoluteExpression(CurrValue))
3420 return true;
3421
3422 Values.push_back((uint8_t)CurrValue);
3423 }
3424
3425 getStreamer().EmitCFIEscape(Values);
3426 return false;
3427}
3428
Rafael Espindola16d7d432012-01-23 21:51:52 +00003429/// ParseDirectiveCFISignalFrame
3430/// ::= .cfi_signal_frame
3431bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3432 SMLoc DirectiveLoc) {
3433 if (getLexer().isNot(AsmToken::EndOfStatement))
3434 return Error(getLexer().getLoc(),
3435 "unexpected token in '" + Directive + "' directive");
3436
3437 getStreamer().EmitCFISignalFrame();
3438
3439 return false;
3440}
3441
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003442/// ParseDirectiveCFIUndefined
3443/// ::= .cfi_undefined register
3444bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3445 SMLoc DirectiveLoc) {
3446 int64_t Register = 0;
3447
3448 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3449 return true;
3450
3451 getStreamer().EmitCFIUndefined(Register);
3452
3453 return false;
3454}
3455
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003456/// ParseDirectiveCFIRegister
3457/// ::= .cfi_register register, register
3458bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3459 SMLoc DirectiveLoc) {
3460 int64_t Register1 = 0;
3461
3462 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3463 return true;
3464
3465 if (getLexer().isNot(AsmToken::Comma))
3466 return TokError("unexpected token in directive");
3467 Lex();
3468
3469 int64_t Register2 = 0;
3470
3471 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3472 return true;
3473
3474 getStreamer().EmitCFIRegister(Register1, Register2);
3475
3476 return false;
3477}
3478
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003479/// ParseDirectiveMacrosOnOff
3480/// ::= .macros_on
3481/// ::= .macros_off
3482bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3483 SMLoc DirectiveLoc) {
3484 if (getLexer().isNot(AsmToken::EndOfStatement))
3485 return Error(getLexer().getLoc(),
3486 "unexpected token in '" + Directive + "' directive");
3487
Eli Bendersky733c3362013-01-14 18:08:41 +00003488 getParser().SetMacrosEnabled(Directive == ".macros_on");
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003489
3490 return false;
3491}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003492
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003493/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003494/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003495bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3496 SMLoc DirectiveLoc) {
3497 StringRef Name;
3498 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003499 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003500
Rafael Espindola8a403d32012-08-08 14:51:03 +00003501 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003502 // Argument delimiter is initially unknown. It will be set by
3503 // ParseMacroArgument()
3504 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003505 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003506 for (;;) {
3507 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003508 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003509 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003510
3511 if (getLexer().is(AsmToken::Equal)) {
3512 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003513 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003514 return true;
3515 }
3516
Rafael Espindola65366442011-06-05 02:43:45 +00003517 Parameters.push_back(Parameter);
3518
Preston Gurd7b6f2032012-09-19 20:36:12 +00003519 if (getLexer().is(AsmToken::Comma))
3520 Lex();
3521 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003522 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003523 }
3524 }
3525
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003526 // Eat the end of statement.
3527 Lex();
3528
3529 AsmToken EndToken, StartToken = getTok();
3530
3531 // Lex the macro definition.
3532 for (;;) {
3533 // Check whether we have reached the end of the file.
3534 if (getLexer().is(AsmToken::Eof))
3535 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3536
3537 // Otherwise, check whether we have reach the .endmacro.
3538 if (getLexer().is(AsmToken::Identifier) &&
3539 (getTok().getIdentifier() == ".endm" ||
3540 getTok().getIdentifier() == ".endmacro")) {
3541 EndToken = getTok();
3542 Lex();
3543 if (getLexer().isNot(AsmToken::EndOfStatement))
3544 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3545 "' directive");
3546 break;
3547 }
3548
3549 // Otherwise, scan til the end of the statement.
3550 getParser().EatToEndOfStatement();
3551 }
3552
3553 if (getParser().MacroMap.lookup(Name)) {
3554 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3555 }
3556
3557 const char *BodyStart = StartToken.getLoc().getPointer();
3558 const char *BodyEnd = EndToken.getLoc().getPointer();
3559 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003560 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003561 return false;
3562}
3563
3564/// ParseDirectiveEndMacro
3565/// ::= .endm
3566/// ::= .endmacro
3567bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003568 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003569 if (getLexer().isNot(AsmToken::EndOfStatement))
3570 return TokError("unexpected token in '" + Directive + "' directive");
3571
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003572 // If we are inside a macro instantiation, terminate the current
3573 // instantiation.
3574 if (!getParser().ActiveMacros.empty()) {
3575 getParser().HandleMacroExit();
3576 return false;
3577 }
3578
3579 // Otherwise, this .endmacro is a stray entry in the file; well formed
3580 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003581 return TokError("unexpected '" + Directive + "' in file, "
3582 "no current macro definition");
3583}
3584
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003585/// ParseDirectivePurgeMacro
3586/// ::= .purgem
3587bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3588 SMLoc DirectiveLoc) {
3589 StringRef Name;
3590 if (getParser().ParseIdentifier(Name))
3591 return TokError("expected identifier in '.purgem' directive");
3592
3593 if (getLexer().isNot(AsmToken::EndOfStatement))
3594 return TokError("unexpected token in '.purgem' directive");
3595
3596 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3597 if (I == getParser().MacroMap.end())
3598 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3599
3600 // Undefine the macro.
3601 delete I->getValue();
3602 getParser().MacroMap.erase(I);
3603 return false;
3604}
3605
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003606bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003607 getParser().CheckForValidSection();
3608
3609 const MCExpr *Value;
3610
3611 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003612 return true;
3613
3614 if (getLexer().isNot(AsmToken::EndOfStatement))
3615 return TokError("unexpected token in directive");
3616
3617 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003618 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003619 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003620 getStreamer().EmitULEB128Value(Value);
3621
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003622 return false;
3623}
3624
Rafael Espindola761cb062012-06-03 23:57:14 +00003625Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003626 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003627
Rafael Espindola761cb062012-06-03 23:57:14 +00003628 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003629 for (;;) {
3630 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003631 if (getLexer().is(AsmToken::Eof)) {
3632 Error(DirectiveLoc, "no matching '.endr' in definition");
3633 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003634 }
3635
Rafael Espindola761cb062012-06-03 23:57:14 +00003636 if (Lexer.is(AsmToken::Identifier) &&
3637 (getTok().getIdentifier() == ".rept")) {
3638 ++NestLevel;
3639 }
3640
3641 // Otherwise, check whether we have reached the .endr.
3642 if (Lexer.is(AsmToken::Identifier) &&
3643 getTok().getIdentifier() == ".endr") {
3644 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003645 EndToken = getTok();
3646 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003647 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3648 TokError("unexpected token in '.endr' directive");
3649 return 0;
3650 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003651 break;
3652 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003653 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003654 }
3655
Rafael Espindola761cb062012-06-03 23:57:14 +00003656 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003657 EatToEndOfStatement();
3658 }
3659
3660 const char *BodyStart = StartToken.getLoc().getPointer();
3661 const char *BodyEnd = EndToken.getLoc().getPointer();
3662 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3663
Rafael Espindola761cb062012-06-03 23:57:14 +00003664 // We Are Anonymous.
3665 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003666 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003667 return new Macro(Name, Body, Parameters);
3668}
3669
3670void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3671 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003672 OS << ".endr\n";
3673
3674 MemoryBuffer *Instantiation =
3675 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3676
Rafael Espindola761cb062012-06-03 23:57:14 +00003677 // Create the macro instantiation object and add to the current macro
3678 // instantiation stack.
3679 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00003680 CurBuffer,
Rafael Espindola761cb062012-06-03 23:57:14 +00003681 getTok().getLoc(),
3682 Instantiation);
3683 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003684
Rafael Espindola761cb062012-06-03 23:57:14 +00003685 // Jump to the macro instantiation and prime the lexer.
3686 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3687 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3688 Lex();
3689}
3690
3691bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3692 int64_t Count;
3693 if (ParseAbsoluteExpression(Count))
3694 return TokError("unexpected token in '.rept' directive");
3695
3696 if (Count < 0)
3697 return TokError("Count is negative");
3698
3699 if (Lexer.isNot(AsmToken::EndOfStatement))
3700 return TokError("unexpected token in '.rept' directive");
3701
3702 // Eat the end of statement.
3703 Lex();
3704
3705 // Lex the rept definition.
3706 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3707 if (!M)
3708 return true;
3709
3710 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3711 // to hold the macro body with substitutions.
3712 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003713 MacroParameters Parameters;
3714 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003715 raw_svector_ostream OS(Buf);
3716 while (Count--) {
3717 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3718 return true;
3719 }
3720 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003721
3722 return false;
3723}
3724
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003725/// ParseDirectiveIrp
3726/// ::= .irp symbol,values
3727bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003728 MacroParameters Parameters;
3729 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003730
Preston Gurd6c9176a2012-09-19 20:29:04 +00003731 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003732 return TokError("expected identifier in '.irp' directive");
3733
3734 Parameters.push_back(Parameter);
3735
3736 if (Lexer.isNot(AsmToken::Comma))
3737 return TokError("expected comma in '.irp' directive");
3738
3739 Lex();
3740
Rafael Espindola8a403d32012-08-08 14:51:03 +00003741 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003742 if (ParseMacroArguments(0, A))
3743 return true;
3744
3745 // Eat the end of statement.
3746 Lex();
3747
3748 // Lex the irp definition.
3749 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3750 if (!M)
3751 return true;
3752
3753 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3754 // to hold the macro body with substitutions.
3755 SmallString<256> Buf;
3756 raw_svector_ostream OS(Buf);
3757
Rafael Espindola7996d042012-08-21 16:06:48 +00003758 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3759 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003760 Args.push_back(*i);
3761
3762 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3763 return true;
3764 }
3765
3766 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3767
3768 return false;
3769}
3770
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003771/// ParseDirectiveIrpc
3772/// ::= .irpc symbol,values
3773bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003774 MacroParameters Parameters;
3775 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003776
Preston Gurd6c9176a2012-09-19 20:29:04 +00003777 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003778 return TokError("expected identifier in '.irpc' directive");
3779
3780 Parameters.push_back(Parameter);
3781
3782 if (Lexer.isNot(AsmToken::Comma))
3783 return TokError("expected comma in '.irpc' directive");
3784
3785 Lex();
3786
Rafael Espindola8a403d32012-08-08 14:51:03 +00003787 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003788 if (ParseMacroArguments(0, A))
3789 return true;
3790
3791 if (A.size() != 1 || A.front().size() != 1)
3792 return TokError("unexpected token in '.irpc' directive");
3793
3794 // Eat the end of statement.
3795 Lex();
3796
3797 // Lex the irpc definition.
3798 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3799 if (!M)
3800 return true;
3801
3802 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3803 // to hold the macro body with substitutions.
3804 SmallString<256> Buf;
3805 raw_svector_ostream OS(Buf);
3806
3807 StringRef Values = A.front().front().getString();
3808 std::size_t I, End = Values.size();
3809 for (I = 0; I < End; ++I) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00003810 MCAsmMacroArgument Arg;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003811 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3812
Rafael Espindola8a403d32012-08-08 14:51:03 +00003813 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003814 Args.push_back(Arg);
3815
3816 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3817 return true;
3818 }
3819
3820 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3821
3822 return false;
3823}
3824
Rafael Espindola761cb062012-06-03 23:57:14 +00003825bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3826 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003827 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003828
3829 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003830 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003831 assert(getLexer().is(AsmToken::EndOfStatement));
3832
Rafael Espindola761cb062012-06-03 23:57:14 +00003833 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003834 return false;
3835}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003836
Eli Friedman2128aae2012-10-22 23:58:19 +00003837bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) {
3838 const MCExpr *Value;
3839 SMLoc ExprLoc = getLexer().getLoc();
3840 if (ParseExpression(Value))
3841 return true;
3842 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3843 if (!MCE)
3844 return Error(ExprLoc, "unexpected expression in _emit");
3845 uint64_t IntValue = MCE->getValue();
3846 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
3847 return Error(ExprLoc, "literal value out of range for directive");
3848
3849 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5));
3850 return false;
3851}
3852
Chad Rosierb1f8c132012-10-18 15:49:34 +00003853bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3854 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003855 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003856 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003857 SmallVectorImpl<std::string> &Clobbers,
3858 const MCInstrInfo *MII,
3859 const MCInstPrinter *IP,
3860 MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003861 SmallVector<void *, 4> InputDecls;
3862 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003863 SmallVector<bool, 4> InputDeclsAddressOf;
3864 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003865 SmallVector<std::string, 4> InputConstraints;
3866 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003867 std::set<std::string> ClobberRegs;
3868
Chad Rosier4e472d22012-10-20 01:02:45 +00003869 SmallVector<struct AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003870
3871 // Prime the lexer.
3872 Lex();
3873
3874 // While we have input, parse each statement.
3875 unsigned InputIdx = 0;
3876 unsigned OutputIdx = 0;
3877 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00003878 ParseStatementInfo Info(&AsmStrRewrites);
3879 if (ParseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00003880 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003881
Chad Rosier57498012012-12-12 22:45:52 +00003882 if (Info.ParseError)
3883 return true;
3884
Eli Friedman2128aae2012-10-22 23:58:19 +00003885 if (Info.Opcode != ~0U) {
3886 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003887
3888 // Build the list of clobbers, outputs and inputs.
Eli Friedman2128aae2012-10-22 23:58:19 +00003889 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
3890 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003891
3892 // Immediate.
3893 if (Operand->isImm()) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00003894 if (Operand->needAsmRewrite())
3895 AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix,
3896 Operand->getStartLoc()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003897 continue;
3898 }
3899
3900 // Register operand.
Chad Rosierc1ec2072013-01-10 22:10:27 +00003901 if (Operand->isReg() && !Operand->needAddressOf()) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003902 unsigned NumDefs = Desc.getNumDefs();
3903 // Clobber.
3904 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3905 std::string Reg;
3906 raw_string_ostream OS(Reg);
3907 IP->printRegName(OS, Operand->getReg());
3908 ClobberRegs.insert(StringRef(OS.str()));
3909 }
3910 continue;
3911 }
3912
3913 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003914 unsigned Size;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003915 bool IsVarDecl;
Chad Rosier32989592012-10-18 20:27:15 +00003916 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +00003917 Size, IsVarDecl);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003918 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003919 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierc1ec2072013-01-10 22:10:27 +00003920 if (Operand->isMem() && Operand->needSizeDirective())
Chad Rosier4e472d22012-10-20 01:02:45 +00003921 AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective,
Chad Rosierefcb3d92012-10-26 18:04:20 +00003922 Operand->getStartLoc(),
3923 /*Len*/0,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003924 Operand->getMemSize()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003925 if (isOutput) {
3926 std::string Constraint = "=";
3927 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003928 OutputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003929 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003930 Constraint += Operand->getConstraint().str();
3931 OutputConstraints.push_back(Constraint);
Chad Rosier4e472d22012-10-20 01:02:45 +00003932 AsmStrRewrites.push_back(AsmRewrite(AOK_Output,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003933 Operand->getStartLoc(),
3934 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003935 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003936 InputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003937 InputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003938 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosier4e472d22012-10-20 01:02:45 +00003939 AsmStrRewrites.push_back(AsmRewrite(AOK_Input,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003940 Operand->getStartLoc(),
3941 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003942 }
3943 }
3944 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003945 }
3946 }
3947
3948 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003949 NumOutputs = OutputDecls.size();
3950 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003951
3952 // Set the unique clobbers.
3953 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3954 E = ClobberRegs.end(); I != E; ++I)
3955 Clobbers.push_back(*I);
3956
3957 // Merge the various outputs and inputs. Output are expected first.
3958 if (NumOutputs || NumInputs) {
3959 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003960 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003961 Constraints.resize(NumExprs);
Chad Rosier5a719fc2012-10-23 17:43:43 +00003962 // FIXME: Constraints are hard coded to 'm', but we need an 'r'
Chad Rosierc1ec2072013-01-10 22:10:27 +00003963 // constraint for addressof. This needs to be cleaned up!
Chad Rosierb1f8c132012-10-18 15:49:34 +00003964 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003965 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
3966 Constraints[i] = OutputDeclsAddressOf[i] ? "=r" : OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003967 }
3968 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003969 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
3970 Constraints[j] = InputDeclsAddressOf[i] ? "r" : InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003971 }
3972 }
3973
3974 // Build the IR assembly string.
3975 std::string AsmStringIR;
Chad Rosier4e472d22012-10-20 01:02:45 +00003976 AsmRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003977 raw_string_ostream OS(AsmStringIR);
3978 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
Chad Rosier4e472d22012-10-20 01:02:45 +00003979 for (SmallVectorImpl<struct AsmRewrite>::iterator
Chad Rosierb1f8c132012-10-18 15:49:34 +00003980 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3981 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003982
Chad Rosier4e472d22012-10-20 01:02:45 +00003983 AsmRewriteKind Kind = (*I).Kind;
Chad Rosier96d58e62012-10-19 20:57:14 +00003984
3985 // Emit everything up to the immediate/expression. If the previous rewrite
3986 // was a size directive, then this has already been done.
3987 if (PrevKind != AOK_SizeDirective)
3988 OS << StringRef(Start, Loc - Start);
3989 PrevKind = Kind;
3990
Chad Rosier5a719fc2012-10-23 17:43:43 +00003991 // Skip the original expression.
3992 if (Kind == AOK_Skip) {
3993 Start = Loc + (*I).Len;
3994 continue;
3995 }
3996
Chad Rosierb1f8c132012-10-18 15:49:34 +00003997 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00003998 switch (Kind) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003999 default: break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004000 case AOK_Imm:
Chad Rosierefcb3d92012-10-26 18:04:20 +00004001 OS << Twine("$$");
4002 OS << (*I).Val;
4003 break;
4004 case AOK_ImmPrefix:
4005 OS << Twine("$$");
Chad Rosierb1f8c132012-10-18 15:49:34 +00004006 break;
4007 case AOK_Input:
4008 OS << '$';
4009 OS << InputIdx++;
4010 break;
4011 case AOK_Output:
4012 OS << '$';
4013 OS << OutputIdx++;
4014 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004015 case AOK_SizeDirective:
Chad Rosier6a020a72012-10-25 20:41:34 +00004016 switch((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004017 default: break;
4018 case 8: OS << "byte ptr "; break;
4019 case 16: OS << "word ptr "; break;
4020 case 32: OS << "dword ptr "; break;
4021 case 64: OS << "qword ptr "; break;
4022 case 80: OS << "xword ptr "; break;
4023 case 128: OS << "xmmword ptr "; break;
4024 case 256: OS << "ymmword ptr "; break;
4025 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004026 break;
4027 case AOK_Emit:
4028 OS << ".byte";
4029 break;
Chad Rosier6a020a72012-10-25 20:41:34 +00004030 case AOK_DotOperator:
4031 OS << (*I).Val;
4032 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004033 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004034
Chad Rosierb1f8c132012-10-18 15:49:34 +00004035 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00004036 if (Kind != AOK_SizeDirective)
4037 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004038 }
4039
4040 // Emit the remainder of the asm string.
4041 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4042 if (Start != AsmEnd)
4043 OS << StringRef(Start, AsmEnd - Start);
4044
4045 AsmString = OS.str();
4046 return false;
4047}
4048
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004049/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004050MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004051 MCContext &C, MCStreamer &Out,
4052 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004053 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004054}