blob: ecf35fff6614699935c790fed6926e0a7aaafe48 [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.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000054typedef std::vector<AsmToken> MacroArgument;
Rafael Espindola8a403d32012-08-08 14:51:03 +000055typedef std::vector<MacroArgument> MacroArguments;
Preston Gurd6c9176a2012-09-19 20:29:04 +000056typedef std::pair<StringRef, MacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000057typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000058
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000059struct Macro {
60 StringRef Name;
61 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000062 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000063
64public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000065 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000066 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000067};
68
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000069/// \brief Helper class for storing information about an active macro
70/// instantiation.
71struct MacroInstantiation {
72 /// The macro being instantiated.
73 const Macro *TheMacro;
74
75 /// The macro instantiation with substitutions.
76 MemoryBuffer *Instantiation;
77
78 /// The location of the instantiation.
79 SMLoc InstantiationLoc;
80
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000081 /// The buffer where parsing should resume upon instantiation completion.
82 int ExitBuffer;
83
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000084 /// The location where parsing should resume upon instantiation completion.
85 SMLoc ExitLoc;
86
87public:
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000088 MacroInstantiation(const Macro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000089 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000090};
91
Chad Rosier6a020a72012-10-25 20:41:34 +000092//struct AsmRewrite;
Eli Friedman2128aae2012-10-22 23:58:19 +000093struct ParseStatementInfo {
94 /// ParsedOperands - The parsed operands from the last parsed statement.
95 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
96
97 /// Opcode - The opcode from the last parsed instruction.
98 unsigned Opcode;
99
Chad Rosier57498012012-12-12 22:45:52 +0000100 /// Error - Was there an error parsing the inline assembly?
101 bool ParseError;
102
Eli Friedman2128aae2012-10-22 23:58:19 +0000103 SmallVectorImpl<AsmRewrite> *AsmRewrites;
104
Chad Rosier57498012012-12-12 22:45:52 +0000105 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000106 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier57498012012-12-12 22:45:52 +0000107 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000108
109 ~ParseStatementInfo() {
110 // Free any parsed operands.
111 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
112 delete ParsedOperands[i];
113 ParsedOperands.clear();
114 }
115};
116
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000117/// \brief The concrete assembly parser instance.
118class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000119 friend class GenericAsmParser;
120
Craig Topper85aadc02012-09-15 16:23:52 +0000121 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
122 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000123private:
124 AsmLexer Lexer;
125 MCContext &Ctx;
126 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000127 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000128 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000129 SourceMgr::DiagHandlerTy SavedDiagHandler;
130 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000131 MCAsmParserExtension *GenericParser;
132 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000133
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
136 int CurBuffer;
137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
141 /// DirectiveMap - This is a table handlers for directives. Each handler is
142 /// invoked after the directive identifier is read and is responsible for
143 /// parsing and validating the rest of the directive. The handler is passed
144 /// in the directive name and the location of the directive keyword.
145 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000146
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000147 /// MacroMap - Map of currently defined macros.
148 StringMap<Macro*> MacroMap;
149
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000150 /// ActiveMacros - Stack of active macro instantiations.
151 std::vector<MacroInstantiation*> ActiveMacros;
152
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000153 /// Boolean tracking whether macro substitution is enabled.
154 unsigned MacrosEnabled : 1;
155
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000156 /// Flag tracking whether any errors have been encountered.
157 unsigned HadError : 1;
158
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000159 /// The values from the last parsed cpp hash file line comment if any.
160 StringRef CppHashFilename;
161 int64_t CppHashLineNumber;
162 SMLoc CppHashLoc;
Kevin Enderby32c1a822012-11-05 21:55:41 +0000163 int CppHashBuf;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000164
Devang Patel0db58bf2012-01-31 18:14:05 +0000165 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
166 unsigned AssemblerDialect;
167
Preston Gurd7b6f2032012-09-19 20:36:12 +0000168 /// IsDarwin - is Darwin compatibility enabled?
169 bool IsDarwin;
170
Chad Rosier8f138d12012-10-15 17:19:13 +0000171 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000172 bool ParsingInlineAsm;
173
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000174public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000175 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000176 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000177 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000178
179 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
180
Craig Topper345d16d2012-08-29 05:48:09 +0000181 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
182 StringRef Directive,
183 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 DirectiveMap[Directive] = std::make_pair(Object, Handler);
185 }
186
187public:
188 /// @name MCAsmParser Interface
189 /// {
190
191 virtual SourceMgr &getSourceManager() { return SrcMgr; }
192 virtual MCAsmLexer &getLexer() { return Lexer; }
193 virtual MCContext &getContext() { return Ctx; }
194 virtual MCStreamer &getStreamer() { return Out; }
Eric Christopher2318ba12012-12-18 00:30:54 +0000195 virtual unsigned getAssemblerDialect() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000196 if (AssemblerDialect == ~0U)
Eric Christopher2318ba12012-12-18 00:30:54 +0000197 return MAI.getAssemblerDialect();
Devang Patel0db58bf2012-01-31 18:14:05 +0000198 else
199 return AssemblerDialect;
200 }
201 virtual void setAssemblerDialect(unsigned i) {
202 AssemblerDialect = i;
203 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000205 virtual bool Warning(SMLoc L, const Twine &Msg,
206 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
207 virtual bool Error(SMLoc L, const Twine &Msg,
208 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000209
Craig Topper345d16d2012-08-29 05:48:09 +0000210 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000211
Chad Rosier84125ca2012-10-13 00:26:04 +0000212 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000213 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000214
215 bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
216 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000217 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000218 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000219 SmallVectorImpl<std::string> &Clobbers,
220 const MCInstrInfo *MII,
221 const MCInstPrinter *IP,
222 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000223
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224 bool ParseExpression(const MCExpr *&Res);
225 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
226 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
227 virtual bool ParseAbsoluteExpression(int64_t &Res);
228
229 /// }
230
231private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000232 void CheckForValidSection();
233
Eli Friedman2128aae2012-10-22 23:58:19 +0000234 bool ParseStatement(ParseStatementInfo &Info);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000235 void EatToEndOfLine();
236 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000237
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000238 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000239 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000240 const MacroParameters &Parameters,
241 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000242 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000243 void HandleMacroExit();
244
245 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000246 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000247 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
248 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000249 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000250 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000251
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000252 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
253 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000254 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
255 /// This returns true on failure.
256 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000257
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000258 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000259 /// current token is not set; clients should ensure Lex() is called
260 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000261 ///
262 /// \param InBuffer If not -1, should be the known buffer id that contains the
263 /// location.
264 void JumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000265
Craig Topper345d16d2012-08-29 05:48:09 +0000266 virtual void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000267
Preston Gurd7b6f2032012-09-19 20:36:12 +0000268 bool ParseMacroArgument(MacroArgument &MA,
269 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000270 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000271
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000272 /// \brief Parse up to the end of statement and a return the contents from the
273 /// current token until the end of the statement; the current token on exit
274 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000275 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000276
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000277 /// \brief Parse until the end of a statement or a comma is encountered,
278 /// return the contents from the current token up to the end or comma.
279 StringRef ParseStringToComma();
280
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000281 bool ParseAssignment(StringRef Name, bool allow_redef,
282 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000283
284 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
285 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
286 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000287 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000288
289 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000290 /// and set \p Res to the identifier contents.
Craig Topper345d16d2012-08-29 05:48:09 +0000291 virtual bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000292
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000293 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000294
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000295 enum DirectiveKind {
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000296 DK_NO_DIRECTIVE, // Placeholder
297 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
298 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
299 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
300 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL,
301 DK_SPACE, DK_SKIP, DK_ENDR,
302 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"
319 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000320 bool ParseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000321 // ".set", ".equ", ".equiv"
322 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000323 bool ParseDirectiveOrg(); // ".org"
324 // ".align{,32}", ".p2align{,w,l}"
325 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
326
Eli Bendersky4766ef42012-12-20 19:05:53 +0000327 // ".bundle_align_mode"
328 bool ParseDirectiveBundleAlignMode();
329 // ".bundle_lock"
330 bool ParseDirectiveBundleLock();
331 // ".bundle_unlock"
332 bool ParseDirectiveBundleUnlock();
333
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000334 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
335 /// accepts a single symbol (which should be a label or an external).
336 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000337
338 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
339
340 bool ParseDirectiveAbort(); // ".abort"
341 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000342 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000343
344 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000345 // ".ifb" or ".ifnb", depending on ExpectBlank.
346 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000347 // ".ifc" or ".ifnc", depending on ExpectEqual.
348 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000349 // ".ifdef" or ".ifndef", depending on expect_defined
350 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000351 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
352 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
353 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
354
355 /// ParseEscapedString - Parse the current token as a string which may include
356 /// escaped characters and return the string contents.
357 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000358
359 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
360 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000361
Rafael Espindola761cb062012-06-03 23:57:14 +0000362 // Macro-like directives
363 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
364 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
365 raw_svector_ostream &OS);
366 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000367 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000368 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000369 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000370
Eli Friedman2128aae2012-10-22 23:58:19 +0000371 // "_emit"
372 bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000373
374 void initializeDirectiveKindMapping();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000375};
376
Eli Bendersky63e6f482013-01-10 23:32:57 +0000377/// \brief Generic implementation of directive handling, etc. which is shared
378/// (or the default, at least) for all assembler parsers.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000379class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000380 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
381 void AddDirectiveHandler(StringRef Directive) {
382 getParser().AddDirectiveHandler(this, Directive,
383 HandleDirective<GenericAsmParser, Handler>);
384 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000385public:
386 GenericAsmParser() {}
387
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000388 AsmParser &getParser() {
389 return (AsmParser&) this->MCAsmParserExtension::getParser();
390 }
391
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000392 virtual void Initialize(MCAsmParser &Parser) {
393 // Call the base implementation.
394 this->MCAsmParserExtension::Initialize(Parser);
395
396 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000397 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
398 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
399 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000400 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000401
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000402 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000403 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
404 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000405 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
406 ".cfi_startproc");
407 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
408 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000409 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
410 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000411 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
412 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000413 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
414 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000415 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
416 ".cfi_def_cfa_register");
417 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
418 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000419 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
420 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000421 AddDirectiveHandler<
422 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
423 AddDirectiveHandler<
424 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000425 AddDirectiveHandler<
426 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
427 AddDirectiveHandler<
428 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000429 AddDirectiveHandler<
430 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000431 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000432 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
433 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000434 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000435 AddDirectiveHandler<
436 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000437 AddDirectiveHandler<
438 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000439 AddDirectiveHandler<
440 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000441
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000442 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000443 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
444 ".macros_on");
445 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
446 ".macros_off");
447 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
448 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
449 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000450 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000451
452 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
453 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000454 }
455
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000456 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
457
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000458 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
459 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
460 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000461 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000462 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000463 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
464 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000465 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000466 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000467 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000468 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
469 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000470 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000471 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000472 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
473 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000474 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000475 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000476 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000477 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000478 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000479 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000480
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000481 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000482 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
483 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000484 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000485
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000486 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000487};
488
489}
490
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000491namespace llvm {
492
493extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000494extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000495extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000496
497}
498
Chris Lattneraaec2052010-01-19 19:46:13 +0000499enum { DEFAULT_ADDRSPACE = 0 };
500
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000501AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000502 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000503 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000504 GenericParser(new GenericAsmParser), PlatformParser(0),
Preston Gurd7b6f2032012-09-19 20:36:12 +0000505 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000506 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000507 // Save the old handler.
508 SavedDiagHandler = SrcMgr.getDiagHandler();
509 SavedDiagContext = SrcMgr.getDiagContext();
510 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000511 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000512 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000513
514 // Initialize the generic parser.
515 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000516
517 // Initialize the platform / file format parser.
518 //
519 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
520 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000521 if (_MAI.hasMicrosoftFastStdCallMangling()) {
522 PlatformParser = createCOFFAsmParser();
523 PlatformParser->Initialize(*this);
524 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000525 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000526 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000527 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000528 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000529 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000530 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000531 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000532
533 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000534}
535
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000536AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000537 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
538
539 // Destroy any macros.
540 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
541 ie = MacroMap.end(); it != ie; ++it)
542 delete it->getValue();
543
Daniel Dunbare4749702010-07-12 18:12:02 +0000544 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000545 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000546}
547
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000548void AsmParser::PrintMacroInstantiations() {
549 // Print the active macro instantiation stack.
550 for (std::vector<MacroInstantiation*>::const_reverse_iterator
551 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000552 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
553 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000554}
555
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000556bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000557 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000558 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000559 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000560 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000561 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000562}
563
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000564bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000565 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000566 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000567 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000568 return true;
569}
570
Sean Callananfd0b0282010-01-21 00:19:58 +0000571bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000572 std::string IncludedFile;
573 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000574 if (NewBuf == -1)
575 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000576
Sean Callananfd0b0282010-01-21 00:19:58 +0000577 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000578
Sean Callananfd0b0282010-01-21 00:19:58 +0000579 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000580
Sean Callananfd0b0282010-01-21 00:19:58 +0000581 return false;
582}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000583
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000584/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000585/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000586/// returns true on failure.
587bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
588 std::string IncludedFile;
589 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
590 if (NewBuf == -1)
591 return true;
592
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000593 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000594 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
595 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000596 return false;
597}
598
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000599void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
600 if (InBuffer != -1) {
601 CurBuffer = InBuffer;
602 } else {
603 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
604 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000605 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
606}
607
Sean Callananfd0b0282010-01-21 00:19:58 +0000608const AsmToken &AsmParser::Lex() {
609 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000610
Sean Callananfd0b0282010-01-21 00:19:58 +0000611 if (tok->is(AsmToken::Eof)) {
612 // If this is the end of an included file, pop the parent file off the
613 // include stack.
614 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
615 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000616 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000617 tok = &Lexer.Lex();
618 }
619 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000620
Sean Callananfd0b0282010-01-21 00:19:58 +0000621 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000622 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000623
Sean Callananfd0b0282010-01-21 00:19:58 +0000624 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000625}
626
Chris Lattner79180e22010-04-05 23:15:42 +0000627bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000628 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000629 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000630 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000631
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000632 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000633 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000634
635 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000636 AsmCond StartingCondState = TheCondState;
637
Kevin Enderby613b7572011-11-01 22:27:22 +0000638 // If we are generating dwarf for assembly source files save the initial text
639 // section and generate a .file directive.
640 if (getContext().getGenDwarfForAssembly()) {
641 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000642 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
643 getStreamer().EmitLabel(SectionStartSym);
644 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000645 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000646 StringRef(),
647 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000648 }
649
Chris Lattnerb717fb02009-07-02 21:53:43 +0000650 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000651 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000652 ParseStatementInfo Info;
653 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000654
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000655 // We had an error, validate that one was emitted and recover by skipping to
656 // the next line.
657 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000658 EatToEndOfStatement();
659 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000660
661 if (TheCondState.TheCond != StartingCondState.TheCond ||
662 TheCondState.Ignore != StartingCondState.Ignore)
663 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000664
665 // Check to see there are no empty DwarfFile slots.
666 const std::vector<MCDwarfFile *> &MCDwarfFiles =
667 getContext().getMCDwarfFiles();
668 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000669 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000670 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000671 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000672
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000673 // Check to see that all assembler local symbols were actually defined.
674 // Targets that don't do subsections via symbols may not want this, though,
675 // so conservatively exclude them. Only do this if we're finalizing, though,
676 // as otherwise we won't necessarilly have seen everything yet.
677 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
678 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
679 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
680 e = Symbols.end();
681 i != e; ++i) {
682 MCSymbol *Sym = i->getValue();
683 // Variable symbols may not be marked as defined, so check those
684 // explicitly. If we know it's a variable, we have a definition for
685 // the purposes of this check.
686 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
687 // FIXME: We would really like to refer back to where the symbol was
688 // first referenced for a source location. We need to add something
689 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000690 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
691 "assembler local symbol '" + Sym->getName() +
692 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000693 }
694 }
695
696
Chris Lattner79180e22010-04-05 23:15:42 +0000697 // Finalize the output stream if there are no errors and if the client wants
698 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000699 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000700 Out.Finish();
701
Chris Lattnerb717fb02009-07-02 21:53:43 +0000702 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000703}
704
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000705void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000706 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000707 TokError("expected section directive before assembly directive");
708 Out.SwitchSection(Ctx.getMachOSection(
709 "__TEXT", "__text",
710 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
711 0, SectionKind::getText()));
712 }
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.
1280 if (MacrosEnabled)
1281 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_SPACE:
1347 case DK_SKIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001348 return ParseDirectiveSpace();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001349 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001350 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001351 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001352 EatToEndOfStatement(); // .extern is the default, ignore it.
1353 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001354 case DK_GLOBL:
1355 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001356 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001357 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001358 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001359 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001360 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001361 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001362 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001363 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001364 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001365 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001366 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001367 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001368 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001369 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001370 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001371 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001372 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001373 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001374 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001375 case DK_COMM:
1376 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001377 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001378 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001379 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001380 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001381 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001382 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001383 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001384 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001385 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001386 case DK_CODE16:
1387 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001388 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001389 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001390 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001391 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001392 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001393 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001394 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001395 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001396 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001397 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001398 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001399 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001400 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001401 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001402 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001403 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001404
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001405 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001406 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1407 DirectiveMap.lookup(IDVal);
1408 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001409 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001410
Jim Grosbach686c0182012-05-01 18:38:27 +00001411 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001412 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001413
Eli Friedman2128aae2012-10-22 23:58:19 +00001414 // _emit
1415 if (ParsingInlineAsm && IDVal == "_emit")
1416 return ParseDirectiveEmit(IDLoc, Info);
1417
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001418 CheckForValidSection();
1419
Chris Lattnera7f13542010-05-19 23:34:33 +00001420 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001421 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001422 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001423 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001424
Chad Rosier6a020a72012-10-25 20:41:34 +00001425 ParseInstructionInfo IInfo(Info.AsmRewrites);
1426 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1427 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001428 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001429
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001430 // Dump the parsed representation, if requested.
1431 if (getShowParsedOperands()) {
1432 SmallString<256> Str;
1433 raw_svector_ostream OS(Str);
1434 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001435 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001436 if (i != 0)
1437 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001438 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001439 }
1440 OS << "]";
1441
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001442 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001443 }
1444
Kevin Enderby613b7572011-11-01 22:27:22 +00001445 // If we are generating dwarf for assembly source files and the current
1446 // section is the initial text section then generate a .loc directive for
1447 // the instruction.
1448 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001449 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001450
1451 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1452
1453 // If we previously parsed a cpp hash file line comment then make sure the
1454 // current Dwarf File is for the CppHashFilename if not then emit the
1455 // Dwarf File table for it and adjust the line number for the .loc.
1456 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1457 getContext().getMCDwarfFiles();
1458 if (CppHashFilename.size() != 0) {
1459 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1460 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001461 getStreamer().EmitDwarfFileDirective(
1462 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001463
Kevin Enderby32c1a822012-11-05 21:55:41 +00001464 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001465 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1466 }
1467
Kevin Enderby613b7572011-11-01 22:27:22 +00001468 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001469 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001470 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001471 StringRef());
1472 }
1473
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001474 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001475 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001476 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001477 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1478 Info.ParsedOperands,
1479 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001480 ParsingInlineAsm);
1481 }
Chris Lattner98986712010-01-14 22:21:20 +00001482
Chris Lattnercbf8a982010-09-11 16:18:25 +00001483 // Don't skip the rest of the line, the instruction parser is responsible for
1484 // that.
1485 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001486}
Chris Lattner9a023f72009-06-24 04:43:34 +00001487
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001488/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1489/// since they may not be able to be tokenized to get to the end of line token.
1490void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001491 if (!Lexer.is(AsmToken::EndOfStatement))
1492 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001493 // Eat EOL.
1494 Lex();
1495}
1496
1497/// ParseCppHashLineFilenameComment as this:
1498/// ::= # number "filename"
1499/// or just as a full line comment if it doesn't have a number and a string.
1500bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1501 Lex(); // Eat the hash token.
1502
1503 if (getLexer().isNot(AsmToken::Integer)) {
1504 // Consume the line since in cases it is not a well-formed line directive,
1505 // as if were simply a full line comment.
1506 EatToEndOfLine();
1507 return false;
1508 }
1509
1510 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001511 Lex();
1512
1513 if (getLexer().isNot(AsmToken::String)) {
1514 EatToEndOfLine();
1515 return false;
1516 }
1517
1518 StringRef Filename = getTok().getString();
1519 // Get rid of the enclosing quotes.
1520 Filename = Filename.substr(1, Filename.size()-2);
1521
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001522 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1523 CppHashLoc = L;
1524 CppHashFilename = Filename;
1525 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001526 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001527
1528 // Ignore any trailing characters, they're just comment.
1529 EatToEndOfLine();
1530 return false;
1531}
1532
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001533/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001534/// for the Filename and LineNo if any in the diagnostic.
1535void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1536 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1537 raw_ostream &OS = errs();
1538
1539 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1540 const SMLoc &DiagLoc = Diag.getLoc();
1541 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1542 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1543
1544 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1545 // before printing the message.
1546 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001547 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001548 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1549 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1550 }
1551
Eric Christopher2318ba12012-12-18 00:30:54 +00001552 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001553 // manager changed or buffer changed (like in a nested include) then just
1554 // print the normal diagnostic using its Filename and LineNo.
1555 if (!Parser->CppHashLineNumber ||
1556 &DiagSrcMgr != &Parser->SrcMgr ||
1557 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001558 if (Parser->SavedDiagHandler)
1559 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1560 else
1561 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001562 return;
1563 }
1564
Eric Christopher2318ba12012-12-18 00:30:54 +00001565 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001566 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1567 // the diagnostic.
1568 const std::string Filename = Parser->CppHashFilename;
1569
1570 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1571 int CppHashLocLineNo =
1572 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1573 int LineNo = Parser->CppHashLineNumber - 1 +
1574 (DiagLocLineNo - CppHashLocLineNo);
1575
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001576 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1577 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001578 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001579 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001580
Benjamin Kramer04a04262011-10-16 10:48:29 +00001581 if (Parser->SavedDiagHandler)
1582 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1583 else
1584 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001585}
1586
Rafael Espindola799aacf2012-08-21 18:29:30 +00001587// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1588// difference being that that function accepts '@' as part of identifiers and
1589// we can't do that. AsmLexer.cpp should probably be changed to handle
1590// '@' as a special case when needed.
1591static bool isIdentifierChar(char c) {
1592 return isalnum(c) || c == '_' || c == '$' || c == '.';
1593}
1594
Rafael Espindola761cb062012-06-03 23:57:14 +00001595bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001596 const MacroParameters &Parameters,
1597 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001598 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001599 unsigned NParameters = Parameters.size();
1600 if (NParameters != 0 && NParameters != A.size())
1601 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001602
Preston Gurd7b6f2032012-09-19 20:36:12 +00001603 // A macro without parameters is handled differently on Darwin:
1604 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001605 while (!Body.empty()) {
1606 // Scan for the next substitution.
1607 std::size_t End = Body.size(), Pos = 0;
1608 for (; Pos != End; ++Pos) {
1609 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001610 if (!NParameters) {
1611 // This macro has no parameters, look for $0, $1, etc.
1612 if (Body[Pos] != '$' || Pos + 1 == End)
1613 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001614
Rafael Espindola65366442011-06-05 02:43:45 +00001615 char Next = Body[Pos + 1];
1616 if (Next == '$' || Next == 'n' || isdigit(Next))
1617 break;
1618 } else {
1619 // This macro has parameters, look for \foo, \bar, etc.
1620 if (Body[Pos] == '\\' && Pos + 1 != End)
1621 break;
1622 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001623 }
1624
1625 // Add the prefix.
1626 OS << Body.slice(0, Pos);
1627
1628 // Check if we reached the end.
1629 if (Pos == End)
1630 break;
1631
Rafael Espindola65366442011-06-05 02:43:45 +00001632 if (!NParameters) {
1633 switch (Body[Pos+1]) {
1634 // $$ => $
1635 case '$':
1636 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001637 break;
1638
Rafael Espindola65366442011-06-05 02:43:45 +00001639 // $n => number of arguments
1640 case 'n':
1641 OS << A.size();
1642 break;
1643
1644 // $[0-9] => argument
1645 default: {
1646 // Missing arguments are ignored.
1647 unsigned Index = Body[Pos+1] - '0';
1648 if (Index >= A.size())
1649 break;
1650
1651 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001652 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001653 ie = A[Index].end(); it != ie; ++it)
1654 OS << it->getString();
1655 break;
1656 }
1657 }
1658 Pos += 2;
1659 } else {
1660 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001661 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001662 ++I;
1663
1664 const char *Begin = Body.data() + Pos +1;
1665 StringRef Argument(Begin, I - (Pos +1));
1666 unsigned Index = 0;
1667 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001668 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001669 break;
1670
Preston Gurd7b6f2032012-09-19 20:36:12 +00001671 if (Index == NParameters) {
1672 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1673 Pos += 3;
1674 else {
1675 OS << '\\' << Argument;
1676 Pos = I;
1677 }
1678 } else {
1679 for (MacroArgument::const_iterator it = A[Index].begin(),
1680 ie = A[Index].end(); it != ie; ++it)
1681 if (it->getKind() == AsmToken::String)
1682 OS << it->getStringContents();
1683 else
1684 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001685
Preston Gurd7b6f2032012-09-19 20:36:12 +00001686 Pos += 1 + Argument.size();
1687 }
Rafael Espindola65366442011-06-05 02:43:45 +00001688 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001689 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001690 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001691 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001692
Rafael Espindola65366442011-06-05 02:43:45 +00001693 return false;
1694}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001695
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001696MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL,
1697 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001698 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001699 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1700 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001701{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001702}
1703
Preston Gurd7b6f2032012-09-19 20:36:12 +00001704static bool IsOperator(AsmToken::TokenKind kind)
1705{
1706 switch (kind)
1707 {
1708 default:
1709 return false;
1710 case AsmToken::Plus:
1711 case AsmToken::Minus:
1712 case AsmToken::Tilde:
1713 case AsmToken::Slash:
1714 case AsmToken::Star:
1715 case AsmToken::Dot:
1716 case AsmToken::Equal:
1717 case AsmToken::EqualEqual:
1718 case AsmToken::Pipe:
1719 case AsmToken::PipePipe:
1720 case AsmToken::Caret:
1721 case AsmToken::Amp:
1722 case AsmToken::AmpAmp:
1723 case AsmToken::Exclaim:
1724 case AsmToken::ExclaimEqual:
1725 case AsmToken::Percent:
1726 case AsmToken::Less:
1727 case AsmToken::LessEqual:
1728 case AsmToken::LessLess:
1729 case AsmToken::LessGreater:
1730 case AsmToken::Greater:
1731 case AsmToken::GreaterEqual:
1732 case AsmToken::GreaterGreater:
1733 return true;
1734 }
1735}
1736
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001737/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1738/// This is used for both default macro parameter values and the
1739/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001740bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1741 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001742 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001743 unsigned AddTokens = 0;
1744
1745 // gas accepts arguments separated by whitespace, except on Darwin
1746 if (!IsDarwin)
1747 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001748
1749 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001750 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1751 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001752 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001753 }
1754
1755 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1756 // Spaces and commas cannot be mixed to delimit parameters
1757 if (ArgumentDelimiter == AsmToken::Eof)
1758 ArgumentDelimiter = AsmToken::Comma;
1759 else if (ArgumentDelimiter != AsmToken::Comma) {
1760 Lexer.setSkipSpace(true);
1761 return TokError("expected ' ' for macro argument separator");
1762 }
1763 break;
1764 }
1765
1766 if (Lexer.is(AsmToken::Space)) {
1767 Lex(); // Eat spaces
1768
1769 // Spaces can delimit parameters, but could also be part an expression.
1770 // If the token after a space is an operator, add the token and the next
1771 // one into this argument
1772 if (ArgumentDelimiter == AsmToken::Space ||
1773 ArgumentDelimiter == AsmToken::Eof) {
1774 if (IsOperator(Lexer.getKind())) {
1775 // Check to see whether the token is used as an operator,
1776 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001777 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001778 if (*NextChar == ' ')
1779 AddTokens = 2;
1780 }
1781
1782 if (!AddTokens && ParenLevel == 0) {
1783 if (ArgumentDelimiter == AsmToken::Eof &&
1784 !IsOperator(Lexer.getKind()))
1785 ArgumentDelimiter = AsmToken::Space;
1786 break;
1787 }
1788 }
1789 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001790
1791 // HandleMacroEntry relies on not advancing the lexer here
1792 // to be able to fill in the remaining default parameter values
1793 if (Lexer.is(AsmToken::EndOfStatement))
1794 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001795
1796 // Adjust the current parentheses level.
1797 if (Lexer.is(AsmToken::LParen))
1798 ++ParenLevel;
1799 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1800 --ParenLevel;
1801
1802 // Append the token to the current argument list.
1803 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001804 if (AddTokens)
1805 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001806 Lex();
1807 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001808
1809 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001810 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001811 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001812 return false;
1813}
1814
1815// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001816bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001817 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001818 // Argument delimiter is initially unknown. It will be set by
1819 // ParseMacroArgument()
1820 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001821
1822 // Parse two kinds of macro invocations:
1823 // - macros defined without any parameters accept an arbitrary number of them
1824 // - macros defined with parameters accept at most that many of them
1825 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1826 ++Parameter) {
1827 MacroArgument MA;
1828
Preston Gurd7b6f2032012-09-19 20:36:12 +00001829 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001830 return true;
1831
Preston Gurd6c9176a2012-09-19 20:29:04 +00001832 if (!MA.empty() || !NParameters)
1833 A.push_back(MA);
1834 else if (NParameters) {
1835 if (!M->Parameters[Parameter].second.empty())
1836 A.push_back(M->Parameters[Parameter].second);
1837 }
Jim Grosbach97146442012-07-30 22:44:17 +00001838
Preston Gurd6c9176a2012-09-19 20:29:04 +00001839 // At the end of the statement, fill in remaining arguments that have
1840 // default values. If there aren't any, then the next argument is
1841 // required but missing
1842 if (Lexer.is(AsmToken::EndOfStatement)) {
1843 if (NParameters && Parameter < NParameters - 1) {
1844 if (M->Parameters[Parameter + 1].second.empty())
1845 return TokError("macro argument '" +
1846 Twine(M->Parameters[Parameter + 1].first) +
1847 "' is missing");
1848 else
1849 continue;
1850 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001851 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001852 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001853
1854 if (Lexer.is(AsmToken::Comma))
1855 Lex();
1856 }
1857 return TokError("Too many arguments");
1858}
1859
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001860bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1861 const Macro *M) {
1862 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1863 // this, although we should protect against infinite loops.
1864 if (ActiveMacros.size() == 20)
1865 return TokError("macros cannot be nested more than 20 levels deep");
1866
Rafael Espindola8a403d32012-08-08 14:51:03 +00001867 MacroArguments A;
1868 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001869 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001870
Jim Grosbach97146442012-07-30 22:44:17 +00001871 // Remove any trailing empty arguments. Do this after-the-fact as we have
1872 // to keep empty arguments in the middle of the list or positionality
1873 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001874 while (!A.empty() && A.back().empty())
1875 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001876
Rafael Espindola65366442011-06-05 02:43:45 +00001877 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1878 // to hold the macro body with substitutions.
1879 SmallString<256> Buf;
1880 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001881 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001882
Rafael Espindola8a403d32012-08-08 14:51:03 +00001883 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001884 return true;
1885
Rafael Espindola761cb062012-06-03 23:57:14 +00001886 // We include the .endmacro in the buffer as our queue to exit the macro
1887 // instantiation.
1888 OS << ".endmacro\n";
1889
Rafael Espindola65366442011-06-05 02:43:45 +00001890 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001891 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001892
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001893 // Create the macro instantiation object and add to the current macro
1894 // instantiation stack.
1895 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001896 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001897 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001898 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001899 ActiveMacros.push_back(MI);
1900
1901 // Jump to the macro instantiation and prime the lexer.
1902 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1903 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1904 Lex();
1905
1906 return false;
1907}
1908
1909void AsmParser::HandleMacroExit() {
1910 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001911 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001912 Lex();
1913
1914 // Pop the instantiation entry.
1915 delete ActiveMacros.back();
1916 ActiveMacros.pop_back();
1917}
1918
Rafael Espindolae71cc862012-01-28 05:57:00 +00001919static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001920 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001921 case MCExpr::Binary: {
1922 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1923 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001924 break;
1925 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001926 case MCExpr::Target:
1927 case MCExpr::Constant:
1928 return false;
1929 case MCExpr::SymbolRef: {
1930 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001931 if (S.isVariable())
1932 return IsUsedIn(Sym, S.getVariableValue());
1933 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001934 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001935 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001936 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001937 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001938
1939 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001940}
1941
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001942bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1943 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001944 // FIXME: Use better location, we should use proper tokens.
1945 SMLoc EqualLoc = Lexer.getLoc();
1946
Daniel Dunbar821e3332009-08-31 08:09:28 +00001947 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001948 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001949 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001950
Rafael Espindolae71cc862012-01-28 05:57:00 +00001951 // Note: we don't count b as used in "a = b". This is to allow
1952 // a = b
1953 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001954
Daniel Dunbar3f872332009-07-28 16:08:33 +00001955 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001956 return TokError("unexpected token in assignment");
1957
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001958 // Error on assignment to '.'.
1959 if (Name == ".") {
1960 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1961 "(use '.space' or '.org').)"));
1962 }
1963
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001964 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001965 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001966
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001967 // Validate that the LHS is allowed to be a variable (either it has not been
1968 // used as a symbol, or it is an absolute symbol).
1969 MCSymbol *Sym = getContext().LookupSymbol(Name);
1970 if (Sym) {
1971 // Diagnose assignment to a label.
1972 //
1973 // FIXME: Diagnostics. Note the location of the definition as a label.
1974 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001975 if (IsUsedIn(Sym, Value))
1976 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1977 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001978 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001979 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1980 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001981 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001982 return Error(EqualLoc, "redefinition of '" + Name + "'");
1983 else if (!Sym->isVariable())
1984 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001985 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001986 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1987 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001988
1989 // Don't count these checks as uses.
1990 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001991 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001992 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001993
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001994 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001995
1996 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001997 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001998 if (NoDeadStrip)
1999 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2000
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002001
2002 return false;
2003}
2004
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002005/// ParseIdentifier:
2006/// ::= identifier
2007/// ::= string
2008bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002009 // The assembler has relaxed rules for accepting identifiers, in particular we
2010 // allow things like '.globl $foo', which would normally be separate
2011 // tokens. At this level, we have already lexed so we cannot (currently)
2012 // handle this as a context dependent token, instead we detect adjacent tokens
2013 // and return the combined identifier.
2014 if (Lexer.is(AsmToken::Dollar)) {
2015 SMLoc DollarLoc = getLexer().getLoc();
2016
2017 // Consume the dollar sign, and check for a following identifier.
2018 Lex();
2019 if (Lexer.isNot(AsmToken::Identifier))
2020 return true;
2021
2022 // We have a '$' followed by an identifier, make sure they are adjacent.
2023 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2024 return true;
2025
2026 // Construct the joined identifier and consume the token.
2027 Res = StringRef(DollarLoc.getPointer(),
2028 getTok().getIdentifier().size() + 1);
2029 Lex();
2030 return false;
2031 }
2032
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002033 if (Lexer.isNot(AsmToken::Identifier) &&
2034 Lexer.isNot(AsmToken::String))
2035 return true;
2036
Sean Callanan18b83232010-01-19 21:44:56 +00002037 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002038
Sean Callanan79ed1a82010-01-19 20:22:31 +00002039 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002040
2041 return false;
2042}
2043
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002044/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002045/// ::= .equ identifier ',' expression
2046/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002047/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002048bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002049 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002050
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002051 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002052 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002053
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002054 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002055 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002056 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002057
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002058 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002059}
2060
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002061bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002062 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002063
2064 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002065 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002066 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2067 if (Str[i] != '\\') {
2068 Data += Str[i];
2069 continue;
2070 }
2071
2072 // Recognize escaped characters. Note that this escape semantics currently
2073 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2074 ++i;
2075 if (i == e)
2076 return TokError("unexpected backslash at end of string");
2077
2078 // Recognize octal sequences.
2079 if ((unsigned) (Str[i] - '0') <= 7) {
2080 // Consume up to three octal characters.
2081 unsigned Value = Str[i] - '0';
2082
2083 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2084 ++i;
2085 Value = Value * 8 + (Str[i] - '0');
2086
2087 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2088 ++i;
2089 Value = Value * 8 + (Str[i] - '0');
2090 }
2091 }
2092
2093 if (Value > 255)
2094 return TokError("invalid octal escape sequence (out of range)");
2095
2096 Data += (unsigned char) Value;
2097 continue;
2098 }
2099
2100 // Otherwise recognize individual escapes.
2101 switch (Str[i]) {
2102 default:
2103 // Just reject invalid escape sequences for now.
2104 return TokError("invalid escape sequence (unrecognized character)");
2105
2106 case 'b': Data += '\b'; break;
2107 case 'f': Data += '\f'; break;
2108 case 'n': Data += '\n'; break;
2109 case 'r': Data += '\r'; break;
2110 case 't': Data += '\t'; break;
2111 case '"': Data += '"'; break;
2112 case '\\': Data += '\\'; break;
2113 }
2114 }
2115
2116 return false;
2117}
2118
Daniel Dunbara0d14262009-06-24 23:30:00 +00002119/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002120/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2121bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002122 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002123 CheckForValidSection();
2124
Daniel Dunbara0d14262009-06-24 23:30:00 +00002125 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002126 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002127 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002128
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002129 std::string Data;
2130 if (ParseEscapedString(Data))
2131 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002132
2133 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002134 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002135 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2136
Sean Callanan79ed1a82010-01-19 20:22:31 +00002137 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002138
2139 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002140 break;
2141
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002142 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002143 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002144 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002145 }
2146 }
2147
Sean Callanan79ed1a82010-01-19 20:22:31 +00002148 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002149 return false;
2150}
2151
2152/// ParseDirectiveValue
2153/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2154bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002155 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002156 CheckForValidSection();
2157
Daniel Dunbara0d14262009-06-24 23:30:00 +00002158 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002159 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002160 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002161 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002162 return true;
2163
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002164 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002165 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2166 assert(Size <= 8 && "Invalid size");
2167 uint64_t IntValue = MCE->getValue();
2168 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2169 return Error(ExprLoc, "literal value out of range for directive");
2170 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2171 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002172 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002173
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002174 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002175 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002176
Daniel Dunbara0d14262009-06-24 23:30:00 +00002177 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002178 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002179 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002180 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002181 }
2182 }
2183
Sean Callanan79ed1a82010-01-19 20:22:31 +00002184 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002185 return false;
2186}
2187
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002188/// ParseDirectiveRealValue
2189/// ::= (.single | .double) [ expression (, expression)* ]
2190bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2191 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2192 CheckForValidSection();
2193
2194 for (;;) {
2195 // We don't truly support arithmetic on floating point expressions, so we
2196 // have to manually parse unary prefixes.
2197 bool IsNeg = false;
2198 if (getLexer().is(AsmToken::Minus)) {
2199 Lex();
2200 IsNeg = true;
2201 } else if (getLexer().is(AsmToken::Plus))
2202 Lex();
2203
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002204 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002205 getLexer().isNot(AsmToken::Real) &&
2206 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002207 return TokError("unexpected token in directive");
2208
2209 // Convert to an APFloat.
2210 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002211 StringRef IDVal = getTok().getString();
2212 if (getLexer().is(AsmToken::Identifier)) {
2213 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2214 Value = APFloat::getInf(Semantics);
2215 else if (!IDVal.compare_lower("nan"))
2216 Value = APFloat::getNaN(Semantics, false, ~0);
2217 else
2218 return TokError("invalid floating point literal");
2219 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002220 APFloat::opInvalidOp)
2221 return TokError("invalid floating point literal");
2222 if (IsNeg)
2223 Value.changeSign();
2224
2225 // Consume the numeric token.
2226 Lex();
2227
2228 // Emit the value as an integer.
2229 APInt AsInt = Value.bitcastToAPInt();
2230 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2231 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2232
2233 if (getLexer().is(AsmToken::EndOfStatement))
2234 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002235
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002236 if (getLexer().isNot(AsmToken::Comma))
2237 return TokError("unexpected token in directive");
2238 Lex();
2239 }
2240 }
2241
2242 Lex();
2243 return false;
2244}
2245
Daniel Dunbara0d14262009-06-24 23:30:00 +00002246/// ParseDirectiveSpace
2247/// ::= .space expression [ , expression ]
2248bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002249 CheckForValidSection();
2250
Daniel Dunbara0d14262009-06-24 23:30:00 +00002251 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002252 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002253 return true;
2254
2255 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002256 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2257 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002258 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002259 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002260
Daniel Dunbar475839e2009-06-29 20:37:27 +00002261 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002262 return true;
2263
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002264 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002265 return TokError("unexpected token in '.space' directive");
2266 }
2267
Sean Callanan79ed1a82010-01-19 20:22:31 +00002268 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002269
2270 if (NumBytes <= 0)
2271 return TokError("invalid number of bytes in '.space' directive");
2272
2273 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002274 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002275
2276 return false;
2277}
2278
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002279/// ParseDirectiveZero
2280/// ::= .zero expression
2281bool AsmParser::ParseDirectiveZero() {
2282 CheckForValidSection();
2283
2284 int64_t NumBytes;
2285 if (ParseAbsoluteExpression(NumBytes))
2286 return true;
2287
Rafael Espindolae452b172010-10-05 19:42:57 +00002288 int64_t Val = 0;
2289 if (getLexer().is(AsmToken::Comma)) {
2290 Lex();
2291 if (ParseAbsoluteExpression(Val))
2292 return true;
2293 }
2294
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002295 if (getLexer().isNot(AsmToken::EndOfStatement))
2296 return TokError("unexpected token in '.zero' directive");
2297
2298 Lex();
2299
Rafael Espindolae452b172010-10-05 19:42:57 +00002300 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002301
2302 return false;
2303}
2304
Daniel Dunbara0d14262009-06-24 23:30:00 +00002305/// ParseDirectiveFill
2306/// ::= .fill expression , expression , expression
2307bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002308 CheckForValidSection();
2309
Daniel Dunbara0d14262009-06-24 23:30:00 +00002310 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002311 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002312 return true;
2313
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002314 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002315 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002316 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002317
Daniel Dunbara0d14262009-06-24 23:30:00 +00002318 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002319 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002320 return true;
2321
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002322 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002323 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002324 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002325
Daniel Dunbara0d14262009-06-24 23:30:00 +00002326 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002327 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002328 return true;
2329
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002330 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002331 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002332
Sean Callanan79ed1a82010-01-19 20:22:31 +00002333 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002334
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002335 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2336 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002337
2338 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002339 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002340
2341 return false;
2342}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002343
2344/// ParseDirectiveOrg
2345/// ::= .org expression [ , expression ]
2346bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002347 CheckForValidSection();
2348
Daniel Dunbar821e3332009-08-31 08:09:28 +00002349 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002350 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002351 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002352 return true;
2353
2354 // Parse optional fill expression.
2355 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002356 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2357 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002358 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002359 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002360
Daniel Dunbar475839e2009-06-29 20:37:27 +00002361 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002362 return true;
2363
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002364 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002365 return TokError("unexpected token in '.org' directive");
2366 }
2367
Sean Callanan79ed1a82010-01-19 20:22:31 +00002368 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002369
Jim Grosbachebd4c052012-01-27 00:37:08 +00002370 // Only limited forms of relocatable expressions are accepted here, it
2371 // has to be relative to the current section. The streamer will return
2372 // 'true' if the expression wasn't evaluatable.
2373 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2374 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002375
2376 return false;
2377}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002378
2379/// ParseDirectiveAlign
2380/// ::= {.align, ...} expression [ , expression [ , expression ]]
2381bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002382 CheckForValidSection();
2383
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002384 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002385 int64_t Alignment;
2386 if (ParseAbsoluteExpression(Alignment))
2387 return true;
2388
2389 SMLoc MaxBytesLoc;
2390 bool HasFillExpr = false;
2391 int64_t FillExpr = 0;
2392 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002393 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2394 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002395 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002396 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002397
2398 // The fill expression can be omitted while specifying a maximum number of
2399 // alignment bytes, e.g:
2400 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002401 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002402 HasFillExpr = true;
2403 if (ParseAbsoluteExpression(FillExpr))
2404 return true;
2405 }
2406
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002407 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2408 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002409 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002410 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002411
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002412 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002413 if (ParseAbsoluteExpression(MaxBytesToFill))
2414 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002415
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002416 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002417 return TokError("unexpected token in directive");
2418 }
2419 }
2420
Sean Callanan79ed1a82010-01-19 20:22:31 +00002421 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002422
Daniel Dunbar648ac512010-05-17 21:54:30 +00002423 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002424 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002425
2426 // Compute alignment in bytes.
2427 if (IsPow2) {
2428 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002429 if (Alignment >= 32) {
2430 Error(AlignmentLoc, "invalid alignment value");
2431 Alignment = 31;
2432 }
2433
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002434 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002435 }
2436
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002437 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002438 if (MaxBytesLoc.isValid()) {
2439 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002440 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2441 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002442 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002443 }
2444
2445 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002446 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2447 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002448 MaxBytesToFill = 0;
2449 }
2450 }
2451
Daniel Dunbar648ac512010-05-17 21:54:30 +00002452 // Check whether we should use optimal code alignment for this .align
2453 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002454 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002455 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2456 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002457 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002458 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002459 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002460 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2461 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002462 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002463
2464 return false;
2465}
2466
Eli Bendersky4766ef42012-12-20 19:05:53 +00002467
2468/// ParseDirectiveBundleAlignMode
2469/// ::= {.bundle_align_mode} expression
2470bool AsmParser::ParseDirectiveBundleAlignMode() {
2471 CheckForValidSection();
2472
2473 // Expect a single argument: an expression that evaluates to a constant
2474 // in the inclusive range 0-30.
2475 SMLoc ExprLoc = getLexer().getLoc();
2476 int64_t AlignSizePow2;
2477 if (ParseAbsoluteExpression(AlignSizePow2))
2478 return true;
2479 else if (getLexer().isNot(AsmToken::EndOfStatement))
2480 return TokError("unexpected token after expression in"
2481 " '.bundle_align_mode' directive");
2482 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2483 return Error(ExprLoc,
2484 "invalid bundle alignment size (expected between 0 and 30)");
2485
2486 Lex();
2487
2488 // Because of AlignSizePow2's verified range we can safely truncate it to
2489 // unsigned.
2490 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2491 return false;
2492}
2493
2494/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002495/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002496bool AsmParser::ParseDirectiveBundleLock() {
2497 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002498 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002499
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002500 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2501 StringRef Option;
2502 SMLoc Loc = getTok().getLoc();
2503 const char *kInvalidOptionError =
2504 "invalid option for '.bundle_lock' directive";
2505
2506 if (ParseIdentifier(Option))
2507 return Error(Loc, kInvalidOptionError);
2508
2509 if (Option != "align_to_end")
2510 return Error(Loc, kInvalidOptionError);
2511 else if (getLexer().isNot(AsmToken::EndOfStatement))
2512 return Error(Loc,
2513 "unexpected token after '.bundle_lock' directive option");
2514 AlignToEnd = true;
2515 }
2516
Eli Bendersky4766ef42012-12-20 19:05:53 +00002517 Lex();
2518
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002519 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002520 return false;
2521}
2522
2523/// ParseDirectiveBundleLock
2524/// ::= {.bundle_lock}
2525bool AsmParser::ParseDirectiveBundleUnlock() {
2526 CheckForValidSection();
2527
2528 if (getLexer().isNot(AsmToken::EndOfStatement))
2529 return TokError("unexpected token in '.bundle_unlock' directive");
2530 Lex();
2531
2532 getStreamer().EmitBundleUnlock();
2533 return false;
2534}
2535
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002536/// ParseDirectiveSymbolAttribute
2537/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002538bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002539 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002540 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002541 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002542 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002543
2544 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002545 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002546
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002547 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002548
Jim Grosbach10ec6502011-09-15 17:56:49 +00002549 // Assembler local symbols don't make any sense here. Complain loudly.
2550 if (Sym->isTemporary())
2551 return Error(Loc, "non-local symbol required in directive");
2552
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002553 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002554
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002555 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002556 break;
2557
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002558 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002559 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002560 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002561 }
2562 }
2563
Sean Callanan79ed1a82010-01-19 20:22:31 +00002564 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002565 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002566}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002567
2568/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002569/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2570bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002571 CheckForValidSection();
2572
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002573 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002574 StringRef Name;
2575 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002576 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002577
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002578 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002579 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002580
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002581 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002582 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002583 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002584
2585 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002586 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002587 if (ParseAbsoluteExpression(Size))
2588 return true;
2589
2590 int64_t Pow2Alignment = 0;
2591 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002592 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002593 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002594 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002595 if (ParseAbsoluteExpression(Pow2Alignment))
2596 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002597
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002598 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2599 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002600 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2601
Chris Lattner258281d2010-01-19 06:22:22 +00002602 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002603 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2604 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002605 if (!isPowerOf2_64(Pow2Alignment))
2606 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2607 Pow2Alignment = Log2_64(Pow2Alignment);
2608 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002609 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002610
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002611 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002612 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002613
Sean Callanan79ed1a82010-01-19 20:22:31 +00002614 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002615
Chris Lattner1fc3d752009-07-09 17:25:12 +00002616 // NOTE: a size of zero for a .comm should create a undefined symbol
2617 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002618 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002619 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2620 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002621
Eric Christopherc260a3e2010-05-14 01:38:54 +00002622 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002623 // may internally end up wanting an alignment in bytes.
2624 // FIXME: Diagnose overflow.
2625 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002626 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2627 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002628
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002629 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002630 return Error(IDLoc, "invalid symbol redefinition");
2631
Chris Lattner1fc3d752009-07-09 17:25:12 +00002632 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002633 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002634 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002635 return false;
2636 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002637
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002638 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002639 return false;
2640}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002641
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002642/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002643/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002644bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002645 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002646 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002647
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002648 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002649 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002650 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002651
Sean Callanan79ed1a82010-01-19 20:22:31 +00002652 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002653
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002654 if (Str.empty())
2655 Error(Loc, ".abort detected. Assembly stopping.");
2656 else
2657 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002658 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002659
2660 return false;
2661}
Kevin Enderby71148242009-07-14 21:35:03 +00002662
Kevin Enderby1f049b22009-07-14 23:21:55 +00002663/// ParseDirectiveInclude
2664/// ::= .include "filename"
2665bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002666 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002667 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002668
Sean Callanan18b83232010-01-19 21:44:56 +00002669 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002670 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002671 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002672
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002673 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002674 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002675
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002676 // Strip the quotes.
2677 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002678
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002679 // Attempt to switch the lexer to the included file before consuming the end
2680 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002681 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002682 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002683 return true;
2684 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002685
2686 return false;
2687}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002688
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002689/// ParseDirectiveIncbin
2690/// ::= .incbin "filename"
2691bool AsmParser::ParseDirectiveIncbin() {
2692 if (getLexer().isNot(AsmToken::String))
2693 return TokError("expected string in '.incbin' directive");
2694
2695 std::string Filename = getTok().getString();
2696 SMLoc IncbinLoc = getLexer().getLoc();
2697 Lex();
2698
2699 if (getLexer().isNot(AsmToken::EndOfStatement))
2700 return TokError("unexpected token in '.incbin' directive");
2701
2702 // Strip the quotes.
2703 Filename = Filename.substr(1, Filename.size()-2);
2704
2705 // Attempt to process the included file.
2706 if (ProcessIncbinFile(Filename)) {
2707 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2708 return true;
2709 }
2710
2711 return false;
2712}
2713
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002714/// ParseDirectiveIf
2715/// ::= .if expression
2716bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002717 TheCondStack.push_back(TheCondState);
2718 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002719 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002720 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002721 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002722 int64_t ExprValue;
2723 if (ParseAbsoluteExpression(ExprValue))
2724 return true;
2725
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002726 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002727 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002728
Sean Callanan79ed1a82010-01-19 20:22:31 +00002729 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002730
2731 TheCondState.CondMet = ExprValue;
2732 TheCondState.Ignore = !TheCondState.CondMet;
2733 }
2734
2735 return false;
2736}
2737
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002738/// ParseDirectiveIfb
2739/// ::= .ifb string
2740bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2741 TheCondStack.push_back(TheCondState);
2742 TheCondState.TheCond = AsmCond::IfCond;
2743
Benjamin Kramer29739e72012-05-12 16:52:21 +00002744 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002745 EatToEndOfStatement();
2746 } else {
2747 StringRef Str = ParseStringToEndOfStatement();
2748
2749 if (getLexer().isNot(AsmToken::EndOfStatement))
2750 return TokError("unexpected token in '.ifb' directive");
2751
2752 Lex();
2753
2754 TheCondState.CondMet = ExpectBlank == Str.empty();
2755 TheCondState.Ignore = !TheCondState.CondMet;
2756 }
2757
2758 return false;
2759}
2760
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002761/// ParseDirectiveIfc
2762/// ::= .ifc string1, string2
2763bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2764 TheCondStack.push_back(TheCondState);
2765 TheCondState.TheCond = AsmCond::IfCond;
2766
Benjamin Kramer29739e72012-05-12 16:52:21 +00002767 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002768 EatToEndOfStatement();
2769 } else {
2770 StringRef Str1 = ParseStringToComma();
2771
2772 if (getLexer().isNot(AsmToken::Comma))
2773 return TokError("unexpected token in '.ifc' directive");
2774
2775 Lex();
2776
2777 StringRef Str2 = ParseStringToEndOfStatement();
2778
2779 if (getLexer().isNot(AsmToken::EndOfStatement))
2780 return TokError("unexpected token in '.ifc' directive");
2781
2782 Lex();
2783
2784 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2785 TheCondState.Ignore = !TheCondState.CondMet;
2786 }
2787
2788 return false;
2789}
2790
2791/// ParseDirectiveIfdef
2792/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002793bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2794 StringRef Name;
2795 TheCondStack.push_back(TheCondState);
2796 TheCondState.TheCond = AsmCond::IfCond;
2797
2798 if (TheCondState.Ignore) {
2799 EatToEndOfStatement();
2800 } else {
2801 if (ParseIdentifier(Name))
2802 return TokError("expected identifier after '.ifdef'");
2803
2804 Lex();
2805
2806 MCSymbol *Sym = getContext().LookupSymbol(Name);
2807
2808 if (expect_defined)
2809 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2810 else
2811 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2812 TheCondState.Ignore = !TheCondState.CondMet;
2813 }
2814
2815 return false;
2816}
2817
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002818/// ParseDirectiveElseIf
2819/// ::= .elseif expression
2820bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2821 if (TheCondState.TheCond != AsmCond::IfCond &&
2822 TheCondState.TheCond != AsmCond::ElseIfCond)
2823 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2824 " an .elseif");
2825 TheCondState.TheCond = AsmCond::ElseIfCond;
2826
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002827 bool LastIgnoreState = false;
2828 if (!TheCondStack.empty())
2829 LastIgnoreState = TheCondStack.back().Ignore;
2830 if (LastIgnoreState || TheCondState.CondMet) {
2831 TheCondState.Ignore = true;
2832 EatToEndOfStatement();
2833 }
2834 else {
2835 int64_t ExprValue;
2836 if (ParseAbsoluteExpression(ExprValue))
2837 return true;
2838
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002839 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002840 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002841
Sean Callanan79ed1a82010-01-19 20:22:31 +00002842 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002843 TheCondState.CondMet = ExprValue;
2844 TheCondState.Ignore = !TheCondState.CondMet;
2845 }
2846
2847 return false;
2848}
2849
2850/// ParseDirectiveElse
2851/// ::= .else
2852bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002853 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002854 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002855
Sean Callanan79ed1a82010-01-19 20:22:31 +00002856 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002857
2858 if (TheCondState.TheCond != AsmCond::IfCond &&
2859 TheCondState.TheCond != AsmCond::ElseIfCond)
2860 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2861 ".elseif");
2862 TheCondState.TheCond = AsmCond::ElseCond;
2863 bool LastIgnoreState = false;
2864 if (!TheCondStack.empty())
2865 LastIgnoreState = TheCondStack.back().Ignore;
2866 if (LastIgnoreState || TheCondState.CondMet)
2867 TheCondState.Ignore = true;
2868 else
2869 TheCondState.Ignore = false;
2870
2871 return false;
2872}
2873
2874/// ParseDirectiveEndIf
2875/// ::= .endif
2876bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002877 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002878 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002879
Sean Callanan79ed1a82010-01-19 20:22:31 +00002880 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002881
2882 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2883 TheCondStack.empty())
2884 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2885 ".else");
2886 if (!TheCondStack.empty()) {
2887 TheCondState = TheCondStack.back();
2888 TheCondStack.pop_back();
2889 }
2890
2891 return false;
2892}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002893
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002894void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002895 DirectiveKindMapping[".set"] = DK_SET;
2896 DirectiveKindMapping[".equ"] = DK_EQU;
2897 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2898 DirectiveKindMapping[".ascii"] = DK_ASCII;
2899 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2900 DirectiveKindMapping[".string"] = DK_STRING;
2901 DirectiveKindMapping[".byte"] = DK_BYTE;
2902 DirectiveKindMapping[".short"] = DK_SHORT;
2903 DirectiveKindMapping[".value"] = DK_VALUE;
2904 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2905 DirectiveKindMapping[".long"] = DK_LONG;
2906 DirectiveKindMapping[".int"] = DK_INT;
2907 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2908 DirectiveKindMapping[".quad"] = DK_QUAD;
2909 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2910 DirectiveKindMapping[".single"] = DK_SINGLE;
2911 DirectiveKindMapping[".float"] = DK_FLOAT;
2912 DirectiveKindMapping[".double"] = DK_DOUBLE;
2913 DirectiveKindMapping[".align"] = DK_ALIGN;
2914 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2915 DirectiveKindMapping[".balign"] = DK_BALIGN;
2916 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2917 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2918 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2919 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2920 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2921 DirectiveKindMapping[".org"] = DK_ORG;
2922 DirectiveKindMapping[".fill"] = DK_FILL;
2923 DirectiveKindMapping[".space"] = DK_SPACE;
2924 DirectiveKindMapping[".skip"] = DK_SKIP;
2925 DirectiveKindMapping[".zero"] = DK_ZERO;
2926 DirectiveKindMapping[".extern"] = DK_EXTERN;
2927 DirectiveKindMapping[".globl"] = DK_GLOBL;
2928 DirectiveKindMapping[".global"] = DK_GLOBAL;
2929 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2930 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2931 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2932 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2933 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2934 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2935 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2936 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2937 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2938 DirectiveKindMapping[".comm"] = DK_COMM;
2939 DirectiveKindMapping[".common"] = DK_COMMON;
2940 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2941 DirectiveKindMapping[".abort"] = DK_ABORT;
2942 DirectiveKindMapping[".include"] = DK_INCLUDE;
2943 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2944 DirectiveKindMapping[".code16"] = DK_CODE16;
2945 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2946 DirectiveKindMapping[".rept"] = DK_REPT;
2947 DirectiveKindMapping[".irp"] = DK_IRP;
2948 DirectiveKindMapping[".irpc"] = DK_IRPC;
2949 DirectiveKindMapping[".endr"] = DK_ENDR;
2950 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2951 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2952 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2953 DirectiveKindMapping[".if"] = DK_IF;
2954 DirectiveKindMapping[".ifb"] = DK_IFB;
2955 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2956 DirectiveKindMapping[".ifc"] = DK_IFC;
2957 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2958 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2959 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2960 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2961 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2962 DirectiveKindMapping[".else"] = DK_ELSE;
2963 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002964}
2965
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002966/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002967/// ::= .file [number] filename
2968/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002969bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002970 // FIXME: I'm not sure what this is.
2971 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002972 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002973 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002974 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002975 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002976
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002977 if (FileNumber < 1)
2978 return TokError("file number less than one");
2979 }
2980
Daniel Dunbareceec052010-07-12 17:45:27 +00002981 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002982 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002983
Nick Lewycky44d798d2011-10-17 23:05:28 +00002984 // Usually the directory and filename together, otherwise just the directory.
2985 StringRef Path = getTok().getString();
2986 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002987 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002988
Nick Lewycky44d798d2011-10-17 23:05:28 +00002989 StringRef Directory;
2990 StringRef Filename;
2991 if (getLexer().is(AsmToken::String)) {
2992 if (FileNumber == -1)
2993 return TokError("explicit path specified, but no file number");
2994 Filename = getTok().getString();
2995 Filename = Filename.substr(1, Filename.size()-2);
2996 Directory = Path;
2997 Lex();
2998 } else {
2999 Filename = Path;
3000 }
3001
Daniel Dunbareceec052010-07-12 17:45:27 +00003002 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003003 return TokError("unexpected token in '.file' directive");
3004
Chris Lattnerd32e8032010-01-25 19:02:58 +00003005 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00003006 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00003007 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00003008 if (getContext().getGenDwarfForAssembly() == true)
3009 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
3010 "used to generate dwarf debug info for assembly code");
3011
Nick Lewycky44d798d2011-10-17 23:05:28 +00003012 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003013 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00003014 }
Daniel Dunbareceec052010-07-12 17:45:27 +00003015
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003016 return false;
3017}
3018
3019/// ParseDirectiveLine
3020/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003021bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00003022 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3023 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003024 return TokError("unexpected token in '.line' directive");
3025
Sean Callanan18b83232010-01-19 21:44:56 +00003026 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003027 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00003028 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003029
3030 // FIXME: Do something with the .line.
3031 }
3032
Daniel Dunbareceec052010-07-12 17:45:27 +00003033 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00003034 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003035
3036 return false;
3037}
3038
3039
3040/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003041/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003042/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3043/// The first number is a file number, must have been previously assigned with
3044/// a .file directive, the second number is the line number and optionally the
3045/// third number is a column position (zero if not specified). The remaining
3046/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003047bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003048
Daniel Dunbareceec052010-07-12 17:45:27 +00003049 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003050 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003051 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003052 if (FileNumber < 1)
3053 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003054 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003055 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003056 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003057
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003058 int64_t LineNumber = 0;
3059 if (getLexer().is(AsmToken::Integer)) {
3060 LineNumber = getTok().getIntVal();
3061 if (LineNumber < 1)
3062 return TokError("line number less than one in '.loc' directive");
3063 Lex();
3064 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003065
3066 int64_t ColumnPos = 0;
3067 if (getLexer().is(AsmToken::Integer)) {
3068 ColumnPos = getTok().getIntVal();
3069 if (ColumnPos < 0)
3070 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003071 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003072 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003073
Kevin Enderbyc0957932010-09-30 16:52:03 +00003074 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003075 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003076 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003077 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3078 for (;;) {
3079 if (getLexer().is(AsmToken::EndOfStatement))
3080 break;
3081
3082 StringRef Name;
3083 SMLoc Loc = getTok().getLoc();
3084 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003085 return TokError("unexpected token in '.loc' directive");
3086
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003087 if (Name == "basic_block")
3088 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3089 else if (Name == "prologue_end")
3090 Flags |= DWARF2_FLAG_PROLOGUE_END;
3091 else if (Name == "epilogue_begin")
3092 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3093 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003094 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003095 const MCExpr *Value;
3096 if (getParser().ParseExpression(Value))
3097 return true;
3098 // The expression must be the constant 0 or 1.
3099 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3100 int Value = MCE->getValue();
3101 if (Value == 0)
3102 Flags &= ~DWARF2_FLAG_IS_STMT;
3103 else if (Value == 1)
3104 Flags |= DWARF2_FLAG_IS_STMT;
3105 else
3106 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003107 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003108 else {
3109 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3110 }
3111 }
3112 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003113 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003114 const MCExpr *Value;
3115 if (getParser().ParseExpression(Value))
3116 return true;
3117 // The expression must be a constant greater or equal to 0.
3118 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3119 int Value = MCE->getValue();
3120 if (Value < 0)
3121 return Error(Loc, "isa number less than zero");
3122 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003123 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003124 else {
3125 return Error(Loc, "isa number not a constant value");
3126 }
3127 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003128 else if (Name == "discriminator") {
3129 if (getParser().ParseAbsoluteExpression(Discriminator))
3130 return true;
3131 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003132 else {
3133 return Error(Loc, "unknown sub-directive in '.loc' directive");
3134 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003135
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003136 if (getLexer().is(AsmToken::EndOfStatement))
3137 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003138 }
3139 }
3140
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003141 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003142 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003143
3144 return false;
3145}
3146
Daniel Dunbar138abae2010-10-16 04:56:42 +00003147/// ParseDirectiveStabs
3148/// ::= .stabs string, number, number, number
3149bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3150 SMLoc DirectiveLoc) {
3151 return TokError("unsupported directive '" + Directive + "'");
3152}
3153
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003154/// ParseDirectiveCFISections
3155/// ::= .cfi_sections section [, section]
3156bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3157 SMLoc DirectiveLoc) {
3158 StringRef Name;
3159 bool EH = false;
3160 bool Debug = false;
3161
3162 if (getParser().ParseIdentifier(Name))
3163 return TokError("Expected an identifier");
3164
3165 if (Name == ".eh_frame")
3166 EH = true;
3167 else if (Name == ".debug_frame")
3168 Debug = true;
3169
3170 if (getLexer().is(AsmToken::Comma)) {
3171 Lex();
3172
3173 if (getParser().ParseIdentifier(Name))
3174 return TokError("Expected an identifier");
3175
3176 if (Name == ".eh_frame")
3177 EH = true;
3178 else if (Name == ".debug_frame")
3179 Debug = true;
3180 }
3181
3182 getStreamer().EmitCFISections(EH, Debug);
3183
3184 return false;
3185}
3186
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003187/// ParseDirectiveCFIStartProc
3188/// ::= .cfi_startproc
3189bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3190 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003191 getStreamer().EmitCFIStartProc();
3192 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003193}
3194
3195/// ParseDirectiveCFIEndProc
3196/// ::= .cfi_endproc
3197bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003198 getStreamer().EmitCFIEndProc();
3199 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003200}
3201
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003202/// ParseRegisterOrRegisterNumber - parse register name or number.
3203bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3204 SMLoc DirectiveLoc) {
3205 unsigned RegNo;
3206
Jim Grosbach6f888a82011-06-02 17:14:04 +00003207 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003208 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3209 DirectiveLoc))
3210 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003211 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003212 } else
3213 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003214
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003215 return false;
3216}
3217
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003218/// ParseDirectiveCFIDefCfa
3219/// ::= .cfi_def_cfa register, offset
3220bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3221 SMLoc DirectiveLoc) {
3222 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003223 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003224 return true;
3225
3226 if (getLexer().isNot(AsmToken::Comma))
3227 return TokError("unexpected token in directive");
3228 Lex();
3229
3230 int64_t Offset = 0;
3231 if (getParser().ParseAbsoluteExpression(Offset))
3232 return true;
3233
Rafael Espindola066c2f42011-04-12 23:59:07 +00003234 getStreamer().EmitCFIDefCfa(Register, Offset);
3235 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003236}
3237
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003238/// ParseDirectiveCFIDefCfaOffset
3239/// ::= .cfi_def_cfa_offset offset
3240bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3241 SMLoc DirectiveLoc) {
3242 int64_t Offset = 0;
3243 if (getParser().ParseAbsoluteExpression(Offset))
3244 return true;
3245
Rafael Espindola066c2f42011-04-12 23:59:07 +00003246 getStreamer().EmitCFIDefCfaOffset(Offset);
3247 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003248}
3249
3250/// ParseDirectiveCFIAdjustCfaOffset
3251/// ::= .cfi_adjust_cfa_offset adjustment
3252bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3253 SMLoc DirectiveLoc) {
3254 int64_t Adjustment = 0;
3255 if (getParser().ParseAbsoluteExpression(Adjustment))
3256 return true;
3257
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003258 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3259 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003260}
3261
3262/// ParseDirectiveCFIDefCfaRegister
3263/// ::= .cfi_def_cfa_register register
3264bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3265 SMLoc DirectiveLoc) {
3266 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003267 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003268 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003269
Rafael Espindola066c2f42011-04-12 23:59:07 +00003270 getStreamer().EmitCFIDefCfaRegister(Register);
3271 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003272}
3273
3274/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003275/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003276bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3277 int64_t Register = 0;
3278 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003279
3280 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003281 return true;
3282
3283 if (getLexer().isNot(AsmToken::Comma))
3284 return TokError("unexpected token in directive");
3285 Lex();
3286
3287 if (getParser().ParseAbsoluteExpression(Offset))
3288 return true;
3289
Rafael Espindola066c2f42011-04-12 23:59:07 +00003290 getStreamer().EmitCFIOffset(Register, Offset);
3291 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003292}
3293
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003294/// ParseDirectiveCFIRelOffset
3295/// ::= .cfi_rel_offset register, offset
3296bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3297 SMLoc DirectiveLoc) {
3298 int64_t Register = 0;
3299
3300 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3301 return true;
3302
3303 if (getLexer().isNot(AsmToken::Comma))
3304 return TokError("unexpected token in directive");
3305 Lex();
3306
3307 int64_t Offset = 0;
3308 if (getParser().ParseAbsoluteExpression(Offset))
3309 return true;
3310
Rafael Espindola25f492e2011-04-12 16:12:03 +00003311 getStreamer().EmitCFIRelOffset(Register, Offset);
3312 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003313}
3314
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003315static bool isValidEncoding(int64_t Encoding) {
3316 if (Encoding & ~0xff)
3317 return false;
3318
3319 if (Encoding == dwarf::DW_EH_PE_omit)
3320 return true;
3321
3322 const unsigned Format = Encoding & 0xf;
3323 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3324 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3325 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3326 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3327 return false;
3328
Rafael Espindolacaf11582010-12-29 04:31:26 +00003329 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003330 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003331 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003332 return false;
3333
3334 return true;
3335}
3336
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003337/// ParseDirectiveCFIPersonalityOrLsda
3338/// ::= .cfi_personality encoding, [symbol_name]
3339/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003340bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003341 SMLoc DirectiveLoc) {
3342 int64_t Encoding = 0;
3343 if (getParser().ParseAbsoluteExpression(Encoding))
3344 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003345 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003346 return false;
3347
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003348 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003349 return TokError("unsupported encoding.");
3350
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003351 if (getLexer().isNot(AsmToken::Comma))
3352 return TokError("unexpected token in directive");
3353 Lex();
3354
3355 StringRef Name;
3356 if (getParser().ParseIdentifier(Name))
3357 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003358
3359 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3360
3361 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003362 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003363 else {
3364 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003365 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003366 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003367 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003368}
3369
Rafael Espindolafe024d02010-12-28 18:36:23 +00003370/// ParseDirectiveCFIRememberState
3371/// ::= .cfi_remember_state
3372bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3373 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003374 getStreamer().EmitCFIRememberState();
3375 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003376}
3377
3378/// ParseDirectiveCFIRestoreState
3379/// ::= .cfi_remember_state
3380bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3381 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003382 getStreamer().EmitCFIRestoreState();
3383 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003384}
3385
Rafael Espindolac5754392011-04-12 15:31:05 +00003386/// ParseDirectiveCFISameValue
3387/// ::= .cfi_same_value register
3388bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3389 SMLoc DirectiveLoc) {
3390 int64_t Register = 0;
3391
3392 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3393 return true;
3394
3395 getStreamer().EmitCFISameValue(Register);
3396
3397 return false;
3398}
3399
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003400/// ParseDirectiveCFIRestore
3401/// ::= .cfi_restore register
3402bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003403 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003404 int64_t Register = 0;
3405 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3406 return true;
3407
3408 getStreamer().EmitCFIRestore(Register);
3409
3410 return false;
3411}
3412
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003413/// ParseDirectiveCFIEscape
3414/// ::= .cfi_escape expression[,...]
3415bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003416 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003417 std::string Values;
3418 int64_t CurrValue;
3419 if (getParser().ParseAbsoluteExpression(CurrValue))
3420 return true;
3421
3422 Values.push_back((uint8_t)CurrValue);
3423
3424 while (getLexer().is(AsmToken::Comma)) {
3425 Lex();
3426
3427 if (getParser().ParseAbsoluteExpression(CurrValue))
3428 return true;
3429
3430 Values.push_back((uint8_t)CurrValue);
3431 }
3432
3433 getStreamer().EmitCFIEscape(Values);
3434 return false;
3435}
3436
Rafael Espindola16d7d432012-01-23 21:51:52 +00003437/// ParseDirectiveCFISignalFrame
3438/// ::= .cfi_signal_frame
3439bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3440 SMLoc DirectiveLoc) {
3441 if (getLexer().isNot(AsmToken::EndOfStatement))
3442 return Error(getLexer().getLoc(),
3443 "unexpected token in '" + Directive + "' directive");
3444
3445 getStreamer().EmitCFISignalFrame();
3446
3447 return false;
3448}
3449
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003450/// ParseDirectiveCFIUndefined
3451/// ::= .cfi_undefined register
3452bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3453 SMLoc DirectiveLoc) {
3454 int64_t Register = 0;
3455
3456 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3457 return true;
3458
3459 getStreamer().EmitCFIUndefined(Register);
3460
3461 return false;
3462}
3463
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003464/// ParseDirectiveCFIRegister
3465/// ::= .cfi_register register, register
3466bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3467 SMLoc DirectiveLoc) {
3468 int64_t Register1 = 0;
3469
3470 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3471 return true;
3472
3473 if (getLexer().isNot(AsmToken::Comma))
3474 return TokError("unexpected token in directive");
3475 Lex();
3476
3477 int64_t Register2 = 0;
3478
3479 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3480 return true;
3481
3482 getStreamer().EmitCFIRegister(Register1, Register2);
3483
3484 return false;
3485}
3486
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003487/// ParseDirectiveMacrosOnOff
3488/// ::= .macros_on
3489/// ::= .macros_off
3490bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3491 SMLoc DirectiveLoc) {
3492 if (getLexer().isNot(AsmToken::EndOfStatement))
3493 return Error(getLexer().getLoc(),
3494 "unexpected token in '" + Directive + "' directive");
3495
3496 getParser().MacrosEnabled = Directive == ".macros_on";
3497
3498 return false;
3499}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003500
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003501/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003502/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003503bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3504 SMLoc DirectiveLoc) {
3505 StringRef Name;
3506 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003507 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003508
Rafael Espindola8a403d32012-08-08 14:51:03 +00003509 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003510 // Argument delimiter is initially unknown. It will be set by
3511 // ParseMacroArgument()
3512 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003513 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003514 for (;;) {
3515 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003516 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003517 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003518
3519 if (getLexer().is(AsmToken::Equal)) {
3520 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003521 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003522 return true;
3523 }
3524
Rafael Espindola65366442011-06-05 02:43:45 +00003525 Parameters.push_back(Parameter);
3526
Preston Gurd7b6f2032012-09-19 20:36:12 +00003527 if (getLexer().is(AsmToken::Comma))
3528 Lex();
3529 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003530 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003531 }
3532 }
3533
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003534 // Eat the end of statement.
3535 Lex();
3536
3537 AsmToken EndToken, StartToken = getTok();
3538
3539 // Lex the macro definition.
3540 for (;;) {
3541 // Check whether we have reached the end of the file.
3542 if (getLexer().is(AsmToken::Eof))
3543 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3544
3545 // Otherwise, check whether we have reach the .endmacro.
3546 if (getLexer().is(AsmToken::Identifier) &&
3547 (getTok().getIdentifier() == ".endm" ||
3548 getTok().getIdentifier() == ".endmacro")) {
3549 EndToken = getTok();
3550 Lex();
3551 if (getLexer().isNot(AsmToken::EndOfStatement))
3552 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3553 "' directive");
3554 break;
3555 }
3556
3557 // Otherwise, scan til the end of the statement.
3558 getParser().EatToEndOfStatement();
3559 }
3560
3561 if (getParser().MacroMap.lookup(Name)) {
3562 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3563 }
3564
3565 const char *BodyStart = StartToken.getLoc().getPointer();
3566 const char *BodyEnd = EndToken.getLoc().getPointer();
3567 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003568 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003569 return false;
3570}
3571
3572/// ParseDirectiveEndMacro
3573/// ::= .endm
3574/// ::= .endmacro
3575bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003576 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003577 if (getLexer().isNot(AsmToken::EndOfStatement))
3578 return TokError("unexpected token in '" + Directive + "' directive");
3579
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003580 // If we are inside a macro instantiation, terminate the current
3581 // instantiation.
3582 if (!getParser().ActiveMacros.empty()) {
3583 getParser().HandleMacroExit();
3584 return false;
3585 }
3586
3587 // Otherwise, this .endmacro is a stray entry in the file; well formed
3588 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003589 return TokError("unexpected '" + Directive + "' in file, "
3590 "no current macro definition");
3591}
3592
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003593/// ParseDirectivePurgeMacro
3594/// ::= .purgem
3595bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3596 SMLoc DirectiveLoc) {
3597 StringRef Name;
3598 if (getParser().ParseIdentifier(Name))
3599 return TokError("expected identifier in '.purgem' directive");
3600
3601 if (getLexer().isNot(AsmToken::EndOfStatement))
3602 return TokError("unexpected token in '.purgem' directive");
3603
3604 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3605 if (I == getParser().MacroMap.end())
3606 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3607
3608 // Undefine the macro.
3609 delete I->getValue();
3610 getParser().MacroMap.erase(I);
3611 return false;
3612}
3613
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003614bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003615 getParser().CheckForValidSection();
3616
3617 const MCExpr *Value;
3618
3619 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003620 return true;
3621
3622 if (getLexer().isNot(AsmToken::EndOfStatement))
3623 return TokError("unexpected token in directive");
3624
3625 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003626 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003627 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003628 getStreamer().EmitULEB128Value(Value);
3629
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003630 return false;
3631}
3632
Rafael Espindola761cb062012-06-03 23:57:14 +00003633Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003634 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003635
Rafael Espindola761cb062012-06-03 23:57:14 +00003636 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003637 for (;;) {
3638 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003639 if (getLexer().is(AsmToken::Eof)) {
3640 Error(DirectiveLoc, "no matching '.endr' in definition");
3641 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003642 }
3643
Rafael Espindola761cb062012-06-03 23:57:14 +00003644 if (Lexer.is(AsmToken::Identifier) &&
3645 (getTok().getIdentifier() == ".rept")) {
3646 ++NestLevel;
3647 }
3648
3649 // Otherwise, check whether we have reached the .endr.
3650 if (Lexer.is(AsmToken::Identifier) &&
3651 getTok().getIdentifier() == ".endr") {
3652 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003653 EndToken = getTok();
3654 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003655 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3656 TokError("unexpected token in '.endr' directive");
3657 return 0;
3658 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003659 break;
3660 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003661 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003662 }
3663
Rafael Espindola761cb062012-06-03 23:57:14 +00003664 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003665 EatToEndOfStatement();
3666 }
3667
3668 const char *BodyStart = StartToken.getLoc().getPointer();
3669 const char *BodyEnd = EndToken.getLoc().getPointer();
3670 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3671
Rafael Espindola761cb062012-06-03 23:57:14 +00003672 // We Are Anonymous.
3673 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003674 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003675 return new Macro(Name, Body, Parameters);
3676}
3677
3678void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3679 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003680 OS << ".endr\n";
3681
3682 MemoryBuffer *Instantiation =
3683 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3684
Rafael Espindola761cb062012-06-03 23:57:14 +00003685 // Create the macro instantiation object and add to the current macro
3686 // instantiation stack.
3687 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00003688 CurBuffer,
Rafael Espindola761cb062012-06-03 23:57:14 +00003689 getTok().getLoc(),
3690 Instantiation);
3691 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003692
Rafael Espindola761cb062012-06-03 23:57:14 +00003693 // Jump to the macro instantiation and prime the lexer.
3694 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3695 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3696 Lex();
3697}
3698
3699bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3700 int64_t Count;
3701 if (ParseAbsoluteExpression(Count))
3702 return TokError("unexpected token in '.rept' directive");
3703
3704 if (Count < 0)
3705 return TokError("Count is negative");
3706
3707 if (Lexer.isNot(AsmToken::EndOfStatement))
3708 return TokError("unexpected token in '.rept' directive");
3709
3710 // Eat the end of statement.
3711 Lex();
3712
3713 // Lex the rept definition.
3714 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3715 if (!M)
3716 return true;
3717
3718 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3719 // to hold the macro body with substitutions.
3720 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003721 MacroParameters Parameters;
3722 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003723 raw_svector_ostream OS(Buf);
3724 while (Count--) {
3725 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3726 return true;
3727 }
3728 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003729
3730 return false;
3731}
3732
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003733/// ParseDirectiveIrp
3734/// ::= .irp symbol,values
3735bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003736 MacroParameters Parameters;
3737 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003738
Preston Gurd6c9176a2012-09-19 20:29:04 +00003739 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003740 return TokError("expected identifier in '.irp' directive");
3741
3742 Parameters.push_back(Parameter);
3743
3744 if (Lexer.isNot(AsmToken::Comma))
3745 return TokError("expected comma in '.irp' directive");
3746
3747 Lex();
3748
Rafael Espindola8a403d32012-08-08 14:51:03 +00003749 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003750 if (ParseMacroArguments(0, A))
3751 return true;
3752
3753 // Eat the end of statement.
3754 Lex();
3755
3756 // Lex the irp definition.
3757 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3758 if (!M)
3759 return true;
3760
3761 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3762 // to hold the macro body with substitutions.
3763 SmallString<256> Buf;
3764 raw_svector_ostream OS(Buf);
3765
Rafael Espindola7996d042012-08-21 16:06:48 +00003766 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3767 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003768 Args.push_back(*i);
3769
3770 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3771 return true;
3772 }
3773
3774 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3775
3776 return false;
3777}
3778
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003779/// ParseDirectiveIrpc
3780/// ::= .irpc symbol,values
3781bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003782 MacroParameters Parameters;
3783 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003784
Preston Gurd6c9176a2012-09-19 20:29:04 +00003785 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003786 return TokError("expected identifier in '.irpc' directive");
3787
3788 Parameters.push_back(Parameter);
3789
3790 if (Lexer.isNot(AsmToken::Comma))
3791 return TokError("expected comma in '.irpc' directive");
3792
3793 Lex();
3794
Rafael Espindola8a403d32012-08-08 14:51:03 +00003795 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003796 if (ParseMacroArguments(0, A))
3797 return true;
3798
3799 if (A.size() != 1 || A.front().size() != 1)
3800 return TokError("unexpected token in '.irpc' directive");
3801
3802 // Eat the end of statement.
3803 Lex();
3804
3805 // Lex the irpc definition.
3806 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3807 if (!M)
3808 return true;
3809
3810 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3811 // to hold the macro body with substitutions.
3812 SmallString<256> Buf;
3813 raw_svector_ostream OS(Buf);
3814
3815 StringRef Values = A.front().front().getString();
3816 std::size_t I, End = Values.size();
3817 for (I = 0; I < End; ++I) {
3818 MacroArgument Arg;
3819 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3820
Rafael Espindola8a403d32012-08-08 14:51:03 +00003821 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003822 Args.push_back(Arg);
3823
3824 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3825 return true;
3826 }
3827
3828 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3829
3830 return false;
3831}
3832
Rafael Espindola761cb062012-06-03 23:57:14 +00003833bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3834 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003835 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003836
3837 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003838 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003839 assert(getLexer().is(AsmToken::EndOfStatement));
3840
Rafael Espindola761cb062012-06-03 23:57:14 +00003841 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003842 return false;
3843}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003844
Eli Friedman2128aae2012-10-22 23:58:19 +00003845bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) {
3846 const MCExpr *Value;
3847 SMLoc ExprLoc = getLexer().getLoc();
3848 if (ParseExpression(Value))
3849 return true;
3850 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3851 if (!MCE)
3852 return Error(ExprLoc, "unexpected expression in _emit");
3853 uint64_t IntValue = MCE->getValue();
3854 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
3855 return Error(ExprLoc, "literal value out of range for directive");
3856
3857 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5));
3858 return false;
3859}
3860
Chad Rosierb1f8c132012-10-18 15:49:34 +00003861bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3862 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003863 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003864 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003865 SmallVectorImpl<std::string> &Clobbers,
3866 const MCInstrInfo *MII,
3867 const MCInstPrinter *IP,
3868 MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003869 SmallVector<void *, 4> InputDecls;
3870 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003871 SmallVector<bool, 4> InputDeclsAddressOf;
3872 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003873 SmallVector<std::string, 4> InputConstraints;
3874 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003875 std::set<std::string> ClobberRegs;
3876
Chad Rosier4e472d22012-10-20 01:02:45 +00003877 SmallVector<struct AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003878
3879 // Prime the lexer.
3880 Lex();
3881
3882 // While we have input, parse each statement.
3883 unsigned InputIdx = 0;
3884 unsigned OutputIdx = 0;
3885 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00003886 ParseStatementInfo Info(&AsmStrRewrites);
3887 if (ParseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00003888 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003889
Chad Rosier57498012012-12-12 22:45:52 +00003890 if (Info.ParseError)
3891 return true;
3892
Eli Friedman2128aae2012-10-22 23:58:19 +00003893 if (Info.Opcode != ~0U) {
3894 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003895
3896 // Build the list of clobbers, outputs and inputs.
Eli Friedman2128aae2012-10-22 23:58:19 +00003897 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
3898 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003899
3900 // Immediate.
3901 if (Operand->isImm()) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00003902 if (Operand->needAsmRewrite())
3903 AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix,
3904 Operand->getStartLoc()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003905 continue;
3906 }
3907
3908 // Register operand.
Chad Rosierc1ec2072013-01-10 22:10:27 +00003909 if (Operand->isReg() && !Operand->needAddressOf()) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003910 unsigned NumDefs = Desc.getNumDefs();
3911 // Clobber.
3912 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3913 std::string Reg;
3914 raw_string_ostream OS(Reg);
3915 IP->printRegName(OS, Operand->getReg());
3916 ClobberRegs.insert(StringRef(OS.str()));
3917 }
3918 continue;
3919 }
3920
3921 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003922 unsigned Size;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003923 bool IsVarDecl;
Chad Rosier32989592012-10-18 20:27:15 +00003924 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +00003925 Size, IsVarDecl);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003926 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003927 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierc1ec2072013-01-10 22:10:27 +00003928 if (Operand->isMem() && Operand->needSizeDirective())
Chad Rosier4e472d22012-10-20 01:02:45 +00003929 AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective,
Chad Rosierefcb3d92012-10-26 18:04:20 +00003930 Operand->getStartLoc(),
3931 /*Len*/0,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003932 Operand->getMemSize()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003933 if (isOutput) {
3934 std::string Constraint = "=";
3935 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003936 OutputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003937 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003938 Constraint += Operand->getConstraint().str();
3939 OutputConstraints.push_back(Constraint);
Chad Rosier4e472d22012-10-20 01:02:45 +00003940 AsmStrRewrites.push_back(AsmRewrite(AOK_Output,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003941 Operand->getStartLoc(),
3942 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003943 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003944 InputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003945 InputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003946 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosier4e472d22012-10-20 01:02:45 +00003947 AsmStrRewrites.push_back(AsmRewrite(AOK_Input,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003948 Operand->getStartLoc(),
3949 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003950 }
3951 }
3952 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003953 }
3954 }
3955
3956 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003957 NumOutputs = OutputDecls.size();
3958 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003959
3960 // Set the unique clobbers.
3961 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3962 E = ClobberRegs.end(); I != E; ++I)
3963 Clobbers.push_back(*I);
3964
3965 // Merge the various outputs and inputs. Output are expected first.
3966 if (NumOutputs || NumInputs) {
3967 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003968 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003969 Constraints.resize(NumExprs);
Chad Rosier5a719fc2012-10-23 17:43:43 +00003970 // FIXME: Constraints are hard coded to 'm', but we need an 'r'
Chad Rosierc1ec2072013-01-10 22:10:27 +00003971 // constraint for addressof. This needs to be cleaned up!
Chad Rosierb1f8c132012-10-18 15:49:34 +00003972 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003973 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
3974 Constraints[i] = OutputDeclsAddressOf[i] ? "=r" : OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003975 }
3976 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003977 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
3978 Constraints[j] = InputDeclsAddressOf[i] ? "r" : InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003979 }
3980 }
3981
3982 // Build the IR assembly string.
3983 std::string AsmStringIR;
Chad Rosier4e472d22012-10-20 01:02:45 +00003984 AsmRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003985 raw_string_ostream OS(AsmStringIR);
3986 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
Chad Rosier4e472d22012-10-20 01:02:45 +00003987 for (SmallVectorImpl<struct AsmRewrite>::iterator
Chad Rosierb1f8c132012-10-18 15:49:34 +00003988 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3989 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003990
Chad Rosier4e472d22012-10-20 01:02:45 +00003991 AsmRewriteKind Kind = (*I).Kind;
Chad Rosier96d58e62012-10-19 20:57:14 +00003992
3993 // Emit everything up to the immediate/expression. If the previous rewrite
3994 // was a size directive, then this has already been done.
3995 if (PrevKind != AOK_SizeDirective)
3996 OS << StringRef(Start, Loc - Start);
3997 PrevKind = Kind;
3998
Chad Rosier5a719fc2012-10-23 17:43:43 +00003999 // Skip the original expression.
4000 if (Kind == AOK_Skip) {
4001 Start = Loc + (*I).Len;
4002 continue;
4003 }
4004
Chad Rosierb1f8c132012-10-18 15:49:34 +00004005 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004006 switch (Kind) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004007 default: break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004008 case AOK_Imm:
Chad Rosierefcb3d92012-10-26 18:04:20 +00004009 OS << Twine("$$");
4010 OS << (*I).Val;
4011 break;
4012 case AOK_ImmPrefix:
4013 OS << Twine("$$");
Chad Rosierb1f8c132012-10-18 15:49:34 +00004014 break;
4015 case AOK_Input:
4016 OS << '$';
4017 OS << InputIdx++;
4018 break;
4019 case AOK_Output:
4020 OS << '$';
4021 OS << OutputIdx++;
4022 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004023 case AOK_SizeDirective:
Chad Rosier6a020a72012-10-25 20:41:34 +00004024 switch((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004025 default: break;
4026 case 8: OS << "byte ptr "; break;
4027 case 16: OS << "word ptr "; break;
4028 case 32: OS << "dword ptr "; break;
4029 case 64: OS << "qword ptr "; break;
4030 case 80: OS << "xword ptr "; break;
4031 case 128: OS << "xmmword ptr "; break;
4032 case 256: OS << "ymmword ptr "; break;
4033 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004034 break;
4035 case AOK_Emit:
4036 OS << ".byte";
4037 break;
Chad Rosier6a020a72012-10-25 20:41:34 +00004038 case AOK_DotOperator:
4039 OS << (*I).Val;
4040 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004041 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004042
Chad Rosierb1f8c132012-10-18 15:49:34 +00004043 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00004044 if (Kind != AOK_SizeDirective)
4045 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004046 }
4047
4048 // Emit the remainder of the asm string.
4049 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4050 if (Start != AsmEnd)
4051 OS << StringRef(Start, AsmEnd - Start);
4052
4053 AsmString = OS.str();
4054 return false;
4055}
4056
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004057/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004058MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004059 MCContext &C, MCStreamer &Out,
4060 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004061 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004062}