blob: 3621982259c482e41c833651a0d7b2fc2516a13c [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000020#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Chad Rosierb1f8c132012-10-18 15:49:34 +000022#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000024#include "llvm/MC/MCParser/AsmCond.h"
25#include "llvm/MC/MCParser/AsmLexer.h"
26#include "llvm/MC/MCParser/MCAsmParser.h"
27#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000028#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000029#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000030#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000032#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000033#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000034#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000035#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000036#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000037#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000038#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000039#include <cctype>
Chad Rosierb1f8c132012-10-18 15:49:34 +000040#include <set>
41#include <string>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000042#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000043using namespace llvm;
44
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000045static cl::opt<bool>
46FatalAssemblerWarnings("fatal-assembler-warnings",
47 cl::desc("Consider warnings as error"));
48
Eric Christopher2318ba12012-12-18 00:30:54 +000049MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewycky0d7d11d2012-10-19 07:00:09 +000050
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000051namespace {
52
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053/// \brief Helper class for tracking macro definitions.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000054typedef std::vector<AsmToken> MacroArgument;
Rafael Espindola8a403d32012-08-08 14:51:03 +000055typedef std::vector<MacroArgument> MacroArguments;
Preston Gurd6c9176a2012-09-19 20:29:04 +000056typedef std::pair<StringRef, MacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000057typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000058
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000059struct Macro {
60 StringRef Name;
61 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000062 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000063
64public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000065 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000066 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000067};
68
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000069/// \brief Helper class for storing information about an active macro
70/// instantiation.
71struct MacroInstantiation {
72 /// The macro being instantiated.
73 const Macro *TheMacro;
74
75 /// The macro instantiation with substitutions.
76 MemoryBuffer *Instantiation;
77
78 /// The location of the instantiation.
79 SMLoc InstantiationLoc;
80
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000081 /// The buffer where parsing should resume upon instantiation completion.
82 int ExitBuffer;
83
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000084 /// The location where parsing should resume upon instantiation completion.
85 SMLoc ExitLoc;
86
87public:
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000088 MacroInstantiation(const Macro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000089 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000090};
91
Chad Rosier6a020a72012-10-25 20:41:34 +000092//struct AsmRewrite;
Eli Friedman2128aae2012-10-22 23:58:19 +000093struct ParseStatementInfo {
94 /// ParsedOperands - The parsed operands from the last parsed statement.
95 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
96
97 /// Opcode - The opcode from the last parsed instruction.
98 unsigned Opcode;
99
Chad Rosier57498012012-12-12 22:45:52 +0000100 /// Error - Was there an error parsing the inline assembly?
101 bool ParseError;
102
Eli Friedman2128aae2012-10-22 23:58:19 +0000103 SmallVectorImpl<AsmRewrite> *AsmRewrites;
104
Chad Rosier57498012012-12-12 22:45:52 +0000105 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000106 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier57498012012-12-12 22:45:52 +0000107 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000108
109 ~ParseStatementInfo() {
110 // Free any parsed operands.
111 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
112 delete ParsedOperands[i];
113 ParsedOperands.clear();
114 }
115};
116
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000117/// \brief The concrete assembly parser instance.
118class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000119 friend class GenericAsmParser;
120
Craig Topper85aadc02012-09-15 16:23:52 +0000121 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
122 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000123private:
124 AsmLexer Lexer;
125 MCContext &Ctx;
126 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000127 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000128 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000129 SourceMgr::DiagHandlerTy SavedDiagHandler;
130 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000131 MCAsmParserExtension *GenericParser;
132 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000133
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
136 int CurBuffer;
137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
141 /// DirectiveMap - This is a table handlers for directives. Each handler is
142 /// invoked after the directive identifier is read and is responsible for
143 /// parsing and validating the rest of the directive. The handler is passed
144 /// in the directive name and the location of the directive keyword.
145 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000146
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000147 /// MacroMap - Map of currently defined macros.
148 StringMap<Macro*> MacroMap;
149
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000150 /// ActiveMacros - Stack of active macro instantiations.
151 std::vector<MacroInstantiation*> ActiveMacros;
152
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000153 /// Boolean tracking whether macro substitution is enabled.
Eli Bendersky733c3362013-01-14 18:08:41 +0000154 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000155
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000156 /// Flag tracking whether any errors have been encountered.
157 unsigned HadError : 1;
158
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000159 /// The values from the last parsed cpp hash file line comment if any.
160 StringRef CppHashFilename;
161 int64_t CppHashLineNumber;
162 SMLoc CppHashLoc;
Kevin Enderby32c1a822012-11-05 21:55:41 +0000163 int CppHashBuf;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000164
Devang Patel0db58bf2012-01-31 18:14:05 +0000165 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
166 unsigned AssemblerDialect;
167
Preston Gurd7b6f2032012-09-19 20:36:12 +0000168 /// IsDarwin - is Darwin compatibility enabled?
169 bool IsDarwin;
170
Chad Rosier8f138d12012-10-15 17:19:13 +0000171 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000172 bool ParsingInlineAsm;
173
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000174public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000175 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000176 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000177 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000178
179 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
180
Craig Topper345d16d2012-08-29 05:48:09 +0000181 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
182 StringRef Directive,
183 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 DirectiveMap[Directive] = std::make_pair(Object, Handler);
185 }
186
187public:
188 /// @name MCAsmParser Interface
189 /// {
190
191 virtual SourceMgr &getSourceManager() { return SrcMgr; }
192 virtual MCAsmLexer &getLexer() { return Lexer; }
193 virtual MCContext &getContext() { return Ctx; }
194 virtual MCStreamer &getStreamer() { return Out; }
Eric Christopher2318ba12012-12-18 00:30:54 +0000195 virtual unsigned getAssemblerDialect() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000196 if (AssemblerDialect == ~0U)
Eric Christopher2318ba12012-12-18 00:30:54 +0000197 return MAI.getAssemblerDialect();
Devang Patel0db58bf2012-01-31 18:14:05 +0000198 else
199 return AssemblerDialect;
200 }
201 virtual void setAssemblerDialect(unsigned i) {
202 AssemblerDialect = i;
203 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000205 virtual bool Warning(SMLoc L, const Twine &Msg,
206 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
207 virtual bool Error(SMLoc L, const Twine &Msg,
208 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000209
Craig Topper345d16d2012-08-29 05:48:09 +0000210 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000211
Chad Rosier84125ca2012-10-13 00:26:04 +0000212 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000213 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000214
215 bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
216 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000217 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000218 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000219 SmallVectorImpl<std::string> &Clobbers,
220 const MCInstrInfo *MII,
221 const MCInstPrinter *IP,
222 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000223
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224 bool ParseExpression(const MCExpr *&Res);
225 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
226 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
227 virtual bool ParseAbsoluteExpression(int64_t &Res);
228
Eli Benderskybf706b32013-01-12 00:05:00 +0000229 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
230 /// and set \p Res to the identifier contents.
231 virtual bool ParseIdentifier(StringRef &Res);
Eli Benderskyb2f0b592013-01-12 00:23:24 +0000232 virtual void EatToEndOfStatement();
Eli Benderskybf706b32013-01-12 00:05:00 +0000233
Eli Bendersky733c3362013-01-14 18:08:41 +0000234 virtual bool MacrosEnabled() {return MacrosEnabledFlag;}
235 virtual void SetMacrosEnabled(bool flag) {MacrosEnabledFlag = flag;}
236
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000237 /// }
238
239private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000240 void CheckForValidSection();
241
Eli Friedman2128aae2012-10-22 23:58:19 +0000242 bool ParseStatement(ParseStatementInfo &Info);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000243 void EatToEndOfLine();
244 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000245
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000246 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000247 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000248 const MacroParameters &Parameters,
249 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000250 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000251 void HandleMacroExit();
252
253 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000254 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000255 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
256 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000257 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000258 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000259
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000260 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
261 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000262 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
263 /// This returns true on failure.
264 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000265
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000266 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000267 /// current token is not set; clients should ensure Lex() is called
268 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000269 ///
270 /// \param InBuffer If not -1, should be the known buffer id that contains the
271 /// location.
272 void JumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000273
Preston Gurd7b6f2032012-09-19 20:36:12 +0000274 bool ParseMacroArgument(MacroArgument &MA,
275 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000276 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000277
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000278 /// \brief Parse up to the end of statement and a return the contents from the
279 /// current token until the end of the statement; the current token on exit
280 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000281 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000282
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000283 /// \brief Parse until the end of a statement or a comma is encountered,
284 /// return the contents from the current token up to the end or comma.
285 StringRef ParseStringToComma();
286
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000287 bool ParseAssignment(StringRef Name, bool allow_redef,
288 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000289
290 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
291 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
292 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000293 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000294
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000295 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000296
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000297 enum DirectiveKind {
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000298 DK_NO_DIRECTIVE, // Placeholder
299 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
300 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
301 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000302 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000303 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
304 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL,
305 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
306 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
307 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
308 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
309 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
310 DK_ELSEIF, DK_ELSE, DK_ENDIF
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000311 };
312
313 StringMap<DirectiveKind> DirectiveKindMapping;
314
315 // ".ascii", ".asciz", ".string"
Rafael Espindola787c3372010-10-28 20:02:27 +0000316 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000317 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000318 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000319 bool ParseDirectiveFill(); // ".fill"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000320 bool ParseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000321 // ".set", ".equ", ".equiv"
322 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000323 bool ParseDirectiveOrg(); // ".org"
324 // ".align{,32}", ".p2align{,w,l}"
325 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
326
Eli Bendersky4766ef42012-12-20 19:05:53 +0000327 // ".bundle_align_mode"
328 bool ParseDirectiveBundleAlignMode();
329 // ".bundle_lock"
330 bool ParseDirectiveBundleLock();
331 // ".bundle_unlock"
332 bool ParseDirectiveBundleUnlock();
333
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000334 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
335 /// accepts a single symbol (which should be a label or an external).
336 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000337
338 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
339
340 bool ParseDirectiveAbort(); // ".abort"
341 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000342 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000343
344 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000345 // ".ifb" or ".ifnb", depending on ExpectBlank.
346 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000347 // ".ifc" or ".ifnc", depending on ExpectEqual.
348 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000349 // ".ifdef" or ".ifndef", depending on expect_defined
350 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000351 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
352 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
353 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
354
355 /// ParseEscapedString - Parse the current token as a string which may include
356 /// escaped characters and return the string contents.
357 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000358
359 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
360 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000361
Rafael Espindola761cb062012-06-03 23:57:14 +0000362 // Macro-like directives
363 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
364 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
365 raw_svector_ostream &OS);
366 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000367 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000368 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000369 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000370
Eli Friedman2128aae2012-10-22 23:58:19 +0000371 // "_emit"
372 bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000373
374 void initializeDirectiveKindMapping();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000375};
376
Eli Bendersky63e6f482013-01-10 23:32:57 +0000377/// \brief Generic implementation of directive handling, etc. which is shared
378/// (or the default, at least) for all assembler parsers.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000379class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000380 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
381 void AddDirectiveHandler(StringRef Directive) {
382 getParser().AddDirectiveHandler(this, Directive,
383 HandleDirective<GenericAsmParser, Handler>);
384 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000385public:
386 GenericAsmParser() {}
387
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000388 AsmParser &getParser() {
389 return (AsmParser&) this->MCAsmParserExtension::getParser();
390 }
391
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000392 virtual void Initialize(MCAsmParser &Parser) {
393 // Call the base implementation.
394 this->MCAsmParserExtension::Initialize(Parser);
395
396 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000397 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
398 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
399 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000400 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000401
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000402 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".space");
403 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".skip");
404
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000405 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000406 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
407 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000408 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
409 ".cfi_startproc");
410 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
411 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000412 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
413 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000414 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
415 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000416 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
417 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000418 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
419 ".cfi_def_cfa_register");
420 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
421 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000422 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
423 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000424 AddDirectiveHandler<
425 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
426 AddDirectiveHandler<
427 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000428 AddDirectiveHandler<
429 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
430 AddDirectiveHandler<
431 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000432 AddDirectiveHandler<
433 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000434 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000435 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
436 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000437 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000438 AddDirectiveHandler<
439 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000440 AddDirectiveHandler<
441 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000442 AddDirectiveHandler<
443 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000444
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000445 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000446 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
447 ".macros_on");
448 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
449 ".macros_off");
450 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
451 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
452 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000453 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000454
455 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
456 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000457 }
458
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000459 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
460
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000461 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
462 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
463 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000464 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000465 bool ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000466 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000467 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
468 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000469 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000470 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000471 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000472 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
473 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000474 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000475 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000476 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
477 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000478 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000479 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000480 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000481 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000482 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000483 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000484
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000485 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000486 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
487 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000488 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000489
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000490 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000491};
492
493}
494
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000495namespace llvm {
496
497extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000498extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000499extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000500
501}
502
Chris Lattneraaec2052010-01-19 19:46:13 +0000503enum { DEFAULT_ADDRSPACE = 0 };
504
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000505AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000506 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000507 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000508 GenericParser(new GenericAsmParser), PlatformParser(0),
Eli Bendersky733c3362013-01-14 18:08:41 +0000509 CurBuffer(0), MacrosEnabledFlag(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000510 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000511 // Save the old handler.
512 SavedDiagHandler = SrcMgr.getDiagHandler();
513 SavedDiagContext = SrcMgr.getDiagContext();
514 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000515 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000516 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000517
518 // Initialize the generic parser.
519 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000520
521 // Initialize the platform / file format parser.
522 //
523 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
524 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000525 if (_MAI.hasMicrosoftFastStdCallMangling()) {
526 PlatformParser = createCOFFAsmParser();
527 PlatformParser->Initialize(*this);
528 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000529 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000530 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000531 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000532 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000533 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000534 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000535 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000536
537 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000538}
539
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000540AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000541 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
542
543 // Destroy any macros.
544 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
545 ie = MacroMap.end(); it != ie; ++it)
546 delete it->getValue();
547
Daniel Dunbare4749702010-07-12 18:12:02 +0000548 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000549 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000550}
551
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000552void AsmParser::PrintMacroInstantiations() {
553 // Print the active macro instantiation stack.
554 for (std::vector<MacroInstantiation*>::const_reverse_iterator
555 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000556 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
557 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000558}
559
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000560bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000561 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000562 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000563 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000564 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000565 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000566}
567
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000568bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000569 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000570 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000571 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000572 return true;
573}
574
Sean Callananfd0b0282010-01-21 00:19:58 +0000575bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000576 std::string IncludedFile;
577 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000578 if (NewBuf == -1)
579 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000580
Sean Callananfd0b0282010-01-21 00:19:58 +0000581 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000582
Sean Callananfd0b0282010-01-21 00:19:58 +0000583 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000584
Sean Callananfd0b0282010-01-21 00:19:58 +0000585 return false;
586}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000587
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000588/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000589/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000590/// returns true on failure.
591bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
592 std::string IncludedFile;
593 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
594 if (NewBuf == -1)
595 return true;
596
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000597 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000598 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
599 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000600 return false;
601}
602
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000603void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
604 if (InBuffer != -1) {
605 CurBuffer = InBuffer;
606 } else {
607 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
608 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000609 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
610}
611
Sean Callananfd0b0282010-01-21 00:19:58 +0000612const AsmToken &AsmParser::Lex() {
613 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000614
Sean Callananfd0b0282010-01-21 00:19:58 +0000615 if (tok->is(AsmToken::Eof)) {
616 // If this is the end of an included file, pop the parent file off the
617 // include stack.
618 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
619 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000620 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000621 tok = &Lexer.Lex();
622 }
623 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000624
Sean Callananfd0b0282010-01-21 00:19:58 +0000625 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000626 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000627
Sean Callananfd0b0282010-01-21 00:19:58 +0000628 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000629}
630
Chris Lattner79180e22010-04-05 23:15:42 +0000631bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000632 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000633 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000634 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000635
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000636 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000637 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000638
639 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000640 AsmCond StartingCondState = TheCondState;
641
Kevin Enderby613b7572011-11-01 22:27:22 +0000642 // If we are generating dwarf for assembly source files save the initial text
643 // section and generate a .file directive.
644 if (getContext().getGenDwarfForAssembly()) {
645 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000646 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
647 getStreamer().EmitLabel(SectionStartSym);
648 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000649 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000650 StringRef(),
651 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000652 }
653
Chris Lattnerb717fb02009-07-02 21:53:43 +0000654 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000655 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000656 ParseStatementInfo Info;
657 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000658
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000659 // We had an error, validate that one was emitted and recover by skipping to
660 // the next line.
661 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000662 EatToEndOfStatement();
663 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000664
665 if (TheCondState.TheCond != StartingCondState.TheCond ||
666 TheCondState.Ignore != StartingCondState.Ignore)
667 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000668
669 // Check to see there are no empty DwarfFile slots.
670 const std::vector<MCDwarfFile *> &MCDwarfFiles =
671 getContext().getMCDwarfFiles();
672 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000673 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000674 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000675 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000676
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000677 // Check to see that all assembler local symbols were actually defined.
678 // Targets that don't do subsections via symbols may not want this, though,
679 // so conservatively exclude them. Only do this if we're finalizing, though,
680 // as otherwise we won't necessarilly have seen everything yet.
681 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
682 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
683 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
684 e = Symbols.end();
685 i != e; ++i) {
686 MCSymbol *Sym = i->getValue();
687 // Variable symbols may not be marked as defined, so check those
688 // explicitly. If we know it's a variable, we have a definition for
689 // the purposes of this check.
690 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
691 // FIXME: We would really like to refer back to where the symbol was
692 // first referenced for a source location. We need to add something
693 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000694 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
695 "assembler local symbol '" + Sym->getName() +
696 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000697 }
698 }
699
700
Chris Lattner79180e22010-04-05 23:15:42 +0000701 // Finalize the output stream if there are no errors and if the client wants
702 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000703 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000704 Out.Finish();
705
Chris Lattnerb717fb02009-07-02 21:53:43 +0000706 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000707}
708
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000709void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000710 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000711 TokError("expected section directive before assembly directive");
712 Out.SwitchSection(Ctx.getMachOSection(
713 "__TEXT", "__text",
714 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
715 0, SectionKind::getText()));
716 }
717}
718
Chris Lattner2cf5f142009-06-22 01:29:09 +0000719/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
720void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000721 while (Lexer.isNot(AsmToken::EndOfStatement) &&
722 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000723 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000724
Chris Lattner2cf5f142009-06-22 01:29:09 +0000725 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000726 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000727 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000728}
729
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000730StringRef AsmParser::ParseStringToEndOfStatement() {
731 const char *Start = getTok().getLoc().getPointer();
732
733 while (Lexer.isNot(AsmToken::EndOfStatement) &&
734 Lexer.isNot(AsmToken::Eof))
735 Lex();
736
737 const char *End = getTok().getLoc().getPointer();
738 return StringRef(Start, End - Start);
739}
Chris Lattnerc4193832009-06-22 05:51:26 +0000740
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000741StringRef AsmParser::ParseStringToComma() {
742 const char *Start = getTok().getLoc().getPointer();
743
744 while (Lexer.isNot(AsmToken::EndOfStatement) &&
745 Lexer.isNot(AsmToken::Comma) &&
746 Lexer.isNot(AsmToken::Eof))
747 Lex();
748
749 const char *End = getTok().getLoc().getPointer();
750 return StringRef(Start, End - Start);
751}
752
Chris Lattner74ec1a32009-06-22 06:32:03 +0000753/// ParseParenExpr - Parse a paren expression and return it.
754/// NOTE: This assumes the leading '(' has already been consumed.
755///
756/// parenexpr ::= expr)
757///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000758bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000759 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000760 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000761 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000762 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000763 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000764 return false;
765}
Chris Lattnerc4193832009-06-22 05:51:26 +0000766
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000767/// ParseBracketExpr - Parse a bracket expression and return it.
768/// NOTE: This assumes the leading '[' has already been consumed.
769///
770/// bracketexpr ::= expr]
771///
772bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
773 if (ParseExpression(Res)) return true;
774 if (Lexer.isNot(AsmToken::RBrac))
775 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000776 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000777 Lex();
778 return false;
779}
780
Chris Lattner74ec1a32009-06-22 06:32:03 +0000781/// ParsePrimaryExpr - Parse a primary expression and return it.
782/// primaryexpr ::= (parenexpr
783/// primaryexpr ::= symbol
784/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000785/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000786/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000787bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000788 switch (Lexer.getKind()) {
789 default:
790 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000791 // If we have an error assume that we've already handled it.
792 case AsmToken::Error:
793 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000794 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000795 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000796 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000798 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000799 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000800 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000801 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000802 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000803 StringRef Identifier;
804 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000805 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000806
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000807 EndLoc = SMLoc::getFromPointer(Identifier.end());
808
Daniel Dunbarfffff912009-10-16 01:34:54 +0000809 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000810 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000811 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000812
813 // Lookup the symbol variant if used.
814 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000815 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000816 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000817 if (Variant == MCSymbolRefExpr::VK_Invalid) {
818 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000819 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000820 }
821 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000822
Daniel Dunbarfffff912009-10-16 01:34:54 +0000823 // If this is an absolute variable reference, substitute it now to preserve
824 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000825 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000826 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000827 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000828
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000829 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000830 return false;
831 }
832
833 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000834 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000835 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000836 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000837 case AsmToken::Integer: {
838 SMLoc Loc = getTok().getLoc();
839 int64_t IntVal = getTok().getIntVal();
840 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000841 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000842 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000843 // Look for 'b' or 'f' following an Integer as a directional label
844 if (Lexer.getKind() == AsmToken::Identifier) {
845 StringRef IDVal = getTok().getString();
846 if (IDVal == "f" || IDVal == "b"){
847 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
848 IDVal == "f" ? 1 : 0);
849 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
850 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000851 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000852 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000853 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000854 Lex(); // Eat identifier.
855 }
856 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000857 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000858 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000859 case AsmToken::Real: {
860 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000861 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000862 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000863 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000864 Lex(); // Eat token.
865 return false;
866 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000867 case AsmToken::Dot: {
868 // This is a '.' reference, which references the current PC. Emit a
869 // temporary label to the streamer and refer to it.
870 MCSymbol *Sym = Ctx.CreateTempSymbol();
871 Out.EmitLabel(Sym);
872 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000873 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000874 Lex(); // Eat identifier.
875 return false;
876 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000877 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000878 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000879 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000880 case AsmToken::LBrac:
881 if (!PlatformParser->HasBracketExpressions())
882 return TokError("brackets expression not supported on this target");
883 Lex(); // Eat the '['.
884 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000885 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000886 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000887 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000888 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000889 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000890 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000891 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000892 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000893 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000894 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000895 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000896 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000897 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000898 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000899 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000900 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000901 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000902 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000903 }
904}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000905
Chris Lattnerb4307b32010-01-15 19:28:38 +0000906bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000907 SMLoc EndLoc;
908 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000909}
910
Daniel Dunbarcceba832010-09-17 02:47:07 +0000911const MCExpr *
912AsmParser::ApplyModifierToExpr(const MCExpr *E,
913 MCSymbolRefExpr::VariantKind Variant) {
914 // Recurse over the given expression, rebuilding it to apply the given variant
915 // if there is exactly one symbol.
916 switch (E->getKind()) {
917 case MCExpr::Target:
918 case MCExpr::Constant:
919 return 0;
920
921 case MCExpr::SymbolRef: {
922 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
923
924 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
925 TokError("invalid variant on expression '" +
926 getTok().getIdentifier() + "' (already modified)");
927 return E;
928 }
929
930 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
931 }
932
933 case MCExpr::Unary: {
934 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
935 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
936 if (!Sub)
937 return 0;
938 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
939 }
940
941 case MCExpr::Binary: {
942 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
943 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
944 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
945
946 if (!LHS && !RHS)
947 return 0;
948
949 if (!LHS) LHS = BE->getLHS();
950 if (!RHS) RHS = BE->getRHS();
951
952 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
953 }
954 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000955
Craig Topper85814382012-02-07 05:05:23 +0000956 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000957}
958
Chris Lattner74ec1a32009-06-22 06:32:03 +0000959/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000960///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000961/// expr ::= expr &&,|| expr -> lowest.
962/// expr ::= expr |,^,&,! expr
963/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
964/// expr ::= expr <<,>> expr
965/// expr ::= expr +,- expr
966/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000967/// expr ::= primaryexpr
968///
Chris Lattner54482b42010-01-15 19:39:23 +0000969bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000970 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000971 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000972 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
973 return true;
974
Daniel Dunbarcceba832010-09-17 02:47:07 +0000975 // As a special case, we support 'a op b @ modifier' by rewriting the
976 // expression to include the modifier. This is inefficient, but in general we
977 // expect users to use 'a@modifier op b'.
978 if (Lexer.getKind() == AsmToken::At) {
979 Lex();
980
981 if (Lexer.isNot(AsmToken::Identifier))
982 return TokError("unexpected symbol modifier following '@'");
983
984 MCSymbolRefExpr::VariantKind Variant =
985 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
986 if (Variant == MCSymbolRefExpr::VK_Invalid)
987 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
988
989 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
990 if (!ModifiedRes) {
991 return TokError("invalid modifier '" + getTok().getIdentifier() +
992 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000993 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000994
Daniel Dunbarcceba832010-09-17 02:47:07 +0000995 Res = ModifiedRes;
996 Lex();
997 }
998
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000999 // Try to constant fold it up front, if possible.
1000 int64_t Value;
1001 if (Res->EvaluateAsAbsolute(Value))
1002 Res = MCConstantExpr::Create(Value, getContext());
1003
1004 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +00001005}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001006
Chris Lattnerb4307b32010-01-15 19:28:38 +00001007bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +00001008 Res = 0;
1009 return ParseParenExpr(Res, EndLoc) ||
1010 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +00001011}
1012
Daniel Dunbar475839e2009-06-29 20:37:27 +00001013bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001014 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001015
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001016 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +00001017 if (ParseExpression(Expr))
1018 return true;
1019
Daniel Dunbare00b0112009-10-16 01:57:52 +00001020 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001021 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001022
1023 return false;
1024}
1025
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001026static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001027 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001028 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001029 default:
1030 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001031
Jim Grosbachfbe16812011-08-20 16:24:13 +00001032 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001033 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001034 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001035 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001036 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001037 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001038 return 1;
1039
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001040
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001041 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +00001042 //
1043 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001044 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001045 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001046 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001047 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001048 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001049 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001050 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001051 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001052 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001053
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001054 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001055 case AsmToken::EqualEqual:
1056 Kind = MCBinaryExpr::EQ;
1057 return 3;
1058 case AsmToken::ExclaimEqual:
1059 case AsmToken::LessGreater:
1060 Kind = MCBinaryExpr::NE;
1061 return 3;
1062 case AsmToken::Less:
1063 Kind = MCBinaryExpr::LT;
1064 return 3;
1065 case AsmToken::LessEqual:
1066 Kind = MCBinaryExpr::LTE;
1067 return 3;
1068 case AsmToken::Greater:
1069 Kind = MCBinaryExpr::GT;
1070 return 3;
1071 case AsmToken::GreaterEqual:
1072 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001073 return 3;
1074
Jim Grosbachfbe16812011-08-20 16:24:13 +00001075 // Intermediate Precedence: <<, >>
1076 case AsmToken::LessLess:
1077 Kind = MCBinaryExpr::Shl;
1078 return 4;
1079 case AsmToken::GreaterGreater:
1080 Kind = MCBinaryExpr::Shr;
1081 return 4;
1082
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001083 // High Intermediate Precedence: +, -
1084 case AsmToken::Plus:
1085 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001086 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001087 case AsmToken::Minus:
1088 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001089 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001090
Jim Grosbachfbe16812011-08-20 16:24:13 +00001091 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001092 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001093 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001094 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001095 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001096 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001097 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001098 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001099 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001100 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001101 }
1102}
1103
1104
1105/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1106/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001107bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1108 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001109 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001110 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001111 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001112
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001113 // If the next token is lower precedence than we are allowed to eat, return
1114 // successfully with what we ate already.
1115 if (TokPrec < Precedence)
1116 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001117
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001119
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001120 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001121 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001122 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001123
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001124 // If BinOp binds less tightly with RHS than the operator after RHS, let
1125 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001126 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001127 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001128 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001129 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001130 }
1131
Daniel Dunbar475839e2009-06-29 20:37:27 +00001132 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001133 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001134 }
1135}
1136
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001137/// ParseStatement:
1138/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001139/// ::= Label* Directive ...Operands... EndOfStatement
1140/// ::= Label* Identifier OperandList* EndOfStatement
Eli Friedman2128aae2012-10-22 23:58:19 +00001141bool AsmParser::ParseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001142 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001143 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001144 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001145 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001146 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001147
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001148 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001149 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001150 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001151 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001152 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001153 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001154 if (Lexer.is(AsmToken::Hash))
1155 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001156
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001157 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001158 if (Lexer.is(AsmToken::Integer)) {
1159 LocalLabelVal = getTok().getIntVal();
1160 if (LocalLabelVal < 0) {
1161 if (!TheCondState.Ignore)
1162 return TokError("unexpected token at start of statement");
1163 IDVal = "";
1164 }
1165 else {
1166 IDVal = getTok().getString();
1167 Lex(); // Consume the integer token to be used as an identifier token.
1168 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001169 if (!TheCondState.Ignore)
1170 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001171 }
1172 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001173
1174 } else if (Lexer.is(AsmToken::Dot)) {
1175 // Treat '.' as a valid identifier in this context.
1176 Lex();
1177 IDVal = ".";
1178
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001179 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001180 if (!TheCondState.Ignore)
1181 return TokError("unexpected token at start of statement");
1182 IDVal = "";
1183 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001184
Chris Lattner7834fac2010-04-17 18:14:27 +00001185 // Handle conditional assembly here before checking for skipping. We
1186 // have to do this so that .endif isn't skipped in a ".if 0" block for
1187 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001188 StringMap<DirectiveKind>::const_iterator DirKindIt =
1189 DirectiveKindMapping.find(IDVal);
1190 DirectiveKind DirKind =
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001191 (DirKindIt == DirectiveKindMapping.end()) ? DK_NO_DIRECTIVE :
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001192 DirKindIt->getValue();
1193 switch (DirKind) {
1194 default:
1195 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001196 case DK_IF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001197 return ParseDirectiveIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001198 case DK_IFB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001199 return ParseDirectiveIfb(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001200 case DK_IFNB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001201 return ParseDirectiveIfb(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001202 case DK_IFC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001203 return ParseDirectiveIfc(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001204 case DK_IFNC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001205 return ParseDirectiveIfc(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001206 case DK_IFDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001207 return ParseDirectiveIfdef(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001208 case DK_IFNDEF:
1209 case DK_IFNOTDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001210 return ParseDirectiveIfdef(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001211 case DK_ELSEIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001212 return ParseDirectiveElseIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001213 case DK_ELSE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001214 return ParseDirectiveElse(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001215 case DK_ENDIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001216 return ParseDirectiveEndIf(IDLoc);
1217 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001218
Chris Lattner7834fac2010-04-17 18:14:27 +00001219 // If we are in a ".if 0" block, ignore this statement.
Chad Rosier17feeec2012-10-20 00:47:08 +00001220 if (TheCondState.Ignore) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001221 EatToEndOfStatement();
1222 return false;
1223 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001224
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001225 // FIXME: Recurse on local labels?
1226
1227 // See what kind of statement we have.
1228 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001229 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001230 CheckForValidSection();
1231
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001232 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001233 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001234
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001235 // Diagnose attempt to use '.' as a label.
1236 if (IDVal == ".")
1237 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1238
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001239 // Diagnose attempt to use a variable as a label.
1240 //
1241 // FIXME: Diagnostics. Note the location of the definition as a label.
1242 // FIXME: This doesn't diagnose assignment to a symbol which has been
1243 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001244 MCSymbol *Sym;
1245 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001246 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001247 else
1248 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001249 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001250 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001251
Daniel Dunbar959fd882009-08-26 22:13:22 +00001252 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001253 if (!ParsingInlineAsm)
1254 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001255
Kevin Enderby94c2e852011-12-09 18:09:40 +00001256 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001257 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001258 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001259 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1260 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001261
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001262 // Consume any end of statement token, if present, to avoid spurious
1263 // AddBlankLine calls().
1264 if (Lexer.is(AsmToken::EndOfStatement)) {
1265 Lex();
1266 if (Lexer.is(AsmToken::Eof))
1267 return false;
1268 }
1269
Eli Friedman2128aae2012-10-22 23:58:19 +00001270 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001271 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001272
Daniel Dunbar3f872332009-07-28 16:08:33 +00001273 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001274 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001275 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001276
Nico Weber4c4c7322011-01-28 03:04:41 +00001277 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001278
1279 default: // Normal instruction or directive.
1280 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001281 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001282
1283 // If macros are enabled, check to see if this is a macro instantiation.
Eli Bendersky733c3362013-01-14 18:08:41 +00001284 if (MacrosEnabled())
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001285 if (const Macro *M = MacroMap.lookup(IDVal))
1286 return HandleMacroEntry(IDVal, IDLoc, M);
1287
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001288 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001289 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001290
1291 // Target hook for parsing target specific directives.
1292 if (!getTargetParser().ParseDirective(ID))
1293 return false;
1294
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001295 switch (DirKind) {
1296 default:
1297 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001298 case DK_SET:
1299 case DK_EQU:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001300 return ParseDirectiveSet(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001301 case DK_EQUIV:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001302 return ParseDirectiveSet(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001303 case DK_ASCII:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001304 return ParseDirectiveAscii(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001305 case DK_ASCIZ:
1306 case DK_STRING:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001307 return ParseDirectiveAscii(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001308 case DK_BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001309 return ParseDirectiveValue(1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001310 case DK_SHORT:
1311 case DK_VALUE:
1312 case DK_2BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001313 return ParseDirectiveValue(2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001314 case DK_LONG:
1315 case DK_INT:
1316 case DK_4BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001317 return ParseDirectiveValue(4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001318 case DK_QUAD:
1319 case DK_8BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001320 return ParseDirectiveValue(8);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001321 case DK_SINGLE:
1322 case DK_FLOAT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001323 return ParseDirectiveRealValue(APFloat::IEEEsingle);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001324 case DK_DOUBLE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001325 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001326 case DK_ALIGN: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001327 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1328 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1329 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001330 case DK_ALIGN32: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001331 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1332 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1333 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001334 case DK_BALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001335 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001336 case DK_BALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001337 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001338 case DK_BALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001339 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001340 case DK_P2ALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001341 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001342 case DK_P2ALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001343 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001344 case DK_P2ALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001345 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001346 case DK_ORG:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001347 return ParseDirectiveOrg();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001348 case DK_FILL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001349 return ParseDirectiveFill();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001350 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001351 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001352 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001353 EatToEndOfStatement(); // .extern is the default, ignore it.
1354 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001355 case DK_GLOBL:
1356 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001357 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001358 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001359 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001360 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001361 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001362 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001363 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001364 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001365 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001366 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001367 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001368 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001369 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001370 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001371 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001372 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001373 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001374 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001375 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001376 case DK_COMM:
1377 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001378 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001379 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001380 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001381 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001382 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001383 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001384 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001385 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001386 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001387 case DK_CODE16:
1388 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001389 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001390 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001391 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001392 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001393 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001394 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001395 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001396 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001397 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001398 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001399 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001400 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001401 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001402 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001403 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001404 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001405
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001406 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001407 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1408 DirectiveMap.lookup(IDVal);
1409 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001410 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001411
Jim Grosbach686c0182012-05-01 18:38:27 +00001412 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001413 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001414
Eli Friedman2128aae2012-10-22 23:58:19 +00001415 // _emit
1416 if (ParsingInlineAsm && IDVal == "_emit")
1417 return ParseDirectiveEmit(IDLoc, Info);
1418
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001419 CheckForValidSection();
1420
Chris Lattnera7f13542010-05-19 23:34:33 +00001421 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001422 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001423 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001424 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001425
Chad Rosier6a020a72012-10-25 20:41:34 +00001426 ParseInstructionInfo IInfo(Info.AsmRewrites);
1427 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1428 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001429 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001430
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001431 // Dump the parsed representation, if requested.
1432 if (getShowParsedOperands()) {
1433 SmallString<256> Str;
1434 raw_svector_ostream OS(Str);
1435 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001436 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001437 if (i != 0)
1438 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001439 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001440 }
1441 OS << "]";
1442
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001443 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001444 }
1445
Kevin Enderby613b7572011-11-01 22:27:22 +00001446 // If we are generating dwarf for assembly source files and the current
1447 // section is the initial text section then generate a .loc directive for
1448 // the instruction.
1449 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001450 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001451
1452 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1453
1454 // If we previously parsed a cpp hash file line comment then make sure the
1455 // current Dwarf File is for the CppHashFilename if not then emit the
1456 // Dwarf File table for it and adjust the line number for the .loc.
1457 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1458 getContext().getMCDwarfFiles();
1459 if (CppHashFilename.size() != 0) {
1460 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1461 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001462 getStreamer().EmitDwarfFileDirective(
1463 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001464
Kevin Enderby32c1a822012-11-05 21:55:41 +00001465 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001466 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1467 }
1468
Kevin Enderby613b7572011-11-01 22:27:22 +00001469 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001470 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001471 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001472 StringRef());
1473 }
1474
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001475 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001476 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001477 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001478 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1479 Info.ParsedOperands,
1480 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001481 ParsingInlineAsm);
1482 }
Chris Lattner98986712010-01-14 22:21:20 +00001483
Chris Lattnercbf8a982010-09-11 16:18:25 +00001484 // Don't skip the rest of the line, the instruction parser is responsible for
1485 // that.
1486 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001487}
Chris Lattner9a023f72009-06-24 04:43:34 +00001488
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001489/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1490/// since they may not be able to be tokenized to get to the end of line token.
1491void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001492 if (!Lexer.is(AsmToken::EndOfStatement))
1493 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001494 // Eat EOL.
1495 Lex();
1496}
1497
1498/// ParseCppHashLineFilenameComment as this:
1499/// ::= # number "filename"
1500/// or just as a full line comment if it doesn't have a number and a string.
1501bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1502 Lex(); // Eat the hash token.
1503
1504 if (getLexer().isNot(AsmToken::Integer)) {
1505 // Consume the line since in cases it is not a well-formed line directive,
1506 // as if were simply a full line comment.
1507 EatToEndOfLine();
1508 return false;
1509 }
1510
1511 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001512 Lex();
1513
1514 if (getLexer().isNot(AsmToken::String)) {
1515 EatToEndOfLine();
1516 return false;
1517 }
1518
1519 StringRef Filename = getTok().getString();
1520 // Get rid of the enclosing quotes.
1521 Filename = Filename.substr(1, Filename.size()-2);
1522
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001523 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1524 CppHashLoc = L;
1525 CppHashFilename = Filename;
1526 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001527 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001528
1529 // Ignore any trailing characters, they're just comment.
1530 EatToEndOfLine();
1531 return false;
1532}
1533
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001534/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001535/// for the Filename and LineNo if any in the diagnostic.
1536void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1537 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1538 raw_ostream &OS = errs();
1539
1540 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1541 const SMLoc &DiagLoc = Diag.getLoc();
1542 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1543 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1544
1545 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1546 // before printing the message.
1547 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001548 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001549 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1550 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1551 }
1552
Eric Christopher2318ba12012-12-18 00:30:54 +00001553 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001554 // manager changed or buffer changed (like in a nested include) then just
1555 // print the normal diagnostic using its Filename and LineNo.
1556 if (!Parser->CppHashLineNumber ||
1557 &DiagSrcMgr != &Parser->SrcMgr ||
1558 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001559 if (Parser->SavedDiagHandler)
1560 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1561 else
1562 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001563 return;
1564 }
1565
Eric Christopher2318ba12012-12-18 00:30:54 +00001566 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001567 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1568 // the diagnostic.
1569 const std::string Filename = Parser->CppHashFilename;
1570
1571 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1572 int CppHashLocLineNo =
1573 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1574 int LineNo = Parser->CppHashLineNumber - 1 +
1575 (DiagLocLineNo - CppHashLocLineNo);
1576
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001577 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1578 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001579 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001580 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001581
Benjamin Kramer04a04262011-10-16 10:48:29 +00001582 if (Parser->SavedDiagHandler)
1583 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1584 else
1585 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001586}
1587
Rafael Espindola799aacf2012-08-21 18:29:30 +00001588// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1589// difference being that that function accepts '@' as part of identifiers and
1590// we can't do that. AsmLexer.cpp should probably be changed to handle
1591// '@' as a special case when needed.
1592static bool isIdentifierChar(char c) {
1593 return isalnum(c) || c == '_' || c == '$' || c == '.';
1594}
1595
Rafael Espindola761cb062012-06-03 23:57:14 +00001596bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001597 const MacroParameters &Parameters,
1598 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001599 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001600 unsigned NParameters = Parameters.size();
1601 if (NParameters != 0 && NParameters != A.size())
1602 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001603
Preston Gurd7b6f2032012-09-19 20:36:12 +00001604 // A macro without parameters is handled differently on Darwin:
1605 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001606 while (!Body.empty()) {
1607 // Scan for the next substitution.
1608 std::size_t End = Body.size(), Pos = 0;
1609 for (; Pos != End; ++Pos) {
1610 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001611 if (!NParameters) {
1612 // This macro has no parameters, look for $0, $1, etc.
1613 if (Body[Pos] != '$' || Pos + 1 == End)
1614 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001615
Rafael Espindola65366442011-06-05 02:43:45 +00001616 char Next = Body[Pos + 1];
1617 if (Next == '$' || Next == 'n' || isdigit(Next))
1618 break;
1619 } else {
1620 // This macro has parameters, look for \foo, \bar, etc.
1621 if (Body[Pos] == '\\' && Pos + 1 != End)
1622 break;
1623 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001624 }
1625
1626 // Add the prefix.
1627 OS << Body.slice(0, Pos);
1628
1629 // Check if we reached the end.
1630 if (Pos == End)
1631 break;
1632
Rafael Espindola65366442011-06-05 02:43:45 +00001633 if (!NParameters) {
1634 switch (Body[Pos+1]) {
1635 // $$ => $
1636 case '$':
1637 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001638 break;
1639
Rafael Espindola65366442011-06-05 02:43:45 +00001640 // $n => number of arguments
1641 case 'n':
1642 OS << A.size();
1643 break;
1644
1645 // $[0-9] => argument
1646 default: {
1647 // Missing arguments are ignored.
1648 unsigned Index = Body[Pos+1] - '0';
1649 if (Index >= A.size())
1650 break;
1651
1652 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001653 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001654 ie = A[Index].end(); it != ie; ++it)
1655 OS << it->getString();
1656 break;
1657 }
1658 }
1659 Pos += 2;
1660 } else {
1661 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001662 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001663 ++I;
1664
1665 const char *Begin = Body.data() + Pos +1;
1666 StringRef Argument(Begin, I - (Pos +1));
1667 unsigned Index = 0;
1668 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001669 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001670 break;
1671
Preston Gurd7b6f2032012-09-19 20:36:12 +00001672 if (Index == NParameters) {
1673 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1674 Pos += 3;
1675 else {
1676 OS << '\\' << Argument;
1677 Pos = I;
1678 }
1679 } else {
1680 for (MacroArgument::const_iterator it = A[Index].begin(),
1681 ie = A[Index].end(); it != ie; ++it)
1682 if (it->getKind() == AsmToken::String)
1683 OS << it->getStringContents();
1684 else
1685 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001686
Preston Gurd7b6f2032012-09-19 20:36:12 +00001687 Pos += 1 + Argument.size();
1688 }
Rafael Espindola65366442011-06-05 02:43:45 +00001689 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001690 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001691 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001692 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001693
Rafael Espindola65366442011-06-05 02:43:45 +00001694 return false;
1695}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001696
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001697MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL,
1698 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001699 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001700 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1701 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001702{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001703}
1704
Preston Gurd7b6f2032012-09-19 20:36:12 +00001705static bool IsOperator(AsmToken::TokenKind kind)
1706{
1707 switch (kind)
1708 {
1709 default:
1710 return false;
1711 case AsmToken::Plus:
1712 case AsmToken::Minus:
1713 case AsmToken::Tilde:
1714 case AsmToken::Slash:
1715 case AsmToken::Star:
1716 case AsmToken::Dot:
1717 case AsmToken::Equal:
1718 case AsmToken::EqualEqual:
1719 case AsmToken::Pipe:
1720 case AsmToken::PipePipe:
1721 case AsmToken::Caret:
1722 case AsmToken::Amp:
1723 case AsmToken::AmpAmp:
1724 case AsmToken::Exclaim:
1725 case AsmToken::ExclaimEqual:
1726 case AsmToken::Percent:
1727 case AsmToken::Less:
1728 case AsmToken::LessEqual:
1729 case AsmToken::LessLess:
1730 case AsmToken::LessGreater:
1731 case AsmToken::Greater:
1732 case AsmToken::GreaterEqual:
1733 case AsmToken::GreaterGreater:
1734 return true;
1735 }
1736}
1737
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001738/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1739/// This is used for both default macro parameter values and the
1740/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001741bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1742 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001743 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001744 unsigned AddTokens = 0;
1745
1746 // gas accepts arguments separated by whitespace, except on Darwin
1747 if (!IsDarwin)
1748 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001749
1750 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001751 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1752 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001753 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001754 }
1755
1756 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1757 // Spaces and commas cannot be mixed to delimit parameters
1758 if (ArgumentDelimiter == AsmToken::Eof)
1759 ArgumentDelimiter = AsmToken::Comma;
1760 else if (ArgumentDelimiter != AsmToken::Comma) {
1761 Lexer.setSkipSpace(true);
1762 return TokError("expected ' ' for macro argument separator");
1763 }
1764 break;
1765 }
1766
1767 if (Lexer.is(AsmToken::Space)) {
1768 Lex(); // Eat spaces
1769
1770 // Spaces can delimit parameters, but could also be part an expression.
1771 // If the token after a space is an operator, add the token and the next
1772 // one into this argument
1773 if (ArgumentDelimiter == AsmToken::Space ||
1774 ArgumentDelimiter == AsmToken::Eof) {
1775 if (IsOperator(Lexer.getKind())) {
1776 // Check to see whether the token is used as an operator,
1777 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001778 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001779 if (*NextChar == ' ')
1780 AddTokens = 2;
1781 }
1782
1783 if (!AddTokens && ParenLevel == 0) {
1784 if (ArgumentDelimiter == AsmToken::Eof &&
1785 !IsOperator(Lexer.getKind()))
1786 ArgumentDelimiter = AsmToken::Space;
1787 break;
1788 }
1789 }
1790 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001791
1792 // HandleMacroEntry relies on not advancing the lexer here
1793 // to be able to fill in the remaining default parameter values
1794 if (Lexer.is(AsmToken::EndOfStatement))
1795 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001796
1797 // Adjust the current parentheses level.
1798 if (Lexer.is(AsmToken::LParen))
1799 ++ParenLevel;
1800 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1801 --ParenLevel;
1802
1803 // Append the token to the current argument list.
1804 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001805 if (AddTokens)
1806 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001807 Lex();
1808 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001809
1810 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001811 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001812 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001813 return false;
1814}
1815
1816// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001817bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001818 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001819 // Argument delimiter is initially unknown. It will be set by
1820 // ParseMacroArgument()
1821 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001822
1823 // Parse two kinds of macro invocations:
1824 // - macros defined without any parameters accept an arbitrary number of them
1825 // - macros defined with parameters accept at most that many of them
1826 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1827 ++Parameter) {
1828 MacroArgument MA;
1829
Preston Gurd7b6f2032012-09-19 20:36:12 +00001830 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001831 return true;
1832
Preston Gurd6c9176a2012-09-19 20:29:04 +00001833 if (!MA.empty() || !NParameters)
1834 A.push_back(MA);
1835 else if (NParameters) {
1836 if (!M->Parameters[Parameter].second.empty())
1837 A.push_back(M->Parameters[Parameter].second);
1838 }
Jim Grosbach97146442012-07-30 22:44:17 +00001839
Preston Gurd6c9176a2012-09-19 20:29:04 +00001840 // At the end of the statement, fill in remaining arguments that have
1841 // default values. If there aren't any, then the next argument is
1842 // required but missing
1843 if (Lexer.is(AsmToken::EndOfStatement)) {
1844 if (NParameters && Parameter < NParameters - 1) {
1845 if (M->Parameters[Parameter + 1].second.empty())
1846 return TokError("macro argument '" +
1847 Twine(M->Parameters[Parameter + 1].first) +
1848 "' is missing");
1849 else
1850 continue;
1851 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001852 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001853 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001854
1855 if (Lexer.is(AsmToken::Comma))
1856 Lex();
1857 }
1858 return TokError("Too many arguments");
1859}
1860
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001861bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1862 const Macro *M) {
1863 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1864 // this, although we should protect against infinite loops.
1865 if (ActiveMacros.size() == 20)
1866 return TokError("macros cannot be nested more than 20 levels deep");
1867
Rafael Espindola8a403d32012-08-08 14:51:03 +00001868 MacroArguments A;
1869 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001870 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001871
Jim Grosbach97146442012-07-30 22:44:17 +00001872 // Remove any trailing empty arguments. Do this after-the-fact as we have
1873 // to keep empty arguments in the middle of the list or positionality
1874 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001875 while (!A.empty() && A.back().empty())
1876 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001877
Rafael Espindola65366442011-06-05 02:43:45 +00001878 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1879 // to hold the macro body with substitutions.
1880 SmallString<256> Buf;
1881 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001882 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001883
Rafael Espindola8a403d32012-08-08 14:51:03 +00001884 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001885 return true;
1886
Rafael Espindola761cb062012-06-03 23:57:14 +00001887 // We include the .endmacro in the buffer as our queue to exit the macro
1888 // instantiation.
1889 OS << ".endmacro\n";
1890
Rafael Espindola65366442011-06-05 02:43:45 +00001891 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001892 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001893
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001894 // Create the macro instantiation object and add to the current macro
1895 // instantiation stack.
1896 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001897 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001898 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001899 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001900 ActiveMacros.push_back(MI);
1901
1902 // Jump to the macro instantiation and prime the lexer.
1903 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1904 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1905 Lex();
1906
1907 return false;
1908}
1909
1910void AsmParser::HandleMacroExit() {
1911 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001912 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001913 Lex();
1914
1915 // Pop the instantiation entry.
1916 delete ActiveMacros.back();
1917 ActiveMacros.pop_back();
1918}
1919
Rafael Espindolae71cc862012-01-28 05:57:00 +00001920static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001921 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001922 case MCExpr::Binary: {
1923 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1924 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001925 break;
1926 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001927 case MCExpr::Target:
1928 case MCExpr::Constant:
1929 return false;
1930 case MCExpr::SymbolRef: {
1931 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001932 if (S.isVariable())
1933 return IsUsedIn(Sym, S.getVariableValue());
1934 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001935 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001936 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001937 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001938 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001939
1940 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001941}
1942
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001943bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1944 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001945 // FIXME: Use better location, we should use proper tokens.
1946 SMLoc EqualLoc = Lexer.getLoc();
1947
Daniel Dunbar821e3332009-08-31 08:09:28 +00001948 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001949 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001950 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001951
Rafael Espindolae71cc862012-01-28 05:57:00 +00001952 // Note: we don't count b as used in "a = b". This is to allow
1953 // a = b
1954 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001955
Daniel Dunbar3f872332009-07-28 16:08:33 +00001956 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001957 return TokError("unexpected token in assignment");
1958
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001959 // Error on assignment to '.'.
1960 if (Name == ".") {
1961 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1962 "(use '.space' or '.org').)"));
1963 }
1964
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001965 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001966 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001967
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001968 // Validate that the LHS is allowed to be a variable (either it has not been
1969 // used as a symbol, or it is an absolute symbol).
1970 MCSymbol *Sym = getContext().LookupSymbol(Name);
1971 if (Sym) {
1972 // Diagnose assignment to a label.
1973 //
1974 // FIXME: Diagnostics. Note the location of the definition as a label.
1975 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001976 if (IsUsedIn(Sym, Value))
1977 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1978 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001979 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001980 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1981 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001982 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001983 return Error(EqualLoc, "redefinition of '" + Name + "'");
1984 else if (!Sym->isVariable())
1985 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001986 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001987 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1988 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001989
1990 // Don't count these checks as uses.
1991 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001992 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001993 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001994
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001995 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001996
1997 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001998 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001999 if (NoDeadStrip)
2000 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
2001
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002002
2003 return false;
2004}
2005
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002006/// ParseIdentifier:
2007/// ::= identifier
2008/// ::= string
2009bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002010 // The assembler has relaxed rules for accepting identifiers, in particular we
2011 // allow things like '.globl $foo', which would normally be separate
2012 // tokens. At this level, we have already lexed so we cannot (currently)
2013 // handle this as a context dependent token, instead we detect adjacent tokens
2014 // and return the combined identifier.
2015 if (Lexer.is(AsmToken::Dollar)) {
2016 SMLoc DollarLoc = getLexer().getLoc();
2017
2018 // Consume the dollar sign, and check for a following identifier.
2019 Lex();
2020 if (Lexer.isNot(AsmToken::Identifier))
2021 return true;
2022
2023 // We have a '$' followed by an identifier, make sure they are adjacent.
2024 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2025 return true;
2026
2027 // Construct the joined identifier and consume the token.
2028 Res = StringRef(DollarLoc.getPointer(),
2029 getTok().getIdentifier().size() + 1);
2030 Lex();
2031 return false;
2032 }
2033
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002034 if (Lexer.isNot(AsmToken::Identifier) &&
2035 Lexer.isNot(AsmToken::String))
2036 return true;
2037
Sean Callanan18b83232010-01-19 21:44:56 +00002038 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002039
Sean Callanan79ed1a82010-01-19 20:22:31 +00002040 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002041
2042 return false;
2043}
2044
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002045/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002046/// ::= .equ identifier ',' expression
2047/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002048/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002049bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002050 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002051
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002052 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002053 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002054
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002055 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002056 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002057 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002058
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002059 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002060}
2061
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002062bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002063 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002064
2065 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002066 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002067 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2068 if (Str[i] != '\\') {
2069 Data += Str[i];
2070 continue;
2071 }
2072
2073 // Recognize escaped characters. Note that this escape semantics currently
2074 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2075 ++i;
2076 if (i == e)
2077 return TokError("unexpected backslash at end of string");
2078
2079 // Recognize octal sequences.
2080 if ((unsigned) (Str[i] - '0') <= 7) {
2081 // Consume up to three octal characters.
2082 unsigned Value = Str[i] - '0';
2083
2084 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2085 ++i;
2086 Value = Value * 8 + (Str[i] - '0');
2087
2088 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2089 ++i;
2090 Value = Value * 8 + (Str[i] - '0');
2091 }
2092 }
2093
2094 if (Value > 255)
2095 return TokError("invalid octal escape sequence (out of range)");
2096
2097 Data += (unsigned char) Value;
2098 continue;
2099 }
2100
2101 // Otherwise recognize individual escapes.
2102 switch (Str[i]) {
2103 default:
2104 // Just reject invalid escape sequences for now.
2105 return TokError("invalid escape sequence (unrecognized character)");
2106
2107 case 'b': Data += '\b'; break;
2108 case 'f': Data += '\f'; break;
2109 case 'n': Data += '\n'; break;
2110 case 'r': Data += '\r'; break;
2111 case 't': Data += '\t'; break;
2112 case '"': Data += '"'; break;
2113 case '\\': Data += '\\'; break;
2114 }
2115 }
2116
2117 return false;
2118}
2119
Daniel Dunbara0d14262009-06-24 23:30:00 +00002120/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002121/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2122bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002123 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002124 CheckForValidSection();
2125
Daniel Dunbara0d14262009-06-24 23:30:00 +00002126 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002127 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002128 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002129
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002130 std::string Data;
2131 if (ParseEscapedString(Data))
2132 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002133
2134 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002135 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2137
Sean Callanan79ed1a82010-01-19 20:22:31 +00002138 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002139
2140 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002141 break;
2142
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002143 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002144 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002145 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002146 }
2147 }
2148
Sean Callanan79ed1a82010-01-19 20:22:31 +00002149 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002150 return false;
2151}
2152
2153/// ParseDirectiveValue
2154/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2155bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002156 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002157 CheckForValidSection();
2158
Daniel Dunbara0d14262009-06-24 23:30:00 +00002159 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002160 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002161 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002162 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002163 return true;
2164
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002165 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002166 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2167 assert(Size <= 8 && "Invalid size");
2168 uint64_t IntValue = MCE->getValue();
2169 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2170 return Error(ExprLoc, "literal value out of range for directive");
2171 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2172 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002173 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002174
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002175 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002176 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002177
Daniel Dunbara0d14262009-06-24 23:30:00 +00002178 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002179 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002180 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002181 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002182 }
2183 }
2184
Sean Callanan79ed1a82010-01-19 20:22:31 +00002185 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002186 return false;
2187}
2188
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002189/// ParseDirectiveRealValue
2190/// ::= (.single | .double) [ expression (, expression)* ]
2191bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2192 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2193 CheckForValidSection();
2194
2195 for (;;) {
2196 // We don't truly support arithmetic on floating point expressions, so we
2197 // have to manually parse unary prefixes.
2198 bool IsNeg = false;
2199 if (getLexer().is(AsmToken::Minus)) {
2200 Lex();
2201 IsNeg = true;
2202 } else if (getLexer().is(AsmToken::Plus))
2203 Lex();
2204
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002205 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002206 getLexer().isNot(AsmToken::Real) &&
2207 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002208 return TokError("unexpected token in directive");
2209
2210 // Convert to an APFloat.
2211 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002212 StringRef IDVal = getTok().getString();
2213 if (getLexer().is(AsmToken::Identifier)) {
2214 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2215 Value = APFloat::getInf(Semantics);
2216 else if (!IDVal.compare_lower("nan"))
2217 Value = APFloat::getNaN(Semantics, false, ~0);
2218 else
2219 return TokError("invalid floating point literal");
2220 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002221 APFloat::opInvalidOp)
2222 return TokError("invalid floating point literal");
2223 if (IsNeg)
2224 Value.changeSign();
2225
2226 // Consume the numeric token.
2227 Lex();
2228
2229 // Emit the value as an integer.
2230 APInt AsInt = Value.bitcastToAPInt();
2231 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2232 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2233
2234 if (getLexer().is(AsmToken::EndOfStatement))
2235 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002236
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002237 if (getLexer().isNot(AsmToken::Comma))
2238 return TokError("unexpected token in directive");
2239 Lex();
2240 }
2241 }
2242
2243 Lex();
2244 return false;
2245}
2246
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002247/// ParseDirectiveZero
2248/// ::= .zero expression
2249bool AsmParser::ParseDirectiveZero() {
2250 CheckForValidSection();
2251
2252 int64_t NumBytes;
2253 if (ParseAbsoluteExpression(NumBytes))
2254 return true;
2255
Rafael Espindolae452b172010-10-05 19:42:57 +00002256 int64_t Val = 0;
2257 if (getLexer().is(AsmToken::Comma)) {
2258 Lex();
2259 if (ParseAbsoluteExpression(Val))
2260 return true;
2261 }
2262
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002263 if (getLexer().isNot(AsmToken::EndOfStatement))
2264 return TokError("unexpected token in '.zero' directive");
2265
2266 Lex();
2267
Rafael Espindolae452b172010-10-05 19:42:57 +00002268 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002269
2270 return false;
2271}
2272
Daniel Dunbara0d14262009-06-24 23:30:00 +00002273/// ParseDirectiveFill
2274/// ::= .fill expression , expression , expression
2275bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002276 CheckForValidSection();
2277
Daniel Dunbara0d14262009-06-24 23:30:00 +00002278 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002279 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002280 return true;
2281
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002282 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002283 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002284 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002285
Daniel Dunbara0d14262009-06-24 23:30:00 +00002286 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002287 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002288 return true;
2289
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002290 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002291 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002292 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002293
Daniel Dunbara0d14262009-06-24 23:30:00 +00002294 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002295 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002296 return true;
2297
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002298 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002299 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002300
Sean Callanan79ed1a82010-01-19 20:22:31 +00002301 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002302
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002303 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2304 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002305
2306 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002307 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002308
2309 return false;
2310}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002311
2312/// ParseDirectiveOrg
2313/// ::= .org expression [ , expression ]
2314bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002315 CheckForValidSection();
2316
Daniel Dunbar821e3332009-08-31 08:09:28 +00002317 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002318 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002319 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002320 return true;
2321
2322 // Parse optional fill expression.
2323 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002324 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2325 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002326 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002327 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002328
Daniel Dunbar475839e2009-06-29 20:37:27 +00002329 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002330 return true;
2331
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002332 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002333 return TokError("unexpected token in '.org' directive");
2334 }
2335
Sean Callanan79ed1a82010-01-19 20:22:31 +00002336 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002337
Jim Grosbachebd4c052012-01-27 00:37:08 +00002338 // Only limited forms of relocatable expressions are accepted here, it
2339 // has to be relative to the current section. The streamer will return
2340 // 'true' if the expression wasn't evaluatable.
2341 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2342 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002343
2344 return false;
2345}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002346
2347/// ParseDirectiveAlign
2348/// ::= {.align, ...} expression [ , expression [ , expression ]]
2349bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002350 CheckForValidSection();
2351
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002352 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002353 int64_t Alignment;
2354 if (ParseAbsoluteExpression(Alignment))
2355 return true;
2356
2357 SMLoc MaxBytesLoc;
2358 bool HasFillExpr = false;
2359 int64_t FillExpr = 0;
2360 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002361 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2362 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002363 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002364 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002365
2366 // The fill expression can be omitted while specifying a maximum number of
2367 // alignment bytes, e.g:
2368 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002369 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002370 HasFillExpr = true;
2371 if (ParseAbsoluteExpression(FillExpr))
2372 return true;
2373 }
2374
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002375 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2376 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002377 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002378 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002379
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002380 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002381 if (ParseAbsoluteExpression(MaxBytesToFill))
2382 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002383
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002384 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002385 return TokError("unexpected token in directive");
2386 }
2387 }
2388
Sean Callanan79ed1a82010-01-19 20:22:31 +00002389 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002390
Daniel Dunbar648ac512010-05-17 21:54:30 +00002391 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002392 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002393
2394 // Compute alignment in bytes.
2395 if (IsPow2) {
2396 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002397 if (Alignment >= 32) {
2398 Error(AlignmentLoc, "invalid alignment value");
2399 Alignment = 31;
2400 }
2401
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002402 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002403 }
2404
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002405 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002406 if (MaxBytesLoc.isValid()) {
2407 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002408 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2409 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002410 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002411 }
2412
2413 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002414 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2415 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002416 MaxBytesToFill = 0;
2417 }
2418 }
2419
Daniel Dunbar648ac512010-05-17 21:54:30 +00002420 // Check whether we should use optimal code alignment for this .align
2421 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002422 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002423 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2424 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002425 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002426 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002427 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002428 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2429 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002430 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002431
2432 return false;
2433}
2434
Eli Bendersky4766ef42012-12-20 19:05:53 +00002435
2436/// ParseDirectiveBundleAlignMode
2437/// ::= {.bundle_align_mode} expression
2438bool AsmParser::ParseDirectiveBundleAlignMode() {
2439 CheckForValidSection();
2440
2441 // Expect a single argument: an expression that evaluates to a constant
2442 // in the inclusive range 0-30.
2443 SMLoc ExprLoc = getLexer().getLoc();
2444 int64_t AlignSizePow2;
2445 if (ParseAbsoluteExpression(AlignSizePow2))
2446 return true;
2447 else if (getLexer().isNot(AsmToken::EndOfStatement))
2448 return TokError("unexpected token after expression in"
2449 " '.bundle_align_mode' directive");
2450 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2451 return Error(ExprLoc,
2452 "invalid bundle alignment size (expected between 0 and 30)");
2453
2454 Lex();
2455
2456 // Because of AlignSizePow2's verified range we can safely truncate it to
2457 // unsigned.
2458 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2459 return false;
2460}
2461
2462/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002463/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002464bool AsmParser::ParseDirectiveBundleLock() {
2465 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002466 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002467
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002468 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2469 StringRef Option;
2470 SMLoc Loc = getTok().getLoc();
2471 const char *kInvalidOptionError =
2472 "invalid option for '.bundle_lock' directive";
2473
2474 if (ParseIdentifier(Option))
2475 return Error(Loc, kInvalidOptionError);
2476
2477 if (Option != "align_to_end")
2478 return Error(Loc, kInvalidOptionError);
2479 else if (getLexer().isNot(AsmToken::EndOfStatement))
2480 return Error(Loc,
2481 "unexpected token after '.bundle_lock' directive option");
2482 AlignToEnd = true;
2483 }
2484
Eli Bendersky4766ef42012-12-20 19:05:53 +00002485 Lex();
2486
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002487 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002488 return false;
2489}
2490
2491/// ParseDirectiveBundleLock
2492/// ::= {.bundle_lock}
2493bool AsmParser::ParseDirectiveBundleUnlock() {
2494 CheckForValidSection();
2495
2496 if (getLexer().isNot(AsmToken::EndOfStatement))
2497 return TokError("unexpected token in '.bundle_unlock' directive");
2498 Lex();
2499
2500 getStreamer().EmitBundleUnlock();
2501 return false;
2502}
2503
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002504/// ParseDirectiveSymbolAttribute
2505/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002506bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002507 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002508 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002509 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002510 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002511
2512 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002513 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002514
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002515 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002516
Jim Grosbach10ec6502011-09-15 17:56:49 +00002517 // Assembler local symbols don't make any sense here. Complain loudly.
2518 if (Sym->isTemporary())
2519 return Error(Loc, "non-local symbol required in directive");
2520
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002521 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002522
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002523 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002524 break;
2525
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002526 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002527 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002528 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002529 }
2530 }
2531
Sean Callanan79ed1a82010-01-19 20:22:31 +00002532 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002533 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002534}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002535
2536/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002537/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2538bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002539 CheckForValidSection();
2540
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002541 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002542 StringRef Name;
2543 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002544 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002545
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002546 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002547 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002548
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002549 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002550 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002551 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002552
2553 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002554 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002555 if (ParseAbsoluteExpression(Size))
2556 return true;
2557
2558 int64_t Pow2Alignment = 0;
2559 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002560 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002561 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002562 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002563 if (ParseAbsoluteExpression(Pow2Alignment))
2564 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002565
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002566 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2567 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002568 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2569
Chris Lattner258281d2010-01-19 06:22:22 +00002570 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002571 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2572 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002573 if (!isPowerOf2_64(Pow2Alignment))
2574 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2575 Pow2Alignment = Log2_64(Pow2Alignment);
2576 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002577 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002578
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002579 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002580 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002581
Sean Callanan79ed1a82010-01-19 20:22:31 +00002582 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002583
Chris Lattner1fc3d752009-07-09 17:25:12 +00002584 // NOTE: a size of zero for a .comm should create a undefined symbol
2585 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002586 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002587 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2588 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002589
Eric Christopherc260a3e2010-05-14 01:38:54 +00002590 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002591 // may internally end up wanting an alignment in bytes.
2592 // FIXME: Diagnose overflow.
2593 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002594 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2595 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002596
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002597 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002598 return Error(IDLoc, "invalid symbol redefinition");
2599
Chris Lattner1fc3d752009-07-09 17:25:12 +00002600 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002601 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002602 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002603 return false;
2604 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002605
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002606 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002607 return false;
2608}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002609
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002610/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002611/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002612bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002613 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002614 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002615
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002616 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002617 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002618 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002619
Sean Callanan79ed1a82010-01-19 20:22:31 +00002620 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002621
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002622 if (Str.empty())
2623 Error(Loc, ".abort detected. Assembly stopping.");
2624 else
2625 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002626 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002627
2628 return false;
2629}
Kevin Enderby71148242009-07-14 21:35:03 +00002630
Kevin Enderby1f049b22009-07-14 23:21:55 +00002631/// ParseDirectiveInclude
2632/// ::= .include "filename"
2633bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002634 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002635 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002636
Sean Callanan18b83232010-01-19 21:44:56 +00002637 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002638 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002639 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002640
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002641 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002642 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002643
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002644 // Strip the quotes.
2645 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002646
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002647 // Attempt to switch the lexer to the included file before consuming the end
2648 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002649 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002650 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002651 return true;
2652 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002653
2654 return false;
2655}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002656
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002657/// ParseDirectiveIncbin
2658/// ::= .incbin "filename"
2659bool AsmParser::ParseDirectiveIncbin() {
2660 if (getLexer().isNot(AsmToken::String))
2661 return TokError("expected string in '.incbin' directive");
2662
2663 std::string Filename = getTok().getString();
2664 SMLoc IncbinLoc = getLexer().getLoc();
2665 Lex();
2666
2667 if (getLexer().isNot(AsmToken::EndOfStatement))
2668 return TokError("unexpected token in '.incbin' directive");
2669
2670 // Strip the quotes.
2671 Filename = Filename.substr(1, Filename.size()-2);
2672
2673 // Attempt to process the included file.
2674 if (ProcessIncbinFile(Filename)) {
2675 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2676 return true;
2677 }
2678
2679 return false;
2680}
2681
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002682/// ParseDirectiveIf
2683/// ::= .if expression
2684bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002685 TheCondStack.push_back(TheCondState);
2686 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002687 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002688 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002689 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002690 int64_t ExprValue;
2691 if (ParseAbsoluteExpression(ExprValue))
2692 return true;
2693
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002694 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002695 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002696
Sean Callanan79ed1a82010-01-19 20:22:31 +00002697 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002698
2699 TheCondState.CondMet = ExprValue;
2700 TheCondState.Ignore = !TheCondState.CondMet;
2701 }
2702
2703 return false;
2704}
2705
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002706/// ParseDirectiveIfb
2707/// ::= .ifb string
2708bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2709 TheCondStack.push_back(TheCondState);
2710 TheCondState.TheCond = AsmCond::IfCond;
2711
Benjamin Kramer29739e72012-05-12 16:52:21 +00002712 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002713 EatToEndOfStatement();
2714 } else {
2715 StringRef Str = ParseStringToEndOfStatement();
2716
2717 if (getLexer().isNot(AsmToken::EndOfStatement))
2718 return TokError("unexpected token in '.ifb' directive");
2719
2720 Lex();
2721
2722 TheCondState.CondMet = ExpectBlank == Str.empty();
2723 TheCondState.Ignore = !TheCondState.CondMet;
2724 }
2725
2726 return false;
2727}
2728
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002729/// ParseDirectiveIfc
2730/// ::= .ifc string1, string2
2731bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2732 TheCondStack.push_back(TheCondState);
2733 TheCondState.TheCond = AsmCond::IfCond;
2734
Benjamin Kramer29739e72012-05-12 16:52:21 +00002735 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002736 EatToEndOfStatement();
2737 } else {
2738 StringRef Str1 = ParseStringToComma();
2739
2740 if (getLexer().isNot(AsmToken::Comma))
2741 return TokError("unexpected token in '.ifc' directive");
2742
2743 Lex();
2744
2745 StringRef Str2 = ParseStringToEndOfStatement();
2746
2747 if (getLexer().isNot(AsmToken::EndOfStatement))
2748 return TokError("unexpected token in '.ifc' directive");
2749
2750 Lex();
2751
2752 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2753 TheCondState.Ignore = !TheCondState.CondMet;
2754 }
2755
2756 return false;
2757}
2758
2759/// ParseDirectiveIfdef
2760/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002761bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2762 StringRef Name;
2763 TheCondStack.push_back(TheCondState);
2764 TheCondState.TheCond = AsmCond::IfCond;
2765
2766 if (TheCondState.Ignore) {
2767 EatToEndOfStatement();
2768 } else {
2769 if (ParseIdentifier(Name))
2770 return TokError("expected identifier after '.ifdef'");
2771
2772 Lex();
2773
2774 MCSymbol *Sym = getContext().LookupSymbol(Name);
2775
2776 if (expect_defined)
2777 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2778 else
2779 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2780 TheCondState.Ignore = !TheCondState.CondMet;
2781 }
2782
2783 return false;
2784}
2785
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002786/// ParseDirectiveElseIf
2787/// ::= .elseif expression
2788bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2789 if (TheCondState.TheCond != AsmCond::IfCond &&
2790 TheCondState.TheCond != AsmCond::ElseIfCond)
2791 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2792 " an .elseif");
2793 TheCondState.TheCond = AsmCond::ElseIfCond;
2794
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002795 bool LastIgnoreState = false;
2796 if (!TheCondStack.empty())
2797 LastIgnoreState = TheCondStack.back().Ignore;
2798 if (LastIgnoreState || TheCondState.CondMet) {
2799 TheCondState.Ignore = true;
2800 EatToEndOfStatement();
2801 }
2802 else {
2803 int64_t ExprValue;
2804 if (ParseAbsoluteExpression(ExprValue))
2805 return true;
2806
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002807 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002808 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002809
Sean Callanan79ed1a82010-01-19 20:22:31 +00002810 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002811 TheCondState.CondMet = ExprValue;
2812 TheCondState.Ignore = !TheCondState.CondMet;
2813 }
2814
2815 return false;
2816}
2817
2818/// ParseDirectiveElse
2819/// ::= .else
2820bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002821 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002822 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002823
Sean Callanan79ed1a82010-01-19 20:22:31 +00002824 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002825
2826 if (TheCondState.TheCond != AsmCond::IfCond &&
2827 TheCondState.TheCond != AsmCond::ElseIfCond)
2828 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2829 ".elseif");
2830 TheCondState.TheCond = AsmCond::ElseCond;
2831 bool LastIgnoreState = false;
2832 if (!TheCondStack.empty())
2833 LastIgnoreState = TheCondStack.back().Ignore;
2834 if (LastIgnoreState || TheCondState.CondMet)
2835 TheCondState.Ignore = true;
2836 else
2837 TheCondState.Ignore = false;
2838
2839 return false;
2840}
2841
2842/// ParseDirectiveEndIf
2843/// ::= .endif
2844bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002845 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002846 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002847
Sean Callanan79ed1a82010-01-19 20:22:31 +00002848 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002849
2850 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2851 TheCondStack.empty())
2852 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2853 ".else");
2854 if (!TheCondStack.empty()) {
2855 TheCondState = TheCondStack.back();
2856 TheCondStack.pop_back();
2857 }
2858
2859 return false;
2860}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002861
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002862void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002863 DirectiveKindMapping[".set"] = DK_SET;
2864 DirectiveKindMapping[".equ"] = DK_EQU;
2865 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2866 DirectiveKindMapping[".ascii"] = DK_ASCII;
2867 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2868 DirectiveKindMapping[".string"] = DK_STRING;
2869 DirectiveKindMapping[".byte"] = DK_BYTE;
2870 DirectiveKindMapping[".short"] = DK_SHORT;
2871 DirectiveKindMapping[".value"] = DK_VALUE;
2872 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2873 DirectiveKindMapping[".long"] = DK_LONG;
2874 DirectiveKindMapping[".int"] = DK_INT;
2875 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2876 DirectiveKindMapping[".quad"] = DK_QUAD;
2877 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2878 DirectiveKindMapping[".single"] = DK_SINGLE;
2879 DirectiveKindMapping[".float"] = DK_FLOAT;
2880 DirectiveKindMapping[".double"] = DK_DOUBLE;
2881 DirectiveKindMapping[".align"] = DK_ALIGN;
2882 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2883 DirectiveKindMapping[".balign"] = DK_BALIGN;
2884 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2885 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2886 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2887 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2888 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2889 DirectiveKindMapping[".org"] = DK_ORG;
2890 DirectiveKindMapping[".fill"] = DK_FILL;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002891 DirectiveKindMapping[".zero"] = DK_ZERO;
2892 DirectiveKindMapping[".extern"] = DK_EXTERN;
2893 DirectiveKindMapping[".globl"] = DK_GLOBL;
2894 DirectiveKindMapping[".global"] = DK_GLOBAL;
2895 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2896 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2897 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2898 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2899 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2900 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2901 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2902 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2903 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2904 DirectiveKindMapping[".comm"] = DK_COMM;
2905 DirectiveKindMapping[".common"] = DK_COMMON;
2906 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2907 DirectiveKindMapping[".abort"] = DK_ABORT;
2908 DirectiveKindMapping[".include"] = DK_INCLUDE;
2909 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2910 DirectiveKindMapping[".code16"] = DK_CODE16;
2911 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2912 DirectiveKindMapping[".rept"] = DK_REPT;
2913 DirectiveKindMapping[".irp"] = DK_IRP;
2914 DirectiveKindMapping[".irpc"] = DK_IRPC;
2915 DirectiveKindMapping[".endr"] = DK_ENDR;
2916 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2917 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2918 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2919 DirectiveKindMapping[".if"] = DK_IF;
2920 DirectiveKindMapping[".ifb"] = DK_IFB;
2921 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2922 DirectiveKindMapping[".ifc"] = DK_IFC;
2923 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2924 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2925 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2926 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2927 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2928 DirectiveKindMapping[".else"] = DK_ELSE;
2929 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002930}
2931
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002932/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002933/// ::= .file [number] filename
2934/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002935bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002936 // FIXME: I'm not sure what this is.
2937 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002938 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002939 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002940 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002941 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002942
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002943 if (FileNumber < 1)
2944 return TokError("file number less than one");
2945 }
2946
Daniel Dunbareceec052010-07-12 17:45:27 +00002947 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002948 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002949
Nick Lewycky44d798d2011-10-17 23:05:28 +00002950 // Usually the directory and filename together, otherwise just the directory.
2951 StringRef Path = getTok().getString();
2952 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002953 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002954
Nick Lewycky44d798d2011-10-17 23:05:28 +00002955 StringRef Directory;
2956 StringRef Filename;
2957 if (getLexer().is(AsmToken::String)) {
2958 if (FileNumber == -1)
2959 return TokError("explicit path specified, but no file number");
2960 Filename = getTok().getString();
2961 Filename = Filename.substr(1, Filename.size()-2);
2962 Directory = Path;
2963 Lex();
2964 } else {
2965 Filename = Path;
2966 }
2967
Daniel Dunbareceec052010-07-12 17:45:27 +00002968 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002969 return TokError("unexpected token in '.file' directive");
2970
Chris Lattnerd32e8032010-01-25 19:02:58 +00002971 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002972 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002973 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002974 if (getContext().getGenDwarfForAssembly() == true)
2975 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2976 "used to generate dwarf debug info for assembly code");
2977
Nick Lewycky44d798d2011-10-17 23:05:28 +00002978 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002979 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002980 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002981
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002982 return false;
2983}
2984
2985/// ParseDirectiveLine
2986/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002987bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002988 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2989 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002990 return TokError("unexpected token in '.line' directive");
2991
Sean Callanan18b83232010-01-19 21:44:56 +00002992 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002993 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002994 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002995
2996 // FIXME: Do something with the .line.
2997 }
2998
Daniel Dunbareceec052010-07-12 17:45:27 +00002999 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00003000 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003001
3002 return false;
3003}
3004
3005
3006/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003007/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003008/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3009/// The first number is a file number, must have been previously assigned with
3010/// a .file directive, the second number is the line number and optionally the
3011/// third number is a column position (zero if not specified). The remaining
3012/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003013bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003014
Daniel Dunbareceec052010-07-12 17:45:27 +00003015 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003016 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003017 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003018 if (FileNumber < 1)
3019 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003020 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003021 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003022 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003023
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003024 int64_t LineNumber = 0;
3025 if (getLexer().is(AsmToken::Integer)) {
3026 LineNumber = getTok().getIntVal();
3027 if (LineNumber < 1)
3028 return TokError("line number less than one in '.loc' directive");
3029 Lex();
3030 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003031
3032 int64_t ColumnPos = 0;
3033 if (getLexer().is(AsmToken::Integer)) {
3034 ColumnPos = getTok().getIntVal();
3035 if (ColumnPos < 0)
3036 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003037 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003038 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003039
Kevin Enderbyc0957932010-09-30 16:52:03 +00003040 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003041 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003042 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003043 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3044 for (;;) {
3045 if (getLexer().is(AsmToken::EndOfStatement))
3046 break;
3047
3048 StringRef Name;
3049 SMLoc Loc = getTok().getLoc();
3050 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003051 return TokError("unexpected token in '.loc' directive");
3052
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003053 if (Name == "basic_block")
3054 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3055 else if (Name == "prologue_end")
3056 Flags |= DWARF2_FLAG_PROLOGUE_END;
3057 else if (Name == "epilogue_begin")
3058 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3059 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003060 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003061 const MCExpr *Value;
3062 if (getParser().ParseExpression(Value))
3063 return true;
3064 // The expression must be the constant 0 or 1.
3065 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3066 int Value = MCE->getValue();
3067 if (Value == 0)
3068 Flags &= ~DWARF2_FLAG_IS_STMT;
3069 else if (Value == 1)
3070 Flags |= DWARF2_FLAG_IS_STMT;
3071 else
3072 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003073 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003074 else {
3075 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3076 }
3077 }
3078 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003079 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003080 const MCExpr *Value;
3081 if (getParser().ParseExpression(Value))
3082 return true;
3083 // The expression must be a constant greater or equal to 0.
3084 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3085 int Value = MCE->getValue();
3086 if (Value < 0)
3087 return Error(Loc, "isa number less than zero");
3088 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003089 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003090 else {
3091 return Error(Loc, "isa number not a constant value");
3092 }
3093 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003094 else if (Name == "discriminator") {
3095 if (getParser().ParseAbsoluteExpression(Discriminator))
3096 return true;
3097 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003098 else {
3099 return Error(Loc, "unknown sub-directive in '.loc' directive");
3100 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003101
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003102 if (getLexer().is(AsmToken::EndOfStatement))
3103 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003104 }
3105 }
3106
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003107 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003108 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003109
3110 return false;
3111}
3112
Daniel Dunbar138abae2010-10-16 04:56:42 +00003113/// ParseDirectiveStabs
3114/// ::= .stabs string, number, number, number
3115bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3116 SMLoc DirectiveLoc) {
3117 return TokError("unsupported directive '" + Directive + "'");
3118}
3119
Eli Bendersky9b1bb052013-01-11 22:55:28 +00003120/// ParseDirectiveSpace
3121/// ::= .space expression [ , expression ]
3122bool GenericAsmParser::ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc) {
3123 getParser().CheckForValidSection();
3124
3125 int64_t NumBytes;
3126 if (getParser().ParseAbsoluteExpression(NumBytes))
3127 return true;
3128
3129 int64_t FillExpr = 0;
3130 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3131 if (getLexer().isNot(AsmToken::Comma))
3132 return TokError("unexpected token in '.space' directive");
3133 Lex();
3134
3135 if (getParser().ParseAbsoluteExpression(FillExpr))
3136 return true;
3137
3138 if (getLexer().isNot(AsmToken::EndOfStatement))
3139 return TokError("unexpected token in '.space' directive");
3140 }
3141
3142 Lex();
3143
3144 if (NumBytes <= 0)
3145 return TokError("invalid number of bytes in '.space' directive");
3146
3147 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3148 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
3149
3150 return false;
3151}
3152
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003153/// ParseDirectiveCFISections
3154/// ::= .cfi_sections section [, section]
3155bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3156 SMLoc DirectiveLoc) {
3157 StringRef Name;
3158 bool EH = false;
3159 bool Debug = false;
3160
3161 if (getParser().ParseIdentifier(Name))
3162 return TokError("Expected an identifier");
3163
3164 if (Name == ".eh_frame")
3165 EH = true;
3166 else if (Name == ".debug_frame")
3167 Debug = true;
3168
3169 if (getLexer().is(AsmToken::Comma)) {
3170 Lex();
3171
3172 if (getParser().ParseIdentifier(Name))
3173 return TokError("Expected an identifier");
3174
3175 if (Name == ".eh_frame")
3176 EH = true;
3177 else if (Name == ".debug_frame")
3178 Debug = true;
3179 }
3180
3181 getStreamer().EmitCFISections(EH, Debug);
3182
3183 return false;
3184}
3185
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003186/// ParseDirectiveCFIStartProc
3187/// ::= .cfi_startproc
3188bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3189 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003190 getStreamer().EmitCFIStartProc();
3191 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003192}
3193
3194/// ParseDirectiveCFIEndProc
3195/// ::= .cfi_endproc
3196bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003197 getStreamer().EmitCFIEndProc();
3198 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003199}
3200
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003201/// ParseRegisterOrRegisterNumber - parse register name or number.
3202bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3203 SMLoc DirectiveLoc) {
3204 unsigned RegNo;
3205
Jim Grosbach6f888a82011-06-02 17:14:04 +00003206 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003207 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3208 DirectiveLoc))
3209 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003210 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003211 } else
3212 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003213
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003214 return false;
3215}
3216
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003217/// ParseDirectiveCFIDefCfa
3218/// ::= .cfi_def_cfa register, offset
3219bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3220 SMLoc DirectiveLoc) {
3221 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003222 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003223 return true;
3224
3225 if (getLexer().isNot(AsmToken::Comma))
3226 return TokError("unexpected token in directive");
3227 Lex();
3228
3229 int64_t Offset = 0;
3230 if (getParser().ParseAbsoluteExpression(Offset))
3231 return true;
3232
Rafael Espindola066c2f42011-04-12 23:59:07 +00003233 getStreamer().EmitCFIDefCfa(Register, Offset);
3234 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003235}
3236
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003237/// ParseDirectiveCFIDefCfaOffset
3238/// ::= .cfi_def_cfa_offset offset
3239bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3240 SMLoc DirectiveLoc) {
3241 int64_t Offset = 0;
3242 if (getParser().ParseAbsoluteExpression(Offset))
3243 return true;
3244
Rafael Espindola066c2f42011-04-12 23:59:07 +00003245 getStreamer().EmitCFIDefCfaOffset(Offset);
3246 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003247}
3248
3249/// ParseDirectiveCFIAdjustCfaOffset
3250/// ::= .cfi_adjust_cfa_offset adjustment
3251bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3252 SMLoc DirectiveLoc) {
3253 int64_t Adjustment = 0;
3254 if (getParser().ParseAbsoluteExpression(Adjustment))
3255 return true;
3256
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003257 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3258 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003259}
3260
3261/// ParseDirectiveCFIDefCfaRegister
3262/// ::= .cfi_def_cfa_register register
3263bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3264 SMLoc DirectiveLoc) {
3265 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003266 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003267 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003268
Rafael Espindola066c2f42011-04-12 23:59:07 +00003269 getStreamer().EmitCFIDefCfaRegister(Register);
3270 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003271}
3272
3273/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003274/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003275bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3276 int64_t Register = 0;
3277 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003278
3279 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003280 return true;
3281
3282 if (getLexer().isNot(AsmToken::Comma))
3283 return TokError("unexpected token in directive");
3284 Lex();
3285
3286 if (getParser().ParseAbsoluteExpression(Offset))
3287 return true;
3288
Rafael Espindola066c2f42011-04-12 23:59:07 +00003289 getStreamer().EmitCFIOffset(Register, Offset);
3290 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003291}
3292
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003293/// ParseDirectiveCFIRelOffset
3294/// ::= .cfi_rel_offset register, offset
3295bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3296 SMLoc DirectiveLoc) {
3297 int64_t Register = 0;
3298
3299 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3300 return true;
3301
3302 if (getLexer().isNot(AsmToken::Comma))
3303 return TokError("unexpected token in directive");
3304 Lex();
3305
3306 int64_t Offset = 0;
3307 if (getParser().ParseAbsoluteExpression(Offset))
3308 return true;
3309
Rafael Espindola25f492e2011-04-12 16:12:03 +00003310 getStreamer().EmitCFIRelOffset(Register, Offset);
3311 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003312}
3313
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003314static bool isValidEncoding(int64_t Encoding) {
3315 if (Encoding & ~0xff)
3316 return false;
3317
3318 if (Encoding == dwarf::DW_EH_PE_omit)
3319 return true;
3320
3321 const unsigned Format = Encoding & 0xf;
3322 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3323 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3324 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3325 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3326 return false;
3327
Rafael Espindolacaf11582010-12-29 04:31:26 +00003328 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003329 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003330 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003331 return false;
3332
3333 return true;
3334}
3335
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003336/// ParseDirectiveCFIPersonalityOrLsda
3337/// ::= .cfi_personality encoding, [symbol_name]
3338/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003339bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003340 SMLoc DirectiveLoc) {
3341 int64_t Encoding = 0;
3342 if (getParser().ParseAbsoluteExpression(Encoding))
3343 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003344 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003345 return false;
3346
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003347 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003348 return TokError("unsupported encoding.");
3349
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003350 if (getLexer().isNot(AsmToken::Comma))
3351 return TokError("unexpected token in directive");
3352 Lex();
3353
3354 StringRef Name;
3355 if (getParser().ParseIdentifier(Name))
3356 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003357
3358 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3359
3360 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003361 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003362 else {
3363 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003364 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003365 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003366 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003367}
3368
Rafael Espindolafe024d02010-12-28 18:36:23 +00003369/// ParseDirectiveCFIRememberState
3370/// ::= .cfi_remember_state
3371bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3372 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003373 getStreamer().EmitCFIRememberState();
3374 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003375}
3376
3377/// ParseDirectiveCFIRestoreState
3378/// ::= .cfi_remember_state
3379bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3380 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003381 getStreamer().EmitCFIRestoreState();
3382 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003383}
3384
Rafael Espindolac5754392011-04-12 15:31:05 +00003385/// ParseDirectiveCFISameValue
3386/// ::= .cfi_same_value register
3387bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3388 SMLoc DirectiveLoc) {
3389 int64_t Register = 0;
3390
3391 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3392 return true;
3393
3394 getStreamer().EmitCFISameValue(Register);
3395
3396 return false;
3397}
3398
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003399/// ParseDirectiveCFIRestore
3400/// ::= .cfi_restore register
3401bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003402 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003403 int64_t Register = 0;
3404 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3405 return true;
3406
3407 getStreamer().EmitCFIRestore(Register);
3408
3409 return false;
3410}
3411
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003412/// ParseDirectiveCFIEscape
3413/// ::= .cfi_escape expression[,...]
3414bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003415 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003416 std::string Values;
3417 int64_t CurrValue;
3418 if (getParser().ParseAbsoluteExpression(CurrValue))
3419 return true;
3420
3421 Values.push_back((uint8_t)CurrValue);
3422
3423 while (getLexer().is(AsmToken::Comma)) {
3424 Lex();
3425
3426 if (getParser().ParseAbsoluteExpression(CurrValue))
3427 return true;
3428
3429 Values.push_back((uint8_t)CurrValue);
3430 }
3431
3432 getStreamer().EmitCFIEscape(Values);
3433 return false;
3434}
3435
Rafael Espindola16d7d432012-01-23 21:51:52 +00003436/// ParseDirectiveCFISignalFrame
3437/// ::= .cfi_signal_frame
3438bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3439 SMLoc DirectiveLoc) {
3440 if (getLexer().isNot(AsmToken::EndOfStatement))
3441 return Error(getLexer().getLoc(),
3442 "unexpected token in '" + Directive + "' directive");
3443
3444 getStreamer().EmitCFISignalFrame();
3445
3446 return false;
3447}
3448
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003449/// ParseDirectiveCFIUndefined
3450/// ::= .cfi_undefined register
3451bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3452 SMLoc DirectiveLoc) {
3453 int64_t Register = 0;
3454
3455 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3456 return true;
3457
3458 getStreamer().EmitCFIUndefined(Register);
3459
3460 return false;
3461}
3462
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003463/// ParseDirectiveCFIRegister
3464/// ::= .cfi_register register, register
3465bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3466 SMLoc DirectiveLoc) {
3467 int64_t Register1 = 0;
3468
3469 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3470 return true;
3471
3472 if (getLexer().isNot(AsmToken::Comma))
3473 return TokError("unexpected token in directive");
3474 Lex();
3475
3476 int64_t Register2 = 0;
3477
3478 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3479 return true;
3480
3481 getStreamer().EmitCFIRegister(Register1, Register2);
3482
3483 return false;
3484}
3485
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003486/// ParseDirectiveMacrosOnOff
3487/// ::= .macros_on
3488/// ::= .macros_off
3489bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3490 SMLoc DirectiveLoc) {
3491 if (getLexer().isNot(AsmToken::EndOfStatement))
3492 return Error(getLexer().getLoc(),
3493 "unexpected token in '" + Directive + "' directive");
3494
Eli Bendersky733c3362013-01-14 18:08:41 +00003495 getParser().SetMacrosEnabled(Directive == ".macros_on");
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003496
3497 return false;
3498}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003499
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003500/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003501/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003502bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3503 SMLoc DirectiveLoc) {
3504 StringRef Name;
3505 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003506 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003507
Rafael Espindola8a403d32012-08-08 14:51:03 +00003508 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003509 // Argument delimiter is initially unknown. It will be set by
3510 // ParseMacroArgument()
3511 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003512 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003513 for (;;) {
3514 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003515 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003516 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003517
3518 if (getLexer().is(AsmToken::Equal)) {
3519 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003520 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003521 return true;
3522 }
3523
Rafael Espindola65366442011-06-05 02:43:45 +00003524 Parameters.push_back(Parameter);
3525
Preston Gurd7b6f2032012-09-19 20:36:12 +00003526 if (getLexer().is(AsmToken::Comma))
3527 Lex();
3528 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003529 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003530 }
3531 }
3532
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003533 // Eat the end of statement.
3534 Lex();
3535
3536 AsmToken EndToken, StartToken = getTok();
3537
3538 // Lex the macro definition.
3539 for (;;) {
3540 // Check whether we have reached the end of the file.
3541 if (getLexer().is(AsmToken::Eof))
3542 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3543
3544 // Otherwise, check whether we have reach the .endmacro.
3545 if (getLexer().is(AsmToken::Identifier) &&
3546 (getTok().getIdentifier() == ".endm" ||
3547 getTok().getIdentifier() == ".endmacro")) {
3548 EndToken = getTok();
3549 Lex();
3550 if (getLexer().isNot(AsmToken::EndOfStatement))
3551 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3552 "' directive");
3553 break;
3554 }
3555
3556 // Otherwise, scan til the end of the statement.
3557 getParser().EatToEndOfStatement();
3558 }
3559
3560 if (getParser().MacroMap.lookup(Name)) {
3561 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3562 }
3563
3564 const char *BodyStart = StartToken.getLoc().getPointer();
3565 const char *BodyEnd = EndToken.getLoc().getPointer();
3566 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003567 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003568 return false;
3569}
3570
3571/// ParseDirectiveEndMacro
3572/// ::= .endm
3573/// ::= .endmacro
3574bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003575 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003576 if (getLexer().isNot(AsmToken::EndOfStatement))
3577 return TokError("unexpected token in '" + Directive + "' directive");
3578
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003579 // If we are inside a macro instantiation, terminate the current
3580 // instantiation.
3581 if (!getParser().ActiveMacros.empty()) {
3582 getParser().HandleMacroExit();
3583 return false;
3584 }
3585
3586 // Otherwise, this .endmacro is a stray entry in the file; well formed
3587 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003588 return TokError("unexpected '" + Directive + "' in file, "
3589 "no current macro definition");
3590}
3591
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003592/// ParseDirectivePurgeMacro
3593/// ::= .purgem
3594bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3595 SMLoc DirectiveLoc) {
3596 StringRef Name;
3597 if (getParser().ParseIdentifier(Name))
3598 return TokError("expected identifier in '.purgem' directive");
3599
3600 if (getLexer().isNot(AsmToken::EndOfStatement))
3601 return TokError("unexpected token in '.purgem' directive");
3602
3603 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3604 if (I == getParser().MacroMap.end())
3605 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3606
3607 // Undefine the macro.
3608 delete I->getValue();
3609 getParser().MacroMap.erase(I);
3610 return false;
3611}
3612
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003613bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003614 getParser().CheckForValidSection();
3615
3616 const MCExpr *Value;
3617
3618 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003619 return true;
3620
3621 if (getLexer().isNot(AsmToken::EndOfStatement))
3622 return TokError("unexpected token in directive");
3623
3624 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003625 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003626 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003627 getStreamer().EmitULEB128Value(Value);
3628
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003629 return false;
3630}
3631
Rafael Espindola761cb062012-06-03 23:57:14 +00003632Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003633 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003634
Rafael Espindola761cb062012-06-03 23:57:14 +00003635 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003636 for (;;) {
3637 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003638 if (getLexer().is(AsmToken::Eof)) {
3639 Error(DirectiveLoc, "no matching '.endr' in definition");
3640 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003641 }
3642
Rafael Espindola761cb062012-06-03 23:57:14 +00003643 if (Lexer.is(AsmToken::Identifier) &&
3644 (getTok().getIdentifier() == ".rept")) {
3645 ++NestLevel;
3646 }
3647
3648 // Otherwise, check whether we have reached the .endr.
3649 if (Lexer.is(AsmToken::Identifier) &&
3650 getTok().getIdentifier() == ".endr") {
3651 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003652 EndToken = getTok();
3653 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003654 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3655 TokError("unexpected token in '.endr' directive");
3656 return 0;
3657 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003658 break;
3659 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003660 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003661 }
3662
Rafael Espindola761cb062012-06-03 23:57:14 +00003663 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003664 EatToEndOfStatement();
3665 }
3666
3667 const char *BodyStart = StartToken.getLoc().getPointer();
3668 const char *BodyEnd = EndToken.getLoc().getPointer();
3669 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3670
Rafael Espindola761cb062012-06-03 23:57:14 +00003671 // We Are Anonymous.
3672 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003673 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003674 return new Macro(Name, Body, Parameters);
3675}
3676
3677void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3678 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003679 OS << ".endr\n";
3680
3681 MemoryBuffer *Instantiation =
3682 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3683
Rafael Espindola761cb062012-06-03 23:57:14 +00003684 // Create the macro instantiation object and add to the current macro
3685 // instantiation stack.
3686 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00003687 CurBuffer,
Rafael Espindola761cb062012-06-03 23:57:14 +00003688 getTok().getLoc(),
3689 Instantiation);
3690 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003691
Rafael Espindola761cb062012-06-03 23:57:14 +00003692 // Jump to the macro instantiation and prime the lexer.
3693 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3694 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3695 Lex();
3696}
3697
3698bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3699 int64_t Count;
3700 if (ParseAbsoluteExpression(Count))
3701 return TokError("unexpected token in '.rept' directive");
3702
3703 if (Count < 0)
3704 return TokError("Count is negative");
3705
3706 if (Lexer.isNot(AsmToken::EndOfStatement))
3707 return TokError("unexpected token in '.rept' directive");
3708
3709 // Eat the end of statement.
3710 Lex();
3711
3712 // Lex the rept definition.
3713 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3714 if (!M)
3715 return true;
3716
3717 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3718 // to hold the macro body with substitutions.
3719 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003720 MacroParameters Parameters;
3721 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003722 raw_svector_ostream OS(Buf);
3723 while (Count--) {
3724 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3725 return true;
3726 }
3727 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003728
3729 return false;
3730}
3731
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003732/// ParseDirectiveIrp
3733/// ::= .irp symbol,values
3734bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003735 MacroParameters Parameters;
3736 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003737
Preston Gurd6c9176a2012-09-19 20:29:04 +00003738 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003739 return TokError("expected identifier in '.irp' directive");
3740
3741 Parameters.push_back(Parameter);
3742
3743 if (Lexer.isNot(AsmToken::Comma))
3744 return TokError("expected comma in '.irp' directive");
3745
3746 Lex();
3747
Rafael Espindola8a403d32012-08-08 14:51:03 +00003748 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003749 if (ParseMacroArguments(0, A))
3750 return true;
3751
3752 // Eat the end of statement.
3753 Lex();
3754
3755 // Lex the irp definition.
3756 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3757 if (!M)
3758 return true;
3759
3760 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3761 // to hold the macro body with substitutions.
3762 SmallString<256> Buf;
3763 raw_svector_ostream OS(Buf);
3764
Rafael Espindola7996d042012-08-21 16:06:48 +00003765 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3766 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003767 Args.push_back(*i);
3768
3769 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3770 return true;
3771 }
3772
3773 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3774
3775 return false;
3776}
3777
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003778/// ParseDirectiveIrpc
3779/// ::= .irpc symbol,values
3780bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003781 MacroParameters Parameters;
3782 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003783
Preston Gurd6c9176a2012-09-19 20:29:04 +00003784 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003785 return TokError("expected identifier in '.irpc' directive");
3786
3787 Parameters.push_back(Parameter);
3788
3789 if (Lexer.isNot(AsmToken::Comma))
3790 return TokError("expected comma in '.irpc' directive");
3791
3792 Lex();
3793
Rafael Espindola8a403d32012-08-08 14:51:03 +00003794 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003795 if (ParseMacroArguments(0, A))
3796 return true;
3797
3798 if (A.size() != 1 || A.front().size() != 1)
3799 return TokError("unexpected token in '.irpc' directive");
3800
3801 // Eat the end of statement.
3802 Lex();
3803
3804 // Lex the irpc definition.
3805 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3806 if (!M)
3807 return true;
3808
3809 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3810 // to hold the macro body with substitutions.
3811 SmallString<256> Buf;
3812 raw_svector_ostream OS(Buf);
3813
3814 StringRef Values = A.front().front().getString();
3815 std::size_t I, End = Values.size();
3816 for (I = 0; I < End; ++I) {
3817 MacroArgument Arg;
3818 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3819
Rafael Espindola8a403d32012-08-08 14:51:03 +00003820 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003821 Args.push_back(Arg);
3822
3823 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3824 return true;
3825 }
3826
3827 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3828
3829 return false;
3830}
3831
Rafael Espindola761cb062012-06-03 23:57:14 +00003832bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3833 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003834 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003835
3836 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003837 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003838 assert(getLexer().is(AsmToken::EndOfStatement));
3839
Rafael Espindola761cb062012-06-03 23:57:14 +00003840 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003841 return false;
3842}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003843
Eli Friedman2128aae2012-10-22 23:58:19 +00003844bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) {
3845 const MCExpr *Value;
3846 SMLoc ExprLoc = getLexer().getLoc();
3847 if (ParseExpression(Value))
3848 return true;
3849 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3850 if (!MCE)
3851 return Error(ExprLoc, "unexpected expression in _emit");
3852 uint64_t IntValue = MCE->getValue();
3853 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
3854 return Error(ExprLoc, "literal value out of range for directive");
3855
3856 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5));
3857 return false;
3858}
3859
Chad Rosierb1f8c132012-10-18 15:49:34 +00003860bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3861 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003862 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003863 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003864 SmallVectorImpl<std::string> &Clobbers,
3865 const MCInstrInfo *MII,
3866 const MCInstPrinter *IP,
3867 MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003868 SmallVector<void *, 4> InputDecls;
3869 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003870 SmallVector<bool, 4> InputDeclsAddressOf;
3871 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003872 SmallVector<std::string, 4> InputConstraints;
3873 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003874 std::set<std::string> ClobberRegs;
3875
Chad Rosier4e472d22012-10-20 01:02:45 +00003876 SmallVector<struct AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003877
3878 // Prime the lexer.
3879 Lex();
3880
3881 // While we have input, parse each statement.
3882 unsigned InputIdx = 0;
3883 unsigned OutputIdx = 0;
3884 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00003885 ParseStatementInfo Info(&AsmStrRewrites);
3886 if (ParseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00003887 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003888
Chad Rosier57498012012-12-12 22:45:52 +00003889 if (Info.ParseError)
3890 return true;
3891
Eli Friedman2128aae2012-10-22 23:58:19 +00003892 if (Info.Opcode != ~0U) {
3893 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003894
3895 // Build the list of clobbers, outputs and inputs.
Eli Friedman2128aae2012-10-22 23:58:19 +00003896 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
3897 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003898
3899 // Immediate.
3900 if (Operand->isImm()) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00003901 if (Operand->needAsmRewrite())
3902 AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix,
3903 Operand->getStartLoc()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003904 continue;
3905 }
3906
3907 // Register operand.
Chad Rosierc1ec2072013-01-10 22:10:27 +00003908 if (Operand->isReg() && !Operand->needAddressOf()) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003909 unsigned NumDefs = Desc.getNumDefs();
3910 // Clobber.
3911 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3912 std::string Reg;
3913 raw_string_ostream OS(Reg);
3914 IP->printRegName(OS, Operand->getReg());
3915 ClobberRegs.insert(StringRef(OS.str()));
3916 }
3917 continue;
3918 }
3919
3920 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003921 unsigned Size;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003922 bool IsVarDecl;
Chad Rosier32989592012-10-18 20:27:15 +00003923 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +00003924 Size, IsVarDecl);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003925 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003926 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierc1ec2072013-01-10 22:10:27 +00003927 if (Operand->isMem() && Operand->needSizeDirective())
Chad Rosier4e472d22012-10-20 01:02:45 +00003928 AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective,
Chad Rosierefcb3d92012-10-26 18:04:20 +00003929 Operand->getStartLoc(),
3930 /*Len*/0,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003931 Operand->getMemSize()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003932 if (isOutput) {
3933 std::string Constraint = "=";
3934 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003935 OutputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003936 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003937 Constraint += Operand->getConstraint().str();
3938 OutputConstraints.push_back(Constraint);
Chad Rosier4e472d22012-10-20 01:02:45 +00003939 AsmStrRewrites.push_back(AsmRewrite(AOK_Output,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003940 Operand->getStartLoc(),
3941 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003942 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003943 InputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003944 InputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003945 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosier4e472d22012-10-20 01:02:45 +00003946 AsmStrRewrites.push_back(AsmRewrite(AOK_Input,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003947 Operand->getStartLoc(),
3948 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003949 }
3950 }
3951 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003952 }
3953 }
3954
3955 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003956 NumOutputs = OutputDecls.size();
3957 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003958
3959 // Set the unique clobbers.
3960 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3961 E = ClobberRegs.end(); I != E; ++I)
3962 Clobbers.push_back(*I);
3963
3964 // Merge the various outputs and inputs. Output are expected first.
3965 if (NumOutputs || NumInputs) {
3966 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003967 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003968 Constraints.resize(NumExprs);
Chad Rosier5a719fc2012-10-23 17:43:43 +00003969 // FIXME: Constraints are hard coded to 'm', but we need an 'r'
Chad Rosierc1ec2072013-01-10 22:10:27 +00003970 // constraint for addressof. This needs to be cleaned up!
Chad Rosierb1f8c132012-10-18 15:49:34 +00003971 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003972 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
3973 Constraints[i] = OutputDeclsAddressOf[i] ? "=r" : OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003974 }
3975 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003976 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
3977 Constraints[j] = InputDeclsAddressOf[i] ? "r" : InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003978 }
3979 }
3980
3981 // Build the IR assembly string.
3982 std::string AsmStringIR;
Chad Rosier4e472d22012-10-20 01:02:45 +00003983 AsmRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003984 raw_string_ostream OS(AsmStringIR);
3985 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
Chad Rosier4e472d22012-10-20 01:02:45 +00003986 for (SmallVectorImpl<struct AsmRewrite>::iterator
Chad Rosierb1f8c132012-10-18 15:49:34 +00003987 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3988 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003989
Chad Rosier4e472d22012-10-20 01:02:45 +00003990 AsmRewriteKind Kind = (*I).Kind;
Chad Rosier96d58e62012-10-19 20:57:14 +00003991
3992 // Emit everything up to the immediate/expression. If the previous rewrite
3993 // was a size directive, then this has already been done.
3994 if (PrevKind != AOK_SizeDirective)
3995 OS << StringRef(Start, Loc - Start);
3996 PrevKind = Kind;
3997
Chad Rosier5a719fc2012-10-23 17:43:43 +00003998 // Skip the original expression.
3999 if (Kind == AOK_Skip) {
4000 Start = Loc + (*I).Len;
4001 continue;
4002 }
4003
Chad Rosierb1f8c132012-10-18 15:49:34 +00004004 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004005 switch (Kind) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004006 default: break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004007 case AOK_Imm:
Chad Rosierefcb3d92012-10-26 18:04:20 +00004008 OS << Twine("$$");
4009 OS << (*I).Val;
4010 break;
4011 case AOK_ImmPrefix:
4012 OS << Twine("$$");
Chad Rosierb1f8c132012-10-18 15:49:34 +00004013 break;
4014 case AOK_Input:
4015 OS << '$';
4016 OS << InputIdx++;
4017 break;
4018 case AOK_Output:
4019 OS << '$';
4020 OS << OutputIdx++;
4021 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004022 case AOK_SizeDirective:
Chad Rosier6a020a72012-10-25 20:41:34 +00004023 switch((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004024 default: break;
4025 case 8: OS << "byte ptr "; break;
4026 case 16: OS << "word ptr "; break;
4027 case 32: OS << "dword ptr "; break;
4028 case 64: OS << "qword ptr "; break;
4029 case 80: OS << "xword ptr "; break;
4030 case 128: OS << "xmmword ptr "; break;
4031 case 256: OS << "ymmword ptr "; break;
4032 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004033 break;
4034 case AOK_Emit:
4035 OS << ".byte";
4036 break;
Chad Rosier6a020a72012-10-25 20:41:34 +00004037 case AOK_DotOperator:
4038 OS << (*I).Val;
4039 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004040 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004041
Chad Rosierb1f8c132012-10-18 15:49:34 +00004042 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00004043 if (Kind != AOK_SizeDirective)
4044 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004045 }
4046
4047 // Emit the remainder of the asm string.
4048 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4049 if (Start != AsmEnd)
4050 OS << StringRef(Start, AsmEnd - Start);
4051
4052 AsmString = OS.str();
4053 return false;
4054}
4055
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004056/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004057MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004058 MCContext &C, MCStreamer &Out,
4059 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004060 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004061}