blob: 66d3bc745a92b444fb1e619f916f1bb51e98bd13 [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
378 virtual void Initialize(MCAsmParser &Parser) {
379 // Call the base implementation.
380 this->MCAsmParserExtension::Initialize(Parser);
381
382 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000383 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
384 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
385 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000386 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000387
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000388 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".space");
389 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".skip");
390
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000391 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000392 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
393 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000394 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
395 ".cfi_startproc");
396 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
397 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000398 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
399 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000400 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
401 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000402 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
403 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000404 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
405 ".cfi_def_cfa_register");
406 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
407 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000408 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
409 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000410 AddDirectiveHandler<
411 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
412 AddDirectiveHandler<
413 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000414 AddDirectiveHandler<
415 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
416 AddDirectiveHandler<
417 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000418 AddDirectiveHandler<
419 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000420 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000421 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
422 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000423 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000424 AddDirectiveHandler<
425 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000426 AddDirectiveHandler<
427 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000428 AddDirectiveHandler<
429 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000430
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000431 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000432 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
433 ".macros_on");
434 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
435 ".macros_off");
436 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
437 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
438 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000439 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000440
441 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
442 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000443 }
444
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000445 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
446
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000447 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
448 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
449 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000450 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000451 bool ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000452 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000453 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
454 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000455 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000456 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000457 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000458 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
459 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000460 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000461 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000462 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
463 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000464 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000465 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000466 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000467 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000468 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000469 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000470
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000471 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000472 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
473 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000474 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000475
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000476 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000477};
478
479}
480
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000481namespace llvm {
482
483extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000484extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000485extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000486
487}
488
Chris Lattneraaec2052010-01-19 19:46:13 +0000489enum { DEFAULT_ADDRSPACE = 0 };
490
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000491AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000492 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000493 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000494 GenericParser(new GenericAsmParser), PlatformParser(0),
Eli Bendersky733c3362013-01-14 18:08:41 +0000495 CurBuffer(0), MacrosEnabledFlag(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000496 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000497 // Save the old handler.
498 SavedDiagHandler = SrcMgr.getDiagHandler();
499 SavedDiagContext = SrcMgr.getDiagContext();
500 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000501 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000502 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000503
504 // Initialize the generic parser.
505 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000506
507 // Initialize the platform / file format parser.
508 //
509 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
510 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000511 if (_MAI.hasMicrosoftFastStdCallMangling()) {
512 PlatformParser = createCOFFAsmParser();
513 PlatformParser->Initialize(*this);
514 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000515 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000516 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000517 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000518 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000519 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000520 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000521 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000522
523 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000524}
525
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000526AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000527 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
528
529 // Destroy any macros.
Eli Benderskyc0c67b02013-01-14 23:22:36 +0000530 for (StringMap<MCAsmMacro*>::iterator it = MacroMap.begin(),
Daniel Dunbar56491302010-07-29 01:51:55 +0000531 ie = MacroMap.end(); it != ie; ++it)
532 delete it->getValue();
533
Daniel Dunbare4749702010-07-12 18:12:02 +0000534 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000535 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000536}
537
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000538void AsmParser::PrintMacroInstantiations() {
539 // Print the active macro instantiation stack.
540 for (std::vector<MacroInstantiation*>::const_reverse_iterator
541 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000542 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
543 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000544}
545
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000546bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000547 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000548 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000549 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000550 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000551 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000552}
553
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000554bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000555 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000556 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000557 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000558 return true;
559}
560
Sean Callananfd0b0282010-01-21 00:19:58 +0000561bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000562 std::string IncludedFile;
563 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000564 if (NewBuf == -1)
565 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000566
Sean Callananfd0b0282010-01-21 00:19:58 +0000567 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000568
Sean Callananfd0b0282010-01-21 00:19:58 +0000569 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000570
Sean Callananfd0b0282010-01-21 00:19:58 +0000571 return false;
572}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000573
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000574/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000575/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000576/// returns true on failure.
577bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
578 std::string IncludedFile;
579 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
580 if (NewBuf == -1)
581 return true;
582
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000583 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000584 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
585 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000586 return false;
587}
588
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000589void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
590 if (InBuffer != -1) {
591 CurBuffer = InBuffer;
592 } else {
593 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
594 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000595 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
596}
597
Sean Callananfd0b0282010-01-21 00:19:58 +0000598const AsmToken &AsmParser::Lex() {
599 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000600
Sean Callananfd0b0282010-01-21 00:19:58 +0000601 if (tok->is(AsmToken::Eof)) {
602 // If this is the end of an included file, pop the parent file off the
603 // include stack.
604 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
605 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000606 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000607 tok = &Lexer.Lex();
608 }
609 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000610
Sean Callananfd0b0282010-01-21 00:19:58 +0000611 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000612 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000613
Sean Callananfd0b0282010-01-21 00:19:58 +0000614 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000615}
616
Chris Lattner79180e22010-04-05 23:15:42 +0000617bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000618 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000619 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000620 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000621
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000622 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000623 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000624
625 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000626 AsmCond StartingCondState = TheCondState;
627
Kevin Enderby613b7572011-11-01 22:27:22 +0000628 // If we are generating dwarf for assembly source files save the initial text
629 // section and generate a .file directive.
630 if (getContext().getGenDwarfForAssembly()) {
631 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000632 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
633 getStreamer().EmitLabel(SectionStartSym);
634 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000635 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000636 StringRef(),
637 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000638 }
639
Chris Lattnerb717fb02009-07-02 21:53:43 +0000640 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000641 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000642 ParseStatementInfo Info;
643 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000644
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000645 // We had an error, validate that one was emitted and recover by skipping to
646 // the next line.
647 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000648 EatToEndOfStatement();
649 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000650
651 if (TheCondState.TheCond != StartingCondState.TheCond ||
652 TheCondState.Ignore != StartingCondState.Ignore)
653 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000654
655 // Check to see there are no empty DwarfFile slots.
656 const std::vector<MCDwarfFile *> &MCDwarfFiles =
657 getContext().getMCDwarfFiles();
658 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000659 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000660 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000661 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000662
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000663 // Check to see that all assembler local symbols were actually defined.
664 // Targets that don't do subsections via symbols may not want this, though,
665 // so conservatively exclude them. Only do this if we're finalizing, though,
666 // as otherwise we won't necessarilly have seen everything yet.
667 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
668 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
669 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
670 e = Symbols.end();
671 i != e; ++i) {
672 MCSymbol *Sym = i->getValue();
673 // Variable symbols may not be marked as defined, so check those
674 // explicitly. If we know it's a variable, we have a definition for
675 // the purposes of this check.
676 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
677 // FIXME: We would really like to refer back to where the symbol was
678 // first referenced for a source location. We need to add something
679 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000680 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
681 "assembler local symbol '" + Sym->getName() +
682 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000683 }
684 }
685
686
Chris Lattner79180e22010-04-05 23:15:42 +0000687 // Finalize the output stream if there are no errors and if the client wants
688 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000689 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000690 Out.Finish();
691
Chris Lattnerb717fb02009-07-02 21:53:43 +0000692 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000693}
694
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000695void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000696 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000697 TokError("expected section directive before assembly directive");
Eli Bendersky030f63a2013-01-14 19:04:57 +0000698 Out.InitToTextSection();
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000699 }
700}
701
Chris Lattner2cf5f142009-06-22 01:29:09 +0000702/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
703void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000704 while (Lexer.isNot(AsmToken::EndOfStatement) &&
705 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000706 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000707
Chris Lattner2cf5f142009-06-22 01:29:09 +0000708 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000709 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000710 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000711}
712
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000713StringRef AsmParser::ParseStringToEndOfStatement() {
714 const char *Start = getTok().getLoc().getPointer();
715
716 while (Lexer.isNot(AsmToken::EndOfStatement) &&
717 Lexer.isNot(AsmToken::Eof))
718 Lex();
719
720 const char *End = getTok().getLoc().getPointer();
721 return StringRef(Start, End - Start);
722}
Chris Lattnerc4193832009-06-22 05:51:26 +0000723
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000724StringRef AsmParser::ParseStringToComma() {
725 const char *Start = getTok().getLoc().getPointer();
726
727 while (Lexer.isNot(AsmToken::EndOfStatement) &&
728 Lexer.isNot(AsmToken::Comma) &&
729 Lexer.isNot(AsmToken::Eof))
730 Lex();
731
732 const char *End = getTok().getLoc().getPointer();
733 return StringRef(Start, End - Start);
734}
735
Chris Lattner74ec1a32009-06-22 06:32:03 +0000736/// ParseParenExpr - Parse a paren expression and return it.
737/// NOTE: This assumes the leading '(' has already been consumed.
738///
739/// parenexpr ::= expr)
740///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000741bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000742 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000743 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000744 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000745 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000746 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000747 return false;
748}
Chris Lattnerc4193832009-06-22 05:51:26 +0000749
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000750/// ParseBracketExpr - Parse a bracket expression and return it.
751/// NOTE: This assumes the leading '[' has already been consumed.
752///
753/// bracketexpr ::= expr]
754///
755bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
756 if (ParseExpression(Res)) return true;
757 if (Lexer.isNot(AsmToken::RBrac))
758 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000759 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000760 Lex();
761 return false;
762}
763
Chris Lattner74ec1a32009-06-22 06:32:03 +0000764/// ParsePrimaryExpr - Parse a primary expression and return it.
765/// primaryexpr ::= (parenexpr
766/// primaryexpr ::= symbol
767/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000768/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000769/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000770bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000771 switch (Lexer.getKind()) {
772 default:
773 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000774 // If we have an error assume that we've already handled it.
775 case AsmToken::Error:
776 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000777 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000778 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000779 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000780 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000781 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000782 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000783 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000784 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000785 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000786 StringRef Identifier;
787 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000788 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000789
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000790 EndLoc = SMLoc::getFromPointer(Identifier.end());
791
Daniel Dunbarfffff912009-10-16 01:34:54 +0000792 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000793 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000794 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000795
796 // Lookup the symbol variant if used.
797 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000798 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000799 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000800 if (Variant == MCSymbolRefExpr::VK_Invalid) {
801 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000802 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000803 }
804 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000805
Daniel Dunbarfffff912009-10-16 01:34:54 +0000806 // If this is an absolute variable reference, substitute it now to preserve
807 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000808 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000809 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000810 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000811
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000812 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000813 return false;
814 }
815
816 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000817 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000818 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000819 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000820 case AsmToken::Integer: {
821 SMLoc Loc = getTok().getLoc();
822 int64_t IntVal = getTok().getIntVal();
823 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000824 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000825 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000826 // Look for 'b' or 'f' following an Integer as a directional label
827 if (Lexer.getKind() == AsmToken::Identifier) {
828 StringRef IDVal = getTok().getString();
829 if (IDVal == "f" || IDVal == "b"){
830 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
831 IDVal == "f" ? 1 : 0);
832 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
833 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000834 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000835 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000836 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000837 Lex(); // Eat identifier.
838 }
839 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000840 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000841 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000842 case AsmToken::Real: {
843 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000844 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000845 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000846 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000847 Lex(); // Eat token.
848 return false;
849 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000850 case AsmToken::Dot: {
851 // This is a '.' reference, which references the current PC. Emit a
852 // temporary label to the streamer and refer to it.
853 MCSymbol *Sym = Ctx.CreateTempSymbol();
854 Out.EmitLabel(Sym);
855 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000856 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000857 Lex(); // Eat identifier.
858 return false;
859 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000860 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000861 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000862 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000863 case AsmToken::LBrac:
864 if (!PlatformParser->HasBracketExpressions())
865 return TokError("brackets expression not supported on this target");
866 Lex(); // Eat the '['.
867 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000868 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000869 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000870 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000871 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000872 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000873 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000874 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000875 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000876 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000877 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000878 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000879 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000880 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000881 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000882 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000883 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000884 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000885 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000886 }
887}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000888
Chris Lattnerb4307b32010-01-15 19:28:38 +0000889bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000890 SMLoc EndLoc;
891 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000892}
893
Daniel Dunbarcceba832010-09-17 02:47:07 +0000894const MCExpr *
895AsmParser::ApplyModifierToExpr(const MCExpr *E,
896 MCSymbolRefExpr::VariantKind Variant) {
897 // Recurse over the given expression, rebuilding it to apply the given variant
898 // if there is exactly one symbol.
899 switch (E->getKind()) {
900 case MCExpr::Target:
901 case MCExpr::Constant:
902 return 0;
903
904 case MCExpr::SymbolRef: {
905 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
906
907 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
908 TokError("invalid variant on expression '" +
909 getTok().getIdentifier() + "' (already modified)");
910 return E;
911 }
912
913 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
914 }
915
916 case MCExpr::Unary: {
917 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
918 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
919 if (!Sub)
920 return 0;
921 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
922 }
923
924 case MCExpr::Binary: {
925 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
926 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
927 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
928
929 if (!LHS && !RHS)
930 return 0;
931
932 if (!LHS) LHS = BE->getLHS();
933 if (!RHS) RHS = BE->getRHS();
934
935 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
936 }
937 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000938
Craig Topper85814382012-02-07 05:05:23 +0000939 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000940}
941
Chris Lattner74ec1a32009-06-22 06:32:03 +0000942/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000943///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000944/// expr ::= expr &&,|| expr -> lowest.
945/// expr ::= expr |,^,&,! expr
946/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
947/// expr ::= expr <<,>> expr
948/// expr ::= expr +,- expr
949/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000950/// expr ::= primaryexpr
951///
Chris Lattner54482b42010-01-15 19:39:23 +0000952bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000953 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000954 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000955 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
956 return true;
957
Daniel Dunbarcceba832010-09-17 02:47:07 +0000958 // As a special case, we support 'a op b @ modifier' by rewriting the
959 // expression to include the modifier. This is inefficient, but in general we
960 // expect users to use 'a@modifier op b'.
961 if (Lexer.getKind() == AsmToken::At) {
962 Lex();
963
964 if (Lexer.isNot(AsmToken::Identifier))
965 return TokError("unexpected symbol modifier following '@'");
966
967 MCSymbolRefExpr::VariantKind Variant =
968 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
969 if (Variant == MCSymbolRefExpr::VK_Invalid)
970 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
971
972 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
973 if (!ModifiedRes) {
974 return TokError("invalid modifier '" + getTok().getIdentifier() +
975 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000976 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000977
Daniel Dunbarcceba832010-09-17 02:47:07 +0000978 Res = ModifiedRes;
979 Lex();
980 }
981
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000982 // Try to constant fold it up front, if possible.
983 int64_t Value;
984 if (Res->EvaluateAsAbsolute(Value))
985 Res = MCConstantExpr::Create(Value, getContext());
986
987 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000988}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000989
Chris Lattnerb4307b32010-01-15 19:28:38 +0000990bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000991 Res = 0;
992 return ParseParenExpr(Res, EndLoc) ||
993 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000994}
995
Daniel Dunbar475839e2009-06-29 20:37:27 +0000996bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000997 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000998
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000999 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +00001000 if (ParseExpression(Expr))
1001 return true;
1002
Daniel Dunbare00b0112009-10-16 01:57:52 +00001003 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001004 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001005
1006 return false;
1007}
1008
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001009static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001010 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001011 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001012 default:
1013 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001014
Jim Grosbachfbe16812011-08-20 16:24:13 +00001015 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001016 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001017 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001018 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001019 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001020 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001021 return 1;
1022
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001023
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001024 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +00001025 //
1026 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001027 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001028 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001029 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001030 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001031 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001032 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001033 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001034 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001035 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001036
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001037 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001038 case AsmToken::EqualEqual:
1039 Kind = MCBinaryExpr::EQ;
1040 return 3;
1041 case AsmToken::ExclaimEqual:
1042 case AsmToken::LessGreater:
1043 Kind = MCBinaryExpr::NE;
1044 return 3;
1045 case AsmToken::Less:
1046 Kind = MCBinaryExpr::LT;
1047 return 3;
1048 case AsmToken::LessEqual:
1049 Kind = MCBinaryExpr::LTE;
1050 return 3;
1051 case AsmToken::Greater:
1052 Kind = MCBinaryExpr::GT;
1053 return 3;
1054 case AsmToken::GreaterEqual:
1055 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001056 return 3;
1057
Jim Grosbachfbe16812011-08-20 16:24:13 +00001058 // Intermediate Precedence: <<, >>
1059 case AsmToken::LessLess:
1060 Kind = MCBinaryExpr::Shl;
1061 return 4;
1062 case AsmToken::GreaterGreater:
1063 Kind = MCBinaryExpr::Shr;
1064 return 4;
1065
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001066 // High Intermediate Precedence: +, -
1067 case AsmToken::Plus:
1068 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001069 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001070 case AsmToken::Minus:
1071 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001072 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001073
Jim Grosbachfbe16812011-08-20 16:24:13 +00001074 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001075 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001076 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001077 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001078 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001079 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001080 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001081 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001082 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001083 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001084 }
1085}
1086
1087
1088/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1089/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001090bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1091 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001092 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001093 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001094 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001095
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001096 // If the next token is lower precedence than we are allowed to eat, return
1097 // successfully with what we ate already.
1098 if (TokPrec < Precedence)
1099 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001100
Sean Callanan79ed1a82010-01-19 20:22:31 +00001101 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001102
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001103 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001104 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001105 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001106
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001107 // If BinOp binds less tightly with RHS than the operator after RHS, let
1108 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001109 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001110 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001111 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001112 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001113 }
1114
Daniel Dunbar475839e2009-06-29 20:37:27 +00001115 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001116 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001117 }
1118}
1119
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001120/// ParseStatement:
1121/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001122/// ::= Label* Directive ...Operands... EndOfStatement
1123/// ::= Label* Identifier OperandList* EndOfStatement
Eli Friedman2128aae2012-10-22 23:58:19 +00001124bool AsmParser::ParseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001125 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001126 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001127 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001128 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001129 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001130
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001131 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001132 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001133 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001134 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001135 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001136 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001137 if (Lexer.is(AsmToken::Hash))
1138 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001139
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001140 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001141 if (Lexer.is(AsmToken::Integer)) {
1142 LocalLabelVal = getTok().getIntVal();
1143 if (LocalLabelVal < 0) {
1144 if (!TheCondState.Ignore)
1145 return TokError("unexpected token at start of statement");
1146 IDVal = "";
1147 }
1148 else {
1149 IDVal = getTok().getString();
1150 Lex(); // Consume the integer token to be used as an identifier token.
1151 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001152 if (!TheCondState.Ignore)
1153 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001154 }
1155 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001156
1157 } else if (Lexer.is(AsmToken::Dot)) {
1158 // Treat '.' as a valid identifier in this context.
1159 Lex();
1160 IDVal = ".";
1161
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001162 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001163 if (!TheCondState.Ignore)
1164 return TokError("unexpected token at start of statement");
1165 IDVal = "";
1166 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001167
Chris Lattner7834fac2010-04-17 18:14:27 +00001168 // Handle conditional assembly here before checking for skipping. We
1169 // have to do this so that .endif isn't skipped in a ".if 0" block for
1170 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001171 StringMap<DirectiveKind>::const_iterator DirKindIt =
1172 DirectiveKindMapping.find(IDVal);
1173 DirectiveKind DirKind =
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001174 (DirKindIt == DirectiveKindMapping.end()) ? DK_NO_DIRECTIVE :
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001175 DirKindIt->getValue();
1176 switch (DirKind) {
1177 default:
1178 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001179 case DK_IF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001180 return ParseDirectiveIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001181 case DK_IFB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001182 return ParseDirectiveIfb(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001183 case DK_IFNB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001184 return ParseDirectiveIfb(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001185 case DK_IFC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001186 return ParseDirectiveIfc(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001187 case DK_IFNC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001188 return ParseDirectiveIfc(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001189 case DK_IFDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001190 return ParseDirectiveIfdef(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001191 case DK_IFNDEF:
1192 case DK_IFNOTDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001193 return ParseDirectiveIfdef(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001194 case DK_ELSEIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001195 return ParseDirectiveElseIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001196 case DK_ELSE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001197 return ParseDirectiveElse(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001198 case DK_ENDIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001199 return ParseDirectiveEndIf(IDLoc);
1200 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001201
Chris Lattner7834fac2010-04-17 18:14:27 +00001202 // If we are in a ".if 0" block, ignore this statement.
Chad Rosier17feeec2012-10-20 00:47:08 +00001203 if (TheCondState.Ignore) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001204 EatToEndOfStatement();
1205 return false;
1206 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001207
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001208 // FIXME: Recurse on local labels?
1209
1210 // See what kind of statement we have.
1211 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001212 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001213 CheckForValidSection();
1214
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001215 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001216 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001217
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001218 // Diagnose attempt to use '.' as a label.
1219 if (IDVal == ".")
1220 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1221
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001222 // Diagnose attempt to use a variable as a label.
1223 //
1224 // FIXME: Diagnostics. Note the location of the definition as a label.
1225 // FIXME: This doesn't diagnose assignment to a symbol which has been
1226 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001227 MCSymbol *Sym;
1228 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001229 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001230 else
1231 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001232 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001233 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001234
Daniel Dunbar959fd882009-08-26 22:13:22 +00001235 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001236 if (!ParsingInlineAsm)
1237 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001238
Kevin Enderby94c2e852011-12-09 18:09:40 +00001239 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001240 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001241 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001242 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1243 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001244
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001245 // Consume any end of statement token, if present, to avoid spurious
1246 // AddBlankLine calls().
1247 if (Lexer.is(AsmToken::EndOfStatement)) {
1248 Lex();
1249 if (Lexer.is(AsmToken::Eof))
1250 return false;
1251 }
1252
Eli Friedman2128aae2012-10-22 23:58:19 +00001253 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001254 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001255
Daniel Dunbar3f872332009-07-28 16:08:33 +00001256 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001257 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001258 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001259
Nico Weber4c4c7322011-01-28 03:04:41 +00001260 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001261
1262 default: // Normal instruction or directive.
1263 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001264 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001265
1266 // If macros are enabled, check to see if this is a macro instantiation.
Eli Bendersky733c3362013-01-14 18:08:41 +00001267 if (MacrosEnabled())
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001268 if (const MCAsmMacro *M = LookupMacro(IDVal)) {
1269 return HandleMacroEntry(M, IDLoc);
1270 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001271
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001272 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001273 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001274
1275 // Target hook for parsing target specific directives.
1276 if (!getTargetParser().ParseDirective(ID))
1277 return false;
1278
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001279 switch (DirKind) {
1280 default:
1281 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001282 case DK_SET:
1283 case DK_EQU:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001284 return ParseDirectiveSet(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001285 case DK_EQUIV:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001286 return ParseDirectiveSet(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001287 case DK_ASCII:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001288 return ParseDirectiveAscii(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001289 case DK_ASCIZ:
1290 case DK_STRING:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001291 return ParseDirectiveAscii(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001292 case DK_BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001293 return ParseDirectiveValue(1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001294 case DK_SHORT:
1295 case DK_VALUE:
1296 case DK_2BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001297 return ParseDirectiveValue(2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001298 case DK_LONG:
1299 case DK_INT:
1300 case DK_4BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001301 return ParseDirectiveValue(4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001302 case DK_QUAD:
1303 case DK_8BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001304 return ParseDirectiveValue(8);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001305 case DK_SINGLE:
1306 case DK_FLOAT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001307 return ParseDirectiveRealValue(APFloat::IEEEsingle);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001308 case DK_DOUBLE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001309 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001310 case DK_ALIGN: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001311 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1312 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1313 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001314 case DK_ALIGN32: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001315 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1316 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1317 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001318 case DK_BALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001319 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001320 case DK_BALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001321 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001322 case DK_BALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001323 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001324 case DK_P2ALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001325 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001326 case DK_P2ALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001327 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001328 case DK_P2ALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001329 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001330 case DK_ORG:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001331 return ParseDirectiveOrg();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001332 case DK_FILL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001333 return ParseDirectiveFill();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001334 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001335 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001336 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001337 EatToEndOfStatement(); // .extern is the default, ignore it.
1338 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001339 case DK_GLOBL:
1340 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001341 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001342 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001343 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001344 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001345 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001346 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001347 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001348 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001349 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001350 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001351 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001352 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001353 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001354 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001355 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001356 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001357 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001358 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001359 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001360 case DK_COMM:
1361 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001362 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001363 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001364 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001365 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001366 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001367 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001368 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001369 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001370 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001371 case DK_CODE16:
1372 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001373 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001374 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001375 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001376 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001377 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001378 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001379 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001380 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001381 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001382 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001383 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001384 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001385 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001386 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001387 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001388 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001389
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001390 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001391 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1392 DirectiveMap.lookup(IDVal);
1393 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001394 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001395
Jim Grosbach686c0182012-05-01 18:38:27 +00001396 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001397 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001398
Eli Friedman2128aae2012-10-22 23:58:19 +00001399 // _emit
1400 if (ParsingInlineAsm && IDVal == "_emit")
1401 return ParseDirectiveEmit(IDLoc, Info);
1402
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001403 CheckForValidSection();
1404
Chris Lattnera7f13542010-05-19 23:34:33 +00001405 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001406 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001407 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001408 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001409
Chad Rosier6a020a72012-10-25 20:41:34 +00001410 ParseInstructionInfo IInfo(Info.AsmRewrites);
1411 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1412 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001413 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001414
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001415 // Dump the parsed representation, if requested.
1416 if (getShowParsedOperands()) {
1417 SmallString<256> Str;
1418 raw_svector_ostream OS(Str);
1419 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001420 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001421 if (i != 0)
1422 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001423 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001424 }
1425 OS << "]";
1426
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001427 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001428 }
1429
Kevin Enderby613b7572011-11-01 22:27:22 +00001430 // If we are generating dwarf for assembly source files and the current
1431 // section is the initial text section then generate a .loc directive for
1432 // the instruction.
1433 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001434 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001435
1436 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1437
1438 // If we previously parsed a cpp hash file line comment then make sure the
1439 // current Dwarf File is for the CppHashFilename if not then emit the
1440 // Dwarf File table for it and adjust the line number for the .loc.
1441 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1442 getContext().getMCDwarfFiles();
1443 if (CppHashFilename.size() != 0) {
1444 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1445 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001446 getStreamer().EmitDwarfFileDirective(
1447 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001448
Kevin Enderby32c1a822012-11-05 21:55:41 +00001449 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001450 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1451 }
1452
Kevin Enderby613b7572011-11-01 22:27:22 +00001453 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001454 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001455 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001456 StringRef());
1457 }
1458
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001459 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001460 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001461 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001462 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1463 Info.ParsedOperands,
1464 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001465 ParsingInlineAsm);
1466 }
Chris Lattner98986712010-01-14 22:21:20 +00001467
Chris Lattnercbf8a982010-09-11 16:18:25 +00001468 // Don't skip the rest of the line, the instruction parser is responsible for
1469 // that.
1470 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001471}
Chris Lattner9a023f72009-06-24 04:43:34 +00001472
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001473/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1474/// since they may not be able to be tokenized to get to the end of line token.
1475void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001476 if (!Lexer.is(AsmToken::EndOfStatement))
1477 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001478 // Eat EOL.
1479 Lex();
1480}
1481
1482/// ParseCppHashLineFilenameComment as this:
1483/// ::= # number "filename"
1484/// or just as a full line comment if it doesn't have a number and a string.
1485bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1486 Lex(); // Eat the hash token.
1487
1488 if (getLexer().isNot(AsmToken::Integer)) {
1489 // Consume the line since in cases it is not a well-formed line directive,
1490 // as if were simply a full line comment.
1491 EatToEndOfLine();
1492 return false;
1493 }
1494
1495 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001496 Lex();
1497
1498 if (getLexer().isNot(AsmToken::String)) {
1499 EatToEndOfLine();
1500 return false;
1501 }
1502
1503 StringRef Filename = getTok().getString();
1504 // Get rid of the enclosing quotes.
1505 Filename = Filename.substr(1, Filename.size()-2);
1506
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001507 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1508 CppHashLoc = L;
1509 CppHashFilename = Filename;
1510 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001511 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001512
1513 // Ignore any trailing characters, they're just comment.
1514 EatToEndOfLine();
1515 return false;
1516}
1517
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001518/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001519/// for the Filename and LineNo if any in the diagnostic.
1520void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1521 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1522 raw_ostream &OS = errs();
1523
1524 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1525 const SMLoc &DiagLoc = Diag.getLoc();
1526 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1527 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1528
1529 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1530 // before printing the message.
1531 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001532 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001533 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1534 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1535 }
1536
Eric Christopher2318ba12012-12-18 00:30:54 +00001537 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001538 // manager changed or buffer changed (like in a nested include) then just
1539 // print the normal diagnostic using its Filename and LineNo.
1540 if (!Parser->CppHashLineNumber ||
1541 &DiagSrcMgr != &Parser->SrcMgr ||
1542 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001543 if (Parser->SavedDiagHandler)
1544 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1545 else
1546 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001547 return;
1548 }
1549
Eric Christopher2318ba12012-12-18 00:30:54 +00001550 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001551 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1552 // the diagnostic.
1553 const std::string Filename = Parser->CppHashFilename;
1554
1555 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1556 int CppHashLocLineNo =
1557 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1558 int LineNo = Parser->CppHashLineNumber - 1 +
1559 (DiagLocLineNo - CppHashLocLineNo);
1560
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001561 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1562 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001563 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001564 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001565
Benjamin Kramer04a04262011-10-16 10:48:29 +00001566 if (Parser->SavedDiagHandler)
1567 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1568 else
1569 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001570}
1571
Rafael Espindola799aacf2012-08-21 18:29:30 +00001572// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1573// difference being that that function accepts '@' as part of identifiers and
1574// we can't do that. AsmLexer.cpp should probably be changed to handle
1575// '@' as a special case when needed.
1576static bool isIdentifierChar(char c) {
1577 return isalnum(c) || c == '_' || c == '$' || c == '.';
1578}
1579
Rafael Espindola761cb062012-06-03 23:57:14 +00001580bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001581 const MCAsmMacroParameters &Parameters,
1582 const MCAsmMacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001583 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001584 unsigned NParameters = Parameters.size();
1585 if (NParameters != 0 && NParameters != A.size())
1586 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001587
Preston Gurd7b6f2032012-09-19 20:36:12 +00001588 // A macro without parameters is handled differently on Darwin:
1589 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001590 while (!Body.empty()) {
1591 // Scan for the next substitution.
1592 std::size_t End = Body.size(), Pos = 0;
1593 for (; Pos != End; ++Pos) {
1594 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001595 if (!NParameters) {
1596 // This macro has no parameters, look for $0, $1, etc.
1597 if (Body[Pos] != '$' || Pos + 1 == End)
1598 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001599
Rafael Espindola65366442011-06-05 02:43:45 +00001600 char Next = Body[Pos + 1];
1601 if (Next == '$' || Next == 'n' || isdigit(Next))
1602 break;
1603 } else {
1604 // This macro has parameters, look for \foo, \bar, etc.
1605 if (Body[Pos] == '\\' && Pos + 1 != End)
1606 break;
1607 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001608 }
1609
1610 // Add the prefix.
1611 OS << Body.slice(0, Pos);
1612
1613 // Check if we reached the end.
1614 if (Pos == End)
1615 break;
1616
Rafael Espindola65366442011-06-05 02:43:45 +00001617 if (!NParameters) {
1618 switch (Body[Pos+1]) {
1619 // $$ => $
1620 case '$':
1621 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001622 break;
1623
Rafael Espindola65366442011-06-05 02:43:45 +00001624 // $n => number of arguments
1625 case 'n':
1626 OS << A.size();
1627 break;
1628
1629 // $[0-9] => argument
1630 default: {
1631 // Missing arguments are ignored.
1632 unsigned Index = Body[Pos+1] - '0';
1633 if (Index >= A.size())
1634 break;
1635
1636 // Otherwise substitute with the token values, with spaces eliminated.
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001637 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001638 ie = A[Index].end(); it != ie; ++it)
1639 OS << it->getString();
1640 break;
1641 }
1642 }
1643 Pos += 2;
1644 } else {
1645 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001646 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001647 ++I;
1648
1649 const char *Begin = Body.data() + Pos +1;
1650 StringRef Argument(Begin, I - (Pos +1));
1651 unsigned Index = 0;
1652 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001653 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001654 break;
1655
Preston Gurd7b6f2032012-09-19 20:36:12 +00001656 if (Index == NParameters) {
1657 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1658 Pos += 3;
1659 else {
1660 OS << '\\' << Argument;
1661 Pos = I;
1662 }
1663 } else {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001664 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Preston Gurd7b6f2032012-09-19 20:36:12 +00001665 ie = A[Index].end(); it != ie; ++it)
1666 if (it->getKind() == AsmToken::String)
1667 OS << it->getStringContents();
1668 else
1669 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001670
Preston Gurd7b6f2032012-09-19 20:36:12 +00001671 Pos += 1 + Argument.size();
1672 }
Rafael Espindola65366442011-06-05 02:43:45 +00001673 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001674 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001675 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001676 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001677
Rafael Espindola65366442011-06-05 02:43:45 +00001678 return false;
1679}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001680
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001681MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001682 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001683 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001684 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1685 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001686{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001687}
1688
Preston Gurd7b6f2032012-09-19 20:36:12 +00001689static bool IsOperator(AsmToken::TokenKind kind)
1690{
1691 switch (kind)
1692 {
1693 default:
1694 return false;
1695 case AsmToken::Plus:
1696 case AsmToken::Minus:
1697 case AsmToken::Tilde:
1698 case AsmToken::Slash:
1699 case AsmToken::Star:
1700 case AsmToken::Dot:
1701 case AsmToken::Equal:
1702 case AsmToken::EqualEqual:
1703 case AsmToken::Pipe:
1704 case AsmToken::PipePipe:
1705 case AsmToken::Caret:
1706 case AsmToken::Amp:
1707 case AsmToken::AmpAmp:
1708 case AsmToken::Exclaim:
1709 case AsmToken::ExclaimEqual:
1710 case AsmToken::Percent:
1711 case AsmToken::Less:
1712 case AsmToken::LessEqual:
1713 case AsmToken::LessLess:
1714 case AsmToken::LessGreater:
1715 case AsmToken::Greater:
1716 case AsmToken::GreaterEqual:
1717 case AsmToken::GreaterGreater:
1718 return true;
1719 }
1720}
1721
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001722bool AsmParser::ParseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +00001723 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001724 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001725 unsigned AddTokens = 0;
1726
1727 // gas accepts arguments separated by whitespace, except on Darwin
1728 if (!IsDarwin)
1729 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001730
1731 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001732 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1733 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001734 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001735 }
1736
1737 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1738 // Spaces and commas cannot be mixed to delimit parameters
1739 if (ArgumentDelimiter == AsmToken::Eof)
1740 ArgumentDelimiter = AsmToken::Comma;
1741 else if (ArgumentDelimiter != AsmToken::Comma) {
1742 Lexer.setSkipSpace(true);
1743 return TokError("expected ' ' for macro argument separator");
1744 }
1745 break;
1746 }
1747
1748 if (Lexer.is(AsmToken::Space)) {
1749 Lex(); // Eat spaces
1750
1751 // Spaces can delimit parameters, but could also be part an expression.
1752 // If the token after a space is an operator, add the token and the next
1753 // one into this argument
1754 if (ArgumentDelimiter == AsmToken::Space ||
1755 ArgumentDelimiter == AsmToken::Eof) {
1756 if (IsOperator(Lexer.getKind())) {
1757 // Check to see whether the token is used as an operator,
1758 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001759 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001760 if (*NextChar == ' ')
1761 AddTokens = 2;
1762 }
1763
1764 if (!AddTokens && ParenLevel == 0) {
1765 if (ArgumentDelimiter == AsmToken::Eof &&
1766 !IsOperator(Lexer.getKind()))
1767 ArgumentDelimiter = AsmToken::Space;
1768 break;
1769 }
1770 }
1771 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001772
1773 // HandleMacroEntry relies on not advancing the lexer here
1774 // to be able to fill in the remaining default parameter values
1775 if (Lexer.is(AsmToken::EndOfStatement))
1776 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001777
1778 // Adjust the current parentheses level.
1779 if (Lexer.is(AsmToken::LParen))
1780 ++ParenLevel;
1781 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1782 --ParenLevel;
1783
1784 // Append the token to the current argument list.
1785 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001786 if (AddTokens)
1787 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001788 Lex();
1789 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001790
1791 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001792 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001793 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001794 return false;
1795}
1796
1797// Parse the macro instantiation arguments.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001798bool AsmParser::ParseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001799 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001800 // Argument delimiter is initially unknown. It will be set by
1801 // ParseMacroArgument()
1802 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001803
1804 // Parse two kinds of macro invocations:
1805 // - macros defined without any parameters accept an arbitrary number of them
1806 // - macros defined with parameters accept at most that many of them
1807 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1808 ++Parameter) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001809 MCAsmMacroArgument MA;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001810
Preston Gurd7b6f2032012-09-19 20:36:12 +00001811 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001812 return true;
1813
Preston Gurd6c9176a2012-09-19 20:29:04 +00001814 if (!MA.empty() || !NParameters)
1815 A.push_back(MA);
1816 else if (NParameters) {
1817 if (!M->Parameters[Parameter].second.empty())
1818 A.push_back(M->Parameters[Parameter].second);
1819 }
Jim Grosbach97146442012-07-30 22:44:17 +00001820
Preston Gurd6c9176a2012-09-19 20:29:04 +00001821 // At the end of the statement, fill in remaining arguments that have
1822 // default values. If there aren't any, then the next argument is
1823 // required but missing
1824 if (Lexer.is(AsmToken::EndOfStatement)) {
1825 if (NParameters && Parameter < NParameters - 1) {
1826 if (M->Parameters[Parameter + 1].second.empty())
1827 return TokError("macro argument '" +
1828 Twine(M->Parameters[Parameter + 1].first) +
1829 "' is missing");
1830 else
1831 continue;
1832 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001833 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001834 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001835
1836 if (Lexer.is(AsmToken::Comma))
1837 Lex();
1838 }
1839 return TokError("Too many arguments");
1840}
1841
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001842const MCAsmMacro* AsmParser::LookupMacro(StringRef Name) {
1843 StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name);
1844 return (I == MacroMap.end()) ? NULL : I->getValue();
1845}
1846
1847void AsmParser::DefineMacro(StringRef Name, const MCAsmMacro& Macro) {
1848 MacroMap[Name] = new MCAsmMacro(Macro);
1849}
1850
1851void AsmParser::UndefineMacro(StringRef Name) {
1852 StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name);
1853 if (I != MacroMap.end()) {
1854 delete I->getValue();
1855 MacroMap.erase(I);
1856 }
1857}
1858
1859bool AsmParser::HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001860 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1861 // this, although we should protect against infinite loops.
1862 if (ActiveMacros.size() == 20)
1863 return TokError("macros cannot be nested more than 20 levels deep");
1864
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001865 MCAsmMacroArguments A;
Rafael Espindola8a403d32012-08-08 14:51:03 +00001866 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001867 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001868
Jim Grosbach97146442012-07-30 22:44:17 +00001869 // Remove any trailing empty arguments. Do this after-the-fact as we have
1870 // to keep empty arguments in the middle of the list or positionality
1871 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001872 while (!A.empty() && A.back().empty())
1873 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001874
Rafael Espindola65366442011-06-05 02:43:45 +00001875 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1876 // to hold the macro body with substitutions.
1877 SmallString<256> Buf;
1878 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001879 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001880
Rafael Espindola8a403d32012-08-08 14:51:03 +00001881 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001882 return true;
1883
Eli Benderskyc0c67b02013-01-14 23:22:36 +00001884 // We include the .endmacro in the buffer as our cue to exit the macro
Rafael Espindola761cb062012-06-03 23:57:14 +00001885 // instantiation.
1886 OS << ".endmacro\n";
1887
Rafael Espindola65366442011-06-05 02:43:45 +00001888 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001889 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001890
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001891 // Create the macro instantiation object and add to the current macro
1892 // instantiation stack.
1893 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001894 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001895 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001896 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001897 ActiveMacros.push_back(MI);
1898
1899 // Jump to the macro instantiation and prime the lexer.
1900 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1901 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1902 Lex();
1903
1904 return false;
1905}
1906
1907void AsmParser::HandleMacroExit() {
1908 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001909 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001910 Lex();
1911
1912 // Pop the instantiation entry.
1913 delete ActiveMacros.back();
1914 ActiveMacros.pop_back();
1915}
1916
Rafael Espindolae71cc862012-01-28 05:57:00 +00001917static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001918 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001919 case MCExpr::Binary: {
1920 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1921 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001922 break;
1923 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001924 case MCExpr::Target:
1925 case MCExpr::Constant:
1926 return false;
1927 case MCExpr::SymbolRef: {
1928 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001929 if (S.isVariable())
1930 return IsUsedIn(Sym, S.getVariableValue());
1931 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001932 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001933 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001934 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001935 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001936
1937 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001938}
1939
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001940bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1941 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001942 // FIXME: Use better location, we should use proper tokens.
1943 SMLoc EqualLoc = Lexer.getLoc();
1944
Daniel Dunbar821e3332009-08-31 08:09:28 +00001945 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001946 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001947 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001948
Rafael Espindolae71cc862012-01-28 05:57:00 +00001949 // Note: we don't count b as used in "a = b". This is to allow
1950 // a = b
1951 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001952
Daniel Dunbar3f872332009-07-28 16:08:33 +00001953 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001954 return TokError("unexpected token in assignment");
1955
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001956 // Error on assignment to '.'.
1957 if (Name == ".") {
1958 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1959 "(use '.space' or '.org').)"));
1960 }
1961
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001962 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001963 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001964
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001965 // Validate that the LHS is allowed to be a variable (either it has not been
1966 // used as a symbol, or it is an absolute symbol).
1967 MCSymbol *Sym = getContext().LookupSymbol(Name);
1968 if (Sym) {
1969 // Diagnose assignment to a label.
1970 //
1971 // FIXME: Diagnostics. Note the location of the definition as a label.
1972 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001973 if (IsUsedIn(Sym, Value))
1974 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1975 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001976 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001977 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1978 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001979 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001980 return Error(EqualLoc, "redefinition of '" + Name + "'");
1981 else if (!Sym->isVariable())
1982 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001983 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001984 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1985 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001986
1987 // Don't count these checks as uses.
1988 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001989 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001990 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001991
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001992 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001993
1994 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001995 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001996 if (NoDeadStrip)
1997 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1998
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001999
2000 return false;
2001}
2002
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002003/// ParseIdentifier:
2004/// ::= identifier
2005/// ::= string
2006bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002007 // The assembler has relaxed rules for accepting identifiers, in particular we
2008 // allow things like '.globl $foo', which would normally be separate
2009 // tokens. At this level, we have already lexed so we cannot (currently)
2010 // handle this as a context dependent token, instead we detect adjacent tokens
2011 // and return the combined identifier.
2012 if (Lexer.is(AsmToken::Dollar)) {
2013 SMLoc DollarLoc = getLexer().getLoc();
2014
2015 // Consume the dollar sign, and check for a following identifier.
2016 Lex();
2017 if (Lexer.isNot(AsmToken::Identifier))
2018 return true;
2019
2020 // We have a '$' followed by an identifier, make sure they are adjacent.
2021 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2022 return true;
2023
2024 // Construct the joined identifier and consume the token.
2025 Res = StringRef(DollarLoc.getPointer(),
2026 getTok().getIdentifier().size() + 1);
2027 Lex();
2028 return false;
2029 }
2030
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002031 if (Lexer.isNot(AsmToken::Identifier) &&
2032 Lexer.isNot(AsmToken::String))
2033 return true;
2034
Sean Callanan18b83232010-01-19 21:44:56 +00002035 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002036
Sean Callanan79ed1a82010-01-19 20:22:31 +00002037 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002038
2039 return false;
2040}
2041
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002042/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002043/// ::= .equ identifier ',' expression
2044/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002045/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002046bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002047 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002048
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002049 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002050 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002051
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002052 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002053 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002054 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002055
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002056 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002057}
2058
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002059bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002061
2062 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002063 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002064 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2065 if (Str[i] != '\\') {
2066 Data += Str[i];
2067 continue;
2068 }
2069
2070 // Recognize escaped characters. Note that this escape semantics currently
2071 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2072 ++i;
2073 if (i == e)
2074 return TokError("unexpected backslash at end of string");
2075
2076 // Recognize octal sequences.
2077 if ((unsigned) (Str[i] - '0') <= 7) {
2078 // Consume up to three octal characters.
2079 unsigned Value = Str[i] - '0';
2080
2081 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2082 ++i;
2083 Value = Value * 8 + (Str[i] - '0');
2084
2085 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2086 ++i;
2087 Value = Value * 8 + (Str[i] - '0');
2088 }
2089 }
2090
2091 if (Value > 255)
2092 return TokError("invalid octal escape sequence (out of range)");
2093
2094 Data += (unsigned char) Value;
2095 continue;
2096 }
2097
2098 // Otherwise recognize individual escapes.
2099 switch (Str[i]) {
2100 default:
2101 // Just reject invalid escape sequences for now.
2102 return TokError("invalid escape sequence (unrecognized character)");
2103
2104 case 'b': Data += '\b'; break;
2105 case 'f': Data += '\f'; break;
2106 case 'n': Data += '\n'; break;
2107 case 'r': Data += '\r'; break;
2108 case 't': Data += '\t'; break;
2109 case '"': Data += '"'; break;
2110 case '\\': Data += '\\'; break;
2111 }
2112 }
2113
2114 return false;
2115}
2116
Daniel Dunbara0d14262009-06-24 23:30:00 +00002117/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002118/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2119bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002120 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002121 CheckForValidSection();
2122
Daniel Dunbara0d14262009-06-24 23:30:00 +00002123 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002124 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002125 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002126
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002127 std::string Data;
2128 if (ParseEscapedString(Data))
2129 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002130
2131 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002132 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002133 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2134
Sean Callanan79ed1a82010-01-19 20:22:31 +00002135 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136
2137 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002138 break;
2139
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002140 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002141 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002143 }
2144 }
2145
Sean Callanan79ed1a82010-01-19 20:22:31 +00002146 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002147 return false;
2148}
2149
2150/// ParseDirectiveValue
2151/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2152bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002153 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002154 CheckForValidSection();
2155
Daniel Dunbara0d14262009-06-24 23:30:00 +00002156 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002157 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002158 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002159 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002160 return true;
2161
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002162 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002163 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2164 assert(Size <= 8 && "Invalid size");
2165 uint64_t IntValue = MCE->getValue();
2166 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2167 return Error(ExprLoc, "literal value out of range for directive");
2168 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2169 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002170 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002171
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002172 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002173 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002174
Daniel Dunbara0d14262009-06-24 23:30:00 +00002175 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002176 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002177 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002178 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002179 }
2180 }
2181
Sean Callanan79ed1a82010-01-19 20:22:31 +00002182 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002183 return false;
2184}
2185
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002186/// ParseDirectiveRealValue
2187/// ::= (.single | .double) [ expression (, expression)* ]
2188bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2189 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2190 CheckForValidSection();
2191
2192 for (;;) {
2193 // We don't truly support arithmetic on floating point expressions, so we
2194 // have to manually parse unary prefixes.
2195 bool IsNeg = false;
2196 if (getLexer().is(AsmToken::Minus)) {
2197 Lex();
2198 IsNeg = true;
2199 } else if (getLexer().is(AsmToken::Plus))
2200 Lex();
2201
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002202 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002203 getLexer().isNot(AsmToken::Real) &&
2204 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002205 return TokError("unexpected token in directive");
2206
2207 // Convert to an APFloat.
2208 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002209 StringRef IDVal = getTok().getString();
2210 if (getLexer().is(AsmToken::Identifier)) {
2211 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2212 Value = APFloat::getInf(Semantics);
2213 else if (!IDVal.compare_lower("nan"))
2214 Value = APFloat::getNaN(Semantics, false, ~0);
2215 else
2216 return TokError("invalid floating point literal");
2217 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002218 APFloat::opInvalidOp)
2219 return TokError("invalid floating point literal");
2220 if (IsNeg)
2221 Value.changeSign();
2222
2223 // Consume the numeric token.
2224 Lex();
2225
2226 // Emit the value as an integer.
2227 APInt AsInt = Value.bitcastToAPInt();
2228 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2229 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2230
2231 if (getLexer().is(AsmToken::EndOfStatement))
2232 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002233
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002234 if (getLexer().isNot(AsmToken::Comma))
2235 return TokError("unexpected token in directive");
2236 Lex();
2237 }
2238 }
2239
2240 Lex();
2241 return false;
2242}
2243
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002244/// ParseDirectiveZero
2245/// ::= .zero expression
2246bool AsmParser::ParseDirectiveZero() {
2247 CheckForValidSection();
2248
2249 int64_t NumBytes;
2250 if (ParseAbsoluteExpression(NumBytes))
2251 return true;
2252
Rafael Espindolae452b172010-10-05 19:42:57 +00002253 int64_t Val = 0;
2254 if (getLexer().is(AsmToken::Comma)) {
2255 Lex();
2256 if (ParseAbsoluteExpression(Val))
2257 return true;
2258 }
2259
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002260 if (getLexer().isNot(AsmToken::EndOfStatement))
2261 return TokError("unexpected token in '.zero' directive");
2262
2263 Lex();
2264
Rafael Espindolae452b172010-10-05 19:42:57 +00002265 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002266
2267 return false;
2268}
2269
Daniel Dunbara0d14262009-06-24 23:30:00 +00002270/// ParseDirectiveFill
2271/// ::= .fill expression , expression , expression
2272bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002273 CheckForValidSection();
2274
Daniel Dunbara0d14262009-06-24 23:30:00 +00002275 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002276 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002277 return true;
2278
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002279 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002280 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002281 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002282
Daniel Dunbara0d14262009-06-24 23:30:00 +00002283 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002284 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002285 return true;
2286
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002287 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002288 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002289 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002290
Daniel Dunbara0d14262009-06-24 23:30:00 +00002291 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002292 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002293 return true;
2294
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002295 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002296 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002297
Sean Callanan79ed1a82010-01-19 20:22:31 +00002298 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002299
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002300 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2301 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002302
2303 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002304 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002305
2306 return false;
2307}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002308
2309/// ParseDirectiveOrg
2310/// ::= .org expression [ , expression ]
2311bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002312 CheckForValidSection();
2313
Daniel Dunbar821e3332009-08-31 08:09:28 +00002314 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002315 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002316 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002317 return true;
2318
2319 // Parse optional fill expression.
2320 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002321 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2322 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002323 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002324 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002325
Daniel Dunbar475839e2009-06-29 20:37:27 +00002326 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002327 return true;
2328
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002329 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002330 return TokError("unexpected token in '.org' directive");
2331 }
2332
Sean Callanan79ed1a82010-01-19 20:22:31 +00002333 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002334
Jim Grosbachebd4c052012-01-27 00:37:08 +00002335 // Only limited forms of relocatable expressions are accepted here, it
2336 // has to be relative to the current section. The streamer will return
2337 // 'true' if the expression wasn't evaluatable.
2338 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2339 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002340
2341 return false;
2342}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002343
2344/// ParseDirectiveAlign
2345/// ::= {.align, ...} expression [ , expression [ , expression ]]
2346bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002347 CheckForValidSection();
2348
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002349 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002350 int64_t Alignment;
2351 if (ParseAbsoluteExpression(Alignment))
2352 return true;
2353
2354 SMLoc MaxBytesLoc;
2355 bool HasFillExpr = false;
2356 int64_t FillExpr = 0;
2357 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002358 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2359 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002360 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002361 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002362
2363 // The fill expression can be omitted while specifying a maximum number of
2364 // alignment bytes, e.g:
2365 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002366 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002367 HasFillExpr = true;
2368 if (ParseAbsoluteExpression(FillExpr))
2369 return true;
2370 }
2371
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002372 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2373 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002374 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002375 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002376
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002377 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002378 if (ParseAbsoluteExpression(MaxBytesToFill))
2379 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002380
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002381 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002382 return TokError("unexpected token in directive");
2383 }
2384 }
2385
Sean Callanan79ed1a82010-01-19 20:22:31 +00002386 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002387
Daniel Dunbar648ac512010-05-17 21:54:30 +00002388 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002389 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002390
2391 // Compute alignment in bytes.
2392 if (IsPow2) {
2393 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002394 if (Alignment >= 32) {
2395 Error(AlignmentLoc, "invalid alignment value");
2396 Alignment = 31;
2397 }
2398
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002399 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002400 }
2401
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002402 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002403 if (MaxBytesLoc.isValid()) {
2404 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002405 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2406 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002407 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002408 }
2409
2410 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002411 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2412 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002413 MaxBytesToFill = 0;
2414 }
2415 }
2416
Daniel Dunbar648ac512010-05-17 21:54:30 +00002417 // Check whether we should use optimal code alignment for this .align
2418 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002419 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002420 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2421 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002422 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002423 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002424 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002425 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2426 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002427 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002428
2429 return false;
2430}
2431
Eli Bendersky4766ef42012-12-20 19:05:53 +00002432
2433/// ParseDirectiveBundleAlignMode
2434/// ::= {.bundle_align_mode} expression
2435bool AsmParser::ParseDirectiveBundleAlignMode() {
2436 CheckForValidSection();
2437
2438 // Expect a single argument: an expression that evaluates to a constant
2439 // in the inclusive range 0-30.
2440 SMLoc ExprLoc = getLexer().getLoc();
2441 int64_t AlignSizePow2;
2442 if (ParseAbsoluteExpression(AlignSizePow2))
2443 return true;
2444 else if (getLexer().isNot(AsmToken::EndOfStatement))
2445 return TokError("unexpected token after expression in"
2446 " '.bundle_align_mode' directive");
2447 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2448 return Error(ExprLoc,
2449 "invalid bundle alignment size (expected between 0 and 30)");
2450
2451 Lex();
2452
2453 // Because of AlignSizePow2's verified range we can safely truncate it to
2454 // unsigned.
2455 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2456 return false;
2457}
2458
2459/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002460/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002461bool AsmParser::ParseDirectiveBundleLock() {
2462 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002463 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002464
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002465 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2466 StringRef Option;
2467 SMLoc Loc = getTok().getLoc();
2468 const char *kInvalidOptionError =
2469 "invalid option for '.bundle_lock' directive";
2470
2471 if (ParseIdentifier(Option))
2472 return Error(Loc, kInvalidOptionError);
2473
2474 if (Option != "align_to_end")
2475 return Error(Loc, kInvalidOptionError);
2476 else if (getLexer().isNot(AsmToken::EndOfStatement))
2477 return Error(Loc,
2478 "unexpected token after '.bundle_lock' directive option");
2479 AlignToEnd = true;
2480 }
2481
Eli Bendersky4766ef42012-12-20 19:05:53 +00002482 Lex();
2483
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002484 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002485 return false;
2486}
2487
2488/// ParseDirectiveBundleLock
2489/// ::= {.bundle_lock}
2490bool AsmParser::ParseDirectiveBundleUnlock() {
2491 CheckForValidSection();
2492
2493 if (getLexer().isNot(AsmToken::EndOfStatement))
2494 return TokError("unexpected token in '.bundle_unlock' directive");
2495 Lex();
2496
2497 getStreamer().EmitBundleUnlock();
2498 return false;
2499}
2500
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002501/// ParseDirectiveSymbolAttribute
2502/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002503bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002504 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002505 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002506 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002507 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002508
2509 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002510 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002511
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002512 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002513
Jim Grosbach10ec6502011-09-15 17:56:49 +00002514 // Assembler local symbols don't make any sense here. Complain loudly.
2515 if (Sym->isTemporary())
2516 return Error(Loc, "non-local symbol required in directive");
2517
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002518 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002519
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002520 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002521 break;
2522
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002523 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002524 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002525 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002526 }
2527 }
2528
Sean Callanan79ed1a82010-01-19 20:22:31 +00002529 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002530 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002531}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002532
2533/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002534/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2535bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002536 CheckForValidSection();
2537
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002538 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002539 StringRef Name;
2540 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002541 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002542
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002543 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002544 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002545
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002546 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002547 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002548 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002549
2550 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002551 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002552 if (ParseAbsoluteExpression(Size))
2553 return true;
2554
2555 int64_t Pow2Alignment = 0;
2556 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002557 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002558 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002559 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002560 if (ParseAbsoluteExpression(Pow2Alignment))
2561 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002562
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002563 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2564 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002565 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2566
Chris Lattner258281d2010-01-19 06:22:22 +00002567 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002568 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2569 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002570 if (!isPowerOf2_64(Pow2Alignment))
2571 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2572 Pow2Alignment = Log2_64(Pow2Alignment);
2573 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002574 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002575
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002576 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002577 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002578
Sean Callanan79ed1a82010-01-19 20:22:31 +00002579 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002580
Chris Lattner1fc3d752009-07-09 17:25:12 +00002581 // NOTE: a size of zero for a .comm should create a undefined symbol
2582 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002583 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002584 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2585 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002586
Eric Christopherc260a3e2010-05-14 01:38:54 +00002587 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002588 // may internally end up wanting an alignment in bytes.
2589 // FIXME: Diagnose overflow.
2590 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002591 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2592 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002593
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002594 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002595 return Error(IDLoc, "invalid symbol redefinition");
2596
Chris Lattner1fc3d752009-07-09 17:25:12 +00002597 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002598 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002599 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002600 return false;
2601 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002602
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002603 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002604 return false;
2605}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002606
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002607/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002608/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002609bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002610 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002611 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002612
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002613 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002614 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002615 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002616
Sean Callanan79ed1a82010-01-19 20:22:31 +00002617 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002618
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002619 if (Str.empty())
2620 Error(Loc, ".abort detected. Assembly stopping.");
2621 else
2622 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002623 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002624
2625 return false;
2626}
Kevin Enderby71148242009-07-14 21:35:03 +00002627
Kevin Enderby1f049b22009-07-14 23:21:55 +00002628/// ParseDirectiveInclude
2629/// ::= .include "filename"
2630bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002631 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002632 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002633
Sean Callanan18b83232010-01-19 21:44:56 +00002634 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002635 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002636 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002637
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002638 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002639 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002640
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002641 // Strip the quotes.
2642 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002643
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002644 // Attempt to switch the lexer to the included file before consuming the end
2645 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002646 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002647 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002648 return true;
2649 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002650
2651 return false;
2652}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002653
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002654/// ParseDirectiveIncbin
2655/// ::= .incbin "filename"
2656bool AsmParser::ParseDirectiveIncbin() {
2657 if (getLexer().isNot(AsmToken::String))
2658 return TokError("expected string in '.incbin' directive");
2659
2660 std::string Filename = getTok().getString();
2661 SMLoc IncbinLoc = getLexer().getLoc();
2662 Lex();
2663
2664 if (getLexer().isNot(AsmToken::EndOfStatement))
2665 return TokError("unexpected token in '.incbin' directive");
2666
2667 // Strip the quotes.
2668 Filename = Filename.substr(1, Filename.size()-2);
2669
2670 // Attempt to process the included file.
2671 if (ProcessIncbinFile(Filename)) {
2672 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2673 return true;
2674 }
2675
2676 return false;
2677}
2678
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002679/// ParseDirectiveIf
2680/// ::= .if expression
2681bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002682 TheCondStack.push_back(TheCondState);
2683 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002684 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002685 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002686 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002687 int64_t ExprValue;
2688 if (ParseAbsoluteExpression(ExprValue))
2689 return true;
2690
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002691 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002692 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002693
Sean Callanan79ed1a82010-01-19 20:22:31 +00002694 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002695
2696 TheCondState.CondMet = ExprValue;
2697 TheCondState.Ignore = !TheCondState.CondMet;
2698 }
2699
2700 return false;
2701}
2702
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002703/// ParseDirectiveIfb
2704/// ::= .ifb string
2705bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2706 TheCondStack.push_back(TheCondState);
2707 TheCondState.TheCond = AsmCond::IfCond;
2708
Benjamin Kramer29739e72012-05-12 16:52:21 +00002709 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002710 EatToEndOfStatement();
2711 } else {
2712 StringRef Str = ParseStringToEndOfStatement();
2713
2714 if (getLexer().isNot(AsmToken::EndOfStatement))
2715 return TokError("unexpected token in '.ifb' directive");
2716
2717 Lex();
2718
2719 TheCondState.CondMet = ExpectBlank == Str.empty();
2720 TheCondState.Ignore = !TheCondState.CondMet;
2721 }
2722
2723 return false;
2724}
2725
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002726/// ParseDirectiveIfc
2727/// ::= .ifc string1, string2
2728bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2729 TheCondStack.push_back(TheCondState);
2730 TheCondState.TheCond = AsmCond::IfCond;
2731
Benjamin Kramer29739e72012-05-12 16:52:21 +00002732 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002733 EatToEndOfStatement();
2734 } else {
2735 StringRef Str1 = ParseStringToComma();
2736
2737 if (getLexer().isNot(AsmToken::Comma))
2738 return TokError("unexpected token in '.ifc' directive");
2739
2740 Lex();
2741
2742 StringRef Str2 = ParseStringToEndOfStatement();
2743
2744 if (getLexer().isNot(AsmToken::EndOfStatement))
2745 return TokError("unexpected token in '.ifc' directive");
2746
2747 Lex();
2748
2749 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2750 TheCondState.Ignore = !TheCondState.CondMet;
2751 }
2752
2753 return false;
2754}
2755
2756/// ParseDirectiveIfdef
2757/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002758bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2759 StringRef Name;
2760 TheCondStack.push_back(TheCondState);
2761 TheCondState.TheCond = AsmCond::IfCond;
2762
2763 if (TheCondState.Ignore) {
2764 EatToEndOfStatement();
2765 } else {
2766 if (ParseIdentifier(Name))
2767 return TokError("expected identifier after '.ifdef'");
2768
2769 Lex();
2770
2771 MCSymbol *Sym = getContext().LookupSymbol(Name);
2772
2773 if (expect_defined)
2774 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2775 else
2776 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2777 TheCondState.Ignore = !TheCondState.CondMet;
2778 }
2779
2780 return false;
2781}
2782
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002783/// ParseDirectiveElseIf
2784/// ::= .elseif expression
2785bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2786 if (TheCondState.TheCond != AsmCond::IfCond &&
2787 TheCondState.TheCond != AsmCond::ElseIfCond)
2788 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2789 " an .elseif");
2790 TheCondState.TheCond = AsmCond::ElseIfCond;
2791
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002792 bool LastIgnoreState = false;
2793 if (!TheCondStack.empty())
2794 LastIgnoreState = TheCondStack.back().Ignore;
2795 if (LastIgnoreState || TheCondState.CondMet) {
2796 TheCondState.Ignore = true;
2797 EatToEndOfStatement();
2798 }
2799 else {
2800 int64_t ExprValue;
2801 if (ParseAbsoluteExpression(ExprValue))
2802 return true;
2803
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002804 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002805 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002806
Sean Callanan79ed1a82010-01-19 20:22:31 +00002807 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002808 TheCondState.CondMet = ExprValue;
2809 TheCondState.Ignore = !TheCondState.CondMet;
2810 }
2811
2812 return false;
2813}
2814
2815/// ParseDirectiveElse
2816/// ::= .else
2817bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002818 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002819 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002820
Sean Callanan79ed1a82010-01-19 20:22:31 +00002821 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002822
2823 if (TheCondState.TheCond != AsmCond::IfCond &&
2824 TheCondState.TheCond != AsmCond::ElseIfCond)
2825 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2826 ".elseif");
2827 TheCondState.TheCond = AsmCond::ElseCond;
2828 bool LastIgnoreState = false;
2829 if (!TheCondStack.empty())
2830 LastIgnoreState = TheCondStack.back().Ignore;
2831 if (LastIgnoreState || TheCondState.CondMet)
2832 TheCondState.Ignore = true;
2833 else
2834 TheCondState.Ignore = false;
2835
2836 return false;
2837}
2838
2839/// ParseDirectiveEndIf
2840/// ::= .endif
2841bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002842 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002843 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002844
Sean Callanan79ed1a82010-01-19 20:22:31 +00002845 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002846
2847 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2848 TheCondStack.empty())
2849 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2850 ".else");
2851 if (!TheCondStack.empty()) {
2852 TheCondState = TheCondStack.back();
2853 TheCondStack.pop_back();
2854 }
2855
2856 return false;
2857}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002858
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002859void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002860 DirectiveKindMapping[".set"] = DK_SET;
2861 DirectiveKindMapping[".equ"] = DK_EQU;
2862 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2863 DirectiveKindMapping[".ascii"] = DK_ASCII;
2864 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2865 DirectiveKindMapping[".string"] = DK_STRING;
2866 DirectiveKindMapping[".byte"] = DK_BYTE;
2867 DirectiveKindMapping[".short"] = DK_SHORT;
2868 DirectiveKindMapping[".value"] = DK_VALUE;
2869 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2870 DirectiveKindMapping[".long"] = DK_LONG;
2871 DirectiveKindMapping[".int"] = DK_INT;
2872 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2873 DirectiveKindMapping[".quad"] = DK_QUAD;
2874 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2875 DirectiveKindMapping[".single"] = DK_SINGLE;
2876 DirectiveKindMapping[".float"] = DK_FLOAT;
2877 DirectiveKindMapping[".double"] = DK_DOUBLE;
2878 DirectiveKindMapping[".align"] = DK_ALIGN;
2879 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2880 DirectiveKindMapping[".balign"] = DK_BALIGN;
2881 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2882 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2883 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2884 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2885 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2886 DirectiveKindMapping[".org"] = DK_ORG;
2887 DirectiveKindMapping[".fill"] = DK_FILL;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002888 DirectiveKindMapping[".zero"] = DK_ZERO;
2889 DirectiveKindMapping[".extern"] = DK_EXTERN;
2890 DirectiveKindMapping[".globl"] = DK_GLOBL;
2891 DirectiveKindMapping[".global"] = DK_GLOBAL;
2892 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2893 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2894 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2895 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2896 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2897 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2898 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2899 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2900 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2901 DirectiveKindMapping[".comm"] = DK_COMM;
2902 DirectiveKindMapping[".common"] = DK_COMMON;
2903 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2904 DirectiveKindMapping[".abort"] = DK_ABORT;
2905 DirectiveKindMapping[".include"] = DK_INCLUDE;
2906 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2907 DirectiveKindMapping[".code16"] = DK_CODE16;
2908 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2909 DirectiveKindMapping[".rept"] = DK_REPT;
2910 DirectiveKindMapping[".irp"] = DK_IRP;
2911 DirectiveKindMapping[".irpc"] = DK_IRPC;
2912 DirectiveKindMapping[".endr"] = DK_ENDR;
2913 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2914 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2915 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2916 DirectiveKindMapping[".if"] = DK_IF;
2917 DirectiveKindMapping[".ifb"] = DK_IFB;
2918 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2919 DirectiveKindMapping[".ifc"] = DK_IFC;
2920 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2921 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2922 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2923 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2924 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2925 DirectiveKindMapping[".else"] = DK_ELSE;
2926 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002927}
2928
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002929/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002930/// ::= .file [number] filename
2931/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002932bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002933 // FIXME: I'm not sure what this is.
2934 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002935 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002936 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002937 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002938 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002939
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002940 if (FileNumber < 1)
2941 return TokError("file number less than one");
2942 }
2943
Daniel Dunbareceec052010-07-12 17:45:27 +00002944 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002945 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002946
Nick Lewycky44d798d2011-10-17 23:05:28 +00002947 // Usually the directory and filename together, otherwise just the directory.
2948 StringRef Path = getTok().getString();
2949 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002950 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002951
Nick Lewycky44d798d2011-10-17 23:05:28 +00002952 StringRef Directory;
2953 StringRef Filename;
2954 if (getLexer().is(AsmToken::String)) {
2955 if (FileNumber == -1)
2956 return TokError("explicit path specified, but no file number");
2957 Filename = getTok().getString();
2958 Filename = Filename.substr(1, Filename.size()-2);
2959 Directory = Path;
2960 Lex();
2961 } else {
2962 Filename = Path;
2963 }
2964
Daniel Dunbareceec052010-07-12 17:45:27 +00002965 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002966 return TokError("unexpected token in '.file' directive");
2967
Chris Lattnerd32e8032010-01-25 19:02:58 +00002968 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002969 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002970 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002971 if (getContext().getGenDwarfForAssembly() == true)
2972 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2973 "used to generate dwarf debug info for assembly code");
2974
Nick Lewycky44d798d2011-10-17 23:05:28 +00002975 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002976 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002977 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002978
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002979 return false;
2980}
2981
2982/// ParseDirectiveLine
2983/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002984bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002985 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2986 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002987 return TokError("unexpected token in '.line' directive");
2988
Sean Callanan18b83232010-01-19 21:44:56 +00002989 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002990 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002991 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002992
2993 // FIXME: Do something with the .line.
2994 }
2995
Daniel Dunbareceec052010-07-12 17:45:27 +00002996 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002997 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002998
2999 return false;
3000}
3001
3002
3003/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003004/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003005/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3006/// The first number is a file number, must have been previously assigned with
3007/// a .file directive, the second number is the line number and optionally the
3008/// third number is a column position (zero if not specified). The remaining
3009/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003010bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003011
Daniel Dunbareceec052010-07-12 17:45:27 +00003012 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003013 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003014 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003015 if (FileNumber < 1)
3016 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003017 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003018 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003019 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003020
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003021 int64_t LineNumber = 0;
3022 if (getLexer().is(AsmToken::Integer)) {
3023 LineNumber = getTok().getIntVal();
3024 if (LineNumber < 1)
3025 return TokError("line number less than one in '.loc' directive");
3026 Lex();
3027 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003028
3029 int64_t ColumnPos = 0;
3030 if (getLexer().is(AsmToken::Integer)) {
3031 ColumnPos = getTok().getIntVal();
3032 if (ColumnPos < 0)
3033 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003034 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003035 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003036
Kevin Enderbyc0957932010-09-30 16:52:03 +00003037 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003038 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003039 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003040 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3041 for (;;) {
3042 if (getLexer().is(AsmToken::EndOfStatement))
3043 break;
3044
3045 StringRef Name;
3046 SMLoc Loc = getTok().getLoc();
3047 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003048 return TokError("unexpected token in '.loc' directive");
3049
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003050 if (Name == "basic_block")
3051 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3052 else if (Name == "prologue_end")
3053 Flags |= DWARF2_FLAG_PROLOGUE_END;
3054 else if (Name == "epilogue_begin")
3055 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3056 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003057 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003058 const MCExpr *Value;
3059 if (getParser().ParseExpression(Value))
3060 return true;
3061 // The expression must be the constant 0 or 1.
3062 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3063 int Value = MCE->getValue();
3064 if (Value == 0)
3065 Flags &= ~DWARF2_FLAG_IS_STMT;
3066 else if (Value == 1)
3067 Flags |= DWARF2_FLAG_IS_STMT;
3068 else
3069 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003070 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003071 else {
3072 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3073 }
3074 }
3075 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003076 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003077 const MCExpr *Value;
3078 if (getParser().ParseExpression(Value))
3079 return true;
3080 // The expression must be a constant greater or equal to 0.
3081 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3082 int Value = MCE->getValue();
3083 if (Value < 0)
3084 return Error(Loc, "isa number less than zero");
3085 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003086 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003087 else {
3088 return Error(Loc, "isa number not a constant value");
3089 }
3090 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003091 else if (Name == "discriminator") {
3092 if (getParser().ParseAbsoluteExpression(Discriminator))
3093 return true;
3094 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003095 else {
3096 return Error(Loc, "unknown sub-directive in '.loc' directive");
3097 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003098
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003099 if (getLexer().is(AsmToken::EndOfStatement))
3100 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003101 }
3102 }
3103
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003104 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003105 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003106
3107 return false;
3108}
3109
Daniel Dunbar138abae2010-10-16 04:56:42 +00003110/// ParseDirectiveStabs
3111/// ::= .stabs string, number, number, number
3112bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3113 SMLoc DirectiveLoc) {
3114 return TokError("unsupported directive '" + Directive + "'");
3115}
3116
Eli Bendersky9b1bb052013-01-11 22:55:28 +00003117/// ParseDirectiveSpace
3118/// ::= .space expression [ , expression ]
3119bool GenericAsmParser::ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc) {
3120 getParser().CheckForValidSection();
3121
3122 int64_t NumBytes;
3123 if (getParser().ParseAbsoluteExpression(NumBytes))
3124 return true;
3125
3126 int64_t FillExpr = 0;
3127 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3128 if (getLexer().isNot(AsmToken::Comma))
3129 return TokError("unexpected token in '.space' directive");
3130 Lex();
3131
3132 if (getParser().ParseAbsoluteExpression(FillExpr))
3133 return true;
3134
3135 if (getLexer().isNot(AsmToken::EndOfStatement))
3136 return TokError("unexpected token in '.space' directive");
3137 }
3138
3139 Lex();
3140
3141 if (NumBytes <= 0)
3142 return TokError("invalid number of bytes in '.space' directive");
3143
3144 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3145 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
3146
3147 return false;
3148}
3149
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003150/// ParseDirectiveCFISections
3151/// ::= .cfi_sections section [, section]
3152bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3153 SMLoc DirectiveLoc) {
3154 StringRef Name;
3155 bool EH = false;
3156 bool Debug = false;
3157
3158 if (getParser().ParseIdentifier(Name))
3159 return TokError("Expected an identifier");
3160
3161 if (Name == ".eh_frame")
3162 EH = true;
3163 else if (Name == ".debug_frame")
3164 Debug = true;
3165
3166 if (getLexer().is(AsmToken::Comma)) {
3167 Lex();
3168
3169 if (getParser().ParseIdentifier(Name))
3170 return TokError("Expected an identifier");
3171
3172 if (Name == ".eh_frame")
3173 EH = true;
3174 else if (Name == ".debug_frame")
3175 Debug = true;
3176 }
3177
3178 getStreamer().EmitCFISections(EH, Debug);
3179
3180 return false;
3181}
3182
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003183/// ParseDirectiveCFIStartProc
3184/// ::= .cfi_startproc
3185bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3186 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003187 getStreamer().EmitCFIStartProc();
3188 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003189}
3190
3191/// ParseDirectiveCFIEndProc
3192/// ::= .cfi_endproc
3193bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003194 getStreamer().EmitCFIEndProc();
3195 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003196}
3197
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003198/// ParseRegisterOrRegisterNumber - parse register name or number.
3199bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3200 SMLoc DirectiveLoc) {
3201 unsigned RegNo;
3202
Jim Grosbach6f888a82011-06-02 17:14:04 +00003203 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003204 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3205 DirectiveLoc))
3206 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003207 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003208 } else
3209 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003210
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003211 return false;
3212}
3213
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003214/// ParseDirectiveCFIDefCfa
3215/// ::= .cfi_def_cfa register, offset
3216bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3217 SMLoc DirectiveLoc) {
3218 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003219 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003220 return true;
3221
3222 if (getLexer().isNot(AsmToken::Comma))
3223 return TokError("unexpected token in directive");
3224 Lex();
3225
3226 int64_t Offset = 0;
3227 if (getParser().ParseAbsoluteExpression(Offset))
3228 return true;
3229
Rafael Espindola066c2f42011-04-12 23:59:07 +00003230 getStreamer().EmitCFIDefCfa(Register, Offset);
3231 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003232}
3233
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003234/// ParseDirectiveCFIDefCfaOffset
3235/// ::= .cfi_def_cfa_offset offset
3236bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3237 SMLoc DirectiveLoc) {
3238 int64_t Offset = 0;
3239 if (getParser().ParseAbsoluteExpression(Offset))
3240 return true;
3241
Rafael Espindola066c2f42011-04-12 23:59:07 +00003242 getStreamer().EmitCFIDefCfaOffset(Offset);
3243 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003244}
3245
3246/// ParseDirectiveCFIAdjustCfaOffset
3247/// ::= .cfi_adjust_cfa_offset adjustment
3248bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3249 SMLoc DirectiveLoc) {
3250 int64_t Adjustment = 0;
3251 if (getParser().ParseAbsoluteExpression(Adjustment))
3252 return true;
3253
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003254 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3255 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003256}
3257
3258/// ParseDirectiveCFIDefCfaRegister
3259/// ::= .cfi_def_cfa_register register
3260bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3261 SMLoc DirectiveLoc) {
3262 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003263 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003264 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003265
Rafael Espindola066c2f42011-04-12 23:59:07 +00003266 getStreamer().EmitCFIDefCfaRegister(Register);
3267 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003268}
3269
3270/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003271/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003272bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3273 int64_t Register = 0;
3274 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003275
3276 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003277 return true;
3278
3279 if (getLexer().isNot(AsmToken::Comma))
3280 return TokError("unexpected token in directive");
3281 Lex();
3282
3283 if (getParser().ParseAbsoluteExpression(Offset))
3284 return true;
3285
Rafael Espindola066c2f42011-04-12 23:59:07 +00003286 getStreamer().EmitCFIOffset(Register, Offset);
3287 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003288}
3289
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003290/// ParseDirectiveCFIRelOffset
3291/// ::= .cfi_rel_offset register, offset
3292bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3293 SMLoc DirectiveLoc) {
3294 int64_t Register = 0;
3295
3296 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3297 return true;
3298
3299 if (getLexer().isNot(AsmToken::Comma))
3300 return TokError("unexpected token in directive");
3301 Lex();
3302
3303 int64_t Offset = 0;
3304 if (getParser().ParseAbsoluteExpression(Offset))
3305 return true;
3306
Rafael Espindola25f492e2011-04-12 16:12:03 +00003307 getStreamer().EmitCFIRelOffset(Register, Offset);
3308 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003309}
3310
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003311static bool isValidEncoding(int64_t Encoding) {
3312 if (Encoding & ~0xff)
3313 return false;
3314
3315 if (Encoding == dwarf::DW_EH_PE_omit)
3316 return true;
3317
3318 const unsigned Format = Encoding & 0xf;
3319 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3320 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3321 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3322 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3323 return false;
3324
Rafael Espindolacaf11582010-12-29 04:31:26 +00003325 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003326 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003327 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003328 return false;
3329
3330 return true;
3331}
3332
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003333/// ParseDirectiveCFIPersonalityOrLsda
3334/// ::= .cfi_personality encoding, [symbol_name]
3335/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003336bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003337 SMLoc DirectiveLoc) {
3338 int64_t Encoding = 0;
3339 if (getParser().ParseAbsoluteExpression(Encoding))
3340 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003341 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003342 return false;
3343
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003344 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003345 return TokError("unsupported encoding.");
3346
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003347 if (getLexer().isNot(AsmToken::Comma))
3348 return TokError("unexpected token in directive");
3349 Lex();
3350
3351 StringRef Name;
3352 if (getParser().ParseIdentifier(Name))
3353 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003354
3355 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3356
3357 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003358 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003359 else {
3360 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003361 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003362 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003363 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003364}
3365
Rafael Espindolafe024d02010-12-28 18:36:23 +00003366/// ParseDirectiveCFIRememberState
3367/// ::= .cfi_remember_state
3368bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3369 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003370 getStreamer().EmitCFIRememberState();
3371 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003372}
3373
3374/// ParseDirectiveCFIRestoreState
3375/// ::= .cfi_remember_state
3376bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3377 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003378 getStreamer().EmitCFIRestoreState();
3379 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003380}
3381
Rafael Espindolac5754392011-04-12 15:31:05 +00003382/// ParseDirectiveCFISameValue
3383/// ::= .cfi_same_value register
3384bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3385 SMLoc DirectiveLoc) {
3386 int64_t Register = 0;
3387
3388 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3389 return true;
3390
3391 getStreamer().EmitCFISameValue(Register);
3392
3393 return false;
3394}
3395
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003396/// ParseDirectiveCFIRestore
3397/// ::= .cfi_restore register
3398bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003399 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003400 int64_t Register = 0;
3401 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3402 return true;
3403
3404 getStreamer().EmitCFIRestore(Register);
3405
3406 return false;
3407}
3408
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003409/// ParseDirectiveCFIEscape
3410/// ::= .cfi_escape expression[,...]
3411bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003412 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003413 std::string Values;
3414 int64_t CurrValue;
3415 if (getParser().ParseAbsoluteExpression(CurrValue))
3416 return true;
3417
3418 Values.push_back((uint8_t)CurrValue);
3419
3420 while (getLexer().is(AsmToken::Comma)) {
3421 Lex();
3422
3423 if (getParser().ParseAbsoluteExpression(CurrValue))
3424 return true;
3425
3426 Values.push_back((uint8_t)CurrValue);
3427 }
3428
3429 getStreamer().EmitCFIEscape(Values);
3430 return false;
3431}
3432
Rafael Espindola16d7d432012-01-23 21:51:52 +00003433/// ParseDirectiveCFISignalFrame
3434/// ::= .cfi_signal_frame
3435bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3436 SMLoc DirectiveLoc) {
3437 if (getLexer().isNot(AsmToken::EndOfStatement))
3438 return Error(getLexer().getLoc(),
3439 "unexpected token in '" + Directive + "' directive");
3440
3441 getStreamer().EmitCFISignalFrame();
3442
3443 return false;
3444}
3445
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003446/// ParseDirectiveCFIUndefined
3447/// ::= .cfi_undefined register
3448bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3449 SMLoc DirectiveLoc) {
3450 int64_t Register = 0;
3451
3452 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3453 return true;
3454
3455 getStreamer().EmitCFIUndefined(Register);
3456
3457 return false;
3458}
3459
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003460/// ParseDirectiveCFIRegister
3461/// ::= .cfi_register register, register
3462bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3463 SMLoc DirectiveLoc) {
3464 int64_t Register1 = 0;
3465
3466 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3467 return true;
3468
3469 if (getLexer().isNot(AsmToken::Comma))
3470 return TokError("unexpected token in directive");
3471 Lex();
3472
3473 int64_t Register2 = 0;
3474
3475 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3476 return true;
3477
3478 getStreamer().EmitCFIRegister(Register1, Register2);
3479
3480 return false;
3481}
3482
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003483/// ParseDirectiveMacrosOnOff
3484/// ::= .macros_on
3485/// ::= .macros_off
3486bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3487 SMLoc DirectiveLoc) {
3488 if (getLexer().isNot(AsmToken::EndOfStatement))
3489 return Error(getLexer().getLoc(),
3490 "unexpected token in '" + Directive + "' directive");
3491
Eli Bendersky733c3362013-01-14 18:08:41 +00003492 getParser().SetMacrosEnabled(Directive == ".macros_on");
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003493
3494 return false;
3495}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003496
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003497/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003498/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003499bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3500 SMLoc DirectiveLoc) {
3501 StringRef Name;
3502 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003503 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003504
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003505 MCAsmMacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003506 // Argument delimiter is initially unknown. It will be set by
3507 // ParseMacroArgument()
3508 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003509 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003510 for (;;) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003511 MCAsmMacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003512 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003513 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003514
3515 if (getLexer().is(AsmToken::Equal)) {
3516 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003517 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003518 return true;
3519 }
3520
Rafael Espindola65366442011-06-05 02:43:45 +00003521 Parameters.push_back(Parameter);
3522
Preston Gurd7b6f2032012-09-19 20:36:12 +00003523 if (getLexer().is(AsmToken::Comma))
3524 Lex();
3525 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003526 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003527 }
3528 }
3529
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003530 // Eat the end of statement.
3531 Lex();
3532
3533 AsmToken EndToken, StartToken = getTok();
3534
3535 // Lex the macro definition.
3536 for (;;) {
3537 // Check whether we have reached the end of the file.
3538 if (getLexer().is(AsmToken::Eof))
3539 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3540
3541 // Otherwise, check whether we have reach the .endmacro.
3542 if (getLexer().is(AsmToken::Identifier) &&
3543 (getTok().getIdentifier() == ".endm" ||
3544 getTok().getIdentifier() == ".endmacro")) {
3545 EndToken = getTok();
3546 Lex();
3547 if (getLexer().isNot(AsmToken::EndOfStatement))
3548 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3549 "' directive");
3550 break;
3551 }
3552
3553 // Otherwise, scan til the end of the statement.
3554 getParser().EatToEndOfStatement();
3555 }
3556
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003557 if (getParser().LookupMacro(Name)) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003558 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3559 }
3560
3561 const char *BodyStart = StartToken.getLoc().getPointer();
3562 const char *BodyEnd = EndToken.getLoc().getPointer();
3563 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003564 getParser().DefineMacro(Name, MCAsmMacro(Name, Body, Parameters));
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003565 return false;
3566}
3567
3568/// ParseDirectiveEndMacro
3569/// ::= .endm
3570/// ::= .endmacro
3571bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003572 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003573 if (getLexer().isNot(AsmToken::EndOfStatement))
3574 return TokError("unexpected token in '" + Directive + "' directive");
3575
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003576 // If we are inside a macro instantiation, terminate the current
3577 // instantiation.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003578 if (getParser().InsideMacroInstantiation()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003579 getParser().HandleMacroExit();
3580 return false;
3581 }
3582
3583 // Otherwise, this .endmacro is a stray entry in the file; well formed
3584 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003585 return TokError("unexpected '" + Directive + "' in file, "
3586 "no current macro definition");
3587}
3588
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003589/// ParseDirectivePurgeMacro
3590/// ::= .purgem
3591bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3592 SMLoc DirectiveLoc) {
3593 StringRef Name;
3594 if (getParser().ParseIdentifier(Name))
3595 return TokError("expected identifier in '.purgem' directive");
3596
3597 if (getLexer().isNot(AsmToken::EndOfStatement))
3598 return TokError("unexpected token in '.purgem' directive");
3599
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003600 if (!getParser().LookupMacro(Name))
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003601 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3602
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003603 getParser().UndefineMacro(Name);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003604 return false;
3605}
3606
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003607bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003608 getParser().CheckForValidSection();
3609
3610 const MCExpr *Value;
3611
3612 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003613 return true;
3614
3615 if (getLexer().isNot(AsmToken::EndOfStatement))
3616 return TokError("unexpected token in directive");
3617
3618 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003619 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003620 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003621 getStreamer().EmitULEB128Value(Value);
3622
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003623 return false;
3624}
3625
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003626MCAsmMacro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003627 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003628
Rafael Espindola761cb062012-06-03 23:57:14 +00003629 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003630 for (;;) {
3631 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003632 if (getLexer().is(AsmToken::Eof)) {
3633 Error(DirectiveLoc, "no matching '.endr' in definition");
3634 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003635 }
3636
Rafael Espindola761cb062012-06-03 23:57:14 +00003637 if (Lexer.is(AsmToken::Identifier) &&
3638 (getTok().getIdentifier() == ".rept")) {
3639 ++NestLevel;
3640 }
3641
3642 // Otherwise, check whether we have reached the .endr.
3643 if (Lexer.is(AsmToken::Identifier) &&
3644 getTok().getIdentifier() == ".endr") {
3645 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003646 EndToken = getTok();
3647 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003648 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3649 TokError("unexpected token in '.endr' directive");
3650 return 0;
3651 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003652 break;
3653 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003654 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003655 }
3656
Rafael Espindola761cb062012-06-03 23:57:14 +00003657 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003658 EatToEndOfStatement();
3659 }
3660
3661 const char *BodyStart = StartToken.getLoc().getPointer();
3662 const char *BodyEnd = EndToken.getLoc().getPointer();
3663 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3664
Rafael Espindola761cb062012-06-03 23:57:14 +00003665 // We Are Anonymous.
3666 StringRef Name;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003667 MCAsmMacroParameters Parameters;
3668 return new MCAsmMacro(Name, Body, Parameters);
Rafael Espindola761cb062012-06-03 23:57:14 +00003669}
3670
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003671void AsmParser::InstantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
Rafael Espindola761cb062012-06-03 23:57:14 +00003672 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003673 OS << ".endr\n";
3674
3675 MemoryBuffer *Instantiation =
3676 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3677
Rafael Espindola761cb062012-06-03 23:57:14 +00003678 // Create the macro instantiation object and add to the current macro
3679 // instantiation stack.
3680 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00003681 CurBuffer,
Rafael Espindola761cb062012-06-03 23:57:14 +00003682 getTok().getLoc(),
3683 Instantiation);
3684 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003685
Rafael Espindola761cb062012-06-03 23:57:14 +00003686 // Jump to the macro instantiation and prime the lexer.
3687 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3688 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3689 Lex();
3690}
3691
3692bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3693 int64_t Count;
3694 if (ParseAbsoluteExpression(Count))
3695 return TokError("unexpected token in '.rept' directive");
3696
3697 if (Count < 0)
3698 return TokError("Count is negative");
3699
3700 if (Lexer.isNot(AsmToken::EndOfStatement))
3701 return TokError("unexpected token in '.rept' directive");
3702
3703 // Eat the end of statement.
3704 Lex();
3705
3706 // Lex the rept definition.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003707 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc);
Rafael Espindola761cb062012-06-03 23:57:14 +00003708 if (!M)
3709 return true;
3710
3711 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3712 // to hold the macro body with substitutions.
3713 SmallString<256> Buf;
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003714 MCAsmMacroParameters Parameters;
3715 MCAsmMacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003716 raw_svector_ostream OS(Buf);
3717 while (Count--) {
3718 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3719 return true;
3720 }
3721 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003722
3723 return false;
3724}
3725
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003726/// ParseDirectiveIrp
3727/// ::= .irp symbol,values
3728bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003729 MCAsmMacroParameters Parameters;
3730 MCAsmMacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003731
Preston Gurd6c9176a2012-09-19 20:29:04 +00003732 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003733 return TokError("expected identifier in '.irp' directive");
3734
3735 Parameters.push_back(Parameter);
3736
3737 if (Lexer.isNot(AsmToken::Comma))
3738 return TokError("expected comma in '.irp' directive");
3739
3740 Lex();
3741
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003742 MCAsmMacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003743 if (ParseMacroArguments(0, A))
3744 return true;
3745
3746 // Eat the end of statement.
3747 Lex();
3748
3749 // Lex the irp definition.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003750 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003751 if (!M)
3752 return true;
3753
3754 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3755 // to hold the macro body with substitutions.
3756 SmallString<256> Buf;
3757 raw_svector_ostream OS(Buf);
3758
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003759 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3760 MCAsmMacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003761 Args.push_back(*i);
3762
3763 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3764 return true;
3765 }
3766
3767 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3768
3769 return false;
3770}
3771
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003772/// ParseDirectiveIrpc
3773/// ::= .irpc symbol,values
3774bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003775 MCAsmMacroParameters Parameters;
3776 MCAsmMacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003777
Preston Gurd6c9176a2012-09-19 20:29:04 +00003778 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003779 return TokError("expected identifier in '.irpc' directive");
3780
3781 Parameters.push_back(Parameter);
3782
3783 if (Lexer.isNot(AsmToken::Comma))
3784 return TokError("expected comma in '.irpc' directive");
3785
3786 Lex();
3787
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003788 MCAsmMacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003789 if (ParseMacroArguments(0, A))
3790 return true;
3791
3792 if (A.size() != 1 || A.front().size() != 1)
3793 return TokError("unexpected token in '.irpc' directive");
3794
3795 // Eat the end of statement.
3796 Lex();
3797
3798 // Lex the irpc definition.
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003799 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003800 if (!M)
3801 return true;
3802
3803 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3804 // to hold the macro body with substitutions.
3805 SmallString<256> Buf;
3806 raw_svector_ostream OS(Buf);
3807
3808 StringRef Values = A.front().front().getString();
3809 std::size_t I, End = Values.size();
3810 for (I = 0; I < End; ++I) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00003811 MCAsmMacroArgument Arg;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003812 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3813
Eli Benderskyc0c67b02013-01-14 23:22:36 +00003814 MCAsmMacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003815 Args.push_back(Arg);
3816
3817 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3818 return true;
3819 }
3820
3821 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3822
3823 return false;
3824}
3825
Rafael Espindola761cb062012-06-03 23:57:14 +00003826bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3827 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003828 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003829
3830 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003831 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003832 assert(getLexer().is(AsmToken::EndOfStatement));
3833
Rafael Espindola761cb062012-06-03 23:57:14 +00003834 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003835 return false;
3836}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003837
Eli Friedman2128aae2012-10-22 23:58:19 +00003838bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) {
3839 const MCExpr *Value;
3840 SMLoc ExprLoc = getLexer().getLoc();
3841 if (ParseExpression(Value))
3842 return true;
3843 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3844 if (!MCE)
3845 return Error(ExprLoc, "unexpected expression in _emit");
3846 uint64_t IntValue = MCE->getValue();
3847 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
3848 return Error(ExprLoc, "literal value out of range for directive");
3849
3850 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5));
3851 return false;
3852}
3853
Chad Rosierb1f8c132012-10-18 15:49:34 +00003854bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3855 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003856 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003857 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003858 SmallVectorImpl<std::string> &Clobbers,
3859 const MCInstrInfo *MII,
3860 const MCInstPrinter *IP,
3861 MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003862 SmallVector<void *, 4> InputDecls;
3863 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003864 SmallVector<bool, 4> InputDeclsAddressOf;
3865 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003866 SmallVector<std::string, 4> InputConstraints;
3867 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003868 std::set<std::string> ClobberRegs;
3869
Chad Rosier4e472d22012-10-20 01:02:45 +00003870 SmallVector<struct AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003871
3872 // Prime the lexer.
3873 Lex();
3874
3875 // While we have input, parse each statement.
3876 unsigned InputIdx = 0;
3877 unsigned OutputIdx = 0;
3878 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00003879 ParseStatementInfo Info(&AsmStrRewrites);
3880 if (ParseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00003881 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003882
Chad Rosier57498012012-12-12 22:45:52 +00003883 if (Info.ParseError)
3884 return true;
3885
Eli Friedman2128aae2012-10-22 23:58:19 +00003886 if (Info.Opcode != ~0U) {
3887 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003888
3889 // Build the list of clobbers, outputs and inputs.
Eli Friedman2128aae2012-10-22 23:58:19 +00003890 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
3891 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003892
3893 // Immediate.
3894 if (Operand->isImm()) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00003895 if (Operand->needAsmRewrite())
3896 AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix,
3897 Operand->getStartLoc()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003898 continue;
3899 }
3900
3901 // Register operand.
Chad Rosierc1ec2072013-01-10 22:10:27 +00003902 if (Operand->isReg() && !Operand->needAddressOf()) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003903 unsigned NumDefs = Desc.getNumDefs();
3904 // Clobber.
3905 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3906 std::string Reg;
3907 raw_string_ostream OS(Reg);
3908 IP->printRegName(OS, Operand->getReg());
3909 ClobberRegs.insert(StringRef(OS.str()));
3910 }
3911 continue;
3912 }
3913
3914 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003915 unsigned Size;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003916 bool IsVarDecl;
Chad Rosier32989592012-10-18 20:27:15 +00003917 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +00003918 Size, IsVarDecl);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003919 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003920 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierc1ec2072013-01-10 22:10:27 +00003921 if (Operand->isMem() && Operand->needSizeDirective())
Chad Rosier4e472d22012-10-20 01:02:45 +00003922 AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective,
Chad Rosierefcb3d92012-10-26 18:04:20 +00003923 Operand->getStartLoc(),
3924 /*Len*/0,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003925 Operand->getMemSize()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003926 if (isOutput) {
3927 std::string Constraint = "=";
3928 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003929 OutputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003930 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003931 Constraint += Operand->getConstraint().str();
3932 OutputConstraints.push_back(Constraint);
Chad Rosier4e472d22012-10-20 01:02:45 +00003933 AsmStrRewrites.push_back(AsmRewrite(AOK_Output,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003934 Operand->getStartLoc(),
3935 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003936 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003937 InputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003938 InputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003939 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosier4e472d22012-10-20 01:02:45 +00003940 AsmStrRewrites.push_back(AsmRewrite(AOK_Input,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003941 Operand->getStartLoc(),
3942 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003943 }
3944 }
3945 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003946 }
3947 }
3948
3949 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003950 NumOutputs = OutputDecls.size();
3951 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003952
3953 // Set the unique clobbers.
3954 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3955 E = ClobberRegs.end(); I != E; ++I)
3956 Clobbers.push_back(*I);
3957
3958 // Merge the various outputs and inputs. Output are expected first.
3959 if (NumOutputs || NumInputs) {
3960 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003961 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003962 Constraints.resize(NumExprs);
Chad Rosier5a719fc2012-10-23 17:43:43 +00003963 // FIXME: Constraints are hard coded to 'm', but we need an 'r'
Chad Rosierc1ec2072013-01-10 22:10:27 +00003964 // constraint for addressof. This needs to be cleaned up!
Chad Rosierb1f8c132012-10-18 15:49:34 +00003965 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003966 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
3967 Constraints[i] = OutputDeclsAddressOf[i] ? "=r" : OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003968 }
3969 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003970 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
3971 Constraints[j] = InputDeclsAddressOf[i] ? "r" : InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003972 }
3973 }
3974
3975 // Build the IR assembly string.
3976 std::string AsmStringIR;
Chad Rosier4e472d22012-10-20 01:02:45 +00003977 AsmRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003978 raw_string_ostream OS(AsmStringIR);
3979 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
Chad Rosier4e472d22012-10-20 01:02:45 +00003980 for (SmallVectorImpl<struct AsmRewrite>::iterator
Chad Rosierb1f8c132012-10-18 15:49:34 +00003981 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3982 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003983
Chad Rosier4e472d22012-10-20 01:02:45 +00003984 AsmRewriteKind Kind = (*I).Kind;
Chad Rosier96d58e62012-10-19 20:57:14 +00003985
3986 // Emit everything up to the immediate/expression. If the previous rewrite
3987 // was a size directive, then this has already been done.
3988 if (PrevKind != AOK_SizeDirective)
3989 OS << StringRef(Start, Loc - Start);
3990 PrevKind = Kind;
3991
Chad Rosier5a719fc2012-10-23 17:43:43 +00003992 // Skip the original expression.
3993 if (Kind == AOK_Skip) {
3994 Start = Loc + (*I).Len;
3995 continue;
3996 }
3997
Chad Rosierb1f8c132012-10-18 15:49:34 +00003998 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00003999 switch (Kind) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004000 default: break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004001 case AOK_Imm:
Chad Rosierefcb3d92012-10-26 18:04:20 +00004002 OS << Twine("$$");
4003 OS << (*I).Val;
4004 break;
4005 case AOK_ImmPrefix:
4006 OS << Twine("$$");
Chad Rosierb1f8c132012-10-18 15:49:34 +00004007 break;
4008 case AOK_Input:
4009 OS << '$';
4010 OS << InputIdx++;
4011 break;
4012 case AOK_Output:
4013 OS << '$';
4014 OS << OutputIdx++;
4015 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004016 case AOK_SizeDirective:
Chad Rosier6a020a72012-10-25 20:41:34 +00004017 switch((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004018 default: break;
4019 case 8: OS << "byte ptr "; break;
4020 case 16: OS << "word ptr "; break;
4021 case 32: OS << "dword ptr "; break;
4022 case 64: OS << "qword ptr "; break;
4023 case 80: OS << "xword ptr "; break;
4024 case 128: OS << "xmmword ptr "; break;
4025 case 256: OS << "ymmword ptr "; break;
4026 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004027 break;
4028 case AOK_Emit:
4029 OS << ".byte";
4030 break;
Chad Rosier6a020a72012-10-25 20:41:34 +00004031 case AOK_DotOperator:
4032 OS << (*I).Val;
4033 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004034 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004035
Chad Rosierb1f8c132012-10-18 15:49:34 +00004036 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00004037 if (Kind != AOK_SizeDirective)
4038 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004039 }
4040
4041 // Emit the remainder of the asm string.
4042 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4043 if (Start != AsmEnd)
4044 OS << StringRef(Start, AsmEnd - Start);
4045
4046 AsmString = OS.str();
4047 return false;
4048}
4049
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004050/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004051MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004052 MCContext &C, MCStreamer &Out,
4053 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004054 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004055}