blob: 665d6729e60bf8c5da0f5cc2e27dc2cbfbca9475 [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
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000054/// \brief Helper class for storing information about an active macro
55/// instantiation.
56struct MacroInstantiation {
57 /// The macro being instantiated.
Eli Benderskyc0c67b02013-01-14 23:22:36 +000058 const MCAsmMacro *TheMacro;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000059
60 /// The macro instantiation with substitutions.
61 MemoryBuffer *Instantiation;
62
63 /// The location of the instantiation.
64 SMLoc InstantiationLoc;
65
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000066 /// The buffer where parsing should resume upon instantiation completion.
67 int ExitBuffer;
68
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000069 /// The location where parsing should resume upon instantiation completion.
70 SMLoc ExitLoc;
71
72public:
Eli Benderskyc0c67b02013-01-14 23:22:36 +000073 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000074 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000075};
76
Chad Rosier6a020a72012-10-25 20:41:34 +000077//struct AsmRewrite;
Eli Friedman2128aae2012-10-22 23:58:19 +000078struct ParseStatementInfo {
79 /// ParsedOperands - The parsed operands from the last parsed statement.
80 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
81
82 /// Opcode - The opcode from the last parsed instruction.
83 unsigned Opcode;
84
Chad Rosier57498012012-12-12 22:45:52 +000085 /// Error - Was there an error parsing the inline assembly?
86 bool ParseError;
87
Eli Friedman2128aae2012-10-22 23:58:19 +000088 SmallVectorImpl<AsmRewrite> *AsmRewrites;
89
Chad Rosier57498012012-12-12 22:45:52 +000090 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman2128aae2012-10-22 23:58:19 +000091 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier57498012012-12-12 22:45:52 +000092 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman2128aae2012-10-22 23:58:19 +000093
94 ~ParseStatementInfo() {
95 // Free any parsed operands.
96 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
97 delete ParsedOperands[i];
98 ParsedOperands.clear();
99 }
100};
101
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000102/// \brief The concrete assembly parser instance.
103class AsmParser : public MCAsmParser {
Craig Topper85aadc02012-09-15 16:23:52 +0000104 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
105 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000106private:
107 AsmLexer Lexer;
108 MCContext &Ctx;
109 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000110 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000111 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000112 SourceMgr::DiagHandlerTy SavedDiagHandler;
113 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000114 MCAsmParserExtension *GenericParser;
115 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000116
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000117 /// This is the current buffer index we're lexing from as managed by the
118 /// SourceMgr object.
119 int CurBuffer;
120
121 AsmCond TheCondState;
122 std::vector<AsmCond> TheCondStack;
123
124 /// DirectiveMap - This is a table handlers for directives. Each handler is
125 /// invoked after the directive identifier is read and is responsible for
126 /// parsing and validating the rest of the directive. The handler is passed
127 /// in the directive name and the location of the directive keyword.
128 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000129
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000130 /// MacroMap - Map of currently defined macros.
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000131 StringMap<MCAsmMacro*> MacroMap;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000132
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000133 /// ActiveMacros - Stack of active macro instantiations.
134 std::vector<MacroInstantiation*> ActiveMacros;
135
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000136 /// Boolean tracking whether macro substitution is enabled.
Eli Bendersky733c3362013-01-14 18:08:41 +0000137 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000138
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000139 /// Flag tracking whether any errors have been encountered.
140 unsigned HadError : 1;
141
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000142 /// The values from the last parsed cpp hash file line comment if any.
143 StringRef CppHashFilename;
144 int64_t CppHashLineNumber;
145 SMLoc CppHashLoc;
Kevin Enderby32c1a822012-11-05 21:55:41 +0000146 int CppHashBuf;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000147
Devang Patel0db58bf2012-01-31 18:14:05 +0000148 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
149 unsigned AssemblerDialect;
150
Preston Gurd7b6f2032012-09-19 20:36:12 +0000151 /// IsDarwin - is Darwin compatibility enabled?
152 bool IsDarwin;
153
Chad Rosier8f138d12012-10-15 17:19:13 +0000154 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000155 bool ParsingInlineAsm;
156
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000157public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000158 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000159 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000160 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000161
162 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
163
Craig Topper345d16d2012-08-29 05:48:09 +0000164 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
165 StringRef Directive,
166 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000167 DirectiveMap[Directive] = std::make_pair(Object, Handler);
168 }
169
170public:
171 /// @name MCAsmParser Interface
172 /// {
173
174 virtual SourceMgr &getSourceManager() { return SrcMgr; }
175 virtual MCAsmLexer &getLexer() { return Lexer; }
176 virtual MCContext &getContext() { return Ctx; }
177 virtual MCStreamer &getStreamer() { return Out; }
Eric Christopher2318ba12012-12-18 00:30:54 +0000178 virtual unsigned getAssemblerDialect() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000179 if (AssemblerDialect == ~0U)
Eric Christopher2318ba12012-12-18 00:30:54 +0000180 return MAI.getAssemblerDialect();
Devang Patel0db58bf2012-01-31 18:14:05 +0000181 else
182 return AssemblerDialect;
183 }
184 virtual void setAssemblerDialect(unsigned i) {
185 AssemblerDialect = i;
186 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000188 virtual bool Warning(SMLoc L, const Twine &Msg,
189 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
190 virtual bool Error(SMLoc L, const Twine &Msg,
191 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000192
Craig Topper345d16d2012-08-29 05:48:09 +0000193 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000194
Chad Rosier84125ca2012-10-13 00:26:04 +0000195 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000196 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000197
198 bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
199 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000200 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000201 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000202 SmallVectorImpl<std::string> &Clobbers,
203 const MCInstrInfo *MII,
204 const MCInstPrinter *IP,
205 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000206
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207 bool ParseExpression(const MCExpr *&Res);
208 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
209 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
210 virtual bool ParseAbsoluteExpression(int64_t &Res);
211
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000212 bool ParseMacroArgument(MCAsmMacroArgument &MA,
213 AsmToken::TokenKind &ArgumentDelimiter);
214
Eli Benderskybf706b32013-01-12 00:05:00 +0000215 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
216 /// and set \p Res to the identifier contents.
217 virtual bool ParseIdentifier(StringRef &Res);
Eli Benderskyb2f0b592013-01-12 00:23:24 +0000218 virtual void EatToEndOfStatement();
Eli Benderskybf706b32013-01-12 00:05:00 +0000219
Eli Bendersky733c3362013-01-14 18:08:41 +0000220 virtual bool MacrosEnabled() {return MacrosEnabledFlag;}
221 virtual void SetMacrosEnabled(bool flag) {MacrosEnabledFlag = flag;}
222
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000223 virtual const MCAsmMacro* LookupMacro(StringRef Name);
224 virtual void DefineMacro(StringRef Name, const MCAsmMacro& Macro);
225 virtual void UndefineMacro(StringRef Name);
226
227 virtual bool InsideMacroInstantiation() {return !ActiveMacros.empty();}
228 virtual bool HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc);
229 void HandleMacroExit();
230
Eli Bendersky318cad32013-01-14 19:15:01 +0000231 virtual void CheckForValidSection();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000232 /// }
233
234private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000235
Eli Friedman2128aae2012-10-22 23:58:19 +0000236 bool ParseStatement(ParseStatementInfo &Info);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000237 void EatToEndOfLine();
238 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000239
Rafael Espindola761cb062012-06-03 23:57:14 +0000240 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000241 const MCAsmMacroParameters &Parameters,
242 const MCAsmMacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000243 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000244
245 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000246 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000247 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
248 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000249 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000250 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000251
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000252 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
253 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000254 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
255 /// This returns true on failure.
256 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000257
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000258 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000259 /// current token is not set; clients should ensure Lex() is called
260 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000261 ///
262 /// \param InBuffer If not -1, should be the known buffer id that contains the
263 /// location.
264 void JumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000265
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000266 bool ParseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000267
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000268 /// \brief Parse up to the end of statement and a return the contents from the
269 /// current token until the end of the statement; the current token on exit
270 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000271 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000272
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000273 /// \brief Parse until the end of a statement or a comma is encountered,
274 /// return the contents from the current token up to the end or comma.
275 StringRef ParseStringToComma();
276
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000277 bool ParseAssignment(StringRef Name, bool allow_redef,
278 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000279
280 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
281 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
282 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000283 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000284
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000285 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000286
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000287 enum DirectiveKind {
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000288 DK_NO_DIRECTIVE, // Placeholder
289 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
290 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
291 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000292 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000293 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
294 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL,
295 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
296 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
297 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
298 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
299 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
300 DK_ELSEIF, DK_ELSE, DK_ENDIF
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000301 };
302
303 StringMap<DirectiveKind> DirectiveKindMapping;
304
305 // ".ascii", ".asciz", ".string"
Rafael Espindola787c3372010-10-28 20:02:27 +0000306 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000307 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000308 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000309 bool ParseDirectiveFill(); // ".fill"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000310 bool ParseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000311 // ".set", ".equ", ".equiv"
312 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000313 bool ParseDirectiveOrg(); // ".org"
314 // ".align{,32}", ".p2align{,w,l}"
315 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
316
Eli Bendersky4766ef42012-12-20 19:05:53 +0000317 // ".bundle_align_mode"
318 bool ParseDirectiveBundleAlignMode();
319 // ".bundle_lock"
320 bool ParseDirectiveBundleLock();
321 // ".bundle_unlock"
322 bool ParseDirectiveBundleUnlock();
323
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000324 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
325 /// accepts a single symbol (which should be a label or an external).
326 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000327
328 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
329
330 bool ParseDirectiveAbort(); // ".abort"
331 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000332 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000333
334 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000335 // ".ifb" or ".ifnb", depending on ExpectBlank.
336 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000337 // ".ifc" or ".ifnc", depending on ExpectEqual.
338 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000339 // ".ifdef" or ".ifndef", depending on expect_defined
340 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000341 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
342 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
343 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
344
345 /// ParseEscapedString - Parse the current token as a string which may include
346 /// escaped characters and return the string contents.
347 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000348
349 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
350 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000351
Rafael Espindola761cb062012-06-03 23:57:14 +0000352 // Macro-like directives
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000353 MCAsmMacro *ParseMacroLikeBody(SMLoc DirectiveLoc);
354 void InstantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +0000355 raw_svector_ostream &OS);
356 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000357 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000358 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000359 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000360
Eli Friedman2128aae2012-10-22 23:58:19 +0000361 // "_emit"
362 bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000363
364 void initializeDirectiveKindMapping();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000365};
366
Eli Bendersky63e6f482013-01-10 23:32:57 +0000367/// \brief Generic implementation of directive handling, etc. which is shared
368/// (or the default, at least) for all assembler parsers.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000369class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000370 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
371 void AddDirectiveHandler(StringRef Directive) {
372 getParser().AddDirectiveHandler(this, Directive,
373 HandleDirective<GenericAsmParser, Handler>);
374 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000375public:
376 GenericAsmParser() {}
377
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000378 AsmParser &getParser() {
379 return (AsmParser&) this->MCAsmParserExtension::getParser();
380 }
381
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000382 virtual void Initialize(MCAsmParser &Parser) {
383 // Call the base implementation.
384 this->MCAsmParserExtension::Initialize(Parser);
385
386 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000387 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
388 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
389 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000390 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000391
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000392 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".space");
393 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".skip");
394
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000395 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000396 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
397 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000398 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
399 ".cfi_startproc");
400 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
401 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000402 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
403 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000404 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
405 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000406 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
407 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000408 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
409 ".cfi_def_cfa_register");
410 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
411 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000412 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
413 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000414 AddDirectiveHandler<
415 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
416 AddDirectiveHandler<
417 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000418 AddDirectiveHandler<
419 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
420 AddDirectiveHandler<
421 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000422 AddDirectiveHandler<
423 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000424 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000425 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
426 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000427 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000428 AddDirectiveHandler<
429 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000430 AddDirectiveHandler<
431 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000432 AddDirectiveHandler<
433 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000434
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000435 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000436 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
437 ".macros_on");
438 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
439 ".macros_off");
440 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
441 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
442 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000443 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000444
445 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
446 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000447 }
448
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000449 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
450
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000451 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
452 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
453 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000454 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000455 bool ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000456 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000457 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
458 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000459 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000460 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000461 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000462 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
463 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000464 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000465 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000466 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
467 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000468 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000469 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000470 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000471 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000472 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000473 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000474
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000475 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000476 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
477 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000478 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000479
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000480 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000481};
482
483}
484
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000485namespace llvm {
486
487extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000488extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000489extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000490
491}
492
Chris Lattneraaec2052010-01-19 19:46:13 +0000493enum { DEFAULT_ADDRSPACE = 0 };
494
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000495AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000496 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000497 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000498 GenericParser(new GenericAsmParser), PlatformParser(0),
Eli Bendersky733c3362013-01-14 18:08:41 +0000499 CurBuffer(0), MacrosEnabledFlag(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000500 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000501 // Save the old handler.
502 SavedDiagHandler = SrcMgr.getDiagHandler();
503 SavedDiagContext = SrcMgr.getDiagContext();
504 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000505 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000506 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000507
508 // Initialize the generic parser.
509 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000510
511 // Initialize the platform / file format parser.
512 //
513 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
514 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000515 if (_MAI.hasMicrosoftFastStdCallMangling()) {
516 PlatformParser = createCOFFAsmParser();
517 PlatformParser->Initialize(*this);
518 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000519 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000520 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000521 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000522 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000523 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000524 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000525 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000526
527 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000528}
529
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000530AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000531 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
532
533 // Destroy any macros.
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000534 for (StringMap<MCAsmMacro*>::iterator it = MacroMap.begin(),
Daniel Dunbar56491302010-07-29 01:51:55 +0000535 ie = MacroMap.end(); it != ie; ++it)
536 delete it->getValue();
537
Daniel Dunbare4749702010-07-12 18:12:02 +0000538 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000539 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000540}
541
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000542void AsmParser::PrintMacroInstantiations() {
543 // Print the active macro instantiation stack.
544 for (std::vector<MacroInstantiation*>::const_reverse_iterator
545 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000546 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
547 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000548}
549
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000550bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000551 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000552 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000553 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000554 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000555 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000556}
557
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000558bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000559 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000560 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000561 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000562 return true;
563}
564
Sean Callananfd0b0282010-01-21 00:19:58 +0000565bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000566 std::string IncludedFile;
567 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000568 if (NewBuf == -1)
569 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000570
Sean Callananfd0b0282010-01-21 00:19:58 +0000571 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000572
Sean Callananfd0b0282010-01-21 00:19:58 +0000573 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000574
Sean Callananfd0b0282010-01-21 00:19:58 +0000575 return false;
576}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000577
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000578/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000579/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000580/// returns true on failure.
581bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
582 std::string IncludedFile;
583 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
584 if (NewBuf == -1)
585 return true;
586
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000587 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000588 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
589 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000590 return false;
591}
592
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000593void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
594 if (InBuffer != -1) {
595 CurBuffer = InBuffer;
596 } else {
597 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
598 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000599 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
600}
601
Sean Callananfd0b0282010-01-21 00:19:58 +0000602const AsmToken &AsmParser::Lex() {
603 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000604
Sean Callananfd0b0282010-01-21 00:19:58 +0000605 if (tok->is(AsmToken::Eof)) {
606 // If this is the end of an included file, pop the parent file off the
607 // include stack.
608 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
609 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000610 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000611 tok = &Lexer.Lex();
612 }
613 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000614
Sean Callananfd0b0282010-01-21 00:19:58 +0000615 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000616 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000617
Sean Callananfd0b0282010-01-21 00:19:58 +0000618 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000619}
620
Chris Lattner79180e22010-04-05 23:15:42 +0000621bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000622 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000623 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000624 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000625
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000626 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000627 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000628
629 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000630 AsmCond StartingCondState = TheCondState;
631
Kevin Enderby613b7572011-11-01 22:27:22 +0000632 // If we are generating dwarf for assembly source files save the initial text
633 // section and generate a .file directive.
634 if (getContext().getGenDwarfForAssembly()) {
635 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000636 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
637 getStreamer().EmitLabel(SectionStartSym);
638 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000639 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000640 StringRef(),
641 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000642 }
643
Chris Lattnerb717fb02009-07-02 21:53:43 +0000644 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000645 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000646 ParseStatementInfo Info;
647 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000648
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000649 // We had an error, validate that one was emitted and recover by skipping to
650 // the next line.
651 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000652 EatToEndOfStatement();
653 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000654
655 if (TheCondState.TheCond != StartingCondState.TheCond ||
656 TheCondState.Ignore != StartingCondState.Ignore)
657 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000658
659 // Check to see there are no empty DwarfFile slots.
660 const std::vector<MCDwarfFile *> &MCDwarfFiles =
661 getContext().getMCDwarfFiles();
662 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000663 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000664 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000665 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000666
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000667 // Check to see that all assembler local symbols were actually defined.
668 // Targets that don't do subsections via symbols may not want this, though,
669 // so conservatively exclude them. Only do this if we're finalizing, though,
670 // as otherwise we won't necessarilly have seen everything yet.
671 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
672 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
673 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
674 e = Symbols.end();
675 i != e; ++i) {
676 MCSymbol *Sym = i->getValue();
677 // Variable symbols may not be marked as defined, so check those
678 // explicitly. If we know it's a variable, we have a definition for
679 // the purposes of this check.
680 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
681 // FIXME: We would really like to refer back to where the symbol was
682 // first referenced for a source location. We need to add something
683 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000684 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
685 "assembler local symbol '" + Sym->getName() +
686 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000687 }
688 }
689
690
Chris Lattner79180e22010-04-05 23:15:42 +0000691 // Finalize the output stream if there are no errors and if the client wants
692 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000693 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000694 Out.Finish();
695
Chris Lattnerb717fb02009-07-02 21:53:43 +0000696 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000697}
698
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000699void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000700 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000701 TokError("expected section directive before assembly directive");
Eli Bendersky030f63a2013-01-14 19:04:57 +0000702 Out.InitToTextSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000703 }
704}
705
Chris Lattner2cf5f142009-06-22 01:29:09 +0000706/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
707void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000708 while (Lexer.isNot(AsmToken::EndOfStatement) &&
709 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000710 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000711
Chris Lattner2cf5f142009-06-22 01:29:09 +0000712 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000713 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000714 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000715}
716
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000717StringRef AsmParser::ParseStringToEndOfStatement() {
718 const char *Start = getTok().getLoc().getPointer();
719
720 while (Lexer.isNot(AsmToken::EndOfStatement) &&
721 Lexer.isNot(AsmToken::Eof))
722 Lex();
723
724 const char *End = getTok().getLoc().getPointer();
725 return StringRef(Start, End - Start);
726}
Chris Lattnerc4193832009-06-22 05:51:26 +0000727
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000728StringRef AsmParser::ParseStringToComma() {
729 const char *Start = getTok().getLoc().getPointer();
730
731 while (Lexer.isNot(AsmToken::EndOfStatement) &&
732 Lexer.isNot(AsmToken::Comma) &&
733 Lexer.isNot(AsmToken::Eof))
734 Lex();
735
736 const char *End = getTok().getLoc().getPointer();
737 return StringRef(Start, End - Start);
738}
739
Chris Lattner74ec1a32009-06-22 06:32:03 +0000740/// ParseParenExpr - Parse a paren expression and return it.
741/// NOTE: This assumes the leading '(' has already been consumed.
742///
743/// parenexpr ::= expr)
744///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000745bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000746 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000747 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000748 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000749 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000750 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000751 return false;
752}
Chris Lattnerc4193832009-06-22 05:51:26 +0000753
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000754/// ParseBracketExpr - Parse a bracket expression and return it.
755/// NOTE: This assumes the leading '[' has already been consumed.
756///
757/// bracketexpr ::= expr]
758///
759bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
760 if (ParseExpression(Res)) return true;
761 if (Lexer.isNot(AsmToken::RBrac))
762 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000763 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000764 Lex();
765 return false;
766}
767
Chris Lattner74ec1a32009-06-22 06:32:03 +0000768/// ParsePrimaryExpr - Parse a primary expression and return it.
769/// primaryexpr ::= (parenexpr
770/// primaryexpr ::= symbol
771/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000772/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000773/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000774bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000775 switch (Lexer.getKind()) {
776 default:
777 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000778 // If we have an error assume that we've already handled it.
779 case AsmToken::Error:
780 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000782 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000783 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000784 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000785 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000787 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000788 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000789 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000790 StringRef Identifier;
791 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000792 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000793
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000794 EndLoc = SMLoc::getFromPointer(Identifier.end());
795
Daniel Dunbarfffff912009-10-16 01:34:54 +0000796 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000797 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000798 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000799
800 // Lookup the symbol variant if used.
801 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000802 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000803 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000804 if (Variant == MCSymbolRefExpr::VK_Invalid) {
805 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000806 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000807 }
808 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000809
Daniel Dunbarfffff912009-10-16 01:34:54 +0000810 // If this is an absolute variable reference, substitute it now to preserve
811 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000812 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000813 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000814 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000815
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000816 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000817 return false;
818 }
819
820 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000821 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000822 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000823 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000824 case AsmToken::Integer: {
825 SMLoc Loc = getTok().getLoc();
826 int64_t IntVal = getTok().getIntVal();
827 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000828 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000829 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000830 // Look for 'b' or 'f' following an Integer as a directional label
831 if (Lexer.getKind() == AsmToken::Identifier) {
832 StringRef IDVal = getTok().getString();
833 if (IDVal == "f" || IDVal == "b"){
834 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
835 IDVal == "f" ? 1 : 0);
836 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
837 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000838 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000839 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000840 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000841 Lex(); // Eat identifier.
842 }
843 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000844 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000845 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000846 case AsmToken::Real: {
847 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000848 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000849 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000850 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000851 Lex(); // Eat token.
852 return false;
853 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000854 case AsmToken::Dot: {
855 // This is a '.' reference, which references the current PC. Emit a
856 // temporary label to the streamer and refer to it.
857 MCSymbol *Sym = Ctx.CreateTempSymbol();
858 Out.EmitLabel(Sym);
859 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000860 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000861 Lex(); // Eat identifier.
862 return false;
863 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000864 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000865 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000866 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000867 case AsmToken::LBrac:
868 if (!PlatformParser->HasBracketExpressions())
869 return TokError("brackets expression not supported on this target");
870 Lex(); // Eat the '['.
871 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000872 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000873 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000874 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000875 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000876 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000877 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000878 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000879 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000880 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000881 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000882 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000883 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000885 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000886 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000887 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000888 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000889 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000890 }
891}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000892
Chris Lattnerb4307b32010-01-15 19:28:38 +0000893bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000894 SMLoc EndLoc;
895 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000896}
897
Daniel Dunbarcceba832010-09-17 02:47:07 +0000898const MCExpr *
899AsmParser::ApplyModifierToExpr(const MCExpr *E,
900 MCSymbolRefExpr::VariantKind Variant) {
901 // Recurse over the given expression, rebuilding it to apply the given variant
902 // if there is exactly one symbol.
903 switch (E->getKind()) {
904 case MCExpr::Target:
905 case MCExpr::Constant:
906 return 0;
907
908 case MCExpr::SymbolRef: {
909 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
910
911 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
912 TokError("invalid variant on expression '" +
913 getTok().getIdentifier() + "' (already modified)");
914 return E;
915 }
916
917 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
918 }
919
920 case MCExpr::Unary: {
921 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
922 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
923 if (!Sub)
924 return 0;
925 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
926 }
927
928 case MCExpr::Binary: {
929 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
930 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
931 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
932
933 if (!LHS && !RHS)
934 return 0;
935
936 if (!LHS) LHS = BE->getLHS();
937 if (!RHS) RHS = BE->getRHS();
938
939 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
940 }
941 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000942
Craig Topper85814382012-02-07 05:05:23 +0000943 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000944}
945
Chris Lattner74ec1a32009-06-22 06:32:03 +0000946/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000947///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000948/// expr ::= expr &&,|| expr -> lowest.
949/// expr ::= expr |,^,&,! expr
950/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
951/// expr ::= expr <<,>> expr
952/// expr ::= expr +,- expr
953/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000954/// expr ::= primaryexpr
955///
Chris Lattner54482b42010-01-15 19:39:23 +0000956bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000957 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000958 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000959 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
960 return true;
961
Daniel Dunbarcceba832010-09-17 02:47:07 +0000962 // As a special case, we support 'a op b @ modifier' by rewriting the
963 // expression to include the modifier. This is inefficient, but in general we
964 // expect users to use 'a@modifier op b'.
965 if (Lexer.getKind() == AsmToken::At) {
966 Lex();
967
968 if (Lexer.isNot(AsmToken::Identifier))
969 return TokError("unexpected symbol modifier following '@'");
970
971 MCSymbolRefExpr::VariantKind Variant =
972 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
973 if (Variant == MCSymbolRefExpr::VK_Invalid)
974 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
975
976 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
977 if (!ModifiedRes) {
978 return TokError("invalid modifier '" + getTok().getIdentifier() +
979 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000980 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000981
Daniel Dunbarcceba832010-09-17 02:47:07 +0000982 Res = ModifiedRes;
983 Lex();
984 }
985
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000986 // Try to constant fold it up front, if possible.
987 int64_t Value;
988 if (Res->EvaluateAsAbsolute(Value))
989 Res = MCConstantExpr::Create(Value, getContext());
990
991 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000992}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000993
Chris Lattnerb4307b32010-01-15 19:28:38 +0000994bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000995 Res = 0;
996 return ParseParenExpr(Res, EndLoc) ||
997 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000998}
999
Daniel Dunbar475839e2009-06-29 20:37:27 +00001000bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001001 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001002
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001003 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +00001004 if (ParseExpression(Expr))
1005 return true;
1006
Daniel Dunbare00b0112009-10-16 01:57:52 +00001007 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001008 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001009
1010 return false;
1011}
1012
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001014 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001015 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001016 default:
1017 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001018
Jim Grosbachfbe16812011-08-20 16:24:13 +00001019 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001020 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001021 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001022 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001023 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001024 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001025 return 1;
1026
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001027
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001028 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +00001029 //
1030 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001031 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001032 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001033 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001034 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001035 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001036 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001037 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001038 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001039 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001040
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001041 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001042 case AsmToken::EqualEqual:
1043 Kind = MCBinaryExpr::EQ;
1044 return 3;
1045 case AsmToken::ExclaimEqual:
1046 case AsmToken::LessGreater:
1047 Kind = MCBinaryExpr::NE;
1048 return 3;
1049 case AsmToken::Less:
1050 Kind = MCBinaryExpr::LT;
1051 return 3;
1052 case AsmToken::LessEqual:
1053 Kind = MCBinaryExpr::LTE;
1054 return 3;
1055 case AsmToken::Greater:
1056 Kind = MCBinaryExpr::GT;
1057 return 3;
1058 case AsmToken::GreaterEqual:
1059 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001060 return 3;
1061
Jim Grosbachfbe16812011-08-20 16:24:13 +00001062 // Intermediate Precedence: <<, >>
1063 case AsmToken::LessLess:
1064 Kind = MCBinaryExpr::Shl;
1065 return 4;
1066 case AsmToken::GreaterGreater:
1067 Kind = MCBinaryExpr::Shr;
1068 return 4;
1069
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001070 // High Intermediate Precedence: +, -
1071 case AsmToken::Plus:
1072 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001073 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001074 case AsmToken::Minus:
1075 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001076 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001077
Jim Grosbachfbe16812011-08-20 16:24:13 +00001078 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001079 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001080 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001081 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001082 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001083 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001084 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001085 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001086 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001087 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001088 }
1089}
1090
1091
1092/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1093/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001094bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1095 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001096 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001097 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001098 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001099
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001100 // If the next token is lower precedence than we are allowed to eat, return
1101 // successfully with what we ate already.
1102 if (TokPrec < Precedence)
1103 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001104
Sean Callanan79ed1a82010-01-19 20:22:31 +00001105 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001106
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001107 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001108 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001109 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001110
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001111 // If BinOp binds less tightly with RHS than the operator after RHS, let
1112 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001113 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001114 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001115 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001116 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001117 }
1118
Daniel Dunbar475839e2009-06-29 20:37:27 +00001119 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001120 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001121 }
1122}
1123
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001124/// ParseStatement:
1125/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001126/// ::= Label* Directive ...Operands... EndOfStatement
1127/// ::= Label* Identifier OperandList* EndOfStatement
Eli Friedman2128aae2012-10-22 23:58:19 +00001128bool AsmParser::ParseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001129 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001130 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001131 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001132 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001133 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001134
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001135 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001136 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001137 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001138 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001139 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001140 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001141 if (Lexer.is(AsmToken::Hash))
1142 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001143
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001144 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001145 if (Lexer.is(AsmToken::Integer)) {
1146 LocalLabelVal = getTok().getIntVal();
1147 if (LocalLabelVal < 0) {
1148 if (!TheCondState.Ignore)
1149 return TokError("unexpected token at start of statement");
1150 IDVal = "";
1151 }
1152 else {
1153 IDVal = getTok().getString();
1154 Lex(); // Consume the integer token to be used as an identifier token.
1155 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001156 if (!TheCondState.Ignore)
1157 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001158 }
1159 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001160
1161 } else if (Lexer.is(AsmToken::Dot)) {
1162 // Treat '.' as a valid identifier in this context.
1163 Lex();
1164 IDVal = ".";
1165
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001166 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001167 if (!TheCondState.Ignore)
1168 return TokError("unexpected token at start of statement");
1169 IDVal = "";
1170 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001171
Chris Lattner7834fac2010-04-17 18:14:27 +00001172 // Handle conditional assembly here before checking for skipping. We
1173 // have to do this so that .endif isn't skipped in a ".if 0" block for
1174 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001175 StringMap<DirectiveKind>::const_iterator DirKindIt =
1176 DirectiveKindMapping.find(IDVal);
1177 DirectiveKind DirKind =
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001178 (DirKindIt == DirectiveKindMapping.end()) ? DK_NO_DIRECTIVE :
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001179 DirKindIt->getValue();
1180 switch (DirKind) {
1181 default:
1182 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001183 case DK_IF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001184 return ParseDirectiveIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001185 case DK_IFB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001186 return ParseDirectiveIfb(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001187 case DK_IFNB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001188 return ParseDirectiveIfb(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001189 case DK_IFC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001190 return ParseDirectiveIfc(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001191 case DK_IFNC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001192 return ParseDirectiveIfc(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001193 case DK_IFDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001194 return ParseDirectiveIfdef(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001195 case DK_IFNDEF:
1196 case DK_IFNOTDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001197 return ParseDirectiveIfdef(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001198 case DK_ELSEIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001199 return ParseDirectiveElseIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001200 case DK_ELSE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001201 return ParseDirectiveElse(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001202 case DK_ENDIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001203 return ParseDirectiveEndIf(IDLoc);
1204 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001205
Chris Lattner7834fac2010-04-17 18:14:27 +00001206 // If we are in a ".if 0" block, ignore this statement.
Chad Rosier17feeec2012-10-20 00:47:08 +00001207 if (TheCondState.Ignore) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001208 EatToEndOfStatement();
1209 return false;
1210 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001211
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001212 // FIXME: Recurse on local labels?
1213
1214 // See what kind of statement we have.
1215 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001216 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001217 CheckForValidSection();
1218
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001219 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001220 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001221
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001222 // Diagnose attempt to use '.' as a label.
1223 if (IDVal == ".")
1224 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1225
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001226 // Diagnose attempt to use a variable as a label.
1227 //
1228 // FIXME: Diagnostics. Note the location of the definition as a label.
1229 // FIXME: This doesn't diagnose assignment to a symbol which has been
1230 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001231 MCSymbol *Sym;
1232 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001233 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001234 else
1235 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001236 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001237 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001238
Daniel Dunbar959fd882009-08-26 22:13:22 +00001239 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001240 if (!ParsingInlineAsm)
1241 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001242
Kevin Enderby94c2e852011-12-09 18:09:40 +00001243 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001244 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001245 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001246 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1247 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001248
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001249 // Consume any end of statement token, if present, to avoid spurious
1250 // AddBlankLine calls().
1251 if (Lexer.is(AsmToken::EndOfStatement)) {
1252 Lex();
1253 if (Lexer.is(AsmToken::Eof))
1254 return false;
1255 }
1256
Eli Friedman2128aae2012-10-22 23:58:19 +00001257 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001258 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001259
Daniel Dunbar3f872332009-07-28 16:08:33 +00001260 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001261 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001262 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001263
Nico Weber4c4c7322011-01-28 03:04:41 +00001264 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001265
1266 default: // Normal instruction or directive.
1267 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001268 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001269
1270 // If macros are enabled, check to see if this is a macro instantiation.
Eli Bendersky733c3362013-01-14 18:08:41 +00001271 if (MacrosEnabled())
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001272 if (const MCAsmMacro *M = LookupMacro(IDVal)) {
1273 return HandleMacroEntry(M, IDLoc);
1274 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001275
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001276 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001277 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001278
1279 // Target hook for parsing target specific directives.
1280 if (!getTargetParser().ParseDirective(ID))
1281 return false;
1282
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001283 switch (DirKind) {
1284 default:
1285 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001286 case DK_SET:
1287 case DK_EQU:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001288 return ParseDirectiveSet(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001289 case DK_EQUIV:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001290 return ParseDirectiveSet(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001291 case DK_ASCII:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001292 return ParseDirectiveAscii(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001293 case DK_ASCIZ:
1294 case DK_STRING:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001295 return ParseDirectiveAscii(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001296 case DK_BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001297 return ParseDirectiveValue(1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001298 case DK_SHORT:
1299 case DK_VALUE:
1300 case DK_2BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001301 return ParseDirectiveValue(2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001302 case DK_LONG:
1303 case DK_INT:
1304 case DK_4BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001305 return ParseDirectiveValue(4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001306 case DK_QUAD:
1307 case DK_8BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001308 return ParseDirectiveValue(8);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001309 case DK_SINGLE:
1310 case DK_FLOAT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001311 return ParseDirectiveRealValue(APFloat::IEEEsingle);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001312 case DK_DOUBLE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001313 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001314 case DK_ALIGN: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001315 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1316 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1317 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001318 case DK_ALIGN32: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001319 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1320 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1321 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001322 case DK_BALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001323 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001324 case DK_BALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001325 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001326 case DK_BALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001327 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001328 case DK_P2ALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001329 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001330 case DK_P2ALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001331 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001332 case DK_P2ALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001333 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001334 case DK_ORG:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001335 return ParseDirectiveOrg();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001336 case DK_FILL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001337 return ParseDirectiveFill();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001338 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001339 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001340 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001341 EatToEndOfStatement(); // .extern is the default, ignore it.
1342 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001343 case DK_GLOBL:
1344 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001345 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001346 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001347 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001348 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001349 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001350 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001351 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001352 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001353 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001354 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001355 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001356 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001357 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001358 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001359 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001360 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001361 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001362 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001363 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001364 case DK_COMM:
1365 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001366 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001367 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001368 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001369 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001370 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001371 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001372 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001373 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001374 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001375 case DK_CODE16:
1376 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001377 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001378 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001379 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001380 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001381 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001382 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001383 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001384 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001385 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001386 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001387 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001388 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001389 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001390 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001391 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001392 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001393
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001394 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001395 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1396 DirectiveMap.lookup(IDVal);
1397 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001398 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001399
Jim Grosbach686c0182012-05-01 18:38:27 +00001400 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001401 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001402
Eli Friedman2128aae2012-10-22 23:58:19 +00001403 // _emit
1404 if (ParsingInlineAsm && IDVal == "_emit")
1405 return ParseDirectiveEmit(IDLoc, Info);
1406
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001407 CheckForValidSection();
1408
Chris Lattnera7f13542010-05-19 23:34:33 +00001409 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001410 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001411 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001412 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001413
Chad Rosier6a020a72012-10-25 20:41:34 +00001414 ParseInstructionInfo IInfo(Info.AsmRewrites);
1415 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1416 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001417 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001418
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001419 // Dump the parsed representation, if requested.
1420 if (getShowParsedOperands()) {
1421 SmallString<256> Str;
1422 raw_svector_ostream OS(Str);
1423 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001424 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001425 if (i != 0)
1426 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001427 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001428 }
1429 OS << "]";
1430
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001431 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001432 }
1433
Kevin Enderby613b7572011-11-01 22:27:22 +00001434 // If we are generating dwarf for assembly source files and the current
1435 // section is the initial text section then generate a .loc directive for
1436 // the instruction.
1437 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001438 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001439
1440 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1441
1442 // If we previously parsed a cpp hash file line comment then make sure the
1443 // current Dwarf File is for the CppHashFilename if not then emit the
1444 // Dwarf File table for it and adjust the line number for the .loc.
1445 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1446 getContext().getMCDwarfFiles();
1447 if (CppHashFilename.size() != 0) {
1448 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1449 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001450 getStreamer().EmitDwarfFileDirective(
1451 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001452
Kevin Enderby32c1a822012-11-05 21:55:41 +00001453 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001454 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1455 }
1456
Kevin Enderby613b7572011-11-01 22:27:22 +00001457 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001458 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001459 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001460 StringRef());
1461 }
1462
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001463 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001464 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001465 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001466 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1467 Info.ParsedOperands,
1468 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001469 ParsingInlineAsm);
1470 }
Chris Lattner98986712010-01-14 22:21:20 +00001471
Chris Lattnercbf8a982010-09-11 16:18:25 +00001472 // Don't skip the rest of the line, the instruction parser is responsible for
1473 // that.
1474 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001475}
Chris Lattner9a023f72009-06-24 04:43:34 +00001476
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001477/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1478/// since they may not be able to be tokenized to get to the end of line token.
1479void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001480 if (!Lexer.is(AsmToken::EndOfStatement))
1481 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001482 // Eat EOL.
1483 Lex();
1484}
1485
1486/// ParseCppHashLineFilenameComment as this:
1487/// ::= # number "filename"
1488/// or just as a full line comment if it doesn't have a number and a string.
1489bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1490 Lex(); // Eat the hash token.
1491
1492 if (getLexer().isNot(AsmToken::Integer)) {
1493 // Consume the line since in cases it is not a well-formed line directive,
1494 // as if were simply a full line comment.
1495 EatToEndOfLine();
1496 return false;
1497 }
1498
1499 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001500 Lex();
1501
1502 if (getLexer().isNot(AsmToken::String)) {
1503 EatToEndOfLine();
1504 return false;
1505 }
1506
1507 StringRef Filename = getTok().getString();
1508 // Get rid of the enclosing quotes.
1509 Filename = Filename.substr(1, Filename.size()-2);
1510
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001511 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1512 CppHashLoc = L;
1513 CppHashFilename = Filename;
1514 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001515 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001516
1517 // Ignore any trailing characters, they're just comment.
1518 EatToEndOfLine();
1519 return false;
1520}
1521
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001522/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001523/// for the Filename and LineNo if any in the diagnostic.
1524void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1525 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1526 raw_ostream &OS = errs();
1527
1528 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1529 const SMLoc &DiagLoc = Diag.getLoc();
1530 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1531 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1532
1533 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1534 // before printing the message.
1535 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001536 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001537 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1538 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1539 }
1540
Eric Christopher2318ba12012-12-18 00:30:54 +00001541 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001542 // manager changed or buffer changed (like in a nested include) then just
1543 // print the normal diagnostic using its Filename and LineNo.
1544 if (!Parser->CppHashLineNumber ||
1545 &DiagSrcMgr != &Parser->SrcMgr ||
1546 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001547 if (Parser->SavedDiagHandler)
1548 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1549 else
1550 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001551 return;
1552 }
1553
Eric Christopher2318ba12012-12-18 00:30:54 +00001554 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001555 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1556 // the diagnostic.
1557 const std::string Filename = Parser->CppHashFilename;
1558
1559 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1560 int CppHashLocLineNo =
1561 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1562 int LineNo = Parser->CppHashLineNumber - 1 +
1563 (DiagLocLineNo - CppHashLocLineNo);
1564
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001565 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1566 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001567 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001568 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001569
Benjamin Kramer04a04262011-10-16 10:48:29 +00001570 if (Parser->SavedDiagHandler)
1571 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1572 else
1573 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001574}
1575
Rafael Espindola799aacf2012-08-21 18:29:30 +00001576// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1577// difference being that that function accepts '@' as part of identifiers and
1578// we can't do that. AsmLexer.cpp should probably be changed to handle
1579// '@' as a special case when needed.
1580static bool isIdentifierChar(char c) {
1581 return isalnum(c) || c == '_' || c == '$' || c == '.';
1582}
1583
Rafael Espindola761cb062012-06-03 23:57:14 +00001584bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001585 const MCAsmMacroParameters &Parameters,
1586 const MCAsmMacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001587 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001588 unsigned NParameters = Parameters.size();
1589 if (NParameters != 0 && NParameters != A.size())
1590 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001591
Preston Gurd7b6f2032012-09-19 20:36:12 +00001592 // A macro without parameters is handled differently on Darwin:
1593 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001594 while (!Body.empty()) {
1595 // Scan for the next substitution.
1596 std::size_t End = Body.size(), Pos = 0;
1597 for (; Pos != End; ++Pos) {
1598 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001599 if (!NParameters) {
1600 // This macro has no parameters, look for $0, $1, etc.
1601 if (Body[Pos] != '$' || Pos + 1 == End)
1602 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001603
Rafael Espindola65366442011-06-05 02:43:45 +00001604 char Next = Body[Pos + 1];
1605 if (Next == '$' || Next == 'n' || isdigit(Next))
1606 break;
1607 } else {
1608 // This macro has parameters, look for \foo, \bar, etc.
1609 if (Body[Pos] == '\\' && Pos + 1 != End)
1610 break;
1611 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001612 }
1613
1614 // Add the prefix.
1615 OS << Body.slice(0, Pos);
1616
1617 // Check if we reached the end.
1618 if (Pos == End)
1619 break;
1620
Rafael Espindola65366442011-06-05 02:43:45 +00001621 if (!NParameters) {
1622 switch (Body[Pos+1]) {
1623 // $$ => $
1624 case '$':
1625 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001626 break;
1627
Rafael Espindola65366442011-06-05 02:43:45 +00001628 // $n => number of arguments
1629 case 'n':
1630 OS << A.size();
1631 break;
1632
1633 // $[0-9] => argument
1634 default: {
1635 // Missing arguments are ignored.
1636 unsigned Index = Body[Pos+1] - '0';
1637 if (Index >= A.size())
1638 break;
1639
1640 // Otherwise substitute with the token values, with spaces eliminated.
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001641 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001642 ie = A[Index].end(); it != ie; ++it)
1643 OS << it->getString();
1644 break;
1645 }
1646 }
1647 Pos += 2;
1648 } else {
1649 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001650 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001651 ++I;
1652
1653 const char *Begin = Body.data() + Pos +1;
1654 StringRef Argument(Begin, I - (Pos +1));
1655 unsigned Index = 0;
1656 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001657 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001658 break;
1659
Preston Gurd7b6f2032012-09-19 20:36:12 +00001660 if (Index == NParameters) {
1661 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1662 Pos += 3;
1663 else {
1664 OS << '\\' << Argument;
1665 Pos = I;
1666 }
1667 } else {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001668 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Preston Gurd7b6f2032012-09-19 20:36:12 +00001669 ie = A[Index].end(); it != ie; ++it)
1670 if (it->getKind() == AsmToken::String)
1671 OS << it->getStringContents();
1672 else
1673 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001674
Preston Gurd7b6f2032012-09-19 20:36:12 +00001675 Pos += 1 + Argument.size();
1676 }
Rafael Espindola65366442011-06-05 02:43:45 +00001677 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001678 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001679 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001680 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001681
Rafael Espindola65366442011-06-05 02:43:45 +00001682 return false;
1683}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001684
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001685MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001686 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001687 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001688 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1689 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001690{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001691}
1692
Preston Gurd7b6f2032012-09-19 20:36:12 +00001693static bool IsOperator(AsmToken::TokenKind kind)
1694{
1695 switch (kind)
1696 {
1697 default:
1698 return false;
1699 case AsmToken::Plus:
1700 case AsmToken::Minus:
1701 case AsmToken::Tilde:
1702 case AsmToken::Slash:
1703 case AsmToken::Star:
1704 case AsmToken::Dot:
1705 case AsmToken::Equal:
1706 case AsmToken::EqualEqual:
1707 case AsmToken::Pipe:
1708 case AsmToken::PipePipe:
1709 case AsmToken::Caret:
1710 case AsmToken::Amp:
1711 case AsmToken::AmpAmp:
1712 case AsmToken::Exclaim:
1713 case AsmToken::ExclaimEqual:
1714 case AsmToken::Percent:
1715 case AsmToken::Less:
1716 case AsmToken::LessEqual:
1717 case AsmToken::LessLess:
1718 case AsmToken::LessGreater:
1719 case AsmToken::Greater:
1720 case AsmToken::GreaterEqual:
1721 case AsmToken::GreaterGreater:
1722 return true;
1723 }
1724}
1725
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001726bool AsmParser::ParseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +00001727 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001728 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001729 unsigned AddTokens = 0;
1730
1731 // gas accepts arguments separated by whitespace, except on Darwin
1732 if (!IsDarwin)
1733 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001734
1735 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001736 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1737 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001738 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001739 }
1740
1741 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1742 // Spaces and commas cannot be mixed to delimit parameters
1743 if (ArgumentDelimiter == AsmToken::Eof)
1744 ArgumentDelimiter = AsmToken::Comma;
1745 else if (ArgumentDelimiter != AsmToken::Comma) {
1746 Lexer.setSkipSpace(true);
1747 return TokError("expected ' ' for macro argument separator");
1748 }
1749 break;
1750 }
1751
1752 if (Lexer.is(AsmToken::Space)) {
1753 Lex(); // Eat spaces
1754
1755 // Spaces can delimit parameters, but could also be part an expression.
1756 // If the token after a space is an operator, add the token and the next
1757 // one into this argument
1758 if (ArgumentDelimiter == AsmToken::Space ||
1759 ArgumentDelimiter == AsmToken::Eof) {
1760 if (IsOperator(Lexer.getKind())) {
1761 // Check to see whether the token is used as an operator,
1762 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001763 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001764 if (*NextChar == ' ')
1765 AddTokens = 2;
1766 }
1767
1768 if (!AddTokens && ParenLevel == 0) {
1769 if (ArgumentDelimiter == AsmToken::Eof &&
1770 !IsOperator(Lexer.getKind()))
1771 ArgumentDelimiter = AsmToken::Space;
1772 break;
1773 }
1774 }
1775 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001776
1777 // HandleMacroEntry relies on not advancing the lexer here
1778 // to be able to fill in the remaining default parameter values
1779 if (Lexer.is(AsmToken::EndOfStatement))
1780 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001781
1782 // Adjust the current parentheses level.
1783 if (Lexer.is(AsmToken::LParen))
1784 ++ParenLevel;
1785 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1786 --ParenLevel;
1787
1788 // Append the token to the current argument list.
1789 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001790 if (AddTokens)
1791 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001792 Lex();
1793 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001794
1795 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001796 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001797 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001798 return false;
1799}
1800
1801// Parse the macro instantiation arguments.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001802bool AsmParser::ParseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001803 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001804 // Argument delimiter is initially unknown. It will be set by
1805 // ParseMacroArgument()
1806 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001807
1808 // Parse two kinds of macro invocations:
1809 // - macros defined without any parameters accept an arbitrary number of them
1810 // - macros defined with parameters accept at most that many of them
1811 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1812 ++Parameter) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001813 MCAsmMacroArgument MA;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001814
Preston Gurd7b6f2032012-09-19 20:36:12 +00001815 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001816 return true;
1817
Preston Gurd6c9176a2012-09-19 20:29:04 +00001818 if (!MA.empty() || !NParameters)
1819 A.push_back(MA);
1820 else if (NParameters) {
1821 if (!M->Parameters[Parameter].second.empty())
1822 A.push_back(M->Parameters[Parameter].second);
1823 }
Jim Grosbach97146442012-07-30 22:44:17 +00001824
Preston Gurd6c9176a2012-09-19 20:29:04 +00001825 // At the end of the statement, fill in remaining arguments that have
1826 // default values. If there aren't any, then the next argument is
1827 // required but missing
1828 if (Lexer.is(AsmToken::EndOfStatement)) {
1829 if (NParameters && Parameter < NParameters - 1) {
1830 if (M->Parameters[Parameter + 1].second.empty())
1831 return TokError("macro argument '" +
1832 Twine(M->Parameters[Parameter + 1].first) +
1833 "' is missing");
1834 else
1835 continue;
1836 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001837 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001838 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001839
1840 if (Lexer.is(AsmToken::Comma))
1841 Lex();
1842 }
1843 return TokError("Too many arguments");
1844}
1845
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001846const MCAsmMacro* AsmParser::LookupMacro(StringRef Name) {
1847 StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name);
1848 return (I == MacroMap.end()) ? NULL : I->getValue();
1849}
1850
1851void AsmParser::DefineMacro(StringRef Name, const MCAsmMacro& Macro) {
1852 MacroMap[Name] = new MCAsmMacro(Macro);
1853}
1854
1855void AsmParser::UndefineMacro(StringRef Name) {
1856 StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name);
1857 if (I != MacroMap.end()) {
1858 delete I->getValue();
1859 MacroMap.erase(I);
1860 }
1861}
1862
1863bool AsmParser::HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001864 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1865 // this, although we should protect against infinite loops.
1866 if (ActiveMacros.size() == 20)
1867 return TokError("macros cannot be nested more than 20 levels deep");
1868
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001869 MCAsmMacroArguments A;
Rafael Espindola8a403d32012-08-08 14:51:03 +00001870 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001871 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001872
Jim Grosbach97146442012-07-30 22:44:17 +00001873 // Remove any trailing empty arguments. Do this after-the-fact as we have
1874 // to keep empty arguments in the middle of the list or positionality
1875 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001876 while (!A.empty() && A.back().empty())
1877 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001878
Rafael Espindola65366442011-06-05 02:43:45 +00001879 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1880 // to hold the macro body with substitutions.
1881 SmallString<256> Buf;
1882 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001883 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001884
Rafael Espindola8a403d32012-08-08 14:51:03 +00001885 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001886 return true;
1887
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001888 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola761cb062012-06-03 23:57:14 +00001889 // instantiation.
1890 OS << ".endmacro\n";
1891
Rafael Espindola65366442011-06-05 02:43:45 +00001892 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001893 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001894
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001895 // Create the macro instantiation object and add to the current macro
1896 // instantiation stack.
1897 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001898 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001899 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001900 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001901 ActiveMacros.push_back(MI);
1902
1903 // Jump to the macro instantiation and prime the lexer.
1904 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1905 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1906 Lex();
1907
1908 return false;
1909}
1910
1911void AsmParser::HandleMacroExit() {
1912 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001913 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001914 Lex();
1915
1916 // Pop the instantiation entry.
1917 delete ActiveMacros.back();
1918 ActiveMacros.pop_back();
1919}
1920
Rafael Espindolae71cc862012-01-28 05:57:00 +00001921static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001922 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001923 case MCExpr::Binary: {
1924 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1925 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001926 break;
1927 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001928 case MCExpr::Target:
1929 case MCExpr::Constant:
1930 return false;
1931 case MCExpr::SymbolRef: {
1932 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001933 if (S.isVariable())
1934 return IsUsedIn(Sym, S.getVariableValue());
1935 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001936 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001937 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001938 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001939 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001940
1941 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001942}
1943
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001944bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1945 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001946 // FIXME: Use better location, we should use proper tokens.
1947 SMLoc EqualLoc = Lexer.getLoc();
1948
Daniel Dunbar821e3332009-08-31 08:09:28 +00001949 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001950 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001951 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001952
Rafael Espindolae71cc862012-01-28 05:57:00 +00001953 // Note: we don't count b as used in "a = b". This is to allow
1954 // a = b
1955 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001956
Daniel Dunbar3f872332009-07-28 16:08:33 +00001957 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001958 return TokError("unexpected token in assignment");
1959
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001960 // Error on assignment to '.'.
1961 if (Name == ".") {
1962 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1963 "(use '.space' or '.org').)"));
1964 }
1965
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001966 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001967 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001968
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001969 // Validate that the LHS is allowed to be a variable (either it has not been
1970 // used as a symbol, or it is an absolute symbol).
1971 MCSymbol *Sym = getContext().LookupSymbol(Name);
1972 if (Sym) {
1973 // Diagnose assignment to a label.
1974 //
1975 // FIXME: Diagnostics. Note the location of the definition as a label.
1976 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001977 if (IsUsedIn(Sym, Value))
1978 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1979 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001980 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001981 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1982 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001983 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001984 return Error(EqualLoc, "redefinition of '" + Name + "'");
1985 else if (!Sym->isVariable())
1986 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001987 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001988 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1989 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001990
1991 // Don't count these checks as uses.
1992 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001993 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001994 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001995
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001996 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001997
1998 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001999 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002000 if (NoDeadStrip)
2001 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2002
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002003
2004 return false;
2005}
2006
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002007/// ParseIdentifier:
2008/// ::= identifier
2009/// ::= string
2010bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002011 // The assembler has relaxed rules for accepting identifiers, in particular we
2012 // allow things like '.globl $foo', which would normally be separate
2013 // tokens. At this level, we have already lexed so we cannot (currently)
2014 // handle this as a context dependent token, instead we detect adjacent tokens
2015 // and return the combined identifier.
2016 if (Lexer.is(AsmToken::Dollar)) {
2017 SMLoc DollarLoc = getLexer().getLoc();
2018
2019 // Consume the dollar sign, and check for a following identifier.
2020 Lex();
2021 if (Lexer.isNot(AsmToken::Identifier))
2022 return true;
2023
2024 // We have a '$' followed by an identifier, make sure they are adjacent.
2025 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2026 return true;
2027
2028 // Construct the joined identifier and consume the token.
2029 Res = StringRef(DollarLoc.getPointer(),
2030 getTok().getIdentifier().size() + 1);
2031 Lex();
2032 return false;
2033 }
2034
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002035 if (Lexer.isNot(AsmToken::Identifier) &&
2036 Lexer.isNot(AsmToken::String))
2037 return true;
2038
Sean Callanan18b83232010-01-19 21:44:56 +00002039 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002040
Sean Callanan79ed1a82010-01-19 20:22:31 +00002041 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002042
2043 return false;
2044}
2045
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002046/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002047/// ::= .equ identifier ',' expression
2048/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002049/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002050bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002051 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002052
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002053 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002054 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002055
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002056 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002057 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002058 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002059
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002060 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002061}
2062
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002063bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002064 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002065
2066 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002067 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002068 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2069 if (Str[i] != '\\') {
2070 Data += Str[i];
2071 continue;
2072 }
2073
2074 // Recognize escaped characters. Note that this escape semantics currently
2075 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2076 ++i;
2077 if (i == e)
2078 return TokError("unexpected backslash at end of string");
2079
2080 // Recognize octal sequences.
2081 if ((unsigned) (Str[i] - '0') <= 7) {
2082 // Consume up to three octal characters.
2083 unsigned Value = Str[i] - '0';
2084
2085 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2086 ++i;
2087 Value = Value * 8 + (Str[i] - '0');
2088
2089 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2090 ++i;
2091 Value = Value * 8 + (Str[i] - '0');
2092 }
2093 }
2094
2095 if (Value > 255)
2096 return TokError("invalid octal escape sequence (out of range)");
2097
2098 Data += (unsigned char) Value;
2099 continue;
2100 }
2101
2102 // Otherwise recognize individual escapes.
2103 switch (Str[i]) {
2104 default:
2105 // Just reject invalid escape sequences for now.
2106 return TokError("invalid escape sequence (unrecognized character)");
2107
2108 case 'b': Data += '\b'; break;
2109 case 'f': Data += '\f'; break;
2110 case 'n': Data += '\n'; break;
2111 case 'r': Data += '\r'; break;
2112 case 't': Data += '\t'; break;
2113 case '"': Data += '"'; break;
2114 case '\\': Data += '\\'; break;
2115 }
2116 }
2117
2118 return false;
2119}
2120
Daniel Dunbara0d14262009-06-24 23:30:00 +00002121/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002122/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2123bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002124 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002125 CheckForValidSection();
2126
Daniel Dunbara0d14262009-06-24 23:30:00 +00002127 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002128 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002129 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002130
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002131 std::string Data;
2132 if (ParseEscapedString(Data))
2133 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002134
2135 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002136 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002137 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2138
Sean Callanan79ed1a82010-01-19 20:22:31 +00002139 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002140
2141 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002142 break;
2143
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002144 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002145 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002146 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002147 }
2148 }
2149
Sean Callanan79ed1a82010-01-19 20:22:31 +00002150 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002151 return false;
2152}
2153
2154/// ParseDirectiveValue
2155/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2156bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002157 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002158 CheckForValidSection();
2159
Daniel Dunbara0d14262009-06-24 23:30:00 +00002160 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002161 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002162 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002163 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002164 return true;
2165
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002166 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002167 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2168 assert(Size <= 8 && "Invalid size");
2169 uint64_t IntValue = MCE->getValue();
2170 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2171 return Error(ExprLoc, "literal value out of range for directive");
2172 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2173 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002174 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002175
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002176 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002177 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002178
Daniel Dunbara0d14262009-06-24 23:30:00 +00002179 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002180 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002181 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002182 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002183 }
2184 }
2185
Sean Callanan79ed1a82010-01-19 20:22:31 +00002186 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002187 return false;
2188}
2189
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002190/// ParseDirectiveRealValue
2191/// ::= (.single | .double) [ expression (, expression)* ]
2192bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2193 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2194 CheckForValidSection();
2195
2196 for (;;) {
2197 // We don't truly support arithmetic on floating point expressions, so we
2198 // have to manually parse unary prefixes.
2199 bool IsNeg = false;
2200 if (getLexer().is(AsmToken::Minus)) {
2201 Lex();
2202 IsNeg = true;
2203 } else if (getLexer().is(AsmToken::Plus))
2204 Lex();
2205
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002206 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002207 getLexer().isNot(AsmToken::Real) &&
2208 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002209 return TokError("unexpected token in directive");
2210
2211 // Convert to an APFloat.
2212 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002213 StringRef IDVal = getTok().getString();
2214 if (getLexer().is(AsmToken::Identifier)) {
2215 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2216 Value = APFloat::getInf(Semantics);
2217 else if (!IDVal.compare_lower("nan"))
2218 Value = APFloat::getNaN(Semantics, false, ~0);
2219 else
2220 return TokError("invalid floating point literal");
2221 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002222 APFloat::opInvalidOp)
2223 return TokError("invalid floating point literal");
2224 if (IsNeg)
2225 Value.changeSign();
2226
2227 // Consume the numeric token.
2228 Lex();
2229
2230 // Emit the value as an integer.
2231 APInt AsInt = Value.bitcastToAPInt();
2232 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2233 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2234
2235 if (getLexer().is(AsmToken::EndOfStatement))
2236 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002237
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002238 if (getLexer().isNot(AsmToken::Comma))
2239 return TokError("unexpected token in directive");
2240 Lex();
2241 }
2242 }
2243
2244 Lex();
2245 return false;
2246}
2247
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002248/// ParseDirectiveZero
2249/// ::= .zero expression
2250bool AsmParser::ParseDirectiveZero() {
2251 CheckForValidSection();
2252
2253 int64_t NumBytes;
2254 if (ParseAbsoluteExpression(NumBytes))
2255 return true;
2256
Rafael Espindolae452b172010-10-05 19:42:57 +00002257 int64_t Val = 0;
2258 if (getLexer().is(AsmToken::Comma)) {
2259 Lex();
2260 if (ParseAbsoluteExpression(Val))
2261 return true;
2262 }
2263
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002264 if (getLexer().isNot(AsmToken::EndOfStatement))
2265 return TokError("unexpected token in '.zero' directive");
2266
2267 Lex();
2268
Rafael Espindolae452b172010-10-05 19:42:57 +00002269 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002270
2271 return false;
2272}
2273
Daniel Dunbara0d14262009-06-24 23:30:00 +00002274/// ParseDirectiveFill
2275/// ::= .fill expression , expression , expression
2276bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002277 CheckForValidSection();
2278
Daniel Dunbara0d14262009-06-24 23:30:00 +00002279 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002280 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002281 return true;
2282
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002283 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002284 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002285 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002286
Daniel Dunbara0d14262009-06-24 23:30:00 +00002287 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002288 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002289 return true;
2290
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002291 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002292 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002293 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002294
Daniel Dunbara0d14262009-06-24 23:30:00 +00002295 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002296 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002297 return true;
2298
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002299 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002300 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002301
Sean Callanan79ed1a82010-01-19 20:22:31 +00002302 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002303
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002304 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2305 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002306
2307 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002308 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002309
2310 return false;
2311}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002312
2313/// ParseDirectiveOrg
2314/// ::= .org expression [ , expression ]
2315bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002316 CheckForValidSection();
2317
Daniel Dunbar821e3332009-08-31 08:09:28 +00002318 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002319 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002320 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002321 return true;
2322
2323 // Parse optional fill expression.
2324 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002325 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2326 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002327 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002328 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002329
Daniel Dunbar475839e2009-06-29 20:37:27 +00002330 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002331 return true;
2332
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002333 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002334 return TokError("unexpected token in '.org' directive");
2335 }
2336
Sean Callanan79ed1a82010-01-19 20:22:31 +00002337 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002338
Jim Grosbachebd4c052012-01-27 00:37:08 +00002339 // Only limited forms of relocatable expressions are accepted here, it
2340 // has to be relative to the current section. The streamer will return
2341 // 'true' if the expression wasn't evaluatable.
2342 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2343 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002344
2345 return false;
2346}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002347
2348/// ParseDirectiveAlign
2349/// ::= {.align, ...} expression [ , expression [ , expression ]]
2350bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002351 CheckForValidSection();
2352
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002353 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002354 int64_t Alignment;
2355 if (ParseAbsoluteExpression(Alignment))
2356 return true;
2357
2358 SMLoc MaxBytesLoc;
2359 bool HasFillExpr = false;
2360 int64_t FillExpr = 0;
2361 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002362 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2363 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002364 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002365 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002366
2367 // The fill expression can be omitted while specifying a maximum number of
2368 // alignment bytes, e.g:
2369 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002370 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002371 HasFillExpr = true;
2372 if (ParseAbsoluteExpression(FillExpr))
2373 return true;
2374 }
2375
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002376 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2377 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002378 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002379 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002380
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002381 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002382 if (ParseAbsoluteExpression(MaxBytesToFill))
2383 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002384
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002385 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002386 return TokError("unexpected token in directive");
2387 }
2388 }
2389
Sean Callanan79ed1a82010-01-19 20:22:31 +00002390 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002391
Daniel Dunbar648ac512010-05-17 21:54:30 +00002392 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002393 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002394
2395 // Compute alignment in bytes.
2396 if (IsPow2) {
2397 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002398 if (Alignment >= 32) {
2399 Error(AlignmentLoc, "invalid alignment value");
2400 Alignment = 31;
2401 }
2402
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002403 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002404 }
2405
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002406 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002407 if (MaxBytesLoc.isValid()) {
2408 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002409 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2410 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002411 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002412 }
2413
2414 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002415 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2416 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002417 MaxBytesToFill = 0;
2418 }
2419 }
2420
Daniel Dunbar648ac512010-05-17 21:54:30 +00002421 // Check whether we should use optimal code alignment for this .align
2422 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002423 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002424 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2425 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002426 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002427 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002428 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002429 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2430 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002431 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002432
2433 return false;
2434}
2435
Eli Bendersky4766ef42012-12-20 19:05:53 +00002436
2437/// ParseDirectiveBundleAlignMode
2438/// ::= {.bundle_align_mode} expression
2439bool AsmParser::ParseDirectiveBundleAlignMode() {
2440 CheckForValidSection();
2441
2442 // Expect a single argument: an expression that evaluates to a constant
2443 // in the inclusive range 0-30.
2444 SMLoc ExprLoc = getLexer().getLoc();
2445 int64_t AlignSizePow2;
2446 if (ParseAbsoluteExpression(AlignSizePow2))
2447 return true;
2448 else if (getLexer().isNot(AsmToken::EndOfStatement))
2449 return TokError("unexpected token after expression in"
2450 " '.bundle_align_mode' directive");
2451 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2452 return Error(ExprLoc,
2453 "invalid bundle alignment size (expected between 0 and 30)");
2454
2455 Lex();
2456
2457 // Because of AlignSizePow2's verified range we can safely truncate it to
2458 // unsigned.
2459 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2460 return false;
2461}
2462
2463/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002464/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002465bool AsmParser::ParseDirectiveBundleLock() {
2466 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002467 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002468
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002469 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2470 StringRef Option;
2471 SMLoc Loc = getTok().getLoc();
2472 const char *kInvalidOptionError =
2473 "invalid option for '.bundle_lock' directive";
2474
2475 if (ParseIdentifier(Option))
2476 return Error(Loc, kInvalidOptionError);
2477
2478 if (Option != "align_to_end")
2479 return Error(Loc, kInvalidOptionError);
2480 else if (getLexer().isNot(AsmToken::EndOfStatement))
2481 return Error(Loc,
2482 "unexpected token after '.bundle_lock' directive option");
2483 AlignToEnd = true;
2484 }
2485
Eli Bendersky4766ef42012-12-20 19:05:53 +00002486 Lex();
2487
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002488 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002489 return false;
2490}
2491
2492/// ParseDirectiveBundleLock
2493/// ::= {.bundle_lock}
2494bool AsmParser::ParseDirectiveBundleUnlock() {
2495 CheckForValidSection();
2496
2497 if (getLexer().isNot(AsmToken::EndOfStatement))
2498 return TokError("unexpected token in '.bundle_unlock' directive");
2499 Lex();
2500
2501 getStreamer().EmitBundleUnlock();
2502 return false;
2503}
2504
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002505/// ParseDirectiveSymbolAttribute
2506/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002507bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002508 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002509 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002510 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002511 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002512
2513 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002514 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002515
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002516 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002517
Jim Grosbach10ec6502011-09-15 17:56:49 +00002518 // Assembler local symbols don't make any sense here. Complain loudly.
2519 if (Sym->isTemporary())
2520 return Error(Loc, "non-local symbol required in directive");
2521
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002522 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002523
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002524 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002525 break;
2526
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002527 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002528 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002529 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002530 }
2531 }
2532
Sean Callanan79ed1a82010-01-19 20:22:31 +00002533 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002534 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002535}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002536
2537/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002538/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2539bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002540 CheckForValidSection();
2541
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002542 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002543 StringRef Name;
2544 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002545 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002546
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002547 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002548 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002549
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002550 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002551 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002552 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002553
2554 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002555 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002556 if (ParseAbsoluteExpression(Size))
2557 return true;
2558
2559 int64_t Pow2Alignment = 0;
2560 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002561 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002562 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002563 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002564 if (ParseAbsoluteExpression(Pow2Alignment))
2565 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002566
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002567 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2568 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002569 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2570
Chris Lattner258281d2010-01-19 06:22:22 +00002571 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002572 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2573 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002574 if (!isPowerOf2_64(Pow2Alignment))
2575 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2576 Pow2Alignment = Log2_64(Pow2Alignment);
2577 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002578 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002579
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002580 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002581 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002582
Sean Callanan79ed1a82010-01-19 20:22:31 +00002583 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002584
Chris Lattner1fc3d752009-07-09 17:25:12 +00002585 // NOTE: a size of zero for a .comm should create a undefined symbol
2586 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002587 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002588 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2589 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002590
Eric Christopherc260a3e2010-05-14 01:38:54 +00002591 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002592 // may internally end up wanting an alignment in bytes.
2593 // FIXME: Diagnose overflow.
2594 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002595 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2596 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002597
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002598 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002599 return Error(IDLoc, "invalid symbol redefinition");
2600
Chris Lattner1fc3d752009-07-09 17:25:12 +00002601 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002602 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002603 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002604 return false;
2605 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002606
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002607 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002608 return false;
2609}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002610
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002611/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002612/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002613bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002614 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002615 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002616
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002617 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002618 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002619 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002620
Sean Callanan79ed1a82010-01-19 20:22:31 +00002621 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002622
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002623 if (Str.empty())
2624 Error(Loc, ".abort detected. Assembly stopping.");
2625 else
2626 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002627 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002628
2629 return false;
2630}
Kevin Enderby71148242009-07-14 21:35:03 +00002631
Kevin Enderby1f049b22009-07-14 23:21:55 +00002632/// ParseDirectiveInclude
2633/// ::= .include "filename"
2634bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002635 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002636 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002637
Sean Callanan18b83232010-01-19 21:44:56 +00002638 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002639 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002640 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002641
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002642 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002643 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002644
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002645 // Strip the quotes.
2646 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002647
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002648 // Attempt to switch the lexer to the included file before consuming the end
2649 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002650 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002651 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002652 return true;
2653 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002654
2655 return false;
2656}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002657
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002658/// ParseDirectiveIncbin
2659/// ::= .incbin "filename"
2660bool AsmParser::ParseDirectiveIncbin() {
2661 if (getLexer().isNot(AsmToken::String))
2662 return TokError("expected string in '.incbin' directive");
2663
2664 std::string Filename = getTok().getString();
2665 SMLoc IncbinLoc = getLexer().getLoc();
2666 Lex();
2667
2668 if (getLexer().isNot(AsmToken::EndOfStatement))
2669 return TokError("unexpected token in '.incbin' directive");
2670
2671 // Strip the quotes.
2672 Filename = Filename.substr(1, Filename.size()-2);
2673
2674 // Attempt to process the included file.
2675 if (ProcessIncbinFile(Filename)) {
2676 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2677 return true;
2678 }
2679
2680 return false;
2681}
2682
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002683/// ParseDirectiveIf
2684/// ::= .if expression
2685bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002686 TheCondStack.push_back(TheCondState);
2687 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002688 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002689 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002690 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002691 int64_t ExprValue;
2692 if (ParseAbsoluteExpression(ExprValue))
2693 return true;
2694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002695 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002696 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002697
Sean Callanan79ed1a82010-01-19 20:22:31 +00002698 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002699
2700 TheCondState.CondMet = ExprValue;
2701 TheCondState.Ignore = !TheCondState.CondMet;
2702 }
2703
2704 return false;
2705}
2706
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002707/// ParseDirectiveIfb
2708/// ::= .ifb string
2709bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2710 TheCondStack.push_back(TheCondState);
2711 TheCondState.TheCond = AsmCond::IfCond;
2712
Benjamin Kramer29739e72012-05-12 16:52:21 +00002713 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002714 EatToEndOfStatement();
2715 } else {
2716 StringRef Str = ParseStringToEndOfStatement();
2717
2718 if (getLexer().isNot(AsmToken::EndOfStatement))
2719 return TokError("unexpected token in '.ifb' directive");
2720
2721 Lex();
2722
2723 TheCondState.CondMet = ExpectBlank == Str.empty();
2724 TheCondState.Ignore = !TheCondState.CondMet;
2725 }
2726
2727 return false;
2728}
2729
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002730/// ParseDirectiveIfc
2731/// ::= .ifc string1, string2
2732bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2733 TheCondStack.push_back(TheCondState);
2734 TheCondState.TheCond = AsmCond::IfCond;
2735
Benjamin Kramer29739e72012-05-12 16:52:21 +00002736 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002737 EatToEndOfStatement();
2738 } else {
2739 StringRef Str1 = ParseStringToComma();
2740
2741 if (getLexer().isNot(AsmToken::Comma))
2742 return TokError("unexpected token in '.ifc' directive");
2743
2744 Lex();
2745
2746 StringRef Str2 = ParseStringToEndOfStatement();
2747
2748 if (getLexer().isNot(AsmToken::EndOfStatement))
2749 return TokError("unexpected token in '.ifc' directive");
2750
2751 Lex();
2752
2753 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2754 TheCondState.Ignore = !TheCondState.CondMet;
2755 }
2756
2757 return false;
2758}
2759
2760/// ParseDirectiveIfdef
2761/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002762bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2763 StringRef Name;
2764 TheCondStack.push_back(TheCondState);
2765 TheCondState.TheCond = AsmCond::IfCond;
2766
2767 if (TheCondState.Ignore) {
2768 EatToEndOfStatement();
2769 } else {
2770 if (ParseIdentifier(Name))
2771 return TokError("expected identifier after '.ifdef'");
2772
2773 Lex();
2774
2775 MCSymbol *Sym = getContext().LookupSymbol(Name);
2776
2777 if (expect_defined)
2778 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2779 else
2780 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2781 TheCondState.Ignore = !TheCondState.CondMet;
2782 }
2783
2784 return false;
2785}
2786
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002787/// ParseDirectiveElseIf
2788/// ::= .elseif expression
2789bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2790 if (TheCondState.TheCond != AsmCond::IfCond &&
2791 TheCondState.TheCond != AsmCond::ElseIfCond)
2792 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2793 " an .elseif");
2794 TheCondState.TheCond = AsmCond::ElseIfCond;
2795
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002796 bool LastIgnoreState = false;
2797 if (!TheCondStack.empty())
2798 LastIgnoreState = TheCondStack.back().Ignore;
2799 if (LastIgnoreState || TheCondState.CondMet) {
2800 TheCondState.Ignore = true;
2801 EatToEndOfStatement();
2802 }
2803 else {
2804 int64_t ExprValue;
2805 if (ParseAbsoluteExpression(ExprValue))
2806 return true;
2807
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002808 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002809 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002810
Sean Callanan79ed1a82010-01-19 20:22:31 +00002811 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002812 TheCondState.CondMet = ExprValue;
2813 TheCondState.Ignore = !TheCondState.CondMet;
2814 }
2815
2816 return false;
2817}
2818
2819/// ParseDirectiveElse
2820/// ::= .else
2821bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002822 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002823 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002824
Sean Callanan79ed1a82010-01-19 20:22:31 +00002825 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002826
2827 if (TheCondState.TheCond != AsmCond::IfCond &&
2828 TheCondState.TheCond != AsmCond::ElseIfCond)
2829 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2830 ".elseif");
2831 TheCondState.TheCond = AsmCond::ElseCond;
2832 bool LastIgnoreState = false;
2833 if (!TheCondStack.empty())
2834 LastIgnoreState = TheCondStack.back().Ignore;
2835 if (LastIgnoreState || TheCondState.CondMet)
2836 TheCondState.Ignore = true;
2837 else
2838 TheCondState.Ignore = false;
2839
2840 return false;
2841}
2842
2843/// ParseDirectiveEndIf
2844/// ::= .endif
2845bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002846 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002847 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002848
Sean Callanan79ed1a82010-01-19 20:22:31 +00002849 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002850
2851 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2852 TheCondStack.empty())
2853 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2854 ".else");
2855 if (!TheCondStack.empty()) {
2856 TheCondState = TheCondStack.back();
2857 TheCondStack.pop_back();
2858 }
2859
2860 return false;
2861}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002862
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002863void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002864 DirectiveKindMapping[".set"] = DK_SET;
2865 DirectiveKindMapping[".equ"] = DK_EQU;
2866 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2867 DirectiveKindMapping[".ascii"] = DK_ASCII;
2868 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2869 DirectiveKindMapping[".string"] = DK_STRING;
2870 DirectiveKindMapping[".byte"] = DK_BYTE;
2871 DirectiveKindMapping[".short"] = DK_SHORT;
2872 DirectiveKindMapping[".value"] = DK_VALUE;
2873 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2874 DirectiveKindMapping[".long"] = DK_LONG;
2875 DirectiveKindMapping[".int"] = DK_INT;
2876 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2877 DirectiveKindMapping[".quad"] = DK_QUAD;
2878 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2879 DirectiveKindMapping[".single"] = DK_SINGLE;
2880 DirectiveKindMapping[".float"] = DK_FLOAT;
2881 DirectiveKindMapping[".double"] = DK_DOUBLE;
2882 DirectiveKindMapping[".align"] = DK_ALIGN;
2883 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2884 DirectiveKindMapping[".balign"] = DK_BALIGN;
2885 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2886 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2887 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2888 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2889 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2890 DirectiveKindMapping[".org"] = DK_ORG;
2891 DirectiveKindMapping[".fill"] = DK_FILL;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002892 DirectiveKindMapping[".zero"] = DK_ZERO;
2893 DirectiveKindMapping[".extern"] = DK_EXTERN;
2894 DirectiveKindMapping[".globl"] = DK_GLOBL;
2895 DirectiveKindMapping[".global"] = DK_GLOBAL;
2896 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2897 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2898 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2899 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2900 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2901 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2902 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2903 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2904 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2905 DirectiveKindMapping[".comm"] = DK_COMM;
2906 DirectiveKindMapping[".common"] = DK_COMMON;
2907 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2908 DirectiveKindMapping[".abort"] = DK_ABORT;
2909 DirectiveKindMapping[".include"] = DK_INCLUDE;
2910 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2911 DirectiveKindMapping[".code16"] = DK_CODE16;
2912 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2913 DirectiveKindMapping[".rept"] = DK_REPT;
2914 DirectiveKindMapping[".irp"] = DK_IRP;
2915 DirectiveKindMapping[".irpc"] = DK_IRPC;
2916 DirectiveKindMapping[".endr"] = DK_ENDR;
2917 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2918 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2919 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2920 DirectiveKindMapping[".if"] = DK_IF;
2921 DirectiveKindMapping[".ifb"] = DK_IFB;
2922 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2923 DirectiveKindMapping[".ifc"] = DK_IFC;
2924 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2925 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2926 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2927 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2928 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2929 DirectiveKindMapping[".else"] = DK_ELSE;
2930 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002931}
2932
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002933/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002934/// ::= .file [number] filename
2935/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002936bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002937 // FIXME: I'm not sure what this is.
2938 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002939 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002940 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002941 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002942 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002943
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002944 if (FileNumber < 1)
2945 return TokError("file number less than one");
2946 }
2947
Daniel Dunbareceec052010-07-12 17:45:27 +00002948 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002949 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002950
Nick Lewycky44d798d2011-10-17 23:05:28 +00002951 // Usually the directory and filename together, otherwise just the directory.
2952 StringRef Path = getTok().getString();
2953 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002954 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002955
Nick Lewycky44d798d2011-10-17 23:05:28 +00002956 StringRef Directory;
2957 StringRef Filename;
2958 if (getLexer().is(AsmToken::String)) {
2959 if (FileNumber == -1)
2960 return TokError("explicit path specified, but no file number");
2961 Filename = getTok().getString();
2962 Filename = Filename.substr(1, Filename.size()-2);
2963 Directory = Path;
2964 Lex();
2965 } else {
2966 Filename = Path;
2967 }
2968
Daniel Dunbareceec052010-07-12 17:45:27 +00002969 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002970 return TokError("unexpected token in '.file' directive");
2971
Chris Lattnerd32e8032010-01-25 19:02:58 +00002972 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002973 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002974 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002975 if (getContext().getGenDwarfForAssembly() == true)
2976 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2977 "used to generate dwarf debug info for assembly code");
2978
Nick Lewycky44d798d2011-10-17 23:05:28 +00002979 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002980 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002981 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002982
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002983 return false;
2984}
2985
2986/// ParseDirectiveLine
2987/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002988bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002989 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2990 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002991 return TokError("unexpected token in '.line' directive");
2992
Sean Callanan18b83232010-01-19 21:44:56 +00002993 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002994 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002995 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002996
2997 // FIXME: Do something with the .line.
2998 }
2999
Daniel Dunbareceec052010-07-12 17:45:27 +00003000 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00003001 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003002
3003 return false;
3004}
3005
3006
3007/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003008/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003009/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3010/// The first number is a file number, must have been previously assigned with
3011/// a .file directive, the second number is the line number and optionally the
3012/// third number is a column position (zero if not specified). The remaining
3013/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003014bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003015
Daniel Dunbareceec052010-07-12 17:45:27 +00003016 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003017 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003018 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003019 if (FileNumber < 1)
3020 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003021 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003022 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003023 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003024
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003025 int64_t LineNumber = 0;
3026 if (getLexer().is(AsmToken::Integer)) {
3027 LineNumber = getTok().getIntVal();
3028 if (LineNumber < 1)
3029 return TokError("line number less than one in '.loc' directive");
3030 Lex();
3031 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003032
3033 int64_t ColumnPos = 0;
3034 if (getLexer().is(AsmToken::Integer)) {
3035 ColumnPos = getTok().getIntVal();
3036 if (ColumnPos < 0)
3037 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003038 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003039 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003040
Kevin Enderbyc0957932010-09-30 16:52:03 +00003041 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003042 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003043 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003044 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3045 for (;;) {
3046 if (getLexer().is(AsmToken::EndOfStatement))
3047 break;
3048
3049 StringRef Name;
3050 SMLoc Loc = getTok().getLoc();
3051 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003052 return TokError("unexpected token in '.loc' directive");
3053
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003054 if (Name == "basic_block")
3055 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3056 else if (Name == "prologue_end")
3057 Flags |= DWARF2_FLAG_PROLOGUE_END;
3058 else if (Name == "epilogue_begin")
3059 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3060 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003061 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003062 const MCExpr *Value;
3063 if (getParser().ParseExpression(Value))
3064 return true;
3065 // The expression must be the constant 0 or 1.
3066 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3067 int Value = MCE->getValue();
3068 if (Value == 0)
3069 Flags &= ~DWARF2_FLAG_IS_STMT;
3070 else if (Value == 1)
3071 Flags |= DWARF2_FLAG_IS_STMT;
3072 else
3073 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003074 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003075 else {
3076 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3077 }
3078 }
3079 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003080 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003081 const MCExpr *Value;
3082 if (getParser().ParseExpression(Value))
3083 return true;
3084 // The expression must be a constant greater or equal to 0.
3085 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3086 int Value = MCE->getValue();
3087 if (Value < 0)
3088 return Error(Loc, "isa number less than zero");
3089 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003090 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003091 else {
3092 return Error(Loc, "isa number not a constant value");
3093 }
3094 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003095 else if (Name == "discriminator") {
3096 if (getParser().ParseAbsoluteExpression(Discriminator))
3097 return true;
3098 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003099 else {
3100 return Error(Loc, "unknown sub-directive in '.loc' directive");
3101 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003102
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003103 if (getLexer().is(AsmToken::EndOfStatement))
3104 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003105 }
3106 }
3107
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003108 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003109 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003110
3111 return false;
3112}
3113
Daniel Dunbar138abae2010-10-16 04:56:42 +00003114/// ParseDirectiveStabs
3115/// ::= .stabs string, number, number, number
3116bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3117 SMLoc DirectiveLoc) {
3118 return TokError("unsupported directive '" + Directive + "'");
3119}
3120
Eli Bendersky9b1bb052013-01-11 22:55:28 +00003121/// ParseDirectiveSpace
3122/// ::= .space expression [ , expression ]
3123bool GenericAsmParser::ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc) {
3124 getParser().CheckForValidSection();
3125
3126 int64_t NumBytes;
3127 if (getParser().ParseAbsoluteExpression(NumBytes))
3128 return true;
3129
3130 int64_t FillExpr = 0;
3131 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3132 if (getLexer().isNot(AsmToken::Comma))
3133 return TokError("unexpected token in '.space' directive");
3134 Lex();
3135
3136 if (getParser().ParseAbsoluteExpression(FillExpr))
3137 return true;
3138
3139 if (getLexer().isNot(AsmToken::EndOfStatement))
3140 return TokError("unexpected token in '.space' directive");
3141 }
3142
3143 Lex();
3144
3145 if (NumBytes <= 0)
3146 return TokError("invalid number of bytes in '.space' directive");
3147
3148 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3149 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
3150
3151 return false;
3152}
3153
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003154/// ParseDirectiveCFISections
3155/// ::= .cfi_sections section [, section]
3156bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3157 SMLoc DirectiveLoc) {
3158 StringRef Name;
3159 bool EH = false;
3160 bool Debug = false;
3161
3162 if (getParser().ParseIdentifier(Name))
3163 return TokError("Expected an identifier");
3164
3165 if (Name == ".eh_frame")
3166 EH = true;
3167 else if (Name == ".debug_frame")
3168 Debug = true;
3169
3170 if (getLexer().is(AsmToken::Comma)) {
3171 Lex();
3172
3173 if (getParser().ParseIdentifier(Name))
3174 return TokError("Expected an identifier");
3175
3176 if (Name == ".eh_frame")
3177 EH = true;
3178 else if (Name == ".debug_frame")
3179 Debug = true;
3180 }
3181
3182 getStreamer().EmitCFISections(EH, Debug);
3183
3184 return false;
3185}
3186
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003187/// ParseDirectiveCFIStartProc
3188/// ::= .cfi_startproc
3189bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3190 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003191 getStreamer().EmitCFIStartProc();
3192 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003193}
3194
3195/// ParseDirectiveCFIEndProc
3196/// ::= .cfi_endproc
3197bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003198 getStreamer().EmitCFIEndProc();
3199 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003200}
3201
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003202/// ParseRegisterOrRegisterNumber - parse register name or number.
3203bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3204 SMLoc DirectiveLoc) {
3205 unsigned RegNo;
3206
Jim Grosbach6f888a82011-06-02 17:14:04 +00003207 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003208 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3209 DirectiveLoc))
3210 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003211 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003212 } else
3213 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003214
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003215 return false;
3216}
3217
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003218/// ParseDirectiveCFIDefCfa
3219/// ::= .cfi_def_cfa register, offset
3220bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3221 SMLoc DirectiveLoc) {
3222 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003223 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003224 return true;
3225
3226 if (getLexer().isNot(AsmToken::Comma))
3227 return TokError("unexpected token in directive");
3228 Lex();
3229
3230 int64_t Offset = 0;
3231 if (getParser().ParseAbsoluteExpression(Offset))
3232 return true;
3233
Rafael Espindola066c2f42011-04-12 23:59:07 +00003234 getStreamer().EmitCFIDefCfa(Register, Offset);
3235 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003236}
3237
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003238/// ParseDirectiveCFIDefCfaOffset
3239/// ::= .cfi_def_cfa_offset offset
3240bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3241 SMLoc DirectiveLoc) {
3242 int64_t Offset = 0;
3243 if (getParser().ParseAbsoluteExpression(Offset))
3244 return true;
3245
Rafael Espindola066c2f42011-04-12 23:59:07 +00003246 getStreamer().EmitCFIDefCfaOffset(Offset);
3247 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003248}
3249
3250/// ParseDirectiveCFIAdjustCfaOffset
3251/// ::= .cfi_adjust_cfa_offset adjustment
3252bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3253 SMLoc DirectiveLoc) {
3254 int64_t Adjustment = 0;
3255 if (getParser().ParseAbsoluteExpression(Adjustment))
3256 return true;
3257
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003258 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3259 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003260}
3261
3262/// ParseDirectiveCFIDefCfaRegister
3263/// ::= .cfi_def_cfa_register register
3264bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3265 SMLoc DirectiveLoc) {
3266 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003267 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003268 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003269
Rafael Espindola066c2f42011-04-12 23:59:07 +00003270 getStreamer().EmitCFIDefCfaRegister(Register);
3271 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003272}
3273
3274/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003275/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003276bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3277 int64_t Register = 0;
3278 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003279
3280 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003281 return true;
3282
3283 if (getLexer().isNot(AsmToken::Comma))
3284 return TokError("unexpected token in directive");
3285 Lex();
3286
3287 if (getParser().ParseAbsoluteExpression(Offset))
3288 return true;
3289
Rafael Espindola066c2f42011-04-12 23:59:07 +00003290 getStreamer().EmitCFIOffset(Register, Offset);
3291 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003292}
3293
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003294/// ParseDirectiveCFIRelOffset
3295/// ::= .cfi_rel_offset register, offset
3296bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3297 SMLoc DirectiveLoc) {
3298 int64_t Register = 0;
3299
3300 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3301 return true;
3302
3303 if (getLexer().isNot(AsmToken::Comma))
3304 return TokError("unexpected token in directive");
3305 Lex();
3306
3307 int64_t Offset = 0;
3308 if (getParser().ParseAbsoluteExpression(Offset))
3309 return true;
3310
Rafael Espindola25f492e2011-04-12 16:12:03 +00003311 getStreamer().EmitCFIRelOffset(Register, Offset);
3312 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003313}
3314
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003315static bool isValidEncoding(int64_t Encoding) {
3316 if (Encoding & ~0xff)
3317 return false;
3318
3319 if (Encoding == dwarf::DW_EH_PE_omit)
3320 return true;
3321
3322 const unsigned Format = Encoding & 0xf;
3323 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3324 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3325 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3326 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3327 return false;
3328
Rafael Espindolacaf11582010-12-29 04:31:26 +00003329 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003330 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003331 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003332 return false;
3333
3334 return true;
3335}
3336
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003337/// ParseDirectiveCFIPersonalityOrLsda
3338/// ::= .cfi_personality encoding, [symbol_name]
3339/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003340bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003341 SMLoc DirectiveLoc) {
3342 int64_t Encoding = 0;
3343 if (getParser().ParseAbsoluteExpression(Encoding))
3344 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003345 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003346 return false;
3347
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003348 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003349 return TokError("unsupported encoding.");
3350
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003351 if (getLexer().isNot(AsmToken::Comma))
3352 return TokError("unexpected token in directive");
3353 Lex();
3354
3355 StringRef Name;
3356 if (getParser().ParseIdentifier(Name))
3357 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003358
3359 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3360
3361 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003362 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003363 else {
3364 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003365 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003366 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003367 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003368}
3369
Rafael Espindolafe024d02010-12-28 18:36:23 +00003370/// ParseDirectiveCFIRememberState
3371/// ::= .cfi_remember_state
3372bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3373 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003374 getStreamer().EmitCFIRememberState();
3375 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003376}
3377
3378/// ParseDirectiveCFIRestoreState
3379/// ::= .cfi_remember_state
3380bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3381 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003382 getStreamer().EmitCFIRestoreState();
3383 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003384}
3385
Rafael Espindolac5754392011-04-12 15:31:05 +00003386/// ParseDirectiveCFISameValue
3387/// ::= .cfi_same_value register
3388bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3389 SMLoc DirectiveLoc) {
3390 int64_t Register = 0;
3391
3392 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3393 return true;
3394
3395 getStreamer().EmitCFISameValue(Register);
3396
3397 return false;
3398}
3399
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003400/// ParseDirectiveCFIRestore
3401/// ::= .cfi_restore register
3402bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003403 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003404 int64_t Register = 0;
3405 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3406 return true;
3407
3408 getStreamer().EmitCFIRestore(Register);
3409
3410 return false;
3411}
3412
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003413/// ParseDirectiveCFIEscape
3414/// ::= .cfi_escape expression[,...]
3415bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003416 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003417 std::string Values;
3418 int64_t CurrValue;
3419 if (getParser().ParseAbsoluteExpression(CurrValue))
3420 return true;
3421
3422 Values.push_back((uint8_t)CurrValue);
3423
3424 while (getLexer().is(AsmToken::Comma)) {
3425 Lex();
3426
3427 if (getParser().ParseAbsoluteExpression(CurrValue))
3428 return true;
3429
3430 Values.push_back((uint8_t)CurrValue);
3431 }
3432
3433 getStreamer().EmitCFIEscape(Values);
3434 return false;
3435}
3436
Rafael Espindola16d7d432012-01-23 21:51:52 +00003437/// ParseDirectiveCFISignalFrame
3438/// ::= .cfi_signal_frame
3439bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3440 SMLoc DirectiveLoc) {
3441 if (getLexer().isNot(AsmToken::EndOfStatement))
3442 return Error(getLexer().getLoc(),
3443 "unexpected token in '" + Directive + "' directive");
3444
3445 getStreamer().EmitCFISignalFrame();
3446
3447 return false;
3448}
3449
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003450/// ParseDirectiveCFIUndefined
3451/// ::= .cfi_undefined register
3452bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3453 SMLoc DirectiveLoc) {
3454 int64_t Register = 0;
3455
3456 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3457 return true;
3458
3459 getStreamer().EmitCFIUndefined(Register);
3460
3461 return false;
3462}
3463
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003464/// ParseDirectiveCFIRegister
3465/// ::= .cfi_register register, register
3466bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3467 SMLoc DirectiveLoc) {
3468 int64_t Register1 = 0;
3469
3470 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3471 return true;
3472
3473 if (getLexer().isNot(AsmToken::Comma))
3474 return TokError("unexpected token in directive");
3475 Lex();
3476
3477 int64_t Register2 = 0;
3478
3479 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3480 return true;
3481
3482 getStreamer().EmitCFIRegister(Register1, Register2);
3483
3484 return false;
3485}
3486
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003487/// ParseDirectiveMacrosOnOff
3488/// ::= .macros_on
3489/// ::= .macros_off
3490bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3491 SMLoc DirectiveLoc) {
3492 if (getLexer().isNot(AsmToken::EndOfStatement))
3493 return Error(getLexer().getLoc(),
3494 "unexpected token in '" + Directive + "' directive");
3495
Eli Bendersky733c3362013-01-14 18:08:41 +00003496 getParser().SetMacrosEnabled(Directive == ".macros_on");
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003497
3498 return false;
3499}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003500
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003501/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003502/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003503bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3504 SMLoc DirectiveLoc) {
3505 StringRef Name;
3506 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003507 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003508
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003509 MCAsmMacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003510 // Argument delimiter is initially unknown. It will be set by
3511 // ParseMacroArgument()
3512 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003513 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003514 for (;;) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003515 MCAsmMacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003516 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003517 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003518
3519 if (getLexer().is(AsmToken::Equal)) {
3520 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003521 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003522 return true;
3523 }
3524
Rafael Espindola65366442011-06-05 02:43:45 +00003525 Parameters.push_back(Parameter);
3526
Preston Gurd7b6f2032012-09-19 20:36:12 +00003527 if (getLexer().is(AsmToken::Comma))
3528 Lex();
3529 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003530 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003531 }
3532 }
3533
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003534 // Eat the end of statement.
3535 Lex();
3536
3537 AsmToken EndToken, StartToken = getTok();
3538
3539 // Lex the macro definition.
3540 for (;;) {
3541 // Check whether we have reached the end of the file.
3542 if (getLexer().is(AsmToken::Eof))
3543 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3544
3545 // Otherwise, check whether we have reach the .endmacro.
3546 if (getLexer().is(AsmToken::Identifier) &&
3547 (getTok().getIdentifier() == ".endm" ||
3548 getTok().getIdentifier() == ".endmacro")) {
3549 EndToken = getTok();
3550 Lex();
3551 if (getLexer().isNot(AsmToken::EndOfStatement))
3552 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3553 "' directive");
3554 break;
3555 }
3556
3557 // Otherwise, scan til the end of the statement.
3558 getParser().EatToEndOfStatement();
3559 }
3560
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003561 if (getParser().LookupMacro(Name)) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003562 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3563 }
3564
3565 const char *BodyStart = StartToken.getLoc().getPointer();
3566 const char *BodyEnd = EndToken.getLoc().getPointer();
3567 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003568 getParser().DefineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003569 return false;
3570}
3571
3572/// ParseDirectiveEndMacro
3573/// ::= .endm
3574/// ::= .endmacro
3575bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003576 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003577 if (getLexer().isNot(AsmToken::EndOfStatement))
3578 return TokError("unexpected token in '" + Directive + "' directive");
3579
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003580 // If we are inside a macro instantiation, terminate the current
3581 // instantiation.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003582 if (getParser().InsideMacroInstantiation()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003583 getParser().HandleMacroExit();
3584 return false;
3585 }
3586
3587 // Otherwise, this .endmacro is a stray entry in the file; well formed
3588 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003589 return TokError("unexpected '" + Directive + "' in file, "
3590 "no current macro definition");
3591}
3592
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003593/// ParseDirectivePurgeMacro
3594/// ::= .purgem
3595bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3596 SMLoc DirectiveLoc) {
3597 StringRef Name;
3598 if (getParser().ParseIdentifier(Name))
3599 return TokError("expected identifier in '.purgem' directive");
3600
3601 if (getLexer().isNot(AsmToken::EndOfStatement))
3602 return TokError("unexpected token in '.purgem' directive");
3603
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003604 if (!getParser().LookupMacro(Name))
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003605 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3606
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003607 getParser().UndefineMacro(Name);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003608 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
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003630MCAsmMacro *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;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003671 MCAsmMacroParameters Parameters;
3672 return new MCAsmMacro(Name, Body, Parameters);
Rafael Espindola761cb062012-06-03 23:57:14 +00003673}
3674
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003675void AsmParser::InstantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +00003676 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.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003711 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc);
Rafael Espindola761cb062012-06-03 23:57:14 +00003712 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;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003718 MCAsmMacroParameters Parameters;
3719 MCAsmMacroArguments 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) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003733 MCAsmMacroParameters Parameters;
3734 MCAsmMacroParameter 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
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003746 MCAsmMacroArguments 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.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003754 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003755 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
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003763 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3764 MCAsmMacroArguments 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) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003779 MCAsmMacroParameters Parameters;
3780 MCAsmMacroParameter 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
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003792 MCAsmMacroArguments 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.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003803 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003804 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) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00003815 MCAsmMacroArgument Arg;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003816 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3817
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003818 MCAsmMacroArguments 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}