blob: 2aecb0cdb7211c80beb8e57b74f10d3b771fb33f [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
Eli Benderskybf706b32013-01-12 00:05:00 +0000229 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
230 /// and set \p Res to the identifier contents.
231 virtual bool ParseIdentifier(StringRef &Res);
232
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000233 /// }
234
235private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000236 void CheckForValidSection();
237
Eli Friedman2128aae2012-10-22 23:58:19 +0000238 bool ParseStatement(ParseStatementInfo &Info);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000239 void EatToEndOfLine();
240 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000241
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000242 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000243 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000244 const MacroParameters &Parameters,
245 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000246 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000247 void HandleMacroExit();
248
249 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000250 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000251 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
252 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000253 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000254 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000255
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000256 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
257 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000258 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
259 /// This returns true on failure.
260 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000261
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000262 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000263 /// current token is not set; clients should ensure Lex() is called
264 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000265 ///
266 /// \param InBuffer If not -1, should be the known buffer id that contains the
267 /// location.
268 void JumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000269
Craig Topper345d16d2012-08-29 05:48:09 +0000270 virtual void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000271
Preston Gurd7b6f2032012-09-19 20:36:12 +0000272 bool ParseMacroArgument(MacroArgument &MA,
273 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000274 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000275
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000276 /// \brief Parse up to the end of statement and a return the contents from the
277 /// current token until the end of the statement; the current token on exit
278 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000279 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000280
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000281 /// \brief Parse until the end of a statement or a comma is encountered,
282 /// return the contents from the current token up to the end or comma.
283 StringRef ParseStringToComma();
284
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000285 bool ParseAssignment(StringRef Name, bool allow_redef,
286 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000287
288 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
289 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
290 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000291 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +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,
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000300 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000301 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
302 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL,
303 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
304 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
305 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
306 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
307 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
308 DK_ELSEIF, DK_ELSE, DK_ENDIF
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000309 };
310
311 StringMap<DirectiveKind> DirectiveKindMapping;
312
313 // ".ascii", ".asciz", ".string"
Rafael Espindola787c3372010-10-28 20:02:27 +0000314 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000315 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000316 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000317 bool ParseDirectiveFill(); // ".fill"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000318 bool ParseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000319 // ".set", ".equ", ".equiv"
320 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000321 bool ParseDirectiveOrg(); // ".org"
322 // ".align{,32}", ".p2align{,w,l}"
323 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
324
Eli Bendersky4766ef42012-12-20 19:05:53 +0000325 // ".bundle_align_mode"
326 bool ParseDirectiveBundleAlignMode();
327 // ".bundle_lock"
328 bool ParseDirectiveBundleLock();
329 // ".bundle_unlock"
330 bool ParseDirectiveBundleUnlock();
331
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000332 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
333 /// accepts a single symbol (which should be a label or an external).
334 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000335
336 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
337
338 bool ParseDirectiveAbort(); // ".abort"
339 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000340 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000341
342 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000343 // ".ifb" or ".ifnb", depending on ExpectBlank.
344 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000345 // ".ifc" or ".ifnc", depending on ExpectEqual.
346 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000347 // ".ifdef" or ".ifndef", depending on expect_defined
348 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000349 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
350 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
351 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
352
353 /// ParseEscapedString - Parse the current token as a string which may include
354 /// escaped characters and return the string contents.
355 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000356
357 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
358 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000359
Rafael Espindola761cb062012-06-03 23:57:14 +0000360 // Macro-like directives
361 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
362 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
363 raw_svector_ostream &OS);
364 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000365 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000366 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000367 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000368
Eli Friedman2128aae2012-10-22 23:58:19 +0000369 // "_emit"
370 bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000371
372 void initializeDirectiveKindMapping();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000373};
374
Eli Bendersky63e6f482013-01-10 23:32:57 +0000375/// \brief Generic implementation of directive handling, etc. which is shared
376/// (or the default, at least) for all assembler parsers.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000377class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000378 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
379 void AddDirectiveHandler(StringRef Directive) {
380 getParser().AddDirectiveHandler(this, Directive,
381 HandleDirective<GenericAsmParser, Handler>);
382 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000383public:
384 GenericAsmParser() {}
385
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000386 AsmParser &getParser() {
387 return (AsmParser&) this->MCAsmParserExtension::getParser();
388 }
389
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000390 virtual void Initialize(MCAsmParser &Parser) {
391 // Call the base implementation.
392 this->MCAsmParserExtension::Initialize(Parser);
393
394 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000395 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
396 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
397 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000398 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000399
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000400 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".space");
401 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".skip");
402
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000403 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000404 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
405 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000406 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
407 ".cfi_startproc");
408 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
409 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000410 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
411 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000412 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
413 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000414 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
415 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000416 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
417 ".cfi_def_cfa_register");
418 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
419 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000420 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
421 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000422 AddDirectiveHandler<
423 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
424 AddDirectiveHandler<
425 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000426 AddDirectiveHandler<
427 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
428 AddDirectiveHandler<
429 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000430 AddDirectiveHandler<
431 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000432 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000433 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
434 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000435 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000436 AddDirectiveHandler<
437 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000438 AddDirectiveHandler<
439 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000440 AddDirectiveHandler<
441 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000442
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000443 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000444 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
445 ".macros_on");
446 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
447 ".macros_off");
448 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
449 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
450 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000451 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000452
453 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
454 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000455 }
456
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000457 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
458
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000459 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
460 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
461 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000462 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000463 bool ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000464 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000465 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
466 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000467 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000468 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000469 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000470 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
471 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000472 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000473 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000474 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
475 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000476 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000477 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000478 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000479 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000480 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000481 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000482
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000483 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000484 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
485 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000486 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000487
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000488 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000489};
490
491}
492
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000493namespace llvm {
494
495extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000496extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000497extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000498
499}
500
Chris Lattneraaec2052010-01-19 19:46:13 +0000501enum { DEFAULT_ADDRSPACE = 0 };
502
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000503AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000504 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000505 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000506 GenericParser(new GenericAsmParser), PlatformParser(0),
Preston Gurd7b6f2032012-09-19 20:36:12 +0000507 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000508 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000509 // Save the old handler.
510 SavedDiagHandler = SrcMgr.getDiagHandler();
511 SavedDiagContext = SrcMgr.getDiagContext();
512 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000513 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000514 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000515
516 // Initialize the generic parser.
517 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000518
519 // Initialize the platform / file format parser.
520 //
521 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
522 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000523 if (_MAI.hasMicrosoftFastStdCallMangling()) {
524 PlatformParser = createCOFFAsmParser();
525 PlatformParser->Initialize(*this);
526 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000527 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000528 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000529 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000530 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000531 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000532 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000533 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000534
535 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000536}
537
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000538AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000539 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
540
541 // Destroy any macros.
542 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
543 ie = MacroMap.end(); it != ie; ++it)
544 delete it->getValue();
545
Daniel Dunbare4749702010-07-12 18:12:02 +0000546 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000547 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000548}
549
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000550void AsmParser::PrintMacroInstantiations() {
551 // Print the active macro instantiation stack.
552 for (std::vector<MacroInstantiation*>::const_reverse_iterator
553 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000554 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
555 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000556}
557
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000558bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000559 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000560 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000561 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000562 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000563 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000564}
565
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000566bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000567 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000568 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000569 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000570 return true;
571}
572
Sean Callananfd0b0282010-01-21 00:19:58 +0000573bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000574 std::string IncludedFile;
575 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000576 if (NewBuf == -1)
577 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000578
Sean Callananfd0b0282010-01-21 00:19:58 +0000579 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000580
Sean Callananfd0b0282010-01-21 00:19:58 +0000581 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000582
Sean Callananfd0b0282010-01-21 00:19:58 +0000583 return false;
584}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000585
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000586/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000587/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000588/// returns true on failure.
589bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
590 std::string IncludedFile;
591 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
592 if (NewBuf == -1)
593 return true;
594
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000595 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000596 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
597 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000598 return false;
599}
600
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000601void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
602 if (InBuffer != -1) {
603 CurBuffer = InBuffer;
604 } else {
605 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
606 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000607 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
608}
609
Sean Callananfd0b0282010-01-21 00:19:58 +0000610const AsmToken &AsmParser::Lex() {
611 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000612
Sean Callananfd0b0282010-01-21 00:19:58 +0000613 if (tok->is(AsmToken::Eof)) {
614 // If this is the end of an included file, pop the parent file off the
615 // include stack.
616 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
617 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000618 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000619 tok = &Lexer.Lex();
620 }
621 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000622
Sean Callananfd0b0282010-01-21 00:19:58 +0000623 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000624 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000625
Sean Callananfd0b0282010-01-21 00:19:58 +0000626 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000627}
628
Chris Lattner79180e22010-04-05 23:15:42 +0000629bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000630 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000631 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000632 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000633
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000634 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000635 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000636
637 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000638 AsmCond StartingCondState = TheCondState;
639
Kevin Enderby613b7572011-11-01 22:27:22 +0000640 // If we are generating dwarf for assembly source files save the initial text
641 // section and generate a .file directive.
642 if (getContext().getGenDwarfForAssembly()) {
643 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000644 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
645 getStreamer().EmitLabel(SectionStartSym);
646 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000647 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000648 StringRef(),
649 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000650 }
651
Chris Lattnerb717fb02009-07-02 21:53:43 +0000652 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000653 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000654 ParseStatementInfo Info;
655 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000656
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000657 // We had an error, validate that one was emitted and recover by skipping to
658 // the next line.
659 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000660 EatToEndOfStatement();
661 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000662
663 if (TheCondState.TheCond != StartingCondState.TheCond ||
664 TheCondState.Ignore != StartingCondState.Ignore)
665 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000666
667 // Check to see there are no empty DwarfFile slots.
668 const std::vector<MCDwarfFile *> &MCDwarfFiles =
669 getContext().getMCDwarfFiles();
670 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000671 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000672 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000673 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000674
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000675 // Check to see that all assembler local symbols were actually defined.
676 // Targets that don't do subsections via symbols may not want this, though,
677 // so conservatively exclude them. Only do this if we're finalizing, though,
678 // as otherwise we won't necessarilly have seen everything yet.
679 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
680 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
681 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
682 e = Symbols.end();
683 i != e; ++i) {
684 MCSymbol *Sym = i->getValue();
685 // Variable symbols may not be marked as defined, so check those
686 // explicitly. If we know it's a variable, we have a definition for
687 // the purposes of this check.
688 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
689 // FIXME: We would really like to refer back to where the symbol was
690 // first referenced for a source location. We need to add something
691 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000692 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
693 "assembler local symbol '" + Sym->getName() +
694 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000695 }
696 }
697
698
Chris Lattner79180e22010-04-05 23:15:42 +0000699 // Finalize the output stream if there are no errors and if the client wants
700 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000701 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000702 Out.Finish();
703
Chris Lattnerb717fb02009-07-02 21:53:43 +0000704 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000705}
706
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000707void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000708 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000709 TokError("expected section directive before assembly directive");
710 Out.SwitchSection(Ctx.getMachOSection(
711 "__TEXT", "__text",
712 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
713 0, SectionKind::getText()));
714 }
715}
716
Chris Lattner2cf5f142009-06-22 01:29:09 +0000717/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
718void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000719 while (Lexer.isNot(AsmToken::EndOfStatement) &&
720 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000721 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000722
Chris Lattner2cf5f142009-06-22 01:29:09 +0000723 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000724 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000725 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000726}
727
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000728StringRef AsmParser::ParseStringToEndOfStatement() {
729 const char *Start = getTok().getLoc().getPointer();
730
731 while (Lexer.isNot(AsmToken::EndOfStatement) &&
732 Lexer.isNot(AsmToken::Eof))
733 Lex();
734
735 const char *End = getTok().getLoc().getPointer();
736 return StringRef(Start, End - Start);
737}
Chris Lattnerc4193832009-06-22 05:51:26 +0000738
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000739StringRef AsmParser::ParseStringToComma() {
740 const char *Start = getTok().getLoc().getPointer();
741
742 while (Lexer.isNot(AsmToken::EndOfStatement) &&
743 Lexer.isNot(AsmToken::Comma) &&
744 Lexer.isNot(AsmToken::Eof))
745 Lex();
746
747 const char *End = getTok().getLoc().getPointer();
748 return StringRef(Start, End - Start);
749}
750
Chris Lattner74ec1a32009-06-22 06:32:03 +0000751/// ParseParenExpr - Parse a paren expression and return it.
752/// NOTE: This assumes the leading '(' has already been consumed.
753///
754/// parenexpr ::= expr)
755///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000756bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000757 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000758 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000759 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000760 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000761 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000762 return false;
763}
Chris Lattnerc4193832009-06-22 05:51:26 +0000764
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000765/// ParseBracketExpr - Parse a bracket expression and return it.
766/// NOTE: This assumes the leading '[' has already been consumed.
767///
768/// bracketexpr ::= expr]
769///
770bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
771 if (ParseExpression(Res)) return true;
772 if (Lexer.isNot(AsmToken::RBrac))
773 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000774 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000775 Lex();
776 return false;
777}
778
Chris Lattner74ec1a32009-06-22 06:32:03 +0000779/// ParsePrimaryExpr - Parse a primary expression and return it.
780/// primaryexpr ::= (parenexpr
781/// primaryexpr ::= symbol
782/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000783/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000784/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000785bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000786 switch (Lexer.getKind()) {
787 default:
788 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000789 // If we have an error assume that we've already handled it.
790 case AsmToken::Error:
791 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000792 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000793 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000794 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000795 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000796 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000798 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000799 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000800 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000801 StringRef Identifier;
802 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000803 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000804
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000805 EndLoc = SMLoc::getFromPointer(Identifier.end());
806
Daniel Dunbarfffff912009-10-16 01:34:54 +0000807 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000808 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000809 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000810
811 // Lookup the symbol variant if used.
812 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000813 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000814 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000815 if (Variant == MCSymbolRefExpr::VK_Invalid) {
816 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000817 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000818 }
819 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000820
Daniel Dunbarfffff912009-10-16 01:34:54 +0000821 // If this is an absolute variable reference, substitute it now to preserve
822 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000823 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000824 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000825 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000826
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000827 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000828 return false;
829 }
830
831 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000832 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000833 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000834 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000835 case AsmToken::Integer: {
836 SMLoc Loc = getTok().getLoc();
837 int64_t IntVal = getTok().getIntVal();
838 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000839 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000840 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000841 // Look for 'b' or 'f' following an Integer as a directional label
842 if (Lexer.getKind() == AsmToken::Identifier) {
843 StringRef IDVal = getTok().getString();
844 if (IDVal == "f" || IDVal == "b"){
845 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
846 IDVal == "f" ? 1 : 0);
847 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
848 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000849 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000850 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000851 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000852 Lex(); // Eat identifier.
853 }
854 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000855 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000856 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000857 case AsmToken::Real: {
858 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000859 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000860 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000861 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000862 Lex(); // Eat token.
863 return false;
864 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000865 case AsmToken::Dot: {
866 // This is a '.' reference, which references the current PC. Emit a
867 // temporary label to the streamer and refer to it.
868 MCSymbol *Sym = Ctx.CreateTempSymbol();
869 Out.EmitLabel(Sym);
870 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000871 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000872 Lex(); // Eat identifier.
873 return false;
874 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000875 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000876 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000877 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000878 case AsmToken::LBrac:
879 if (!PlatformParser->HasBracketExpressions())
880 return TokError("brackets expression not supported on this target");
881 Lex(); // Eat the '['.
882 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000883 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000884 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000885 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000886 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000887 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000888 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000889 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000890 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000891 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000892 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000893 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000894 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000895 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000896 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000897 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000898 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000899 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000900 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000901 }
902}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000903
Chris Lattnerb4307b32010-01-15 19:28:38 +0000904bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000905 SMLoc EndLoc;
906 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000907}
908
Daniel Dunbarcceba832010-09-17 02:47:07 +0000909const MCExpr *
910AsmParser::ApplyModifierToExpr(const MCExpr *E,
911 MCSymbolRefExpr::VariantKind Variant) {
912 // Recurse over the given expression, rebuilding it to apply the given variant
913 // if there is exactly one symbol.
914 switch (E->getKind()) {
915 case MCExpr::Target:
916 case MCExpr::Constant:
917 return 0;
918
919 case MCExpr::SymbolRef: {
920 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
921
922 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
923 TokError("invalid variant on expression '" +
924 getTok().getIdentifier() + "' (already modified)");
925 return E;
926 }
927
928 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
929 }
930
931 case MCExpr::Unary: {
932 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
933 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
934 if (!Sub)
935 return 0;
936 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
937 }
938
939 case MCExpr::Binary: {
940 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
941 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
942 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
943
944 if (!LHS && !RHS)
945 return 0;
946
947 if (!LHS) LHS = BE->getLHS();
948 if (!RHS) RHS = BE->getRHS();
949
950 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
951 }
952 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000953
Craig Topper85814382012-02-07 05:05:23 +0000954 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000955}
956
Chris Lattner74ec1a32009-06-22 06:32:03 +0000957/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000958///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000959/// expr ::= expr &&,|| expr -> lowest.
960/// expr ::= expr |,^,&,! expr
961/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
962/// expr ::= expr <<,>> expr
963/// expr ::= expr +,- expr
964/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000965/// expr ::= primaryexpr
966///
Chris Lattner54482b42010-01-15 19:39:23 +0000967bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000968 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000969 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000970 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
971 return true;
972
Daniel Dunbarcceba832010-09-17 02:47:07 +0000973 // As a special case, we support 'a op b @ modifier' by rewriting the
974 // expression to include the modifier. This is inefficient, but in general we
975 // expect users to use 'a@modifier op b'.
976 if (Lexer.getKind() == AsmToken::At) {
977 Lex();
978
979 if (Lexer.isNot(AsmToken::Identifier))
980 return TokError("unexpected symbol modifier following '@'");
981
982 MCSymbolRefExpr::VariantKind Variant =
983 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
984 if (Variant == MCSymbolRefExpr::VK_Invalid)
985 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
986
987 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
988 if (!ModifiedRes) {
989 return TokError("invalid modifier '" + getTok().getIdentifier() +
990 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000991 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000992
Daniel Dunbarcceba832010-09-17 02:47:07 +0000993 Res = ModifiedRes;
994 Lex();
995 }
996
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000997 // Try to constant fold it up front, if possible.
998 int64_t Value;
999 if (Res->EvaluateAsAbsolute(Value))
1000 Res = MCConstantExpr::Create(Value, getContext());
1001
1002 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +00001003}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001004
Chris Lattnerb4307b32010-01-15 19:28:38 +00001005bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +00001006 Res = 0;
1007 return ParseParenExpr(Res, EndLoc) ||
1008 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +00001009}
1010
Daniel Dunbar475839e2009-06-29 20:37:27 +00001011bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001012 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001014 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +00001015 if (ParseExpression(Expr))
1016 return true;
1017
Daniel Dunbare00b0112009-10-16 01:57:52 +00001018 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001019 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001020
1021 return false;
1022}
1023
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001024static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001025 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001026 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001027 default:
1028 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001029
Jim Grosbachfbe16812011-08-20 16:24:13 +00001030 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001031 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001032 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001033 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001034 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001035 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001036 return 1;
1037
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001038
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001039 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +00001040 //
1041 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001042 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001043 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001044 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001045 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001046 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001047 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001048 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001049 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001050 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001051
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001052 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001053 case AsmToken::EqualEqual:
1054 Kind = MCBinaryExpr::EQ;
1055 return 3;
1056 case AsmToken::ExclaimEqual:
1057 case AsmToken::LessGreater:
1058 Kind = MCBinaryExpr::NE;
1059 return 3;
1060 case AsmToken::Less:
1061 Kind = MCBinaryExpr::LT;
1062 return 3;
1063 case AsmToken::LessEqual:
1064 Kind = MCBinaryExpr::LTE;
1065 return 3;
1066 case AsmToken::Greater:
1067 Kind = MCBinaryExpr::GT;
1068 return 3;
1069 case AsmToken::GreaterEqual:
1070 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001071 return 3;
1072
Jim Grosbachfbe16812011-08-20 16:24:13 +00001073 // Intermediate Precedence: <<, >>
1074 case AsmToken::LessLess:
1075 Kind = MCBinaryExpr::Shl;
1076 return 4;
1077 case AsmToken::GreaterGreater:
1078 Kind = MCBinaryExpr::Shr;
1079 return 4;
1080
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001081 // High Intermediate Precedence: +, -
1082 case AsmToken::Plus:
1083 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001084 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001085 case AsmToken::Minus:
1086 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001087 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001088
Jim Grosbachfbe16812011-08-20 16:24:13 +00001089 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001090 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001091 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001092 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001093 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001094 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001095 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001096 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001097 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001098 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001099 }
1100}
1101
1102
1103/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1104/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001105bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1106 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001107 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001108 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001109 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001110
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001111 // If the next token is lower precedence than we are allowed to eat, return
1112 // successfully with what we ate already.
1113 if (TokPrec < Precedence)
1114 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001115
Sean Callanan79ed1a82010-01-19 20:22:31 +00001116 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001117
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001118 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001119 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001120 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001121
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001122 // If BinOp binds less tightly with RHS than the operator after RHS, let
1123 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001124 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001125 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001126 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001127 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001128 }
1129
Daniel Dunbar475839e2009-06-29 20:37:27 +00001130 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001131 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001132 }
1133}
1134
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001135/// ParseStatement:
1136/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001137/// ::= Label* Directive ...Operands... EndOfStatement
1138/// ::= Label* Identifier OperandList* EndOfStatement
Eli Friedman2128aae2012-10-22 23:58:19 +00001139bool AsmParser::ParseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001140 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001141 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001142 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001143 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001144 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001145
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001146 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001147 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001148 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001149 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001150 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001151 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001152 if (Lexer.is(AsmToken::Hash))
1153 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001154
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001155 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001156 if (Lexer.is(AsmToken::Integer)) {
1157 LocalLabelVal = getTok().getIntVal();
1158 if (LocalLabelVal < 0) {
1159 if (!TheCondState.Ignore)
1160 return TokError("unexpected token at start of statement");
1161 IDVal = "";
1162 }
1163 else {
1164 IDVal = getTok().getString();
1165 Lex(); // Consume the integer token to be used as an identifier token.
1166 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001167 if (!TheCondState.Ignore)
1168 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001169 }
1170 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001171
1172 } else if (Lexer.is(AsmToken::Dot)) {
1173 // Treat '.' as a valid identifier in this context.
1174 Lex();
1175 IDVal = ".";
1176
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001177 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001178 if (!TheCondState.Ignore)
1179 return TokError("unexpected token at start of statement");
1180 IDVal = "";
1181 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001182
Chris Lattner7834fac2010-04-17 18:14:27 +00001183 // Handle conditional assembly here before checking for skipping. We
1184 // have to do this so that .endif isn't skipped in a ".if 0" block for
1185 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001186 StringMap<DirectiveKind>::const_iterator DirKindIt =
1187 DirectiveKindMapping.find(IDVal);
1188 DirectiveKind DirKind =
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001189 (DirKindIt == DirectiveKindMapping.end()) ? DK_NO_DIRECTIVE :
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001190 DirKindIt->getValue();
1191 switch (DirKind) {
1192 default:
1193 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001194 case DK_IF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001195 return ParseDirectiveIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001196 case DK_IFB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001197 return ParseDirectiveIfb(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001198 case DK_IFNB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001199 return ParseDirectiveIfb(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001200 case DK_IFC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001201 return ParseDirectiveIfc(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001202 case DK_IFNC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001203 return ParseDirectiveIfc(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001204 case DK_IFDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001205 return ParseDirectiveIfdef(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001206 case DK_IFNDEF:
1207 case DK_IFNOTDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001208 return ParseDirectiveIfdef(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001209 case DK_ELSEIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001210 return ParseDirectiveElseIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001211 case DK_ELSE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001212 return ParseDirectiveElse(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001213 case DK_ENDIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001214 return ParseDirectiveEndIf(IDLoc);
1215 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001216
Chris Lattner7834fac2010-04-17 18:14:27 +00001217 // If we are in a ".if 0" block, ignore this statement.
Chad Rosier17feeec2012-10-20 00:47:08 +00001218 if (TheCondState.Ignore) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001219 EatToEndOfStatement();
1220 return false;
1221 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001222
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001223 // FIXME: Recurse on local labels?
1224
1225 // See what kind of statement we have.
1226 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001227 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001228 CheckForValidSection();
1229
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001230 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001231 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001232
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001233 // Diagnose attempt to use '.' as a label.
1234 if (IDVal == ".")
1235 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1236
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001237 // Diagnose attempt to use a variable as a label.
1238 //
1239 // FIXME: Diagnostics. Note the location of the definition as a label.
1240 // FIXME: This doesn't diagnose assignment to a symbol which has been
1241 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001242 MCSymbol *Sym;
1243 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001244 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001245 else
1246 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001247 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001248 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001249
Daniel Dunbar959fd882009-08-26 22:13:22 +00001250 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001251 if (!ParsingInlineAsm)
1252 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001253
Kevin Enderby94c2e852011-12-09 18:09:40 +00001254 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001255 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001256 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001257 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1258 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001259
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001260 // Consume any end of statement token, if present, to avoid spurious
1261 // AddBlankLine calls().
1262 if (Lexer.is(AsmToken::EndOfStatement)) {
1263 Lex();
1264 if (Lexer.is(AsmToken::Eof))
1265 return false;
1266 }
1267
Eli Friedman2128aae2012-10-22 23:58:19 +00001268 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001269 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001270
Daniel Dunbar3f872332009-07-28 16:08:33 +00001271 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001272 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001273 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001274
Nico Weber4c4c7322011-01-28 03:04:41 +00001275 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001276
1277 default: // Normal instruction or directive.
1278 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001279 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001280
1281 // If macros are enabled, check to see if this is a macro instantiation.
1282 if (MacrosEnabled)
1283 if (const Macro *M = MacroMap.lookup(IDVal))
1284 return HandleMacroEntry(IDVal, IDLoc, M);
1285
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001286 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001287 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001288
1289 // Target hook for parsing target specific directives.
1290 if (!getTargetParser().ParseDirective(ID))
1291 return false;
1292
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001293 switch (DirKind) {
1294 default:
1295 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001296 case DK_SET:
1297 case DK_EQU:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001298 return ParseDirectiveSet(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001299 case DK_EQUIV:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001300 return ParseDirectiveSet(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001301 case DK_ASCII:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001302 return ParseDirectiveAscii(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001303 case DK_ASCIZ:
1304 case DK_STRING:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001305 return ParseDirectiveAscii(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001306 case DK_BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001307 return ParseDirectiveValue(1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001308 case DK_SHORT:
1309 case DK_VALUE:
1310 case DK_2BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001311 return ParseDirectiveValue(2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001312 case DK_LONG:
1313 case DK_INT:
1314 case DK_4BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001315 return ParseDirectiveValue(4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001316 case DK_QUAD:
1317 case DK_8BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001318 return ParseDirectiveValue(8);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001319 case DK_SINGLE:
1320 case DK_FLOAT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001321 return ParseDirectiveRealValue(APFloat::IEEEsingle);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001322 case DK_DOUBLE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001323 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001324 case DK_ALIGN: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001325 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1326 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1327 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001328 case DK_ALIGN32: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001329 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1330 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1331 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001332 case DK_BALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001333 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001334 case DK_BALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001335 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001336 case DK_BALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001337 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001338 case DK_P2ALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001339 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001340 case DK_P2ALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001341 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001342 case DK_P2ALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001343 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001344 case DK_ORG:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001345 return ParseDirectiveOrg();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001346 case DK_FILL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001347 return ParseDirectiveFill();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001348 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001349 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001350 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001351 EatToEndOfStatement(); // .extern is the default, ignore it.
1352 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001353 case DK_GLOBL:
1354 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001355 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001356 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001357 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001358 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001359 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001360 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001361 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001362 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001363 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001364 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001365 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001366 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001367 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001368 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001369 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001370 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001371 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001372 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001373 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001374 case DK_COMM:
1375 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001376 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001377 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001378 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001379 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001380 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001381 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001382 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001383 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001384 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001385 case DK_CODE16:
1386 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001387 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001388 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001389 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001390 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001391 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001392 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001393 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001394 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001395 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001396 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001397 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001398 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001399 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001400 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001401 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001402 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001403
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001404 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001405 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1406 DirectiveMap.lookup(IDVal);
1407 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001408 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001409
Jim Grosbach686c0182012-05-01 18:38:27 +00001410 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001411 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001412
Eli Friedman2128aae2012-10-22 23:58:19 +00001413 // _emit
1414 if (ParsingInlineAsm && IDVal == "_emit")
1415 return ParseDirectiveEmit(IDLoc, Info);
1416
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001417 CheckForValidSection();
1418
Chris Lattnera7f13542010-05-19 23:34:33 +00001419 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001420 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001421 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001422 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001423
Chad Rosier6a020a72012-10-25 20:41:34 +00001424 ParseInstructionInfo IInfo(Info.AsmRewrites);
1425 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1426 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001427 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001428
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001429 // Dump the parsed representation, if requested.
1430 if (getShowParsedOperands()) {
1431 SmallString<256> Str;
1432 raw_svector_ostream OS(Str);
1433 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001434 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001435 if (i != 0)
1436 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001437 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001438 }
1439 OS << "]";
1440
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001441 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001442 }
1443
Kevin Enderby613b7572011-11-01 22:27:22 +00001444 // If we are generating dwarf for assembly source files and the current
1445 // section is the initial text section then generate a .loc directive for
1446 // the instruction.
1447 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001448 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001449
1450 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1451
1452 // If we previously parsed a cpp hash file line comment then make sure the
1453 // current Dwarf File is for the CppHashFilename if not then emit the
1454 // Dwarf File table for it and adjust the line number for the .loc.
1455 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1456 getContext().getMCDwarfFiles();
1457 if (CppHashFilename.size() != 0) {
1458 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1459 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001460 getStreamer().EmitDwarfFileDirective(
1461 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001462
Kevin Enderby32c1a822012-11-05 21:55:41 +00001463 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001464 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1465 }
1466
Kevin Enderby613b7572011-11-01 22:27:22 +00001467 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001468 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001469 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001470 StringRef());
1471 }
1472
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001473 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001474 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001475 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001476 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1477 Info.ParsedOperands,
1478 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001479 ParsingInlineAsm);
1480 }
Chris Lattner98986712010-01-14 22:21:20 +00001481
Chris Lattnercbf8a982010-09-11 16:18:25 +00001482 // Don't skip the rest of the line, the instruction parser is responsible for
1483 // that.
1484 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001485}
Chris Lattner9a023f72009-06-24 04:43:34 +00001486
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001487/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1488/// since they may not be able to be tokenized to get to the end of line token.
1489void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001490 if (!Lexer.is(AsmToken::EndOfStatement))
1491 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001492 // Eat EOL.
1493 Lex();
1494}
1495
1496/// ParseCppHashLineFilenameComment as this:
1497/// ::= # number "filename"
1498/// or just as a full line comment if it doesn't have a number and a string.
1499bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1500 Lex(); // Eat the hash token.
1501
1502 if (getLexer().isNot(AsmToken::Integer)) {
1503 // Consume the line since in cases it is not a well-formed line directive,
1504 // as if were simply a full line comment.
1505 EatToEndOfLine();
1506 return false;
1507 }
1508
1509 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001510 Lex();
1511
1512 if (getLexer().isNot(AsmToken::String)) {
1513 EatToEndOfLine();
1514 return false;
1515 }
1516
1517 StringRef Filename = getTok().getString();
1518 // Get rid of the enclosing quotes.
1519 Filename = Filename.substr(1, Filename.size()-2);
1520
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001521 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1522 CppHashLoc = L;
1523 CppHashFilename = Filename;
1524 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001525 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001526
1527 // Ignore any trailing characters, they're just comment.
1528 EatToEndOfLine();
1529 return false;
1530}
1531
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001532/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001533/// for the Filename and LineNo if any in the diagnostic.
1534void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1535 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1536 raw_ostream &OS = errs();
1537
1538 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1539 const SMLoc &DiagLoc = Diag.getLoc();
1540 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1541 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1542
1543 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1544 // before printing the message.
1545 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001546 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001547 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1548 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1549 }
1550
Eric Christopher2318ba12012-12-18 00:30:54 +00001551 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001552 // manager changed or buffer changed (like in a nested include) then just
1553 // print the normal diagnostic using its Filename and LineNo.
1554 if (!Parser->CppHashLineNumber ||
1555 &DiagSrcMgr != &Parser->SrcMgr ||
1556 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001557 if (Parser->SavedDiagHandler)
1558 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1559 else
1560 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001561 return;
1562 }
1563
Eric Christopher2318ba12012-12-18 00:30:54 +00001564 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001565 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1566 // the diagnostic.
1567 const std::string Filename = Parser->CppHashFilename;
1568
1569 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1570 int CppHashLocLineNo =
1571 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1572 int LineNo = Parser->CppHashLineNumber - 1 +
1573 (DiagLocLineNo - CppHashLocLineNo);
1574
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001575 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1576 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001577 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001578 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001579
Benjamin Kramer04a04262011-10-16 10:48:29 +00001580 if (Parser->SavedDiagHandler)
1581 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1582 else
1583 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001584}
1585
Rafael Espindola799aacf2012-08-21 18:29:30 +00001586// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1587// difference being that that function accepts '@' as part of identifiers and
1588// we can't do that. AsmLexer.cpp should probably be changed to handle
1589// '@' as a special case when needed.
1590static bool isIdentifierChar(char c) {
1591 return isalnum(c) || c == '_' || c == '$' || c == '.';
1592}
1593
Rafael Espindola761cb062012-06-03 23:57:14 +00001594bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001595 const MacroParameters &Parameters,
1596 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001597 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001598 unsigned NParameters = Parameters.size();
1599 if (NParameters != 0 && NParameters != A.size())
1600 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001601
Preston Gurd7b6f2032012-09-19 20:36:12 +00001602 // A macro without parameters is handled differently on Darwin:
1603 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001604 while (!Body.empty()) {
1605 // Scan for the next substitution.
1606 std::size_t End = Body.size(), Pos = 0;
1607 for (; Pos != End; ++Pos) {
1608 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001609 if (!NParameters) {
1610 // This macro has no parameters, look for $0, $1, etc.
1611 if (Body[Pos] != '$' || Pos + 1 == End)
1612 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001613
Rafael Espindola65366442011-06-05 02:43:45 +00001614 char Next = Body[Pos + 1];
1615 if (Next == '$' || Next == 'n' || isdigit(Next))
1616 break;
1617 } else {
1618 // This macro has parameters, look for \foo, \bar, etc.
1619 if (Body[Pos] == '\\' && Pos + 1 != End)
1620 break;
1621 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001622 }
1623
1624 // Add the prefix.
1625 OS << Body.slice(0, Pos);
1626
1627 // Check if we reached the end.
1628 if (Pos == End)
1629 break;
1630
Rafael Espindola65366442011-06-05 02:43:45 +00001631 if (!NParameters) {
1632 switch (Body[Pos+1]) {
1633 // $$ => $
1634 case '$':
1635 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001636 break;
1637
Rafael Espindola65366442011-06-05 02:43:45 +00001638 // $n => number of arguments
1639 case 'n':
1640 OS << A.size();
1641 break;
1642
1643 // $[0-9] => argument
1644 default: {
1645 // Missing arguments are ignored.
1646 unsigned Index = Body[Pos+1] - '0';
1647 if (Index >= A.size())
1648 break;
1649
1650 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001651 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001652 ie = A[Index].end(); it != ie; ++it)
1653 OS << it->getString();
1654 break;
1655 }
1656 }
1657 Pos += 2;
1658 } else {
1659 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001660 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001661 ++I;
1662
1663 const char *Begin = Body.data() + Pos +1;
1664 StringRef Argument(Begin, I - (Pos +1));
1665 unsigned Index = 0;
1666 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001667 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001668 break;
1669
Preston Gurd7b6f2032012-09-19 20:36:12 +00001670 if (Index == NParameters) {
1671 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1672 Pos += 3;
1673 else {
1674 OS << '\\' << Argument;
1675 Pos = I;
1676 }
1677 } else {
1678 for (MacroArgument::const_iterator it = A[Index].begin(),
1679 ie = A[Index].end(); it != ie; ++it)
1680 if (it->getKind() == AsmToken::String)
1681 OS << it->getStringContents();
1682 else
1683 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001684
Preston Gurd7b6f2032012-09-19 20:36:12 +00001685 Pos += 1 + Argument.size();
1686 }
Rafael Espindola65366442011-06-05 02:43:45 +00001687 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001688 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001689 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001690 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001691
Rafael Espindola65366442011-06-05 02:43:45 +00001692 return false;
1693}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001694
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001695MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL,
1696 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001697 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001698 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1699 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001700{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001701}
1702
Preston Gurd7b6f2032012-09-19 20:36:12 +00001703static bool IsOperator(AsmToken::TokenKind kind)
1704{
1705 switch (kind)
1706 {
1707 default:
1708 return false;
1709 case AsmToken::Plus:
1710 case AsmToken::Minus:
1711 case AsmToken::Tilde:
1712 case AsmToken::Slash:
1713 case AsmToken::Star:
1714 case AsmToken::Dot:
1715 case AsmToken::Equal:
1716 case AsmToken::EqualEqual:
1717 case AsmToken::Pipe:
1718 case AsmToken::PipePipe:
1719 case AsmToken::Caret:
1720 case AsmToken::Amp:
1721 case AsmToken::AmpAmp:
1722 case AsmToken::Exclaim:
1723 case AsmToken::ExclaimEqual:
1724 case AsmToken::Percent:
1725 case AsmToken::Less:
1726 case AsmToken::LessEqual:
1727 case AsmToken::LessLess:
1728 case AsmToken::LessGreater:
1729 case AsmToken::Greater:
1730 case AsmToken::GreaterEqual:
1731 case AsmToken::GreaterGreater:
1732 return true;
1733 }
1734}
1735
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001736/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1737/// This is used for both default macro parameter values and the
1738/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001739bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1740 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001741 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001742 unsigned AddTokens = 0;
1743
1744 // gas accepts arguments separated by whitespace, except on Darwin
1745 if (!IsDarwin)
1746 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001747
1748 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001749 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1750 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001751 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001752 }
1753
1754 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1755 // Spaces and commas cannot be mixed to delimit parameters
1756 if (ArgumentDelimiter == AsmToken::Eof)
1757 ArgumentDelimiter = AsmToken::Comma;
1758 else if (ArgumentDelimiter != AsmToken::Comma) {
1759 Lexer.setSkipSpace(true);
1760 return TokError("expected ' ' for macro argument separator");
1761 }
1762 break;
1763 }
1764
1765 if (Lexer.is(AsmToken::Space)) {
1766 Lex(); // Eat spaces
1767
1768 // Spaces can delimit parameters, but could also be part an expression.
1769 // If the token after a space is an operator, add the token and the next
1770 // one into this argument
1771 if (ArgumentDelimiter == AsmToken::Space ||
1772 ArgumentDelimiter == AsmToken::Eof) {
1773 if (IsOperator(Lexer.getKind())) {
1774 // Check to see whether the token is used as an operator,
1775 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001776 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001777 if (*NextChar == ' ')
1778 AddTokens = 2;
1779 }
1780
1781 if (!AddTokens && ParenLevel == 0) {
1782 if (ArgumentDelimiter == AsmToken::Eof &&
1783 !IsOperator(Lexer.getKind()))
1784 ArgumentDelimiter = AsmToken::Space;
1785 break;
1786 }
1787 }
1788 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001789
1790 // HandleMacroEntry relies on not advancing the lexer here
1791 // to be able to fill in the remaining default parameter values
1792 if (Lexer.is(AsmToken::EndOfStatement))
1793 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001794
1795 // Adjust the current parentheses level.
1796 if (Lexer.is(AsmToken::LParen))
1797 ++ParenLevel;
1798 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1799 --ParenLevel;
1800
1801 // Append the token to the current argument list.
1802 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001803 if (AddTokens)
1804 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001805 Lex();
1806 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001807
1808 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001809 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001810 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001811 return false;
1812}
1813
1814// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001815bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001816 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001817 // Argument delimiter is initially unknown. It will be set by
1818 // ParseMacroArgument()
1819 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001820
1821 // Parse two kinds of macro invocations:
1822 // - macros defined without any parameters accept an arbitrary number of them
1823 // - macros defined with parameters accept at most that many of them
1824 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1825 ++Parameter) {
1826 MacroArgument MA;
1827
Preston Gurd7b6f2032012-09-19 20:36:12 +00001828 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001829 return true;
1830
Preston Gurd6c9176a2012-09-19 20:29:04 +00001831 if (!MA.empty() || !NParameters)
1832 A.push_back(MA);
1833 else if (NParameters) {
1834 if (!M->Parameters[Parameter].second.empty())
1835 A.push_back(M->Parameters[Parameter].second);
1836 }
Jim Grosbach97146442012-07-30 22:44:17 +00001837
Preston Gurd6c9176a2012-09-19 20:29:04 +00001838 // At the end of the statement, fill in remaining arguments that have
1839 // default values. If there aren't any, then the next argument is
1840 // required but missing
1841 if (Lexer.is(AsmToken::EndOfStatement)) {
1842 if (NParameters && Parameter < NParameters - 1) {
1843 if (M->Parameters[Parameter + 1].second.empty())
1844 return TokError("macro argument '" +
1845 Twine(M->Parameters[Parameter + 1].first) +
1846 "' is missing");
1847 else
1848 continue;
1849 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001850 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001851 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001852
1853 if (Lexer.is(AsmToken::Comma))
1854 Lex();
1855 }
1856 return TokError("Too many arguments");
1857}
1858
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001859bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1860 const Macro *M) {
1861 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1862 // this, although we should protect against infinite loops.
1863 if (ActiveMacros.size() == 20)
1864 return TokError("macros cannot be nested more than 20 levels deep");
1865
Rafael Espindola8a403d32012-08-08 14:51:03 +00001866 MacroArguments A;
1867 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001868 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001869
Jim Grosbach97146442012-07-30 22:44:17 +00001870 // Remove any trailing empty arguments. Do this after-the-fact as we have
1871 // to keep empty arguments in the middle of the list or positionality
1872 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001873 while (!A.empty() && A.back().empty())
1874 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001875
Rafael Espindola65366442011-06-05 02:43:45 +00001876 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1877 // to hold the macro body with substitutions.
1878 SmallString<256> Buf;
1879 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001880 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001881
Rafael Espindola8a403d32012-08-08 14:51:03 +00001882 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001883 return true;
1884
Rafael Espindola761cb062012-06-03 23:57:14 +00001885 // We include the .endmacro in the buffer as our queue to exit the macro
1886 // instantiation.
1887 OS << ".endmacro\n";
1888
Rafael Espindola65366442011-06-05 02:43:45 +00001889 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001890 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001891
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001892 // Create the macro instantiation object and add to the current macro
1893 // instantiation stack.
1894 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001895 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001896 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001897 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001898 ActiveMacros.push_back(MI);
1899
1900 // Jump to the macro instantiation and prime the lexer.
1901 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1902 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1903 Lex();
1904
1905 return false;
1906}
1907
1908void AsmParser::HandleMacroExit() {
1909 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001910 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001911 Lex();
1912
1913 // Pop the instantiation entry.
1914 delete ActiveMacros.back();
1915 ActiveMacros.pop_back();
1916}
1917
Rafael Espindolae71cc862012-01-28 05:57:00 +00001918static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001919 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001920 case MCExpr::Binary: {
1921 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1922 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001923 break;
1924 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001925 case MCExpr::Target:
1926 case MCExpr::Constant:
1927 return false;
1928 case MCExpr::SymbolRef: {
1929 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001930 if (S.isVariable())
1931 return IsUsedIn(Sym, S.getVariableValue());
1932 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001933 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001934 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001935 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001936 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001937
1938 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001939}
1940
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001941bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1942 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001943 // FIXME: Use better location, we should use proper tokens.
1944 SMLoc EqualLoc = Lexer.getLoc();
1945
Daniel Dunbar821e3332009-08-31 08:09:28 +00001946 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001947 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001948 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001949
Rafael Espindolae71cc862012-01-28 05:57:00 +00001950 // Note: we don't count b as used in "a = b". This is to allow
1951 // a = b
1952 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001953
Daniel Dunbar3f872332009-07-28 16:08:33 +00001954 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001955 return TokError("unexpected token in assignment");
1956
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001957 // Error on assignment to '.'.
1958 if (Name == ".") {
1959 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1960 "(use '.space' or '.org').)"));
1961 }
1962
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001963 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001964 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001965
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001966 // Validate that the LHS is allowed to be a variable (either it has not been
1967 // used as a symbol, or it is an absolute symbol).
1968 MCSymbol *Sym = getContext().LookupSymbol(Name);
1969 if (Sym) {
1970 // Diagnose assignment to a label.
1971 //
1972 // FIXME: Diagnostics. Note the location of the definition as a label.
1973 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001974 if (IsUsedIn(Sym, Value))
1975 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1976 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001977 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001978 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1979 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001980 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001981 return Error(EqualLoc, "redefinition of '" + Name + "'");
1982 else if (!Sym->isVariable())
1983 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001984 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001985 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1986 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001987
1988 // Don't count these checks as uses.
1989 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001990 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001991 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001992
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001993 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001994
1995 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001996 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001997 if (NoDeadStrip)
1998 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1999
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002000
2001 return false;
2002}
2003
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002004/// ParseIdentifier:
2005/// ::= identifier
2006/// ::= string
2007bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002008 // The assembler has relaxed rules for accepting identifiers, in particular we
2009 // allow things like '.globl $foo', which would normally be separate
2010 // tokens. At this level, we have already lexed so we cannot (currently)
2011 // handle this as a context dependent token, instead we detect adjacent tokens
2012 // and return the combined identifier.
2013 if (Lexer.is(AsmToken::Dollar)) {
2014 SMLoc DollarLoc = getLexer().getLoc();
2015
2016 // Consume the dollar sign, and check for a following identifier.
2017 Lex();
2018 if (Lexer.isNot(AsmToken::Identifier))
2019 return true;
2020
2021 // We have a '$' followed by an identifier, make sure they are adjacent.
2022 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2023 return true;
2024
2025 // Construct the joined identifier and consume the token.
2026 Res = StringRef(DollarLoc.getPointer(),
2027 getTok().getIdentifier().size() + 1);
2028 Lex();
2029 return false;
2030 }
2031
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002032 if (Lexer.isNot(AsmToken::Identifier) &&
2033 Lexer.isNot(AsmToken::String))
2034 return true;
2035
Sean Callanan18b83232010-01-19 21:44:56 +00002036 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002037
Sean Callanan79ed1a82010-01-19 20:22:31 +00002038 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002039
2040 return false;
2041}
2042
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002043/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002044/// ::= .equ identifier ',' expression
2045/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002046/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002047bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002048 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002049
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002050 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002051 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002052
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002053 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002054 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002055 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002056
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002057 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002058}
2059
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002060bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002061 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002062
2063 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002064 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002065 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2066 if (Str[i] != '\\') {
2067 Data += Str[i];
2068 continue;
2069 }
2070
2071 // Recognize escaped characters. Note that this escape semantics currently
2072 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2073 ++i;
2074 if (i == e)
2075 return TokError("unexpected backslash at end of string");
2076
2077 // Recognize octal sequences.
2078 if ((unsigned) (Str[i] - '0') <= 7) {
2079 // Consume up to three octal characters.
2080 unsigned Value = Str[i] - '0';
2081
2082 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2083 ++i;
2084 Value = Value * 8 + (Str[i] - '0');
2085
2086 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2087 ++i;
2088 Value = Value * 8 + (Str[i] - '0');
2089 }
2090 }
2091
2092 if (Value > 255)
2093 return TokError("invalid octal escape sequence (out of range)");
2094
2095 Data += (unsigned char) Value;
2096 continue;
2097 }
2098
2099 // Otherwise recognize individual escapes.
2100 switch (Str[i]) {
2101 default:
2102 // Just reject invalid escape sequences for now.
2103 return TokError("invalid escape sequence (unrecognized character)");
2104
2105 case 'b': Data += '\b'; break;
2106 case 'f': Data += '\f'; break;
2107 case 'n': Data += '\n'; break;
2108 case 'r': Data += '\r'; break;
2109 case 't': Data += '\t'; break;
2110 case '"': Data += '"'; break;
2111 case '\\': Data += '\\'; break;
2112 }
2113 }
2114
2115 return false;
2116}
2117
Daniel Dunbara0d14262009-06-24 23:30:00 +00002118/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002119/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2120bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002121 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002122 CheckForValidSection();
2123
Daniel Dunbara0d14262009-06-24 23:30:00 +00002124 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002125 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002126 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002127
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002128 std::string Data;
2129 if (ParseEscapedString(Data))
2130 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002131
2132 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002133 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002134 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2135
Sean Callanan79ed1a82010-01-19 20:22:31 +00002136 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002137
2138 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002139 break;
2140
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002141 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002142 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002143 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002144 }
2145 }
2146
Sean Callanan79ed1a82010-01-19 20:22:31 +00002147 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002148 return false;
2149}
2150
2151/// ParseDirectiveValue
2152/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2153bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002154 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002155 CheckForValidSection();
2156
Daniel Dunbara0d14262009-06-24 23:30:00 +00002157 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002158 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002159 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002160 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002161 return true;
2162
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002163 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002164 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2165 assert(Size <= 8 && "Invalid size");
2166 uint64_t IntValue = MCE->getValue();
2167 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2168 return Error(ExprLoc, "literal value out of range for directive");
2169 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2170 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002171 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002172
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002173 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002174 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002175
Daniel Dunbara0d14262009-06-24 23:30:00 +00002176 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002177 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002178 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002179 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002180 }
2181 }
2182
Sean Callanan79ed1a82010-01-19 20:22:31 +00002183 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002184 return false;
2185}
2186
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002187/// ParseDirectiveRealValue
2188/// ::= (.single | .double) [ expression (, expression)* ]
2189bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2190 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2191 CheckForValidSection();
2192
2193 for (;;) {
2194 // We don't truly support arithmetic on floating point expressions, so we
2195 // have to manually parse unary prefixes.
2196 bool IsNeg = false;
2197 if (getLexer().is(AsmToken::Minus)) {
2198 Lex();
2199 IsNeg = true;
2200 } else if (getLexer().is(AsmToken::Plus))
2201 Lex();
2202
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002203 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002204 getLexer().isNot(AsmToken::Real) &&
2205 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002206 return TokError("unexpected token in directive");
2207
2208 // Convert to an APFloat.
2209 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002210 StringRef IDVal = getTok().getString();
2211 if (getLexer().is(AsmToken::Identifier)) {
2212 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2213 Value = APFloat::getInf(Semantics);
2214 else if (!IDVal.compare_lower("nan"))
2215 Value = APFloat::getNaN(Semantics, false, ~0);
2216 else
2217 return TokError("invalid floating point literal");
2218 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002219 APFloat::opInvalidOp)
2220 return TokError("invalid floating point literal");
2221 if (IsNeg)
2222 Value.changeSign();
2223
2224 // Consume the numeric token.
2225 Lex();
2226
2227 // Emit the value as an integer.
2228 APInt AsInt = Value.bitcastToAPInt();
2229 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2230 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2231
2232 if (getLexer().is(AsmToken::EndOfStatement))
2233 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002234
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002235 if (getLexer().isNot(AsmToken::Comma))
2236 return TokError("unexpected token in directive");
2237 Lex();
2238 }
2239 }
2240
2241 Lex();
2242 return false;
2243}
2244
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002245/// ParseDirectiveZero
2246/// ::= .zero expression
2247bool AsmParser::ParseDirectiveZero() {
2248 CheckForValidSection();
2249
2250 int64_t NumBytes;
2251 if (ParseAbsoluteExpression(NumBytes))
2252 return true;
2253
Rafael Espindolae452b172010-10-05 19:42:57 +00002254 int64_t Val = 0;
2255 if (getLexer().is(AsmToken::Comma)) {
2256 Lex();
2257 if (ParseAbsoluteExpression(Val))
2258 return true;
2259 }
2260
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002261 if (getLexer().isNot(AsmToken::EndOfStatement))
2262 return TokError("unexpected token in '.zero' directive");
2263
2264 Lex();
2265
Rafael Espindolae452b172010-10-05 19:42:57 +00002266 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002267
2268 return false;
2269}
2270
Daniel Dunbara0d14262009-06-24 23:30:00 +00002271/// ParseDirectiveFill
2272/// ::= .fill expression , expression , expression
2273bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002274 CheckForValidSection();
2275
Daniel Dunbara0d14262009-06-24 23:30:00 +00002276 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002277 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002278 return true;
2279
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002280 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002281 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002282 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002283
Daniel Dunbara0d14262009-06-24 23:30:00 +00002284 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002285 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002286 return true;
2287
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002288 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002289 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002290 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002291
Daniel Dunbara0d14262009-06-24 23:30:00 +00002292 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002293 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002294 return true;
2295
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002296 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002297 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002298
Sean Callanan79ed1a82010-01-19 20:22:31 +00002299 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002300
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002301 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2302 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002303
2304 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002305 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002306
2307 return false;
2308}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002309
2310/// ParseDirectiveOrg
2311/// ::= .org expression [ , expression ]
2312bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002313 CheckForValidSection();
2314
Daniel Dunbar821e3332009-08-31 08:09:28 +00002315 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002316 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002317 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002318 return true;
2319
2320 // Parse optional fill expression.
2321 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002322 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2323 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002324 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002325 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002326
Daniel Dunbar475839e2009-06-29 20:37:27 +00002327 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002328 return true;
2329
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002330 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002331 return TokError("unexpected token in '.org' directive");
2332 }
2333
Sean Callanan79ed1a82010-01-19 20:22:31 +00002334 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002335
Jim Grosbachebd4c052012-01-27 00:37:08 +00002336 // Only limited forms of relocatable expressions are accepted here, it
2337 // has to be relative to the current section. The streamer will return
2338 // 'true' if the expression wasn't evaluatable.
2339 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2340 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002341
2342 return false;
2343}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002344
2345/// ParseDirectiveAlign
2346/// ::= {.align, ...} expression [ , expression [ , expression ]]
2347bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002348 CheckForValidSection();
2349
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002350 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002351 int64_t Alignment;
2352 if (ParseAbsoluteExpression(Alignment))
2353 return true;
2354
2355 SMLoc MaxBytesLoc;
2356 bool HasFillExpr = false;
2357 int64_t FillExpr = 0;
2358 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002359 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2360 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002361 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002362 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002363
2364 // The fill expression can be omitted while specifying a maximum number of
2365 // alignment bytes, e.g:
2366 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002367 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002368 HasFillExpr = true;
2369 if (ParseAbsoluteExpression(FillExpr))
2370 return true;
2371 }
2372
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002373 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2374 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002375 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002376 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002377
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002378 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002379 if (ParseAbsoluteExpression(MaxBytesToFill))
2380 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002381
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002382 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002383 return TokError("unexpected token in directive");
2384 }
2385 }
2386
Sean Callanan79ed1a82010-01-19 20:22:31 +00002387 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002388
Daniel Dunbar648ac512010-05-17 21:54:30 +00002389 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002390 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002391
2392 // Compute alignment in bytes.
2393 if (IsPow2) {
2394 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002395 if (Alignment >= 32) {
2396 Error(AlignmentLoc, "invalid alignment value");
2397 Alignment = 31;
2398 }
2399
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002400 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002401 }
2402
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002403 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002404 if (MaxBytesLoc.isValid()) {
2405 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002406 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2407 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002408 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002409 }
2410
2411 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002412 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2413 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002414 MaxBytesToFill = 0;
2415 }
2416 }
2417
Daniel Dunbar648ac512010-05-17 21:54:30 +00002418 // Check whether we should use optimal code alignment for this .align
2419 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002420 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002421 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2422 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002423 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002424 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002425 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002426 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2427 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002428 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002429
2430 return false;
2431}
2432
Eli Bendersky4766ef42012-12-20 19:05:53 +00002433
2434/// ParseDirectiveBundleAlignMode
2435/// ::= {.bundle_align_mode} expression
2436bool AsmParser::ParseDirectiveBundleAlignMode() {
2437 CheckForValidSection();
2438
2439 // Expect a single argument: an expression that evaluates to a constant
2440 // in the inclusive range 0-30.
2441 SMLoc ExprLoc = getLexer().getLoc();
2442 int64_t AlignSizePow2;
2443 if (ParseAbsoluteExpression(AlignSizePow2))
2444 return true;
2445 else if (getLexer().isNot(AsmToken::EndOfStatement))
2446 return TokError("unexpected token after expression in"
2447 " '.bundle_align_mode' directive");
2448 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2449 return Error(ExprLoc,
2450 "invalid bundle alignment size (expected between 0 and 30)");
2451
2452 Lex();
2453
2454 // Because of AlignSizePow2's verified range we can safely truncate it to
2455 // unsigned.
2456 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2457 return false;
2458}
2459
2460/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002461/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002462bool AsmParser::ParseDirectiveBundleLock() {
2463 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002464 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002465
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2467 StringRef Option;
2468 SMLoc Loc = getTok().getLoc();
2469 const char *kInvalidOptionError =
2470 "invalid option for '.bundle_lock' directive";
2471
2472 if (ParseIdentifier(Option))
2473 return Error(Loc, kInvalidOptionError);
2474
2475 if (Option != "align_to_end")
2476 return Error(Loc, kInvalidOptionError);
2477 else if (getLexer().isNot(AsmToken::EndOfStatement))
2478 return Error(Loc,
2479 "unexpected token after '.bundle_lock' directive option");
2480 AlignToEnd = true;
2481 }
2482
Eli Bendersky4766ef42012-12-20 19:05:53 +00002483 Lex();
2484
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002485 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002486 return false;
2487}
2488
2489/// ParseDirectiveBundleLock
2490/// ::= {.bundle_lock}
2491bool AsmParser::ParseDirectiveBundleUnlock() {
2492 CheckForValidSection();
2493
2494 if (getLexer().isNot(AsmToken::EndOfStatement))
2495 return TokError("unexpected token in '.bundle_unlock' directive");
2496 Lex();
2497
2498 getStreamer().EmitBundleUnlock();
2499 return false;
2500}
2501
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002502/// ParseDirectiveSymbolAttribute
2503/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002504bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002505 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002506 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002507 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002508 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002509
2510 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002511 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002512
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002513 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002514
Jim Grosbach10ec6502011-09-15 17:56:49 +00002515 // Assembler local symbols don't make any sense here. Complain loudly.
2516 if (Sym->isTemporary())
2517 return Error(Loc, "non-local symbol required in directive");
2518
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002519 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002520
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002521 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002522 break;
2523
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002524 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002525 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002526 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002527 }
2528 }
2529
Sean Callanan79ed1a82010-01-19 20:22:31 +00002530 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002531 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002532}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002533
2534/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002535/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2536bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002537 CheckForValidSection();
2538
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002539 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002540 StringRef Name;
2541 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002542 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002543
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002544 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002545 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002546
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002547 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002548 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002549 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002550
2551 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002552 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002553 if (ParseAbsoluteExpression(Size))
2554 return true;
2555
2556 int64_t Pow2Alignment = 0;
2557 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002558 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002559 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002560 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002561 if (ParseAbsoluteExpression(Pow2Alignment))
2562 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002563
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002564 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2565 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002566 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2567
Chris Lattner258281d2010-01-19 06:22:22 +00002568 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002569 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2570 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002571 if (!isPowerOf2_64(Pow2Alignment))
2572 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2573 Pow2Alignment = Log2_64(Pow2Alignment);
2574 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002575 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002576
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002577 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002578 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002579
Sean Callanan79ed1a82010-01-19 20:22:31 +00002580 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002581
Chris Lattner1fc3d752009-07-09 17:25:12 +00002582 // NOTE: a size of zero for a .comm should create a undefined symbol
2583 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002584 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002585 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2586 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002587
Eric Christopherc260a3e2010-05-14 01:38:54 +00002588 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002589 // may internally end up wanting an alignment in bytes.
2590 // FIXME: Diagnose overflow.
2591 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002592 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2593 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002594
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002595 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002596 return Error(IDLoc, "invalid symbol redefinition");
2597
Chris Lattner1fc3d752009-07-09 17:25:12 +00002598 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002599 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002600 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002601 return false;
2602 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002603
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002604 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002605 return false;
2606}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002607
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002608/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002609/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002610bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002611 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002612 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002613
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002614 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002615 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002616 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002617
Sean Callanan79ed1a82010-01-19 20:22:31 +00002618 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002619
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002620 if (Str.empty())
2621 Error(Loc, ".abort detected. Assembly stopping.");
2622 else
2623 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002624 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002625
2626 return false;
2627}
Kevin Enderby71148242009-07-14 21:35:03 +00002628
Kevin Enderby1f049b22009-07-14 23:21:55 +00002629/// ParseDirectiveInclude
2630/// ::= .include "filename"
2631bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002632 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002633 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002634
Sean Callanan18b83232010-01-19 21:44:56 +00002635 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002636 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002637 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002638
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002639 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002640 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002641
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002642 // Strip the quotes.
2643 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002644
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002645 // Attempt to switch the lexer to the included file before consuming the end
2646 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002647 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002648 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002649 return true;
2650 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002651
2652 return false;
2653}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002654
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002655/// ParseDirectiveIncbin
2656/// ::= .incbin "filename"
2657bool AsmParser::ParseDirectiveIncbin() {
2658 if (getLexer().isNot(AsmToken::String))
2659 return TokError("expected string in '.incbin' directive");
2660
2661 std::string Filename = getTok().getString();
2662 SMLoc IncbinLoc = getLexer().getLoc();
2663 Lex();
2664
2665 if (getLexer().isNot(AsmToken::EndOfStatement))
2666 return TokError("unexpected token in '.incbin' directive");
2667
2668 // Strip the quotes.
2669 Filename = Filename.substr(1, Filename.size()-2);
2670
2671 // Attempt to process the included file.
2672 if (ProcessIncbinFile(Filename)) {
2673 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2674 return true;
2675 }
2676
2677 return false;
2678}
2679
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002680/// ParseDirectiveIf
2681/// ::= .if expression
2682bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002683 TheCondStack.push_back(TheCondState);
2684 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002685 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002686 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002687 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002688 int64_t ExprValue;
2689 if (ParseAbsoluteExpression(ExprValue))
2690 return true;
2691
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002692 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002693 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002694
Sean Callanan79ed1a82010-01-19 20:22:31 +00002695 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002696
2697 TheCondState.CondMet = ExprValue;
2698 TheCondState.Ignore = !TheCondState.CondMet;
2699 }
2700
2701 return false;
2702}
2703
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002704/// ParseDirectiveIfb
2705/// ::= .ifb string
2706bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2707 TheCondStack.push_back(TheCondState);
2708 TheCondState.TheCond = AsmCond::IfCond;
2709
Benjamin Kramer29739e72012-05-12 16:52:21 +00002710 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002711 EatToEndOfStatement();
2712 } else {
2713 StringRef Str = ParseStringToEndOfStatement();
2714
2715 if (getLexer().isNot(AsmToken::EndOfStatement))
2716 return TokError("unexpected token in '.ifb' directive");
2717
2718 Lex();
2719
2720 TheCondState.CondMet = ExpectBlank == Str.empty();
2721 TheCondState.Ignore = !TheCondState.CondMet;
2722 }
2723
2724 return false;
2725}
2726
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002727/// ParseDirectiveIfc
2728/// ::= .ifc string1, string2
2729bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2730 TheCondStack.push_back(TheCondState);
2731 TheCondState.TheCond = AsmCond::IfCond;
2732
Benjamin Kramer29739e72012-05-12 16:52:21 +00002733 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002734 EatToEndOfStatement();
2735 } else {
2736 StringRef Str1 = ParseStringToComma();
2737
2738 if (getLexer().isNot(AsmToken::Comma))
2739 return TokError("unexpected token in '.ifc' directive");
2740
2741 Lex();
2742
2743 StringRef Str2 = ParseStringToEndOfStatement();
2744
2745 if (getLexer().isNot(AsmToken::EndOfStatement))
2746 return TokError("unexpected token in '.ifc' directive");
2747
2748 Lex();
2749
2750 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2751 TheCondState.Ignore = !TheCondState.CondMet;
2752 }
2753
2754 return false;
2755}
2756
2757/// ParseDirectiveIfdef
2758/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002759bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2760 StringRef Name;
2761 TheCondStack.push_back(TheCondState);
2762 TheCondState.TheCond = AsmCond::IfCond;
2763
2764 if (TheCondState.Ignore) {
2765 EatToEndOfStatement();
2766 } else {
2767 if (ParseIdentifier(Name))
2768 return TokError("expected identifier after '.ifdef'");
2769
2770 Lex();
2771
2772 MCSymbol *Sym = getContext().LookupSymbol(Name);
2773
2774 if (expect_defined)
2775 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2776 else
2777 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2778 TheCondState.Ignore = !TheCondState.CondMet;
2779 }
2780
2781 return false;
2782}
2783
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002784/// ParseDirectiveElseIf
2785/// ::= .elseif expression
2786bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2787 if (TheCondState.TheCond != AsmCond::IfCond &&
2788 TheCondState.TheCond != AsmCond::ElseIfCond)
2789 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2790 " an .elseif");
2791 TheCondState.TheCond = AsmCond::ElseIfCond;
2792
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002793 bool LastIgnoreState = false;
2794 if (!TheCondStack.empty())
2795 LastIgnoreState = TheCondStack.back().Ignore;
2796 if (LastIgnoreState || TheCondState.CondMet) {
2797 TheCondState.Ignore = true;
2798 EatToEndOfStatement();
2799 }
2800 else {
2801 int64_t ExprValue;
2802 if (ParseAbsoluteExpression(ExprValue))
2803 return true;
2804
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002805 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002806 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002807
Sean Callanan79ed1a82010-01-19 20:22:31 +00002808 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002809 TheCondState.CondMet = ExprValue;
2810 TheCondState.Ignore = !TheCondState.CondMet;
2811 }
2812
2813 return false;
2814}
2815
2816/// ParseDirectiveElse
2817/// ::= .else
2818bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002819 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002820 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002821
Sean Callanan79ed1a82010-01-19 20:22:31 +00002822 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002823
2824 if (TheCondState.TheCond != AsmCond::IfCond &&
2825 TheCondState.TheCond != AsmCond::ElseIfCond)
2826 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2827 ".elseif");
2828 TheCondState.TheCond = AsmCond::ElseCond;
2829 bool LastIgnoreState = false;
2830 if (!TheCondStack.empty())
2831 LastIgnoreState = TheCondStack.back().Ignore;
2832 if (LastIgnoreState || TheCondState.CondMet)
2833 TheCondState.Ignore = true;
2834 else
2835 TheCondState.Ignore = false;
2836
2837 return false;
2838}
2839
2840/// ParseDirectiveEndIf
2841/// ::= .endif
2842bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002843 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002844 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002845
Sean Callanan79ed1a82010-01-19 20:22:31 +00002846 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002847
2848 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2849 TheCondStack.empty())
2850 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2851 ".else");
2852 if (!TheCondStack.empty()) {
2853 TheCondState = TheCondStack.back();
2854 TheCondStack.pop_back();
2855 }
2856
2857 return false;
2858}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002859
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002860void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002861 DirectiveKindMapping[".set"] = DK_SET;
2862 DirectiveKindMapping[".equ"] = DK_EQU;
2863 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2864 DirectiveKindMapping[".ascii"] = DK_ASCII;
2865 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2866 DirectiveKindMapping[".string"] = DK_STRING;
2867 DirectiveKindMapping[".byte"] = DK_BYTE;
2868 DirectiveKindMapping[".short"] = DK_SHORT;
2869 DirectiveKindMapping[".value"] = DK_VALUE;
2870 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2871 DirectiveKindMapping[".long"] = DK_LONG;
2872 DirectiveKindMapping[".int"] = DK_INT;
2873 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2874 DirectiveKindMapping[".quad"] = DK_QUAD;
2875 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2876 DirectiveKindMapping[".single"] = DK_SINGLE;
2877 DirectiveKindMapping[".float"] = DK_FLOAT;
2878 DirectiveKindMapping[".double"] = DK_DOUBLE;
2879 DirectiveKindMapping[".align"] = DK_ALIGN;
2880 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2881 DirectiveKindMapping[".balign"] = DK_BALIGN;
2882 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2883 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2884 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2885 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2886 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2887 DirectiveKindMapping[".org"] = DK_ORG;
2888 DirectiveKindMapping[".fill"] = DK_FILL;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002889 DirectiveKindMapping[".zero"] = DK_ZERO;
2890 DirectiveKindMapping[".extern"] = DK_EXTERN;
2891 DirectiveKindMapping[".globl"] = DK_GLOBL;
2892 DirectiveKindMapping[".global"] = DK_GLOBAL;
2893 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2894 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2895 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2896 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2897 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2898 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2899 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2900 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2901 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2902 DirectiveKindMapping[".comm"] = DK_COMM;
2903 DirectiveKindMapping[".common"] = DK_COMMON;
2904 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2905 DirectiveKindMapping[".abort"] = DK_ABORT;
2906 DirectiveKindMapping[".include"] = DK_INCLUDE;
2907 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2908 DirectiveKindMapping[".code16"] = DK_CODE16;
2909 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2910 DirectiveKindMapping[".rept"] = DK_REPT;
2911 DirectiveKindMapping[".irp"] = DK_IRP;
2912 DirectiveKindMapping[".irpc"] = DK_IRPC;
2913 DirectiveKindMapping[".endr"] = DK_ENDR;
2914 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2915 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2916 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2917 DirectiveKindMapping[".if"] = DK_IF;
2918 DirectiveKindMapping[".ifb"] = DK_IFB;
2919 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2920 DirectiveKindMapping[".ifc"] = DK_IFC;
2921 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2922 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2923 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2924 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2925 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2926 DirectiveKindMapping[".else"] = DK_ELSE;
2927 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002928}
2929
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002930/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002931/// ::= .file [number] filename
2932/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002933bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002934 // FIXME: I'm not sure what this is.
2935 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002936 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002937 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002938 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002939 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002940
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002941 if (FileNumber < 1)
2942 return TokError("file number less than one");
2943 }
2944
Daniel Dunbareceec052010-07-12 17:45:27 +00002945 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002946 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002947
Nick Lewycky44d798d2011-10-17 23:05:28 +00002948 // Usually the directory and filename together, otherwise just the directory.
2949 StringRef Path = getTok().getString();
2950 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002951 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002952
Nick Lewycky44d798d2011-10-17 23:05:28 +00002953 StringRef Directory;
2954 StringRef Filename;
2955 if (getLexer().is(AsmToken::String)) {
2956 if (FileNumber == -1)
2957 return TokError("explicit path specified, but no file number");
2958 Filename = getTok().getString();
2959 Filename = Filename.substr(1, Filename.size()-2);
2960 Directory = Path;
2961 Lex();
2962 } else {
2963 Filename = Path;
2964 }
2965
Daniel Dunbareceec052010-07-12 17:45:27 +00002966 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002967 return TokError("unexpected token in '.file' directive");
2968
Chris Lattnerd32e8032010-01-25 19:02:58 +00002969 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002970 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002971 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002972 if (getContext().getGenDwarfForAssembly() == true)
2973 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2974 "used to generate dwarf debug info for assembly code");
2975
Nick Lewycky44d798d2011-10-17 23:05:28 +00002976 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002977 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002978 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002979
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002980 return false;
2981}
2982
2983/// ParseDirectiveLine
2984/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002985bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002986 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2987 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002988 return TokError("unexpected token in '.line' directive");
2989
Sean Callanan18b83232010-01-19 21:44:56 +00002990 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002991 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002992 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002993
2994 // FIXME: Do something with the .line.
2995 }
2996
Daniel Dunbareceec052010-07-12 17:45:27 +00002997 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002998 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002999
3000 return false;
3001}
3002
3003
3004/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003005/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003006/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3007/// The first number is a file number, must have been previously assigned with
3008/// a .file directive, the second number is the line number and optionally the
3009/// third number is a column position (zero if not specified). The remaining
3010/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003011bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003012
Daniel Dunbareceec052010-07-12 17:45:27 +00003013 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003014 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003015 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003016 if (FileNumber < 1)
3017 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003018 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003019 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003020 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003021
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003022 int64_t LineNumber = 0;
3023 if (getLexer().is(AsmToken::Integer)) {
3024 LineNumber = getTok().getIntVal();
3025 if (LineNumber < 1)
3026 return TokError("line number less than one in '.loc' directive");
3027 Lex();
3028 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003029
3030 int64_t ColumnPos = 0;
3031 if (getLexer().is(AsmToken::Integer)) {
3032 ColumnPos = getTok().getIntVal();
3033 if (ColumnPos < 0)
3034 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003035 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003036 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003037
Kevin Enderbyc0957932010-09-30 16:52:03 +00003038 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003039 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003040 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003041 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3042 for (;;) {
3043 if (getLexer().is(AsmToken::EndOfStatement))
3044 break;
3045
3046 StringRef Name;
3047 SMLoc Loc = getTok().getLoc();
3048 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003049 return TokError("unexpected token in '.loc' directive");
3050
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003051 if (Name == "basic_block")
3052 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3053 else if (Name == "prologue_end")
3054 Flags |= DWARF2_FLAG_PROLOGUE_END;
3055 else if (Name == "epilogue_begin")
3056 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3057 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003058 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003059 const MCExpr *Value;
3060 if (getParser().ParseExpression(Value))
3061 return true;
3062 // The expression must be the constant 0 or 1.
3063 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3064 int Value = MCE->getValue();
3065 if (Value == 0)
3066 Flags &= ~DWARF2_FLAG_IS_STMT;
3067 else if (Value == 1)
3068 Flags |= DWARF2_FLAG_IS_STMT;
3069 else
3070 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003071 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003072 else {
3073 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3074 }
3075 }
3076 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003077 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003078 const MCExpr *Value;
3079 if (getParser().ParseExpression(Value))
3080 return true;
3081 // The expression must be a constant greater or equal to 0.
3082 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3083 int Value = MCE->getValue();
3084 if (Value < 0)
3085 return Error(Loc, "isa number less than zero");
3086 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003087 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003088 else {
3089 return Error(Loc, "isa number not a constant value");
3090 }
3091 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003092 else if (Name == "discriminator") {
3093 if (getParser().ParseAbsoluteExpression(Discriminator))
3094 return true;
3095 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003096 else {
3097 return Error(Loc, "unknown sub-directive in '.loc' directive");
3098 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003099
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003100 if (getLexer().is(AsmToken::EndOfStatement))
3101 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003102 }
3103 }
3104
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003105 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003106 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003107
3108 return false;
3109}
3110
Daniel Dunbar138abae2010-10-16 04:56:42 +00003111/// ParseDirectiveStabs
3112/// ::= .stabs string, number, number, number
3113bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3114 SMLoc DirectiveLoc) {
3115 return TokError("unsupported directive '" + Directive + "'");
3116}
3117
Eli Bendersky9b1bb052013-01-11 22:55:28 +00003118/// ParseDirectiveSpace
3119/// ::= .space expression [ , expression ]
3120bool GenericAsmParser::ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc) {
3121 getParser().CheckForValidSection();
3122
3123 int64_t NumBytes;
3124 if (getParser().ParseAbsoluteExpression(NumBytes))
3125 return true;
3126
3127 int64_t FillExpr = 0;
3128 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3129 if (getLexer().isNot(AsmToken::Comma))
3130 return TokError("unexpected token in '.space' directive");
3131 Lex();
3132
3133 if (getParser().ParseAbsoluteExpression(FillExpr))
3134 return true;
3135
3136 if (getLexer().isNot(AsmToken::EndOfStatement))
3137 return TokError("unexpected token in '.space' directive");
3138 }
3139
3140 Lex();
3141
3142 if (NumBytes <= 0)
3143 return TokError("invalid number of bytes in '.space' directive");
3144
3145 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3146 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
3147
3148 return false;
3149}
3150
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003151/// ParseDirectiveCFISections
3152/// ::= .cfi_sections section [, section]
3153bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3154 SMLoc DirectiveLoc) {
3155 StringRef Name;
3156 bool EH = false;
3157 bool Debug = false;
3158
3159 if (getParser().ParseIdentifier(Name))
3160 return TokError("Expected an identifier");
3161
3162 if (Name == ".eh_frame")
3163 EH = true;
3164 else if (Name == ".debug_frame")
3165 Debug = true;
3166
3167 if (getLexer().is(AsmToken::Comma)) {
3168 Lex();
3169
3170 if (getParser().ParseIdentifier(Name))
3171 return TokError("Expected an identifier");
3172
3173 if (Name == ".eh_frame")
3174 EH = true;
3175 else if (Name == ".debug_frame")
3176 Debug = true;
3177 }
3178
3179 getStreamer().EmitCFISections(EH, Debug);
3180
3181 return false;
3182}
3183
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003184/// ParseDirectiveCFIStartProc
3185/// ::= .cfi_startproc
3186bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3187 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003188 getStreamer().EmitCFIStartProc();
3189 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003190}
3191
3192/// ParseDirectiveCFIEndProc
3193/// ::= .cfi_endproc
3194bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003195 getStreamer().EmitCFIEndProc();
3196 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003197}
3198
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003199/// ParseRegisterOrRegisterNumber - parse register name or number.
3200bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3201 SMLoc DirectiveLoc) {
3202 unsigned RegNo;
3203
Jim Grosbach6f888a82011-06-02 17:14:04 +00003204 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003205 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3206 DirectiveLoc))
3207 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003208 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003209 } else
3210 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003211
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003212 return false;
3213}
3214
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003215/// ParseDirectiveCFIDefCfa
3216/// ::= .cfi_def_cfa register, offset
3217bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3218 SMLoc DirectiveLoc) {
3219 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003220 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003221 return true;
3222
3223 if (getLexer().isNot(AsmToken::Comma))
3224 return TokError("unexpected token in directive");
3225 Lex();
3226
3227 int64_t Offset = 0;
3228 if (getParser().ParseAbsoluteExpression(Offset))
3229 return true;
3230
Rafael Espindola066c2f42011-04-12 23:59:07 +00003231 getStreamer().EmitCFIDefCfa(Register, Offset);
3232 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003233}
3234
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003235/// ParseDirectiveCFIDefCfaOffset
3236/// ::= .cfi_def_cfa_offset offset
3237bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3238 SMLoc DirectiveLoc) {
3239 int64_t Offset = 0;
3240 if (getParser().ParseAbsoluteExpression(Offset))
3241 return true;
3242
Rafael Espindola066c2f42011-04-12 23:59:07 +00003243 getStreamer().EmitCFIDefCfaOffset(Offset);
3244 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003245}
3246
3247/// ParseDirectiveCFIAdjustCfaOffset
3248/// ::= .cfi_adjust_cfa_offset adjustment
3249bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3250 SMLoc DirectiveLoc) {
3251 int64_t Adjustment = 0;
3252 if (getParser().ParseAbsoluteExpression(Adjustment))
3253 return true;
3254
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003255 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3256 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003257}
3258
3259/// ParseDirectiveCFIDefCfaRegister
3260/// ::= .cfi_def_cfa_register register
3261bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3262 SMLoc DirectiveLoc) {
3263 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003264 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003265 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003266
Rafael Espindola066c2f42011-04-12 23:59:07 +00003267 getStreamer().EmitCFIDefCfaRegister(Register);
3268 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003269}
3270
3271/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003272/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003273bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3274 int64_t Register = 0;
3275 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003276
3277 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003278 return true;
3279
3280 if (getLexer().isNot(AsmToken::Comma))
3281 return TokError("unexpected token in directive");
3282 Lex();
3283
3284 if (getParser().ParseAbsoluteExpression(Offset))
3285 return true;
3286
Rafael Espindola066c2f42011-04-12 23:59:07 +00003287 getStreamer().EmitCFIOffset(Register, Offset);
3288 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003289}
3290
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003291/// ParseDirectiveCFIRelOffset
3292/// ::= .cfi_rel_offset register, offset
3293bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3294 SMLoc DirectiveLoc) {
3295 int64_t Register = 0;
3296
3297 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3298 return true;
3299
3300 if (getLexer().isNot(AsmToken::Comma))
3301 return TokError("unexpected token in directive");
3302 Lex();
3303
3304 int64_t Offset = 0;
3305 if (getParser().ParseAbsoluteExpression(Offset))
3306 return true;
3307
Rafael Espindola25f492e2011-04-12 16:12:03 +00003308 getStreamer().EmitCFIRelOffset(Register, Offset);
3309 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003310}
3311
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003312static bool isValidEncoding(int64_t Encoding) {
3313 if (Encoding & ~0xff)
3314 return false;
3315
3316 if (Encoding == dwarf::DW_EH_PE_omit)
3317 return true;
3318
3319 const unsigned Format = Encoding & 0xf;
3320 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3321 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3322 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3323 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3324 return false;
3325
Rafael Espindolacaf11582010-12-29 04:31:26 +00003326 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003327 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003328 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003329 return false;
3330
3331 return true;
3332}
3333
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003334/// ParseDirectiveCFIPersonalityOrLsda
3335/// ::= .cfi_personality encoding, [symbol_name]
3336/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003337bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003338 SMLoc DirectiveLoc) {
3339 int64_t Encoding = 0;
3340 if (getParser().ParseAbsoluteExpression(Encoding))
3341 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003342 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003343 return false;
3344
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003345 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003346 return TokError("unsupported encoding.");
3347
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003348 if (getLexer().isNot(AsmToken::Comma))
3349 return TokError("unexpected token in directive");
3350 Lex();
3351
3352 StringRef Name;
3353 if (getParser().ParseIdentifier(Name))
3354 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003355
3356 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3357
3358 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003359 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003360 else {
3361 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003362 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003363 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003364 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003365}
3366
Rafael Espindolafe024d02010-12-28 18:36:23 +00003367/// ParseDirectiveCFIRememberState
3368/// ::= .cfi_remember_state
3369bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3370 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003371 getStreamer().EmitCFIRememberState();
3372 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003373}
3374
3375/// ParseDirectiveCFIRestoreState
3376/// ::= .cfi_remember_state
3377bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3378 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003379 getStreamer().EmitCFIRestoreState();
3380 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003381}
3382
Rafael Espindolac5754392011-04-12 15:31:05 +00003383/// ParseDirectiveCFISameValue
3384/// ::= .cfi_same_value register
3385bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3386 SMLoc DirectiveLoc) {
3387 int64_t Register = 0;
3388
3389 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3390 return true;
3391
3392 getStreamer().EmitCFISameValue(Register);
3393
3394 return false;
3395}
3396
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003397/// ParseDirectiveCFIRestore
3398/// ::= .cfi_restore register
3399bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003400 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003401 int64_t Register = 0;
3402 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3403 return true;
3404
3405 getStreamer().EmitCFIRestore(Register);
3406
3407 return false;
3408}
3409
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003410/// ParseDirectiveCFIEscape
3411/// ::= .cfi_escape expression[,...]
3412bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003413 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003414 std::string Values;
3415 int64_t CurrValue;
3416 if (getParser().ParseAbsoluteExpression(CurrValue))
3417 return true;
3418
3419 Values.push_back((uint8_t)CurrValue);
3420
3421 while (getLexer().is(AsmToken::Comma)) {
3422 Lex();
3423
3424 if (getParser().ParseAbsoluteExpression(CurrValue))
3425 return true;
3426
3427 Values.push_back((uint8_t)CurrValue);
3428 }
3429
3430 getStreamer().EmitCFIEscape(Values);
3431 return false;
3432}
3433
Rafael Espindola16d7d432012-01-23 21:51:52 +00003434/// ParseDirectiveCFISignalFrame
3435/// ::= .cfi_signal_frame
3436bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3437 SMLoc DirectiveLoc) {
3438 if (getLexer().isNot(AsmToken::EndOfStatement))
3439 return Error(getLexer().getLoc(),
3440 "unexpected token in '" + Directive + "' directive");
3441
3442 getStreamer().EmitCFISignalFrame();
3443
3444 return false;
3445}
3446
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003447/// ParseDirectiveCFIUndefined
3448/// ::= .cfi_undefined register
3449bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3450 SMLoc DirectiveLoc) {
3451 int64_t Register = 0;
3452
3453 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3454 return true;
3455
3456 getStreamer().EmitCFIUndefined(Register);
3457
3458 return false;
3459}
3460
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003461/// ParseDirectiveCFIRegister
3462/// ::= .cfi_register register, register
3463bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3464 SMLoc DirectiveLoc) {
3465 int64_t Register1 = 0;
3466
3467 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3468 return true;
3469
3470 if (getLexer().isNot(AsmToken::Comma))
3471 return TokError("unexpected token in directive");
3472 Lex();
3473
3474 int64_t Register2 = 0;
3475
3476 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3477 return true;
3478
3479 getStreamer().EmitCFIRegister(Register1, Register2);
3480
3481 return false;
3482}
3483
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003484/// ParseDirectiveMacrosOnOff
3485/// ::= .macros_on
3486/// ::= .macros_off
3487bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3488 SMLoc DirectiveLoc) {
3489 if (getLexer().isNot(AsmToken::EndOfStatement))
3490 return Error(getLexer().getLoc(),
3491 "unexpected token in '" + Directive + "' directive");
3492
3493 getParser().MacrosEnabled = Directive == ".macros_on";
3494
3495 return false;
3496}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003497
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003498/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003499/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003500bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3501 SMLoc DirectiveLoc) {
3502 StringRef Name;
3503 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003504 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003505
Rafael Espindola8a403d32012-08-08 14:51:03 +00003506 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003507 // Argument delimiter is initially unknown. It will be set by
3508 // ParseMacroArgument()
3509 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003510 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003511 for (;;) {
3512 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003513 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003514 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003515
3516 if (getLexer().is(AsmToken::Equal)) {
3517 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003518 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003519 return true;
3520 }
3521
Rafael Espindola65366442011-06-05 02:43:45 +00003522 Parameters.push_back(Parameter);
3523
Preston Gurd7b6f2032012-09-19 20:36:12 +00003524 if (getLexer().is(AsmToken::Comma))
3525 Lex();
3526 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003527 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003528 }
3529 }
3530
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003531 // Eat the end of statement.
3532 Lex();
3533
3534 AsmToken EndToken, StartToken = getTok();
3535
3536 // Lex the macro definition.
3537 for (;;) {
3538 // Check whether we have reached the end of the file.
3539 if (getLexer().is(AsmToken::Eof))
3540 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3541
3542 // Otherwise, check whether we have reach the .endmacro.
3543 if (getLexer().is(AsmToken::Identifier) &&
3544 (getTok().getIdentifier() == ".endm" ||
3545 getTok().getIdentifier() == ".endmacro")) {
3546 EndToken = getTok();
3547 Lex();
3548 if (getLexer().isNot(AsmToken::EndOfStatement))
3549 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3550 "' directive");
3551 break;
3552 }
3553
3554 // Otherwise, scan til the end of the statement.
3555 getParser().EatToEndOfStatement();
3556 }
3557
3558 if (getParser().MacroMap.lookup(Name)) {
3559 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3560 }
3561
3562 const char *BodyStart = StartToken.getLoc().getPointer();
3563 const char *BodyEnd = EndToken.getLoc().getPointer();
3564 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003565 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003566 return false;
3567}
3568
3569/// ParseDirectiveEndMacro
3570/// ::= .endm
3571/// ::= .endmacro
3572bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003573 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003574 if (getLexer().isNot(AsmToken::EndOfStatement))
3575 return TokError("unexpected token in '" + Directive + "' directive");
3576
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003577 // If we are inside a macro instantiation, terminate the current
3578 // instantiation.
3579 if (!getParser().ActiveMacros.empty()) {
3580 getParser().HandleMacroExit();
3581 return false;
3582 }
3583
3584 // Otherwise, this .endmacro is a stray entry in the file; well formed
3585 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003586 return TokError("unexpected '" + Directive + "' in file, "
3587 "no current macro definition");
3588}
3589
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003590/// ParseDirectivePurgeMacro
3591/// ::= .purgem
3592bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3593 SMLoc DirectiveLoc) {
3594 StringRef Name;
3595 if (getParser().ParseIdentifier(Name))
3596 return TokError("expected identifier in '.purgem' directive");
3597
3598 if (getLexer().isNot(AsmToken::EndOfStatement))
3599 return TokError("unexpected token in '.purgem' directive");
3600
3601 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3602 if (I == getParser().MacroMap.end())
3603 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3604
3605 // Undefine the macro.
3606 delete I->getValue();
3607 getParser().MacroMap.erase(I);
3608 return false;
3609}
3610
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003611bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003612 getParser().CheckForValidSection();
3613
3614 const MCExpr *Value;
3615
3616 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003617 return true;
3618
3619 if (getLexer().isNot(AsmToken::EndOfStatement))
3620 return TokError("unexpected token in directive");
3621
3622 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003623 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003624 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003625 getStreamer().EmitULEB128Value(Value);
3626
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003627 return false;
3628}
3629
Rafael Espindola761cb062012-06-03 23:57:14 +00003630Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003631 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003632
Rafael Espindola761cb062012-06-03 23:57:14 +00003633 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003634 for (;;) {
3635 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003636 if (getLexer().is(AsmToken::Eof)) {
3637 Error(DirectiveLoc, "no matching '.endr' in definition");
3638 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003639 }
3640
Rafael Espindola761cb062012-06-03 23:57:14 +00003641 if (Lexer.is(AsmToken::Identifier) &&
3642 (getTok().getIdentifier() == ".rept")) {
3643 ++NestLevel;
3644 }
3645
3646 // Otherwise, check whether we have reached the .endr.
3647 if (Lexer.is(AsmToken::Identifier) &&
3648 getTok().getIdentifier() == ".endr") {
3649 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003650 EndToken = getTok();
3651 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003652 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3653 TokError("unexpected token in '.endr' directive");
3654 return 0;
3655 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003656 break;
3657 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003658 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003659 }
3660
Rafael Espindola761cb062012-06-03 23:57:14 +00003661 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003662 EatToEndOfStatement();
3663 }
3664
3665 const char *BodyStart = StartToken.getLoc().getPointer();
3666 const char *BodyEnd = EndToken.getLoc().getPointer();
3667 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3668
Rafael Espindola761cb062012-06-03 23:57:14 +00003669 // We Are Anonymous.
3670 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003671 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003672 return new Macro(Name, Body, Parameters);
3673}
3674
3675void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3676 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003677 OS << ".endr\n";
3678
3679 MemoryBuffer *Instantiation =
3680 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3681
Rafael Espindola761cb062012-06-03 23:57:14 +00003682 // Create the macro instantiation object and add to the current macro
3683 // instantiation stack.
3684 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00003685 CurBuffer,
Rafael Espindola761cb062012-06-03 23:57:14 +00003686 getTok().getLoc(),
3687 Instantiation);
3688 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003689
Rafael Espindola761cb062012-06-03 23:57:14 +00003690 // Jump to the macro instantiation and prime the lexer.
3691 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3692 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3693 Lex();
3694}
3695
3696bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3697 int64_t Count;
3698 if (ParseAbsoluteExpression(Count))
3699 return TokError("unexpected token in '.rept' directive");
3700
3701 if (Count < 0)
3702 return TokError("Count is negative");
3703
3704 if (Lexer.isNot(AsmToken::EndOfStatement))
3705 return TokError("unexpected token in '.rept' directive");
3706
3707 // Eat the end of statement.
3708 Lex();
3709
3710 // Lex the rept definition.
3711 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3712 if (!M)
3713 return true;
3714
3715 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3716 // to hold the macro body with substitutions.
3717 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003718 MacroParameters Parameters;
3719 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003720 raw_svector_ostream OS(Buf);
3721 while (Count--) {
3722 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3723 return true;
3724 }
3725 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003726
3727 return false;
3728}
3729
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003730/// ParseDirectiveIrp
3731/// ::= .irp symbol,values
3732bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003733 MacroParameters Parameters;
3734 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003735
Preston Gurd6c9176a2012-09-19 20:29:04 +00003736 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003737 return TokError("expected identifier in '.irp' directive");
3738
3739 Parameters.push_back(Parameter);
3740
3741 if (Lexer.isNot(AsmToken::Comma))
3742 return TokError("expected comma in '.irp' directive");
3743
3744 Lex();
3745
Rafael Espindola8a403d32012-08-08 14:51:03 +00003746 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003747 if (ParseMacroArguments(0, A))
3748 return true;
3749
3750 // Eat the end of statement.
3751 Lex();
3752
3753 // Lex the irp definition.
3754 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3755 if (!M)
3756 return true;
3757
3758 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3759 // to hold the macro body with substitutions.
3760 SmallString<256> Buf;
3761 raw_svector_ostream OS(Buf);
3762
Rafael Espindola7996d042012-08-21 16:06:48 +00003763 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3764 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003765 Args.push_back(*i);
3766
3767 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3768 return true;
3769 }
3770
3771 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3772
3773 return false;
3774}
3775
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003776/// ParseDirectiveIrpc
3777/// ::= .irpc symbol,values
3778bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003779 MacroParameters Parameters;
3780 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003781
Preston Gurd6c9176a2012-09-19 20:29:04 +00003782 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003783 return TokError("expected identifier in '.irpc' directive");
3784
3785 Parameters.push_back(Parameter);
3786
3787 if (Lexer.isNot(AsmToken::Comma))
3788 return TokError("expected comma in '.irpc' directive");
3789
3790 Lex();
3791
Rafael Espindola8a403d32012-08-08 14:51:03 +00003792 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003793 if (ParseMacroArguments(0, A))
3794 return true;
3795
3796 if (A.size() != 1 || A.front().size() != 1)
3797 return TokError("unexpected token in '.irpc' directive");
3798
3799 // Eat the end of statement.
3800 Lex();
3801
3802 // Lex the irpc definition.
3803 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3804 if (!M)
3805 return true;
3806
3807 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3808 // to hold the macro body with substitutions.
3809 SmallString<256> Buf;
3810 raw_svector_ostream OS(Buf);
3811
3812 StringRef Values = A.front().front().getString();
3813 std::size_t I, End = Values.size();
3814 for (I = 0; I < End; ++I) {
3815 MacroArgument Arg;
3816 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3817
Rafael Espindola8a403d32012-08-08 14:51:03 +00003818 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003819 Args.push_back(Arg);
3820
3821 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3822 return true;
3823 }
3824
3825 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3826
3827 return false;
3828}
3829
Rafael Espindola761cb062012-06-03 23:57:14 +00003830bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3831 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003832 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003833
3834 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003835 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003836 assert(getLexer().is(AsmToken::EndOfStatement));
3837
Rafael Espindola761cb062012-06-03 23:57:14 +00003838 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003839 return false;
3840}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003841
Eli Friedman2128aae2012-10-22 23:58:19 +00003842bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) {
3843 const MCExpr *Value;
3844 SMLoc ExprLoc = getLexer().getLoc();
3845 if (ParseExpression(Value))
3846 return true;
3847 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3848 if (!MCE)
3849 return Error(ExprLoc, "unexpected expression in _emit");
3850 uint64_t IntValue = MCE->getValue();
3851 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
3852 return Error(ExprLoc, "literal value out of range for directive");
3853
3854 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5));
3855 return false;
3856}
3857
Chad Rosierb1f8c132012-10-18 15:49:34 +00003858bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3859 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003860 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003861 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003862 SmallVectorImpl<std::string> &Clobbers,
3863 const MCInstrInfo *MII,
3864 const MCInstPrinter *IP,
3865 MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003866 SmallVector<void *, 4> InputDecls;
3867 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003868 SmallVector<bool, 4> InputDeclsAddressOf;
3869 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003870 SmallVector<std::string, 4> InputConstraints;
3871 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003872 std::set<std::string> ClobberRegs;
3873
Chad Rosier4e472d22012-10-20 01:02:45 +00003874 SmallVector<struct AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003875
3876 // Prime the lexer.
3877 Lex();
3878
3879 // While we have input, parse each statement.
3880 unsigned InputIdx = 0;
3881 unsigned OutputIdx = 0;
3882 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00003883 ParseStatementInfo Info(&AsmStrRewrites);
3884 if (ParseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00003885 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003886
Chad Rosier57498012012-12-12 22:45:52 +00003887 if (Info.ParseError)
3888 return true;
3889
Eli Friedman2128aae2012-10-22 23:58:19 +00003890 if (Info.Opcode != ~0U) {
3891 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003892
3893 // Build the list of clobbers, outputs and inputs.
Eli Friedman2128aae2012-10-22 23:58:19 +00003894 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
3895 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003896
3897 // Immediate.
3898 if (Operand->isImm()) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00003899 if (Operand->needAsmRewrite())
3900 AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix,
3901 Operand->getStartLoc()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003902 continue;
3903 }
3904
3905 // Register operand.
Chad Rosierc1ec2072013-01-10 22:10:27 +00003906 if (Operand->isReg() && !Operand->needAddressOf()) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003907 unsigned NumDefs = Desc.getNumDefs();
3908 // Clobber.
3909 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3910 std::string Reg;
3911 raw_string_ostream OS(Reg);
3912 IP->printRegName(OS, Operand->getReg());
3913 ClobberRegs.insert(StringRef(OS.str()));
3914 }
3915 continue;
3916 }
3917
3918 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003919 unsigned Size;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003920 bool IsVarDecl;
Chad Rosier32989592012-10-18 20:27:15 +00003921 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +00003922 Size, IsVarDecl);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003923 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003924 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierc1ec2072013-01-10 22:10:27 +00003925 if (Operand->isMem() && Operand->needSizeDirective())
Chad Rosier4e472d22012-10-20 01:02:45 +00003926 AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective,
Chad Rosierefcb3d92012-10-26 18:04:20 +00003927 Operand->getStartLoc(),
3928 /*Len*/0,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003929 Operand->getMemSize()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003930 if (isOutput) {
3931 std::string Constraint = "=";
3932 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003933 OutputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003934 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003935 Constraint += Operand->getConstraint().str();
3936 OutputConstraints.push_back(Constraint);
Chad Rosier4e472d22012-10-20 01:02:45 +00003937 AsmStrRewrites.push_back(AsmRewrite(AOK_Output,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003938 Operand->getStartLoc(),
3939 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003940 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003941 InputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003942 InputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003943 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosier4e472d22012-10-20 01:02:45 +00003944 AsmStrRewrites.push_back(AsmRewrite(AOK_Input,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003945 Operand->getStartLoc(),
3946 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003947 }
3948 }
3949 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003950 }
3951 }
3952
3953 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003954 NumOutputs = OutputDecls.size();
3955 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003956
3957 // Set the unique clobbers.
3958 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3959 E = ClobberRegs.end(); I != E; ++I)
3960 Clobbers.push_back(*I);
3961
3962 // Merge the various outputs and inputs. Output are expected first.
3963 if (NumOutputs || NumInputs) {
3964 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003965 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003966 Constraints.resize(NumExprs);
Chad Rosier5a719fc2012-10-23 17:43:43 +00003967 // FIXME: Constraints are hard coded to 'm', but we need an 'r'
Chad Rosierc1ec2072013-01-10 22:10:27 +00003968 // constraint for addressof. This needs to be cleaned up!
Chad Rosierb1f8c132012-10-18 15:49:34 +00003969 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003970 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
3971 Constraints[i] = OutputDeclsAddressOf[i] ? "=r" : OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003972 }
3973 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003974 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
3975 Constraints[j] = InputDeclsAddressOf[i] ? "r" : InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003976 }
3977 }
3978
3979 // Build the IR assembly string.
3980 std::string AsmStringIR;
Chad Rosier4e472d22012-10-20 01:02:45 +00003981 AsmRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003982 raw_string_ostream OS(AsmStringIR);
3983 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
Chad Rosier4e472d22012-10-20 01:02:45 +00003984 for (SmallVectorImpl<struct AsmRewrite>::iterator
Chad Rosierb1f8c132012-10-18 15:49:34 +00003985 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3986 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003987
Chad Rosier4e472d22012-10-20 01:02:45 +00003988 AsmRewriteKind Kind = (*I).Kind;
Chad Rosier96d58e62012-10-19 20:57:14 +00003989
3990 // Emit everything up to the immediate/expression. If the previous rewrite
3991 // was a size directive, then this has already been done.
3992 if (PrevKind != AOK_SizeDirective)
3993 OS << StringRef(Start, Loc - Start);
3994 PrevKind = Kind;
3995
Chad Rosier5a719fc2012-10-23 17:43:43 +00003996 // Skip the original expression.
3997 if (Kind == AOK_Skip) {
3998 Start = Loc + (*I).Len;
3999 continue;
4000 }
4001
Chad Rosierb1f8c132012-10-18 15:49:34 +00004002 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004003 switch (Kind) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004004 default: break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004005 case AOK_Imm:
Chad Rosierefcb3d92012-10-26 18:04:20 +00004006 OS << Twine("$$");
4007 OS << (*I).Val;
4008 break;
4009 case AOK_ImmPrefix:
4010 OS << Twine("$$");
Chad Rosierb1f8c132012-10-18 15:49:34 +00004011 break;
4012 case AOK_Input:
4013 OS << '$';
4014 OS << InputIdx++;
4015 break;
4016 case AOK_Output:
4017 OS << '$';
4018 OS << OutputIdx++;
4019 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004020 case AOK_SizeDirective:
Chad Rosier6a020a72012-10-25 20:41:34 +00004021 switch((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004022 default: break;
4023 case 8: OS << "byte ptr "; break;
4024 case 16: OS << "word ptr "; break;
4025 case 32: OS << "dword ptr "; break;
4026 case 64: OS << "qword ptr "; break;
4027 case 80: OS << "xword ptr "; break;
4028 case 128: OS << "xmmword ptr "; break;
4029 case 256: OS << "ymmword ptr "; break;
4030 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004031 break;
4032 case AOK_Emit:
4033 OS << ".byte";
4034 break;
Chad Rosier6a020a72012-10-25 20:41:34 +00004035 case AOK_DotOperator:
4036 OS << (*I).Val;
4037 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004038 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004039
Chad Rosierb1f8c132012-10-18 15:49:34 +00004040 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00004041 if (Kind != AOK_SizeDirective)
4042 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004043 }
4044
4045 // Emit the remainder of the asm string.
4046 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4047 if (Start != AsmEnd)
4048 OS << StringRef(Start, AsmEnd - Start);
4049
4050 AsmString = OS.str();
4051 return false;
4052}
4053
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004054/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004055MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004056 MCContext &C, MCStreamer &Out,
4057 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004058 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004059}