blob: 50579a64a2f6aa74ae656669d234da9a7352fe77 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarb95a0792010-09-24 01:59:56 +000014#include "llvm/ADT/APFloat.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000016#include "llvm/ADT/StringMap.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000018#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000019#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000020#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Chad Rosierb1f8c132012-10-18 15:49:34 +000022#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000024#include "llvm/MC/MCParser/AsmCond.h"
25#include "llvm/MC/MCParser/AsmLexer.h"
26#include "llvm/MC/MCParser/MCAsmParser.h"
27#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000028#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000029#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000030#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000032#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000033#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000034#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000035#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000036#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000037#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000038#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000039#include <cctype>
Chad Rosierb1f8c132012-10-18 15:49:34 +000040#include <set>
41#include <string>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000042#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000043using namespace llvm;
44
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000045static cl::opt<bool>
46FatalAssemblerWarnings("fatal-assembler-warnings",
47 cl::desc("Consider warnings as error"));
48
Eric Christopher2318ba12012-12-18 00:30:54 +000049MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {}
Nick Lewycky0d7d11d2012-10-19 07:00:09 +000050
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000051namespace {
52
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053/// \brief Helper class for tracking macro definitions.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000054typedef std::vector<AsmToken> MacroArgument;
Rafael Espindola8a403d32012-08-08 14:51:03 +000055typedef std::vector<MacroArgument> MacroArguments;
Preston Gurd6c9176a2012-09-19 20:29:04 +000056typedef std::pair<StringRef, MacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000057typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000058
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000059struct Macro {
60 StringRef Name;
61 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000062 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000063
64public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000065 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000066 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000067};
68
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000069/// \brief Helper class for storing information about an active macro
70/// instantiation.
71struct MacroInstantiation {
72 /// The macro being instantiated.
73 const Macro *TheMacro;
74
75 /// The macro instantiation with substitutions.
76 MemoryBuffer *Instantiation;
77
78 /// The location of the instantiation.
79 SMLoc InstantiationLoc;
80
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000081 /// The buffer where parsing should resume upon instantiation completion.
82 int ExitBuffer;
83
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000084 /// The location where parsing should resume upon instantiation completion.
85 SMLoc ExitLoc;
86
87public:
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000088 MacroInstantiation(const Macro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000089 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000090};
91
Chad Rosier6a020a72012-10-25 20:41:34 +000092//struct AsmRewrite;
Eli Friedman2128aae2012-10-22 23:58:19 +000093struct ParseStatementInfo {
94 /// ParsedOperands - The parsed operands from the last parsed statement.
95 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
96
97 /// Opcode - The opcode from the last parsed instruction.
98 unsigned Opcode;
99
Chad Rosier57498012012-12-12 22:45:52 +0000100 /// Error - Was there an error parsing the inline assembly?
101 bool ParseError;
102
Eli Friedman2128aae2012-10-22 23:58:19 +0000103 SmallVectorImpl<AsmRewrite> *AsmRewrites;
104
Chad Rosier57498012012-12-12 22:45:52 +0000105 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000106 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier57498012012-12-12 22:45:52 +0000107 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000108
109 ~ParseStatementInfo() {
110 // Free any parsed operands.
111 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
112 delete ParsedOperands[i];
113 ParsedOperands.clear();
114 }
115};
116
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000117/// \brief The concrete assembly parser instance.
118class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000119 friend class GenericAsmParser;
120
Craig Topper85aadc02012-09-15 16:23:52 +0000121 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
122 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000123private:
124 AsmLexer Lexer;
125 MCContext &Ctx;
126 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000127 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000128 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000129 SourceMgr::DiagHandlerTy SavedDiagHandler;
130 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000131 MCAsmParserExtension *GenericParser;
132 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000133
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000134 /// This is the current buffer index we're lexing from as managed by the
135 /// SourceMgr object.
136 int CurBuffer;
137
138 AsmCond TheCondState;
139 std::vector<AsmCond> TheCondStack;
140
141 /// DirectiveMap - This is a table handlers for directives. Each handler is
142 /// invoked after the directive identifier is read and is responsible for
143 /// parsing and validating the rest of the directive. The handler is passed
144 /// in the directive name and the location of the directive keyword.
145 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000146
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000147 /// MacroMap - Map of currently defined macros.
148 StringMap<Macro*> MacroMap;
149
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000150 /// ActiveMacros - Stack of active macro instantiations.
151 std::vector<MacroInstantiation*> ActiveMacros;
152
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000153 /// Boolean tracking whether macro substitution is enabled.
154 unsigned MacrosEnabled : 1;
155
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000156 /// Flag tracking whether any errors have been encountered.
157 unsigned HadError : 1;
158
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000159 /// The values from the last parsed cpp hash file line comment if any.
160 StringRef CppHashFilename;
161 int64_t CppHashLineNumber;
162 SMLoc CppHashLoc;
Kevin Enderby32c1a822012-11-05 21:55:41 +0000163 int CppHashBuf;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000164
Devang Patel0db58bf2012-01-31 18:14:05 +0000165 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
166 unsigned AssemblerDialect;
167
Preston Gurd7b6f2032012-09-19 20:36:12 +0000168 /// IsDarwin - is Darwin compatibility enabled?
169 bool IsDarwin;
170
Chad Rosier8f138d12012-10-15 17:19:13 +0000171 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000172 bool ParsingInlineAsm;
173
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000174public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000175 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000176 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000177 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000178
179 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
180
Craig Topper345d16d2012-08-29 05:48:09 +0000181 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
182 StringRef Directive,
183 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 DirectiveMap[Directive] = std::make_pair(Object, Handler);
185 }
186
187public:
188 /// @name MCAsmParser Interface
189 /// {
190
191 virtual SourceMgr &getSourceManager() { return SrcMgr; }
192 virtual MCAsmLexer &getLexer() { return Lexer; }
193 virtual MCContext &getContext() { return Ctx; }
194 virtual MCStreamer &getStreamer() { return Out; }
Eric Christopher2318ba12012-12-18 00:30:54 +0000195 virtual unsigned getAssemblerDialect() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000196 if (AssemblerDialect == ~0U)
Eric Christopher2318ba12012-12-18 00:30:54 +0000197 return MAI.getAssemblerDialect();
Devang Patel0db58bf2012-01-31 18:14:05 +0000198 else
199 return AssemblerDialect;
200 }
201 virtual void setAssemblerDialect(unsigned i) {
202 AssemblerDialect = i;
203 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000204
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000205 virtual bool Warning(SMLoc L, const Twine &Msg,
206 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
207 virtual bool Error(SMLoc L, const Twine &Msg,
208 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000209
Craig Topper345d16d2012-08-29 05:48:09 +0000210 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000211
Chad Rosier84125ca2012-10-13 00:26:04 +0000212 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000213 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000214
215 bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
216 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000217 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000218 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000219 SmallVectorImpl<std::string> &Clobbers,
220 const MCInstrInfo *MII,
221 const MCInstPrinter *IP,
222 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000223
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224 bool ParseExpression(const MCExpr *&Res);
225 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
226 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
227 virtual bool ParseAbsoluteExpression(int64_t &Res);
228
Eli Benderskybf706b32013-01-12 00:05:00 +0000229 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
230 /// and set \p Res to the identifier contents.
231 virtual bool ParseIdentifier(StringRef &Res);
Eli Benderskyb2f0b592013-01-12 00:23:24 +0000232 virtual void EatToEndOfStatement();
Eli Benderskybf706b32013-01-12 00:05:00 +0000233
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234 /// }
235
236private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000237 void CheckForValidSection();
238
Eli Friedman2128aae2012-10-22 23:58:19 +0000239 bool ParseStatement(ParseStatementInfo &Info);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000240 void EatToEndOfLine();
241 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000242
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000243 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000244 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000245 const MacroParameters &Parameters,
246 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000247 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000248 void HandleMacroExit();
249
250 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000251 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000252 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
253 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000254 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000255 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000256
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000257 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
258 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000259 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
260 /// This returns true on failure.
261 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000262
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000263 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000264 /// current token is not set; clients should ensure Lex() is called
265 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000266 ///
267 /// \param InBuffer If not -1, should be the known buffer id that contains the
268 /// location.
269 void JumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000270
Preston Gurd7b6f2032012-09-19 20:36:12 +0000271 bool ParseMacroArgument(MacroArgument &MA,
272 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000273 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000274
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000275 /// \brief Parse up to the end of statement and a return the contents from the
276 /// current token until the end of the statement; the current token on exit
277 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000278 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000279
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000280 /// \brief Parse until the end of a statement or a comma is encountered,
281 /// return the contents from the current token up to the end or comma.
282 StringRef ParseStringToComma();
283
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000284 bool ParseAssignment(StringRef Name, bool allow_redef,
285 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000286
287 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
288 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
289 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000290 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000291
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000292 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000293
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000294 enum DirectiveKind {
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000295 DK_NO_DIRECTIVE, // Placeholder
296 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
297 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
298 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000299 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000300 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
301 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL,
302 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
303 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
304 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
305 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
306 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
307 DK_ELSEIF, DK_ELSE, DK_ENDIF
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000308 };
309
310 StringMap<DirectiveKind> DirectiveKindMapping;
311
312 // ".ascii", ".asciz", ".string"
Rafael Espindola787c3372010-10-28 20:02:27 +0000313 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000314 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000315 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000316 bool ParseDirectiveFill(); // ".fill"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000317 bool ParseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000318 // ".set", ".equ", ".equiv"
319 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000320 bool ParseDirectiveOrg(); // ".org"
321 // ".align{,32}", ".p2align{,w,l}"
322 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
323
Eli Bendersky4766ef42012-12-20 19:05:53 +0000324 // ".bundle_align_mode"
325 bool ParseDirectiveBundleAlignMode();
326 // ".bundle_lock"
327 bool ParseDirectiveBundleLock();
328 // ".bundle_unlock"
329 bool ParseDirectiveBundleUnlock();
330
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000331 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
332 /// accepts a single symbol (which should be a label or an external).
333 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000334
335 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
336
337 bool ParseDirectiveAbort(); // ".abort"
338 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000339 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000340
341 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000342 // ".ifb" or ".ifnb", depending on ExpectBlank.
343 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000344 // ".ifc" or ".ifnc", depending on ExpectEqual.
345 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000346 // ".ifdef" or ".ifndef", depending on expect_defined
347 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000348 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
349 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
350 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
351
352 /// ParseEscapedString - Parse the current token as a string which may include
353 /// escaped characters and return the string contents.
354 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000355
356 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
357 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000358
Rafael Espindola761cb062012-06-03 23:57:14 +0000359 // Macro-like directives
360 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
361 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
362 raw_svector_ostream &OS);
363 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000364 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000365 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000366 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000367
Eli Friedman2128aae2012-10-22 23:58:19 +0000368 // "_emit"
369 bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000370
371 void initializeDirectiveKindMapping();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000372};
373
Eli Bendersky63e6f482013-01-10 23:32:57 +0000374/// \brief Generic implementation of directive handling, etc. which is shared
375/// (or the default, at least) for all assembler parsers.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000376class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000377 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
378 void AddDirectiveHandler(StringRef Directive) {
379 getParser().AddDirectiveHandler(this, Directive,
380 HandleDirective<GenericAsmParser, Handler>);
381 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000382public:
383 GenericAsmParser() {}
384
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000385 AsmParser &getParser() {
386 return (AsmParser&) this->MCAsmParserExtension::getParser();
387 }
388
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000389 virtual void Initialize(MCAsmParser &Parser) {
390 // Call the base implementation.
391 this->MCAsmParserExtension::Initialize(Parser);
392
393 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000394 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
395 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
396 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000397 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000398
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000399 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".space");
400 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".skip");
401
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000402 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000403 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
404 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000405 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
406 ".cfi_startproc");
407 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
408 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000409 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
410 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000411 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
412 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000413 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
414 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000415 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
416 ".cfi_def_cfa_register");
417 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
418 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000419 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
420 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000421 AddDirectiveHandler<
422 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
423 AddDirectiveHandler<
424 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000425 AddDirectiveHandler<
426 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
427 AddDirectiveHandler<
428 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000429 AddDirectiveHandler<
430 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000431 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000432 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
433 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000434 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000435 AddDirectiveHandler<
436 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000437 AddDirectiveHandler<
438 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000439 AddDirectiveHandler<
440 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000441
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000442 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000443 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
444 ".macros_on");
445 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
446 ".macros_off");
447 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
448 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
449 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000450 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000451
452 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
453 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000454 }
455
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000456 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
457
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000458 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
459 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
460 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000461 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000462 bool ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000463 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000464 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
465 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000466 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000467 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000468 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000469 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
470 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000471 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000472 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000473 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
474 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000475 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000476 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000477 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000478 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000479 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000480 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000481
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000482 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000483 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
484 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000485 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000486
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000487 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000488};
489
490}
491
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000492namespace llvm {
493
494extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000495extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000496extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000497
498}
499
Chris Lattneraaec2052010-01-19 19:46:13 +0000500enum { DEFAULT_ADDRSPACE = 0 };
501
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000502AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000503 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000504 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000505 GenericParser(new GenericAsmParser), PlatformParser(0),
Preston Gurd7b6f2032012-09-19 20:36:12 +0000506 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000507 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000508 // Save the old handler.
509 SavedDiagHandler = SrcMgr.getDiagHandler();
510 SavedDiagContext = SrcMgr.getDiagContext();
511 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000512 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000513 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000514
515 // Initialize the generic parser.
516 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000517
518 // Initialize the platform / file format parser.
519 //
520 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
521 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000522 if (_MAI.hasMicrosoftFastStdCallMangling()) {
523 PlatformParser = createCOFFAsmParser();
524 PlatformParser->Initialize(*this);
525 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000526 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000527 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000528 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000529 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000530 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000531 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000532 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000533
534 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000535}
536
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000537AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000538 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
539
540 // Destroy any macros.
541 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
542 ie = MacroMap.end(); it != ie; ++it)
543 delete it->getValue();
544
Daniel Dunbare4749702010-07-12 18:12:02 +0000545 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000546 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000547}
548
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000549void AsmParser::PrintMacroInstantiations() {
550 // Print the active macro instantiation stack.
551 for (std::vector<MacroInstantiation*>::const_reverse_iterator
552 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000553 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
554 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000555}
556
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000557bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000558 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000559 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000560 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000561 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000562 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000563}
564
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000565bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000566 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000567 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000568 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000569 return true;
570}
571
Sean Callananfd0b0282010-01-21 00:19:58 +0000572bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000573 std::string IncludedFile;
574 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000575 if (NewBuf == -1)
576 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000577
Sean Callananfd0b0282010-01-21 00:19:58 +0000578 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000579
Sean Callananfd0b0282010-01-21 00:19:58 +0000580 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000581
Sean Callananfd0b0282010-01-21 00:19:58 +0000582 return false;
583}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000584
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000585/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000586/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000587/// returns true on failure.
588bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
589 std::string IncludedFile;
590 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
591 if (NewBuf == -1)
592 return true;
593
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000594 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000595 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
596 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000597 return false;
598}
599
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000600void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
601 if (InBuffer != -1) {
602 CurBuffer = InBuffer;
603 } else {
604 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
605 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000606 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
607}
608
Sean Callananfd0b0282010-01-21 00:19:58 +0000609const AsmToken &AsmParser::Lex() {
610 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000611
Sean Callananfd0b0282010-01-21 00:19:58 +0000612 if (tok->is(AsmToken::Eof)) {
613 // If this is the end of an included file, pop the parent file off the
614 // include stack.
615 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
616 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000617 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000618 tok = &Lexer.Lex();
619 }
620 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000621
Sean Callananfd0b0282010-01-21 00:19:58 +0000622 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000623 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000624
Sean Callananfd0b0282010-01-21 00:19:58 +0000625 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000626}
627
Chris Lattner79180e22010-04-05 23:15:42 +0000628bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000629 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000630 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000631 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000632
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000633 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000634 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000635
636 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000637 AsmCond StartingCondState = TheCondState;
638
Kevin Enderby613b7572011-11-01 22:27:22 +0000639 // If we are generating dwarf for assembly source files save the initial text
640 // section and generate a .file directive.
641 if (getContext().getGenDwarfForAssembly()) {
642 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000643 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
644 getStreamer().EmitLabel(SectionStartSym);
645 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000646 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000647 StringRef(),
648 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000649 }
650
Chris Lattnerb717fb02009-07-02 21:53:43 +0000651 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000652 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000653 ParseStatementInfo Info;
654 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000655
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000656 // We had an error, validate that one was emitted and recover by skipping to
657 // the next line.
658 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000659 EatToEndOfStatement();
660 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000661
662 if (TheCondState.TheCond != StartingCondState.TheCond ||
663 TheCondState.Ignore != StartingCondState.Ignore)
664 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000665
666 // Check to see there are no empty DwarfFile slots.
667 const std::vector<MCDwarfFile *> &MCDwarfFiles =
668 getContext().getMCDwarfFiles();
669 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000670 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000671 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000672 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000673
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000674 // Check to see that all assembler local symbols were actually defined.
675 // Targets that don't do subsections via symbols may not want this, though,
676 // so conservatively exclude them. Only do this if we're finalizing, though,
677 // as otherwise we won't necessarilly have seen everything yet.
678 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
679 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
680 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
681 e = Symbols.end();
682 i != e; ++i) {
683 MCSymbol *Sym = i->getValue();
684 // Variable symbols may not be marked as defined, so check those
685 // explicitly. If we know it's a variable, we have a definition for
686 // the purposes of this check.
687 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
688 // FIXME: We would really like to refer back to where the symbol was
689 // first referenced for a source location. We need to add something
690 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000691 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
692 "assembler local symbol '" + Sym->getName() +
693 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000694 }
695 }
696
697
Chris Lattner79180e22010-04-05 23:15:42 +0000698 // Finalize the output stream if there are no errors and if the client wants
699 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000700 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000701 Out.Finish();
702
Chris Lattnerb717fb02009-07-02 21:53:43 +0000703 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000704}
705
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000706void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000707 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000708 TokError("expected section directive before assembly directive");
709 Out.SwitchSection(Ctx.getMachOSection(
710 "__TEXT", "__text",
711 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
712 0, SectionKind::getText()));
713 }
714}
715
Chris Lattner2cf5f142009-06-22 01:29:09 +0000716/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
717void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000718 while (Lexer.isNot(AsmToken::EndOfStatement) &&
719 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000720 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000721
Chris Lattner2cf5f142009-06-22 01:29:09 +0000722 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000723 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000724 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000725}
726
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000727StringRef AsmParser::ParseStringToEndOfStatement() {
728 const char *Start = getTok().getLoc().getPointer();
729
730 while (Lexer.isNot(AsmToken::EndOfStatement) &&
731 Lexer.isNot(AsmToken::Eof))
732 Lex();
733
734 const char *End = getTok().getLoc().getPointer();
735 return StringRef(Start, End - Start);
736}
Chris Lattnerc4193832009-06-22 05:51:26 +0000737
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000738StringRef AsmParser::ParseStringToComma() {
739 const char *Start = getTok().getLoc().getPointer();
740
741 while (Lexer.isNot(AsmToken::EndOfStatement) &&
742 Lexer.isNot(AsmToken::Comma) &&
743 Lexer.isNot(AsmToken::Eof))
744 Lex();
745
746 const char *End = getTok().getLoc().getPointer();
747 return StringRef(Start, End - Start);
748}
749
Chris Lattner74ec1a32009-06-22 06:32:03 +0000750/// ParseParenExpr - Parse a paren expression and return it.
751/// NOTE: This assumes the leading '(' has already been consumed.
752///
753/// parenexpr ::= expr)
754///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000755bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000756 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000757 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000758 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000759 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000760 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000761 return false;
762}
Chris Lattnerc4193832009-06-22 05:51:26 +0000763
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000764/// ParseBracketExpr - Parse a bracket expression and return it.
765/// NOTE: This assumes the leading '[' has already been consumed.
766///
767/// bracketexpr ::= expr]
768///
769bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
770 if (ParseExpression(Res)) return true;
771 if (Lexer.isNot(AsmToken::RBrac))
772 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000773 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000774 Lex();
775 return false;
776}
777
Chris Lattner74ec1a32009-06-22 06:32:03 +0000778/// ParsePrimaryExpr - Parse a primary expression and return it.
779/// primaryexpr ::= (parenexpr
780/// primaryexpr ::= symbol
781/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000782/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000783/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000784bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000785 switch (Lexer.getKind()) {
786 default:
787 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000788 // If we have an error assume that we've already handled it.
789 case AsmToken::Error:
790 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000791 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000792 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000793 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000794 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000795 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000797 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000798 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000799 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000800 StringRef Identifier;
801 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000802 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000803
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000804 EndLoc = SMLoc::getFromPointer(Identifier.end());
805
Daniel Dunbarfffff912009-10-16 01:34:54 +0000806 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000807 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000808 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000809
810 // Lookup the symbol variant if used.
811 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000812 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000813 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000814 if (Variant == MCSymbolRefExpr::VK_Invalid) {
815 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000816 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000817 }
818 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000819
Daniel Dunbarfffff912009-10-16 01:34:54 +0000820 // If this is an absolute variable reference, substitute it now to preserve
821 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000822 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000823 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000824 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000825
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000826 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000827 return false;
828 }
829
830 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000831 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000832 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000833 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000834 case AsmToken::Integer: {
835 SMLoc Loc = getTok().getLoc();
836 int64_t IntVal = getTok().getIntVal();
837 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000838 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000839 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000840 // Look for 'b' or 'f' following an Integer as a directional label
841 if (Lexer.getKind() == AsmToken::Identifier) {
842 StringRef IDVal = getTok().getString();
843 if (IDVal == "f" || IDVal == "b"){
844 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
845 IDVal == "f" ? 1 : 0);
846 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
847 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000848 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000849 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000850 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000851 Lex(); // Eat identifier.
852 }
853 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000854 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000855 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000856 case AsmToken::Real: {
857 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000858 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000859 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000860 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000861 Lex(); // Eat token.
862 return false;
863 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000864 case AsmToken::Dot: {
865 // This is a '.' reference, which references the current PC. Emit a
866 // temporary label to the streamer and refer to it.
867 MCSymbol *Sym = Ctx.CreateTempSymbol();
868 Out.EmitLabel(Sym);
869 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000870 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000871 Lex(); // Eat identifier.
872 return false;
873 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000874 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000875 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000876 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000877 case AsmToken::LBrac:
878 if (!PlatformParser->HasBracketExpressions())
879 return TokError("brackets expression not supported on this target");
880 Lex(); // Eat the '['.
881 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000882 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000883 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000884 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000885 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000886 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000887 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000888 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000889 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000890 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000891 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000892 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000893 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000894 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000895 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000896 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000897 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000898 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000899 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000900 }
901}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000902
Chris Lattnerb4307b32010-01-15 19:28:38 +0000903bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000904 SMLoc EndLoc;
905 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000906}
907
Daniel Dunbarcceba832010-09-17 02:47:07 +0000908const MCExpr *
909AsmParser::ApplyModifierToExpr(const MCExpr *E,
910 MCSymbolRefExpr::VariantKind Variant) {
911 // Recurse over the given expression, rebuilding it to apply the given variant
912 // if there is exactly one symbol.
913 switch (E->getKind()) {
914 case MCExpr::Target:
915 case MCExpr::Constant:
916 return 0;
917
918 case MCExpr::SymbolRef: {
919 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
920
921 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
922 TokError("invalid variant on expression '" +
923 getTok().getIdentifier() + "' (already modified)");
924 return E;
925 }
926
927 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
928 }
929
930 case MCExpr::Unary: {
931 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
932 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
933 if (!Sub)
934 return 0;
935 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
936 }
937
938 case MCExpr::Binary: {
939 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
940 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
941 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
942
943 if (!LHS && !RHS)
944 return 0;
945
946 if (!LHS) LHS = BE->getLHS();
947 if (!RHS) RHS = BE->getRHS();
948
949 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
950 }
951 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000952
Craig Topper85814382012-02-07 05:05:23 +0000953 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000954}
955
Chris Lattner74ec1a32009-06-22 06:32:03 +0000956/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000957///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000958/// expr ::= expr &&,|| expr -> lowest.
959/// expr ::= expr |,^,&,! expr
960/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
961/// expr ::= expr <<,>> expr
962/// expr ::= expr +,- expr
963/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000964/// expr ::= primaryexpr
965///
Chris Lattner54482b42010-01-15 19:39:23 +0000966bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000967 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000968 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000969 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
970 return true;
971
Daniel Dunbarcceba832010-09-17 02:47:07 +0000972 // As a special case, we support 'a op b @ modifier' by rewriting the
973 // expression to include the modifier. This is inefficient, but in general we
974 // expect users to use 'a@modifier op b'.
975 if (Lexer.getKind() == AsmToken::At) {
976 Lex();
977
978 if (Lexer.isNot(AsmToken::Identifier))
979 return TokError("unexpected symbol modifier following '@'");
980
981 MCSymbolRefExpr::VariantKind Variant =
982 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
983 if (Variant == MCSymbolRefExpr::VK_Invalid)
984 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
985
986 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
987 if (!ModifiedRes) {
988 return TokError("invalid modifier '" + getTok().getIdentifier() +
989 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000990 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000991
Daniel Dunbarcceba832010-09-17 02:47:07 +0000992 Res = ModifiedRes;
993 Lex();
994 }
995
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000996 // Try to constant fold it up front, if possible.
997 int64_t Value;
998 if (Res->EvaluateAsAbsolute(Value))
999 Res = MCConstantExpr::Create(Value, getContext());
1000
1001 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +00001002}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001003
Chris Lattnerb4307b32010-01-15 19:28:38 +00001004bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +00001005 Res = 0;
1006 return ParseParenExpr(Res, EndLoc) ||
1007 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +00001008}
1009
Daniel Dunbar475839e2009-06-29 20:37:27 +00001010bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001011 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001012
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001013 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +00001014 if (ParseExpression(Expr))
1015 return true;
1016
Daniel Dunbare00b0112009-10-16 01:57:52 +00001017 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001018 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001019
1020 return false;
1021}
1022
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001023static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001024 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001025 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001026 default:
1027 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001028
Jim Grosbachfbe16812011-08-20 16:24:13 +00001029 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001030 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001031 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001032 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001033 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001034 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001035 return 1;
1036
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001037
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001038 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +00001039 //
1040 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001041 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001042 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001043 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001044 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001045 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001046 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001047 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001048 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001049 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001050
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001051 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001052 case AsmToken::EqualEqual:
1053 Kind = MCBinaryExpr::EQ;
1054 return 3;
1055 case AsmToken::ExclaimEqual:
1056 case AsmToken::LessGreater:
1057 Kind = MCBinaryExpr::NE;
1058 return 3;
1059 case AsmToken::Less:
1060 Kind = MCBinaryExpr::LT;
1061 return 3;
1062 case AsmToken::LessEqual:
1063 Kind = MCBinaryExpr::LTE;
1064 return 3;
1065 case AsmToken::Greater:
1066 Kind = MCBinaryExpr::GT;
1067 return 3;
1068 case AsmToken::GreaterEqual:
1069 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001070 return 3;
1071
Jim Grosbachfbe16812011-08-20 16:24:13 +00001072 // Intermediate Precedence: <<, >>
1073 case AsmToken::LessLess:
1074 Kind = MCBinaryExpr::Shl;
1075 return 4;
1076 case AsmToken::GreaterGreater:
1077 Kind = MCBinaryExpr::Shr;
1078 return 4;
1079
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001080 // High Intermediate Precedence: +, -
1081 case AsmToken::Plus:
1082 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001083 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001084 case AsmToken::Minus:
1085 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001086 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001087
Jim Grosbachfbe16812011-08-20 16:24:13 +00001088 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001089 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001090 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001091 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001092 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001093 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001094 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001095 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001096 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001097 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001098 }
1099}
1100
1101
1102/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1103/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001104bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1105 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001106 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001107 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001108 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001109
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001110 // If the next token is lower precedence than we are allowed to eat, return
1111 // successfully with what we ate already.
1112 if (TokPrec < Precedence)
1113 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001114
Sean Callanan79ed1a82010-01-19 20:22:31 +00001115 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001116
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001117 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001118 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001119 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001120
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001121 // If BinOp binds less tightly with RHS than the operator after RHS, let
1122 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001123 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001124 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001125 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001126 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001127 }
1128
Daniel Dunbar475839e2009-06-29 20:37:27 +00001129 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001130 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001131 }
1132}
1133
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001134/// ParseStatement:
1135/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001136/// ::= Label* Directive ...Operands... EndOfStatement
1137/// ::= Label* Identifier OperandList* EndOfStatement
Eli Friedman2128aae2012-10-22 23:58:19 +00001138bool AsmParser::ParseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001139 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001140 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001141 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001142 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001143 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001144
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001145 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001146 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001147 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001148 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001149 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001150 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001151 if (Lexer.is(AsmToken::Hash))
1152 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001153
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001154 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001155 if (Lexer.is(AsmToken::Integer)) {
1156 LocalLabelVal = getTok().getIntVal();
1157 if (LocalLabelVal < 0) {
1158 if (!TheCondState.Ignore)
1159 return TokError("unexpected token at start of statement");
1160 IDVal = "";
1161 }
1162 else {
1163 IDVal = getTok().getString();
1164 Lex(); // Consume the integer token to be used as an identifier token.
1165 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001166 if (!TheCondState.Ignore)
1167 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001168 }
1169 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001170
1171 } else if (Lexer.is(AsmToken::Dot)) {
1172 // Treat '.' as a valid identifier in this context.
1173 Lex();
1174 IDVal = ".";
1175
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001176 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001177 if (!TheCondState.Ignore)
1178 return TokError("unexpected token at start of statement");
1179 IDVal = "";
1180 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001181
Chris Lattner7834fac2010-04-17 18:14:27 +00001182 // Handle conditional assembly here before checking for skipping. We
1183 // have to do this so that .endif isn't skipped in a ".if 0" block for
1184 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001185 StringMap<DirectiveKind>::const_iterator DirKindIt =
1186 DirectiveKindMapping.find(IDVal);
1187 DirectiveKind DirKind =
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001188 (DirKindIt == DirectiveKindMapping.end()) ? DK_NO_DIRECTIVE :
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001189 DirKindIt->getValue();
1190 switch (DirKind) {
1191 default:
1192 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001193 case DK_IF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001194 return ParseDirectiveIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001195 case DK_IFB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001196 return ParseDirectiveIfb(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001197 case DK_IFNB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001198 return ParseDirectiveIfb(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001199 case DK_IFC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001200 return ParseDirectiveIfc(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001201 case DK_IFNC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001202 return ParseDirectiveIfc(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001203 case DK_IFDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001204 return ParseDirectiveIfdef(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001205 case DK_IFNDEF:
1206 case DK_IFNOTDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001207 return ParseDirectiveIfdef(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001208 case DK_ELSEIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001209 return ParseDirectiveElseIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001210 case DK_ELSE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001211 return ParseDirectiveElse(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001212 case DK_ENDIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001213 return ParseDirectiveEndIf(IDLoc);
1214 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001215
Chris Lattner7834fac2010-04-17 18:14:27 +00001216 // If we are in a ".if 0" block, ignore this statement.
Chad Rosier17feeec2012-10-20 00:47:08 +00001217 if (TheCondState.Ignore) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001218 EatToEndOfStatement();
1219 return false;
1220 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001221
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001222 // FIXME: Recurse on local labels?
1223
1224 // See what kind of statement we have.
1225 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001226 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001227 CheckForValidSection();
1228
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001229 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001230 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001231
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001232 // Diagnose attempt to use '.' as a label.
1233 if (IDVal == ".")
1234 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1235
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001236 // Diagnose attempt to use a variable as a label.
1237 //
1238 // FIXME: Diagnostics. Note the location of the definition as a label.
1239 // FIXME: This doesn't diagnose assignment to a symbol which has been
1240 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001241 MCSymbol *Sym;
1242 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001243 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001244 else
1245 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001246 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001247 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001248
Daniel Dunbar959fd882009-08-26 22:13:22 +00001249 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001250 if (!ParsingInlineAsm)
1251 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001252
Kevin Enderby94c2e852011-12-09 18:09:40 +00001253 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001254 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001255 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001256 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1257 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001258
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001259 // Consume any end of statement token, if present, to avoid spurious
1260 // AddBlankLine calls().
1261 if (Lexer.is(AsmToken::EndOfStatement)) {
1262 Lex();
1263 if (Lexer.is(AsmToken::Eof))
1264 return false;
1265 }
1266
Eli Friedman2128aae2012-10-22 23:58:19 +00001267 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001268 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001269
Daniel Dunbar3f872332009-07-28 16:08:33 +00001270 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001271 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001272 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001273
Nico Weber4c4c7322011-01-28 03:04:41 +00001274 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001275
1276 default: // Normal instruction or directive.
1277 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001278 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001279
1280 // If macros are enabled, check to see if this is a macro instantiation.
1281 if (MacrosEnabled)
1282 if (const Macro *M = MacroMap.lookup(IDVal))
1283 return HandleMacroEntry(IDVal, IDLoc, M);
1284
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001285 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001286 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001287
1288 // Target hook for parsing target specific directives.
1289 if (!getTargetParser().ParseDirective(ID))
1290 return false;
1291
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001292 switch (DirKind) {
1293 default:
1294 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001295 case DK_SET:
1296 case DK_EQU:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001297 return ParseDirectiveSet(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001298 case DK_EQUIV:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001299 return ParseDirectiveSet(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001300 case DK_ASCII:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001301 return ParseDirectiveAscii(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001302 case DK_ASCIZ:
1303 case DK_STRING:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001304 return ParseDirectiveAscii(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001305 case DK_BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001306 return ParseDirectiveValue(1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001307 case DK_SHORT:
1308 case DK_VALUE:
1309 case DK_2BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001310 return ParseDirectiveValue(2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001311 case DK_LONG:
1312 case DK_INT:
1313 case DK_4BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001314 return ParseDirectiveValue(4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001315 case DK_QUAD:
1316 case DK_8BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001317 return ParseDirectiveValue(8);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001318 case DK_SINGLE:
1319 case DK_FLOAT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001320 return ParseDirectiveRealValue(APFloat::IEEEsingle);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001321 case DK_DOUBLE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001322 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001323 case DK_ALIGN: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001324 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1325 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1326 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001327 case DK_ALIGN32: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001328 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1329 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1330 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001331 case DK_BALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001332 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001333 case DK_BALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001334 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001335 case DK_BALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001336 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001337 case DK_P2ALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001338 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001339 case DK_P2ALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001340 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001341 case DK_P2ALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001342 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001343 case DK_ORG:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001344 return ParseDirectiveOrg();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001345 case DK_FILL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001346 return ParseDirectiveFill();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001347 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001348 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001349 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001350 EatToEndOfStatement(); // .extern is the default, ignore it.
1351 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001352 case DK_GLOBL:
1353 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001354 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001355 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001356 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001357 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001358 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001359 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001360 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001361 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001362 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001363 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001364 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001365 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001366 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001367 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001368 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001369 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001370 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001371 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001372 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001373 case DK_COMM:
1374 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001375 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001376 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001377 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001378 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001379 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001380 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001381 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001382 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001383 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001384 case DK_CODE16:
1385 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001386 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001387 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001388 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001389 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001390 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001391 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001392 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001393 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001394 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001395 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001396 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001397 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001398 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001399 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001400 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001401 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001402
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001403 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001404 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1405 DirectiveMap.lookup(IDVal);
1406 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001407 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001408
Jim Grosbach686c0182012-05-01 18:38:27 +00001409 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001410 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001411
Eli Friedman2128aae2012-10-22 23:58:19 +00001412 // _emit
1413 if (ParsingInlineAsm && IDVal == "_emit")
1414 return ParseDirectiveEmit(IDLoc, Info);
1415
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001416 CheckForValidSection();
1417
Chris Lattnera7f13542010-05-19 23:34:33 +00001418 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001419 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001420 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001421 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001422
Chad Rosier6a020a72012-10-25 20:41:34 +00001423 ParseInstructionInfo IInfo(Info.AsmRewrites);
1424 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1425 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001426 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001427
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001428 // Dump the parsed representation, if requested.
1429 if (getShowParsedOperands()) {
1430 SmallString<256> Str;
1431 raw_svector_ostream OS(Str);
1432 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001433 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001434 if (i != 0)
1435 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001436 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001437 }
1438 OS << "]";
1439
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001440 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001441 }
1442
Kevin Enderby613b7572011-11-01 22:27:22 +00001443 // If we are generating dwarf for assembly source files and the current
1444 // section is the initial text section then generate a .loc directive for
1445 // the instruction.
1446 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001447 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001448
1449 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1450
1451 // If we previously parsed a cpp hash file line comment then make sure the
1452 // current Dwarf File is for the CppHashFilename if not then emit the
1453 // Dwarf File table for it and adjust the line number for the .loc.
1454 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1455 getContext().getMCDwarfFiles();
1456 if (CppHashFilename.size() != 0) {
1457 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1458 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001459 getStreamer().EmitDwarfFileDirective(
1460 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001461
Kevin Enderby32c1a822012-11-05 21:55:41 +00001462 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001463 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1464 }
1465
Kevin Enderby613b7572011-11-01 22:27:22 +00001466 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001467 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001468 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001469 StringRef());
1470 }
1471
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001472 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001473 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001474 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001475 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1476 Info.ParsedOperands,
1477 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001478 ParsingInlineAsm);
1479 }
Chris Lattner98986712010-01-14 22:21:20 +00001480
Chris Lattnercbf8a982010-09-11 16:18:25 +00001481 // Don't skip the rest of the line, the instruction parser is responsible for
1482 // that.
1483 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001484}
Chris Lattner9a023f72009-06-24 04:43:34 +00001485
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001486/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1487/// since they may not be able to be tokenized to get to the end of line token.
1488void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001489 if (!Lexer.is(AsmToken::EndOfStatement))
1490 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001491 // Eat EOL.
1492 Lex();
1493}
1494
1495/// ParseCppHashLineFilenameComment as this:
1496/// ::= # number "filename"
1497/// or just as a full line comment if it doesn't have a number and a string.
1498bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1499 Lex(); // Eat the hash token.
1500
1501 if (getLexer().isNot(AsmToken::Integer)) {
1502 // Consume the line since in cases it is not a well-formed line directive,
1503 // as if were simply a full line comment.
1504 EatToEndOfLine();
1505 return false;
1506 }
1507
1508 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001509 Lex();
1510
1511 if (getLexer().isNot(AsmToken::String)) {
1512 EatToEndOfLine();
1513 return false;
1514 }
1515
1516 StringRef Filename = getTok().getString();
1517 // Get rid of the enclosing quotes.
1518 Filename = Filename.substr(1, Filename.size()-2);
1519
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001520 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1521 CppHashLoc = L;
1522 CppHashFilename = Filename;
1523 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001524 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001525
1526 // Ignore any trailing characters, they're just comment.
1527 EatToEndOfLine();
1528 return false;
1529}
1530
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001531/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001532/// for the Filename and LineNo if any in the diagnostic.
1533void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1534 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1535 raw_ostream &OS = errs();
1536
1537 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1538 const SMLoc &DiagLoc = Diag.getLoc();
1539 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1540 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1541
1542 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1543 // before printing the message.
1544 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001545 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001546 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1547 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1548 }
1549
Eric Christopher2318ba12012-12-18 00:30:54 +00001550 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001551 // manager changed or buffer changed (like in a nested include) then just
1552 // print the normal diagnostic using its Filename and LineNo.
1553 if (!Parser->CppHashLineNumber ||
1554 &DiagSrcMgr != &Parser->SrcMgr ||
1555 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001556 if (Parser->SavedDiagHandler)
1557 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1558 else
1559 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001560 return;
1561 }
1562
Eric Christopher2318ba12012-12-18 00:30:54 +00001563 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001564 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1565 // the diagnostic.
1566 const std::string Filename = Parser->CppHashFilename;
1567
1568 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1569 int CppHashLocLineNo =
1570 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1571 int LineNo = Parser->CppHashLineNumber - 1 +
1572 (DiagLocLineNo - CppHashLocLineNo);
1573
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001574 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1575 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001576 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001577 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001578
Benjamin Kramer04a04262011-10-16 10:48:29 +00001579 if (Parser->SavedDiagHandler)
1580 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1581 else
1582 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001583}
1584
Rafael Espindola799aacf2012-08-21 18:29:30 +00001585// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1586// difference being that that function accepts '@' as part of identifiers and
1587// we can't do that. AsmLexer.cpp should probably be changed to handle
1588// '@' as a special case when needed.
1589static bool isIdentifierChar(char c) {
1590 return isalnum(c) || c == '_' || c == '$' || c == '.';
1591}
1592
Rafael Espindola761cb062012-06-03 23:57:14 +00001593bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001594 const MacroParameters &Parameters,
1595 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001596 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001597 unsigned NParameters = Parameters.size();
1598 if (NParameters != 0 && NParameters != A.size())
1599 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001600
Preston Gurd7b6f2032012-09-19 20:36:12 +00001601 // A macro without parameters is handled differently on Darwin:
1602 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001603 while (!Body.empty()) {
1604 // Scan for the next substitution.
1605 std::size_t End = Body.size(), Pos = 0;
1606 for (; Pos != End; ++Pos) {
1607 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001608 if (!NParameters) {
1609 // This macro has no parameters, look for $0, $1, etc.
1610 if (Body[Pos] != '$' || Pos + 1 == End)
1611 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001612
Rafael Espindola65366442011-06-05 02:43:45 +00001613 char Next = Body[Pos + 1];
1614 if (Next == '$' || Next == 'n' || isdigit(Next))
1615 break;
1616 } else {
1617 // This macro has parameters, look for \foo, \bar, etc.
1618 if (Body[Pos] == '\\' && Pos + 1 != End)
1619 break;
1620 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001621 }
1622
1623 // Add the prefix.
1624 OS << Body.slice(0, Pos);
1625
1626 // Check if we reached the end.
1627 if (Pos == End)
1628 break;
1629
Rafael Espindola65366442011-06-05 02:43:45 +00001630 if (!NParameters) {
1631 switch (Body[Pos+1]) {
1632 // $$ => $
1633 case '$':
1634 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001635 break;
1636
Rafael Espindola65366442011-06-05 02:43:45 +00001637 // $n => number of arguments
1638 case 'n':
1639 OS << A.size();
1640 break;
1641
1642 // $[0-9] => argument
1643 default: {
1644 // Missing arguments are ignored.
1645 unsigned Index = Body[Pos+1] - '0';
1646 if (Index >= A.size())
1647 break;
1648
1649 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001650 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001651 ie = A[Index].end(); it != ie; ++it)
1652 OS << it->getString();
1653 break;
1654 }
1655 }
1656 Pos += 2;
1657 } else {
1658 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001659 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001660 ++I;
1661
1662 const char *Begin = Body.data() + Pos +1;
1663 StringRef Argument(Begin, I - (Pos +1));
1664 unsigned Index = 0;
1665 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001666 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001667 break;
1668
Preston Gurd7b6f2032012-09-19 20:36:12 +00001669 if (Index == NParameters) {
1670 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1671 Pos += 3;
1672 else {
1673 OS << '\\' << Argument;
1674 Pos = I;
1675 }
1676 } else {
1677 for (MacroArgument::const_iterator it = A[Index].begin(),
1678 ie = A[Index].end(); it != ie; ++it)
1679 if (it->getKind() == AsmToken::String)
1680 OS << it->getStringContents();
1681 else
1682 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001683
Preston Gurd7b6f2032012-09-19 20:36:12 +00001684 Pos += 1 + Argument.size();
1685 }
Rafael Espindola65366442011-06-05 02:43:45 +00001686 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001687 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001688 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001689 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001690
Rafael Espindola65366442011-06-05 02:43:45 +00001691 return false;
1692}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001693
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001694MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL,
1695 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001696 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001697 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1698 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001699{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001700}
1701
Preston Gurd7b6f2032012-09-19 20:36:12 +00001702static bool IsOperator(AsmToken::TokenKind kind)
1703{
1704 switch (kind)
1705 {
1706 default:
1707 return false;
1708 case AsmToken::Plus:
1709 case AsmToken::Minus:
1710 case AsmToken::Tilde:
1711 case AsmToken::Slash:
1712 case AsmToken::Star:
1713 case AsmToken::Dot:
1714 case AsmToken::Equal:
1715 case AsmToken::EqualEqual:
1716 case AsmToken::Pipe:
1717 case AsmToken::PipePipe:
1718 case AsmToken::Caret:
1719 case AsmToken::Amp:
1720 case AsmToken::AmpAmp:
1721 case AsmToken::Exclaim:
1722 case AsmToken::ExclaimEqual:
1723 case AsmToken::Percent:
1724 case AsmToken::Less:
1725 case AsmToken::LessEqual:
1726 case AsmToken::LessLess:
1727 case AsmToken::LessGreater:
1728 case AsmToken::Greater:
1729 case AsmToken::GreaterEqual:
1730 case AsmToken::GreaterGreater:
1731 return true;
1732 }
1733}
1734
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001735/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1736/// This is used for both default macro parameter values and the
1737/// arguments in macro invocations
Preston Gurd7b6f2032012-09-19 20:36:12 +00001738bool AsmParser::ParseMacroArgument(MacroArgument &MA,
1739 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001740 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001741 unsigned AddTokens = 0;
1742
1743 // gas accepts arguments separated by whitespace, except on Darwin
1744 if (!IsDarwin)
1745 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001746
1747 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001748 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1749 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001750 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001751 }
1752
1753 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1754 // Spaces and commas cannot be mixed to delimit parameters
1755 if (ArgumentDelimiter == AsmToken::Eof)
1756 ArgumentDelimiter = AsmToken::Comma;
1757 else if (ArgumentDelimiter != AsmToken::Comma) {
1758 Lexer.setSkipSpace(true);
1759 return TokError("expected ' ' for macro argument separator");
1760 }
1761 break;
1762 }
1763
1764 if (Lexer.is(AsmToken::Space)) {
1765 Lex(); // Eat spaces
1766
1767 // Spaces can delimit parameters, but could also be part an expression.
1768 // If the token after a space is an operator, add the token and the next
1769 // one into this argument
1770 if (ArgumentDelimiter == AsmToken::Space ||
1771 ArgumentDelimiter == AsmToken::Eof) {
1772 if (IsOperator(Lexer.getKind())) {
1773 // Check to see whether the token is used as an operator,
1774 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001775 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001776 if (*NextChar == ' ')
1777 AddTokens = 2;
1778 }
1779
1780 if (!AddTokens && ParenLevel == 0) {
1781 if (ArgumentDelimiter == AsmToken::Eof &&
1782 !IsOperator(Lexer.getKind()))
1783 ArgumentDelimiter = AsmToken::Space;
1784 break;
1785 }
1786 }
1787 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001788
1789 // HandleMacroEntry relies on not advancing the lexer here
1790 // to be able to fill in the remaining default parameter values
1791 if (Lexer.is(AsmToken::EndOfStatement))
1792 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001793
1794 // Adjust the current parentheses level.
1795 if (Lexer.is(AsmToken::LParen))
1796 ++ParenLevel;
1797 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1798 --ParenLevel;
1799
1800 // Append the token to the current argument list.
1801 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001802 if (AddTokens)
1803 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001804 Lex();
1805 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001806
1807 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001808 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001809 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001810 return false;
1811}
1812
1813// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001814bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001815 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001816 // Argument delimiter is initially unknown. It will be set by
1817 // ParseMacroArgument()
1818 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001819
1820 // Parse two kinds of macro invocations:
1821 // - macros defined without any parameters accept an arbitrary number of them
1822 // - macros defined with parameters accept at most that many of them
1823 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1824 ++Parameter) {
1825 MacroArgument MA;
1826
Preston Gurd7b6f2032012-09-19 20:36:12 +00001827 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001828 return true;
1829
Preston Gurd6c9176a2012-09-19 20:29:04 +00001830 if (!MA.empty() || !NParameters)
1831 A.push_back(MA);
1832 else if (NParameters) {
1833 if (!M->Parameters[Parameter].second.empty())
1834 A.push_back(M->Parameters[Parameter].second);
1835 }
Jim Grosbach97146442012-07-30 22:44:17 +00001836
Preston Gurd6c9176a2012-09-19 20:29:04 +00001837 // At the end of the statement, fill in remaining arguments that have
1838 // default values. If there aren't any, then the next argument is
1839 // required but missing
1840 if (Lexer.is(AsmToken::EndOfStatement)) {
1841 if (NParameters && Parameter < NParameters - 1) {
1842 if (M->Parameters[Parameter + 1].second.empty())
1843 return TokError("macro argument '" +
1844 Twine(M->Parameters[Parameter + 1].first) +
1845 "' is missing");
1846 else
1847 continue;
1848 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001849 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001850 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001851
1852 if (Lexer.is(AsmToken::Comma))
1853 Lex();
1854 }
1855 return TokError("Too many arguments");
1856}
1857
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001858bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1859 const Macro *M) {
1860 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1861 // this, although we should protect against infinite loops.
1862 if (ActiveMacros.size() == 20)
1863 return TokError("macros cannot be nested more than 20 levels deep");
1864
Rafael Espindola8a403d32012-08-08 14:51:03 +00001865 MacroArguments A;
1866 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001867 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001868
Jim Grosbach97146442012-07-30 22:44:17 +00001869 // Remove any trailing empty arguments. Do this after-the-fact as we have
1870 // to keep empty arguments in the middle of the list or positionality
1871 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001872 while (!A.empty() && A.back().empty())
1873 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001874
Rafael Espindola65366442011-06-05 02:43:45 +00001875 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1876 // to hold the macro body with substitutions.
1877 SmallString<256> Buf;
1878 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001879 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001880
Rafael Espindola8a403d32012-08-08 14:51:03 +00001881 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001882 return true;
1883
Rafael Espindola761cb062012-06-03 23:57:14 +00001884 // We include the .endmacro in the buffer as our queue to exit the macro
1885 // instantiation.
1886 OS << ".endmacro\n";
1887
Rafael Espindola65366442011-06-05 02:43:45 +00001888 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001889 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001890
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001891 // Create the macro instantiation object and add to the current macro
1892 // instantiation stack.
1893 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001894 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001895 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001896 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001897 ActiveMacros.push_back(MI);
1898
1899 // Jump to the macro instantiation and prime the lexer.
1900 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1901 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1902 Lex();
1903
1904 return false;
1905}
1906
1907void AsmParser::HandleMacroExit() {
1908 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001909 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001910 Lex();
1911
1912 // Pop the instantiation entry.
1913 delete ActiveMacros.back();
1914 ActiveMacros.pop_back();
1915}
1916
Rafael Espindolae71cc862012-01-28 05:57:00 +00001917static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001918 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001919 case MCExpr::Binary: {
1920 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1921 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001922 break;
1923 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001924 case MCExpr::Target:
1925 case MCExpr::Constant:
1926 return false;
1927 case MCExpr::SymbolRef: {
1928 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001929 if (S.isVariable())
1930 return IsUsedIn(Sym, S.getVariableValue());
1931 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001932 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001933 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001934 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001935 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001936
1937 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001938}
1939
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001940bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1941 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001942 // FIXME: Use better location, we should use proper tokens.
1943 SMLoc EqualLoc = Lexer.getLoc();
1944
Daniel Dunbar821e3332009-08-31 08:09:28 +00001945 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001946 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001947 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001948
Rafael Espindolae71cc862012-01-28 05:57:00 +00001949 // Note: we don't count b as used in "a = b". This is to allow
1950 // a = b
1951 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001952
Daniel Dunbar3f872332009-07-28 16:08:33 +00001953 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001954 return TokError("unexpected token in assignment");
1955
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001956 // Error on assignment to '.'.
1957 if (Name == ".") {
1958 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1959 "(use '.space' or '.org').)"));
1960 }
1961
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001962 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001963 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001964
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001965 // Validate that the LHS is allowed to be a variable (either it has not been
1966 // used as a symbol, or it is an absolute symbol).
1967 MCSymbol *Sym = getContext().LookupSymbol(Name);
1968 if (Sym) {
1969 // Diagnose assignment to a label.
1970 //
1971 // FIXME: Diagnostics. Note the location of the definition as a label.
1972 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001973 if (IsUsedIn(Sym, Value))
1974 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1975 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001976 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001977 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1978 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001979 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001980 return Error(EqualLoc, "redefinition of '" + Name + "'");
1981 else if (!Sym->isVariable())
1982 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001983 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001984 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1985 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001986
1987 // Don't count these checks as uses.
1988 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001989 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001990 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001991
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001992 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001993
1994 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001995 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001996 if (NoDeadStrip)
1997 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1998
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001999
2000 return false;
2001}
2002
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002003/// ParseIdentifier:
2004/// ::= identifier
2005/// ::= string
2006bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002007 // The assembler has relaxed rules for accepting identifiers, in particular we
2008 // allow things like '.globl $foo', which would normally be separate
2009 // tokens. At this level, we have already lexed so we cannot (currently)
2010 // handle this as a context dependent token, instead we detect adjacent tokens
2011 // and return the combined identifier.
2012 if (Lexer.is(AsmToken::Dollar)) {
2013 SMLoc DollarLoc = getLexer().getLoc();
2014
2015 // Consume the dollar sign, and check for a following identifier.
2016 Lex();
2017 if (Lexer.isNot(AsmToken::Identifier))
2018 return true;
2019
2020 // We have a '$' followed by an identifier, make sure they are adjacent.
2021 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2022 return true;
2023
2024 // Construct the joined identifier and consume the token.
2025 Res = StringRef(DollarLoc.getPointer(),
2026 getTok().getIdentifier().size() + 1);
2027 Lex();
2028 return false;
2029 }
2030
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002031 if (Lexer.isNot(AsmToken::Identifier) &&
2032 Lexer.isNot(AsmToken::String))
2033 return true;
2034
Sean Callanan18b83232010-01-19 21:44:56 +00002035 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002036
Sean Callanan79ed1a82010-01-19 20:22:31 +00002037 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002038
2039 return false;
2040}
2041
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002042/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002043/// ::= .equ identifier ',' expression
2044/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002045/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002046bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002047 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002048
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002049 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002050 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002051
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002052 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002053 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002054 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002055
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002056 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002057}
2058
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002059bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002061
2062 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002063 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002064 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2065 if (Str[i] != '\\') {
2066 Data += Str[i];
2067 continue;
2068 }
2069
2070 // Recognize escaped characters. Note that this escape semantics currently
2071 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2072 ++i;
2073 if (i == e)
2074 return TokError("unexpected backslash at end of string");
2075
2076 // Recognize octal sequences.
2077 if ((unsigned) (Str[i] - '0') <= 7) {
2078 // Consume up to three octal characters.
2079 unsigned Value = Str[i] - '0';
2080
2081 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2082 ++i;
2083 Value = Value * 8 + (Str[i] - '0');
2084
2085 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2086 ++i;
2087 Value = Value * 8 + (Str[i] - '0');
2088 }
2089 }
2090
2091 if (Value > 255)
2092 return TokError("invalid octal escape sequence (out of range)");
2093
2094 Data += (unsigned char) Value;
2095 continue;
2096 }
2097
2098 // Otherwise recognize individual escapes.
2099 switch (Str[i]) {
2100 default:
2101 // Just reject invalid escape sequences for now.
2102 return TokError("invalid escape sequence (unrecognized character)");
2103
2104 case 'b': Data += '\b'; break;
2105 case 'f': Data += '\f'; break;
2106 case 'n': Data += '\n'; break;
2107 case 'r': Data += '\r'; break;
2108 case 't': Data += '\t'; break;
2109 case '"': Data += '"'; break;
2110 case '\\': Data += '\\'; break;
2111 }
2112 }
2113
2114 return false;
2115}
2116
Daniel Dunbara0d14262009-06-24 23:30:00 +00002117/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002118/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2119bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002120 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002121 CheckForValidSection();
2122
Daniel Dunbara0d14262009-06-24 23:30:00 +00002123 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002124 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002125 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002126
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002127 std::string Data;
2128 if (ParseEscapedString(Data))
2129 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002130
2131 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002132 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002133 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2134
Sean Callanan79ed1a82010-01-19 20:22:31 +00002135 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136
2137 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002138 break;
2139
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002140 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002141 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002143 }
2144 }
2145
Sean Callanan79ed1a82010-01-19 20:22:31 +00002146 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002147 return false;
2148}
2149
2150/// ParseDirectiveValue
2151/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2152bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002153 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002154 CheckForValidSection();
2155
Daniel Dunbara0d14262009-06-24 23:30:00 +00002156 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002157 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002158 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002159 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002160 return true;
2161
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002162 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002163 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2164 assert(Size <= 8 && "Invalid size");
2165 uint64_t IntValue = MCE->getValue();
2166 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2167 return Error(ExprLoc, "literal value out of range for directive");
2168 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2169 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002170 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002171
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002172 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002173 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002174
Daniel Dunbara0d14262009-06-24 23:30:00 +00002175 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002176 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002177 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002178 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002179 }
2180 }
2181
Sean Callanan79ed1a82010-01-19 20:22:31 +00002182 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002183 return false;
2184}
2185
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002186/// ParseDirectiveRealValue
2187/// ::= (.single | .double) [ expression (, expression)* ]
2188bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2189 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2190 CheckForValidSection();
2191
2192 for (;;) {
2193 // We don't truly support arithmetic on floating point expressions, so we
2194 // have to manually parse unary prefixes.
2195 bool IsNeg = false;
2196 if (getLexer().is(AsmToken::Minus)) {
2197 Lex();
2198 IsNeg = true;
2199 } else if (getLexer().is(AsmToken::Plus))
2200 Lex();
2201
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002202 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002203 getLexer().isNot(AsmToken::Real) &&
2204 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002205 return TokError("unexpected token in directive");
2206
2207 // Convert to an APFloat.
2208 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002209 StringRef IDVal = getTok().getString();
2210 if (getLexer().is(AsmToken::Identifier)) {
2211 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2212 Value = APFloat::getInf(Semantics);
2213 else if (!IDVal.compare_lower("nan"))
2214 Value = APFloat::getNaN(Semantics, false, ~0);
2215 else
2216 return TokError("invalid floating point literal");
2217 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002218 APFloat::opInvalidOp)
2219 return TokError("invalid floating point literal");
2220 if (IsNeg)
2221 Value.changeSign();
2222
2223 // Consume the numeric token.
2224 Lex();
2225
2226 // Emit the value as an integer.
2227 APInt AsInt = Value.bitcastToAPInt();
2228 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2229 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2230
2231 if (getLexer().is(AsmToken::EndOfStatement))
2232 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002233
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002234 if (getLexer().isNot(AsmToken::Comma))
2235 return TokError("unexpected token in directive");
2236 Lex();
2237 }
2238 }
2239
2240 Lex();
2241 return false;
2242}
2243
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002244/// ParseDirectiveZero
2245/// ::= .zero expression
2246bool AsmParser::ParseDirectiveZero() {
2247 CheckForValidSection();
2248
2249 int64_t NumBytes;
2250 if (ParseAbsoluteExpression(NumBytes))
2251 return true;
2252
Rafael Espindolae452b172010-10-05 19:42:57 +00002253 int64_t Val = 0;
2254 if (getLexer().is(AsmToken::Comma)) {
2255 Lex();
2256 if (ParseAbsoluteExpression(Val))
2257 return true;
2258 }
2259
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002260 if (getLexer().isNot(AsmToken::EndOfStatement))
2261 return TokError("unexpected token in '.zero' directive");
2262
2263 Lex();
2264
Rafael Espindolae452b172010-10-05 19:42:57 +00002265 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002266
2267 return false;
2268}
2269
Daniel Dunbara0d14262009-06-24 23:30:00 +00002270/// ParseDirectiveFill
2271/// ::= .fill expression , expression , expression
2272bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002273 CheckForValidSection();
2274
Daniel Dunbara0d14262009-06-24 23:30:00 +00002275 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002276 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002277 return true;
2278
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002279 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002280 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002281 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002282
Daniel Dunbara0d14262009-06-24 23:30:00 +00002283 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002284 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002285 return true;
2286
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002287 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002288 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002289 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002290
Daniel Dunbara0d14262009-06-24 23:30:00 +00002291 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002292 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002293 return true;
2294
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002295 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002296 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002297
Sean Callanan79ed1a82010-01-19 20:22:31 +00002298 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002299
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002300 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2301 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002302
2303 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002304 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002305
2306 return false;
2307}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002308
2309/// ParseDirectiveOrg
2310/// ::= .org expression [ , expression ]
2311bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002312 CheckForValidSection();
2313
Daniel Dunbar821e3332009-08-31 08:09:28 +00002314 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002315 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002316 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002317 return true;
2318
2319 // Parse optional fill expression.
2320 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002321 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2322 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002323 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002324 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002325
Daniel Dunbar475839e2009-06-29 20:37:27 +00002326 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002327 return true;
2328
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002329 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002330 return TokError("unexpected token in '.org' directive");
2331 }
2332
Sean Callanan79ed1a82010-01-19 20:22:31 +00002333 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002334
Jim Grosbachebd4c052012-01-27 00:37:08 +00002335 // Only limited forms of relocatable expressions are accepted here, it
2336 // has to be relative to the current section. The streamer will return
2337 // 'true' if the expression wasn't evaluatable.
2338 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2339 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002340
2341 return false;
2342}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002343
2344/// ParseDirectiveAlign
2345/// ::= {.align, ...} expression [ , expression [ , expression ]]
2346bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002347 CheckForValidSection();
2348
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002349 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002350 int64_t Alignment;
2351 if (ParseAbsoluteExpression(Alignment))
2352 return true;
2353
2354 SMLoc MaxBytesLoc;
2355 bool HasFillExpr = false;
2356 int64_t FillExpr = 0;
2357 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002358 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2359 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002360 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002361 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002362
2363 // The fill expression can be omitted while specifying a maximum number of
2364 // alignment bytes, e.g:
2365 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002366 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002367 HasFillExpr = true;
2368 if (ParseAbsoluteExpression(FillExpr))
2369 return true;
2370 }
2371
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002372 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2373 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002374 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002375 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002376
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002377 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002378 if (ParseAbsoluteExpression(MaxBytesToFill))
2379 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002380
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002381 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002382 return TokError("unexpected token in directive");
2383 }
2384 }
2385
Sean Callanan79ed1a82010-01-19 20:22:31 +00002386 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002387
Daniel Dunbar648ac512010-05-17 21:54:30 +00002388 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002389 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002390
2391 // Compute alignment in bytes.
2392 if (IsPow2) {
2393 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002394 if (Alignment >= 32) {
2395 Error(AlignmentLoc, "invalid alignment value");
2396 Alignment = 31;
2397 }
2398
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002399 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002400 }
2401
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002402 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002403 if (MaxBytesLoc.isValid()) {
2404 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002405 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2406 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002407 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002408 }
2409
2410 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002411 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2412 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002413 MaxBytesToFill = 0;
2414 }
2415 }
2416
Daniel Dunbar648ac512010-05-17 21:54:30 +00002417 // Check whether we should use optimal code alignment for this .align
2418 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002419 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002420 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2421 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002422 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002423 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002424 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002425 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2426 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002427 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002428
2429 return false;
2430}
2431
Eli Bendersky4766ef42012-12-20 19:05:53 +00002432
2433/// ParseDirectiveBundleAlignMode
2434/// ::= {.bundle_align_mode} expression
2435bool AsmParser::ParseDirectiveBundleAlignMode() {
2436 CheckForValidSection();
2437
2438 // Expect a single argument: an expression that evaluates to a constant
2439 // in the inclusive range 0-30.
2440 SMLoc ExprLoc = getLexer().getLoc();
2441 int64_t AlignSizePow2;
2442 if (ParseAbsoluteExpression(AlignSizePow2))
2443 return true;
2444 else if (getLexer().isNot(AsmToken::EndOfStatement))
2445 return TokError("unexpected token after expression in"
2446 " '.bundle_align_mode' directive");
2447 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2448 return Error(ExprLoc,
2449 "invalid bundle alignment size (expected between 0 and 30)");
2450
2451 Lex();
2452
2453 // Because of AlignSizePow2's verified range we can safely truncate it to
2454 // unsigned.
2455 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2456 return false;
2457}
2458
2459/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002460/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002461bool AsmParser::ParseDirectiveBundleLock() {
2462 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002463 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002464
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002465 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2466 StringRef Option;
2467 SMLoc Loc = getTok().getLoc();
2468 const char *kInvalidOptionError =
2469 "invalid option for '.bundle_lock' directive";
2470
2471 if (ParseIdentifier(Option))
2472 return Error(Loc, kInvalidOptionError);
2473
2474 if (Option != "align_to_end")
2475 return Error(Loc, kInvalidOptionError);
2476 else if (getLexer().isNot(AsmToken::EndOfStatement))
2477 return Error(Loc,
2478 "unexpected token after '.bundle_lock' directive option");
2479 AlignToEnd = true;
2480 }
2481
Eli Bendersky4766ef42012-12-20 19:05:53 +00002482 Lex();
2483
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002484 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002485 return false;
2486}
2487
2488/// ParseDirectiveBundleLock
2489/// ::= {.bundle_lock}
2490bool AsmParser::ParseDirectiveBundleUnlock() {
2491 CheckForValidSection();
2492
2493 if (getLexer().isNot(AsmToken::EndOfStatement))
2494 return TokError("unexpected token in '.bundle_unlock' directive");
2495 Lex();
2496
2497 getStreamer().EmitBundleUnlock();
2498 return false;
2499}
2500
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002501/// ParseDirectiveSymbolAttribute
2502/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002503bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002504 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002505 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002506 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002507 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002508
2509 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002510 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002511
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002512 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002513
Jim Grosbach10ec6502011-09-15 17:56:49 +00002514 // Assembler local symbols don't make any sense here. Complain loudly.
2515 if (Sym->isTemporary())
2516 return Error(Loc, "non-local symbol required in directive");
2517
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002518 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002519
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002520 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002521 break;
2522
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002523 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002524 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002525 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002526 }
2527 }
2528
Sean Callanan79ed1a82010-01-19 20:22:31 +00002529 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002530 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002531}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002532
2533/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002534/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2535bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002536 CheckForValidSection();
2537
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002538 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002539 StringRef Name;
2540 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002541 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002542
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002543 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002544 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002545
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002546 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002547 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002548 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002549
2550 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002551 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002552 if (ParseAbsoluteExpression(Size))
2553 return true;
2554
2555 int64_t Pow2Alignment = 0;
2556 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002557 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002558 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002559 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002560 if (ParseAbsoluteExpression(Pow2Alignment))
2561 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002562
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002563 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2564 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002565 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2566
Chris Lattner258281d2010-01-19 06:22:22 +00002567 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002568 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2569 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002570 if (!isPowerOf2_64(Pow2Alignment))
2571 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2572 Pow2Alignment = Log2_64(Pow2Alignment);
2573 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002574 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002575
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002576 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002577 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002578
Sean Callanan79ed1a82010-01-19 20:22:31 +00002579 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002580
Chris Lattner1fc3d752009-07-09 17:25:12 +00002581 // NOTE: a size of zero for a .comm should create a undefined symbol
2582 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002583 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002584 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2585 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002586
Eric Christopherc260a3e2010-05-14 01:38:54 +00002587 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002588 // may internally end up wanting an alignment in bytes.
2589 // FIXME: Diagnose overflow.
2590 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002591 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2592 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002593
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002594 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002595 return Error(IDLoc, "invalid symbol redefinition");
2596
Chris Lattner1fc3d752009-07-09 17:25:12 +00002597 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002598 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002599 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002600 return false;
2601 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002602
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002603 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002604 return false;
2605}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002606
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002607/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002608/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002609bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002610 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002611 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002612
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002613 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002614 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002615 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002616
Sean Callanan79ed1a82010-01-19 20:22:31 +00002617 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002618
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002619 if (Str.empty())
2620 Error(Loc, ".abort detected. Assembly stopping.");
2621 else
2622 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002623 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002624
2625 return false;
2626}
Kevin Enderby71148242009-07-14 21:35:03 +00002627
Kevin Enderby1f049b22009-07-14 23:21:55 +00002628/// ParseDirectiveInclude
2629/// ::= .include "filename"
2630bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002631 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002632 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002633
Sean Callanan18b83232010-01-19 21:44:56 +00002634 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002635 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002636 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002637
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002638 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002639 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002640
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002641 // Strip the quotes.
2642 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002643
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002644 // Attempt to switch the lexer to the included file before consuming the end
2645 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002646 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002647 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002648 return true;
2649 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002650
2651 return false;
2652}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002653
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002654/// ParseDirectiveIncbin
2655/// ::= .incbin "filename"
2656bool AsmParser::ParseDirectiveIncbin() {
2657 if (getLexer().isNot(AsmToken::String))
2658 return TokError("expected string in '.incbin' directive");
2659
2660 std::string Filename = getTok().getString();
2661 SMLoc IncbinLoc = getLexer().getLoc();
2662 Lex();
2663
2664 if (getLexer().isNot(AsmToken::EndOfStatement))
2665 return TokError("unexpected token in '.incbin' directive");
2666
2667 // Strip the quotes.
2668 Filename = Filename.substr(1, Filename.size()-2);
2669
2670 // Attempt to process the included file.
2671 if (ProcessIncbinFile(Filename)) {
2672 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2673 return true;
2674 }
2675
2676 return false;
2677}
2678
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002679/// ParseDirectiveIf
2680/// ::= .if expression
2681bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002682 TheCondStack.push_back(TheCondState);
2683 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002684 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002685 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002686 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002687 int64_t ExprValue;
2688 if (ParseAbsoluteExpression(ExprValue))
2689 return true;
2690
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002691 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002692 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002693
Sean Callanan79ed1a82010-01-19 20:22:31 +00002694 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002695
2696 TheCondState.CondMet = ExprValue;
2697 TheCondState.Ignore = !TheCondState.CondMet;
2698 }
2699
2700 return false;
2701}
2702
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002703/// ParseDirectiveIfb
2704/// ::= .ifb string
2705bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2706 TheCondStack.push_back(TheCondState);
2707 TheCondState.TheCond = AsmCond::IfCond;
2708
Benjamin Kramer29739e72012-05-12 16:52:21 +00002709 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002710 EatToEndOfStatement();
2711 } else {
2712 StringRef Str = ParseStringToEndOfStatement();
2713
2714 if (getLexer().isNot(AsmToken::EndOfStatement))
2715 return TokError("unexpected token in '.ifb' directive");
2716
2717 Lex();
2718
2719 TheCondState.CondMet = ExpectBlank == Str.empty();
2720 TheCondState.Ignore = !TheCondState.CondMet;
2721 }
2722
2723 return false;
2724}
2725
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002726/// ParseDirectiveIfc
2727/// ::= .ifc string1, string2
2728bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2729 TheCondStack.push_back(TheCondState);
2730 TheCondState.TheCond = AsmCond::IfCond;
2731
Benjamin Kramer29739e72012-05-12 16:52:21 +00002732 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002733 EatToEndOfStatement();
2734 } else {
2735 StringRef Str1 = ParseStringToComma();
2736
2737 if (getLexer().isNot(AsmToken::Comma))
2738 return TokError("unexpected token in '.ifc' directive");
2739
2740 Lex();
2741
2742 StringRef Str2 = ParseStringToEndOfStatement();
2743
2744 if (getLexer().isNot(AsmToken::EndOfStatement))
2745 return TokError("unexpected token in '.ifc' directive");
2746
2747 Lex();
2748
2749 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2750 TheCondState.Ignore = !TheCondState.CondMet;
2751 }
2752
2753 return false;
2754}
2755
2756/// ParseDirectiveIfdef
2757/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002758bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2759 StringRef Name;
2760 TheCondStack.push_back(TheCondState);
2761 TheCondState.TheCond = AsmCond::IfCond;
2762
2763 if (TheCondState.Ignore) {
2764 EatToEndOfStatement();
2765 } else {
2766 if (ParseIdentifier(Name))
2767 return TokError("expected identifier after '.ifdef'");
2768
2769 Lex();
2770
2771 MCSymbol *Sym = getContext().LookupSymbol(Name);
2772
2773 if (expect_defined)
2774 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2775 else
2776 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2777 TheCondState.Ignore = !TheCondState.CondMet;
2778 }
2779
2780 return false;
2781}
2782
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002783/// ParseDirectiveElseIf
2784/// ::= .elseif expression
2785bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2786 if (TheCondState.TheCond != AsmCond::IfCond &&
2787 TheCondState.TheCond != AsmCond::ElseIfCond)
2788 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2789 " an .elseif");
2790 TheCondState.TheCond = AsmCond::ElseIfCond;
2791
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002792 bool LastIgnoreState = false;
2793 if (!TheCondStack.empty())
2794 LastIgnoreState = TheCondStack.back().Ignore;
2795 if (LastIgnoreState || TheCondState.CondMet) {
2796 TheCondState.Ignore = true;
2797 EatToEndOfStatement();
2798 }
2799 else {
2800 int64_t ExprValue;
2801 if (ParseAbsoluteExpression(ExprValue))
2802 return true;
2803
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002804 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002805 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002806
Sean Callanan79ed1a82010-01-19 20:22:31 +00002807 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002808 TheCondState.CondMet = ExprValue;
2809 TheCondState.Ignore = !TheCondState.CondMet;
2810 }
2811
2812 return false;
2813}
2814
2815/// ParseDirectiveElse
2816/// ::= .else
2817bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002818 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002819 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002820
Sean Callanan79ed1a82010-01-19 20:22:31 +00002821 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002822
2823 if (TheCondState.TheCond != AsmCond::IfCond &&
2824 TheCondState.TheCond != AsmCond::ElseIfCond)
2825 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2826 ".elseif");
2827 TheCondState.TheCond = AsmCond::ElseCond;
2828 bool LastIgnoreState = false;
2829 if (!TheCondStack.empty())
2830 LastIgnoreState = TheCondStack.back().Ignore;
2831 if (LastIgnoreState || TheCondState.CondMet)
2832 TheCondState.Ignore = true;
2833 else
2834 TheCondState.Ignore = false;
2835
2836 return false;
2837}
2838
2839/// ParseDirectiveEndIf
2840/// ::= .endif
2841bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002842 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002843 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002844
Sean Callanan79ed1a82010-01-19 20:22:31 +00002845 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002846
2847 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2848 TheCondStack.empty())
2849 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2850 ".else");
2851 if (!TheCondStack.empty()) {
2852 TheCondState = TheCondStack.back();
2853 TheCondStack.pop_back();
2854 }
2855
2856 return false;
2857}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002858
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002859void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002860 DirectiveKindMapping[".set"] = DK_SET;
2861 DirectiveKindMapping[".equ"] = DK_EQU;
2862 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2863 DirectiveKindMapping[".ascii"] = DK_ASCII;
2864 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2865 DirectiveKindMapping[".string"] = DK_STRING;
2866 DirectiveKindMapping[".byte"] = DK_BYTE;
2867 DirectiveKindMapping[".short"] = DK_SHORT;
2868 DirectiveKindMapping[".value"] = DK_VALUE;
2869 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2870 DirectiveKindMapping[".long"] = DK_LONG;
2871 DirectiveKindMapping[".int"] = DK_INT;
2872 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2873 DirectiveKindMapping[".quad"] = DK_QUAD;
2874 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2875 DirectiveKindMapping[".single"] = DK_SINGLE;
2876 DirectiveKindMapping[".float"] = DK_FLOAT;
2877 DirectiveKindMapping[".double"] = DK_DOUBLE;
2878 DirectiveKindMapping[".align"] = DK_ALIGN;
2879 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2880 DirectiveKindMapping[".balign"] = DK_BALIGN;
2881 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2882 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2883 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2884 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2885 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2886 DirectiveKindMapping[".org"] = DK_ORG;
2887 DirectiveKindMapping[".fill"] = DK_FILL;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002888 DirectiveKindMapping[".zero"] = DK_ZERO;
2889 DirectiveKindMapping[".extern"] = DK_EXTERN;
2890 DirectiveKindMapping[".globl"] = DK_GLOBL;
2891 DirectiveKindMapping[".global"] = DK_GLOBAL;
2892 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2893 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2894 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2895 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2896 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2897 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2898 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2899 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2900 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2901 DirectiveKindMapping[".comm"] = DK_COMM;
2902 DirectiveKindMapping[".common"] = DK_COMMON;
2903 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2904 DirectiveKindMapping[".abort"] = DK_ABORT;
2905 DirectiveKindMapping[".include"] = DK_INCLUDE;
2906 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2907 DirectiveKindMapping[".code16"] = DK_CODE16;
2908 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2909 DirectiveKindMapping[".rept"] = DK_REPT;
2910 DirectiveKindMapping[".irp"] = DK_IRP;
2911 DirectiveKindMapping[".irpc"] = DK_IRPC;
2912 DirectiveKindMapping[".endr"] = DK_ENDR;
2913 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2914 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2915 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2916 DirectiveKindMapping[".if"] = DK_IF;
2917 DirectiveKindMapping[".ifb"] = DK_IFB;
2918 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2919 DirectiveKindMapping[".ifc"] = DK_IFC;
2920 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2921 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2922 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2923 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2924 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2925 DirectiveKindMapping[".else"] = DK_ELSE;
2926 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002927}
2928
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002929/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002930/// ::= .file [number] filename
2931/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002932bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002933 // FIXME: I'm not sure what this is.
2934 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002935 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002936 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002937 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002938 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002939
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002940 if (FileNumber < 1)
2941 return TokError("file number less than one");
2942 }
2943
Daniel Dunbareceec052010-07-12 17:45:27 +00002944 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002945 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002946
Nick Lewycky44d798d2011-10-17 23:05:28 +00002947 // Usually the directory and filename together, otherwise just the directory.
2948 StringRef Path = getTok().getString();
2949 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002950 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002951
Nick Lewycky44d798d2011-10-17 23:05:28 +00002952 StringRef Directory;
2953 StringRef Filename;
2954 if (getLexer().is(AsmToken::String)) {
2955 if (FileNumber == -1)
2956 return TokError("explicit path specified, but no file number");
2957 Filename = getTok().getString();
2958 Filename = Filename.substr(1, Filename.size()-2);
2959 Directory = Path;
2960 Lex();
2961 } else {
2962 Filename = Path;
2963 }
2964
Daniel Dunbareceec052010-07-12 17:45:27 +00002965 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002966 return TokError("unexpected token in '.file' directive");
2967
Chris Lattnerd32e8032010-01-25 19:02:58 +00002968 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002969 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002970 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002971 if (getContext().getGenDwarfForAssembly() == true)
2972 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2973 "used to generate dwarf debug info for assembly code");
2974
Nick Lewycky44d798d2011-10-17 23:05:28 +00002975 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002976 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002977 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002978
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002979 return false;
2980}
2981
2982/// ParseDirectiveLine
2983/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002984bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002985 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2986 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002987 return TokError("unexpected token in '.line' directive");
2988
Sean Callanan18b83232010-01-19 21:44:56 +00002989 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002990 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002991 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002992
2993 // FIXME: Do something with the .line.
2994 }
2995
Daniel Dunbareceec052010-07-12 17:45:27 +00002996 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002997 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002998
2999 return false;
3000}
3001
3002
3003/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003004/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003005/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3006/// The first number is a file number, must have been previously assigned with
3007/// a .file directive, the second number is the line number and optionally the
3008/// third number is a column position (zero if not specified). The remaining
3009/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003010bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003011
Daniel Dunbareceec052010-07-12 17:45:27 +00003012 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003013 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003014 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003015 if (FileNumber < 1)
3016 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003017 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003018 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003019 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003020
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003021 int64_t LineNumber = 0;
3022 if (getLexer().is(AsmToken::Integer)) {
3023 LineNumber = getTok().getIntVal();
3024 if (LineNumber < 1)
3025 return TokError("line number less than one in '.loc' directive");
3026 Lex();
3027 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003028
3029 int64_t ColumnPos = 0;
3030 if (getLexer().is(AsmToken::Integer)) {
3031 ColumnPos = getTok().getIntVal();
3032 if (ColumnPos < 0)
3033 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003034 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003035 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003036
Kevin Enderbyc0957932010-09-30 16:52:03 +00003037 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003038 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003039 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003040 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3041 for (;;) {
3042 if (getLexer().is(AsmToken::EndOfStatement))
3043 break;
3044
3045 StringRef Name;
3046 SMLoc Loc = getTok().getLoc();
3047 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003048 return TokError("unexpected token in '.loc' directive");
3049
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003050 if (Name == "basic_block")
3051 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3052 else if (Name == "prologue_end")
3053 Flags |= DWARF2_FLAG_PROLOGUE_END;
3054 else if (Name == "epilogue_begin")
3055 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3056 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003057 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003058 const MCExpr *Value;
3059 if (getParser().ParseExpression(Value))
3060 return true;
3061 // The expression must be the constant 0 or 1.
3062 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3063 int Value = MCE->getValue();
3064 if (Value == 0)
3065 Flags &= ~DWARF2_FLAG_IS_STMT;
3066 else if (Value == 1)
3067 Flags |= DWARF2_FLAG_IS_STMT;
3068 else
3069 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003070 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003071 else {
3072 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3073 }
3074 }
3075 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003076 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003077 const MCExpr *Value;
3078 if (getParser().ParseExpression(Value))
3079 return true;
3080 // The expression must be a constant greater or equal to 0.
3081 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3082 int Value = MCE->getValue();
3083 if (Value < 0)
3084 return Error(Loc, "isa number less than zero");
3085 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003086 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003087 else {
3088 return Error(Loc, "isa number not a constant value");
3089 }
3090 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003091 else if (Name == "discriminator") {
3092 if (getParser().ParseAbsoluteExpression(Discriminator))
3093 return true;
3094 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003095 else {
3096 return Error(Loc, "unknown sub-directive in '.loc' directive");
3097 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003098
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003099 if (getLexer().is(AsmToken::EndOfStatement))
3100 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003101 }
3102 }
3103
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003104 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003105 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003106
3107 return false;
3108}
3109
Daniel Dunbar138abae2010-10-16 04:56:42 +00003110/// ParseDirectiveStabs
3111/// ::= .stabs string, number, number, number
3112bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3113 SMLoc DirectiveLoc) {
3114 return TokError("unsupported directive '" + Directive + "'");
3115}
3116
Eli Bendersky9b1bb052013-01-11 22:55:28 +00003117/// ParseDirectiveSpace
3118/// ::= .space expression [ , expression ]
3119bool GenericAsmParser::ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc) {
3120 getParser().CheckForValidSection();
3121
3122 int64_t NumBytes;
3123 if (getParser().ParseAbsoluteExpression(NumBytes))
3124 return true;
3125
3126 int64_t FillExpr = 0;
3127 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3128 if (getLexer().isNot(AsmToken::Comma))
3129 return TokError("unexpected token in '.space' directive");
3130 Lex();
3131
3132 if (getParser().ParseAbsoluteExpression(FillExpr))
3133 return true;
3134
3135 if (getLexer().isNot(AsmToken::EndOfStatement))
3136 return TokError("unexpected token in '.space' directive");
3137 }
3138
3139 Lex();
3140
3141 if (NumBytes <= 0)
3142 return TokError("invalid number of bytes in '.space' directive");
3143
3144 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3145 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
3146
3147 return false;
3148}
3149
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003150/// ParseDirectiveCFISections
3151/// ::= .cfi_sections section [, section]
3152bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3153 SMLoc DirectiveLoc) {
3154 StringRef Name;
3155 bool EH = false;
3156 bool Debug = false;
3157
3158 if (getParser().ParseIdentifier(Name))
3159 return TokError("Expected an identifier");
3160
3161 if (Name == ".eh_frame")
3162 EH = true;
3163 else if (Name == ".debug_frame")
3164 Debug = true;
3165
3166 if (getLexer().is(AsmToken::Comma)) {
3167 Lex();
3168
3169 if (getParser().ParseIdentifier(Name))
3170 return TokError("Expected an identifier");
3171
3172 if (Name == ".eh_frame")
3173 EH = true;
3174 else if (Name == ".debug_frame")
3175 Debug = true;
3176 }
3177
3178 getStreamer().EmitCFISections(EH, Debug);
3179
3180 return false;
3181}
3182
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003183/// ParseDirectiveCFIStartProc
3184/// ::= .cfi_startproc
3185bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3186 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003187 getStreamer().EmitCFIStartProc();
3188 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003189}
3190
3191/// ParseDirectiveCFIEndProc
3192/// ::= .cfi_endproc
3193bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003194 getStreamer().EmitCFIEndProc();
3195 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003196}
3197
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003198/// ParseRegisterOrRegisterNumber - parse register name or number.
3199bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3200 SMLoc DirectiveLoc) {
3201 unsigned RegNo;
3202
Jim Grosbach6f888a82011-06-02 17:14:04 +00003203 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003204 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3205 DirectiveLoc))
3206 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003207 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003208 } else
3209 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003210
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003211 return false;
3212}
3213
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003214/// ParseDirectiveCFIDefCfa
3215/// ::= .cfi_def_cfa register, offset
3216bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3217 SMLoc DirectiveLoc) {
3218 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003219 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003220 return true;
3221
3222 if (getLexer().isNot(AsmToken::Comma))
3223 return TokError("unexpected token in directive");
3224 Lex();
3225
3226 int64_t Offset = 0;
3227 if (getParser().ParseAbsoluteExpression(Offset))
3228 return true;
3229
Rafael Espindola066c2f42011-04-12 23:59:07 +00003230 getStreamer().EmitCFIDefCfa(Register, Offset);
3231 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003232}
3233
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003234/// ParseDirectiveCFIDefCfaOffset
3235/// ::= .cfi_def_cfa_offset offset
3236bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3237 SMLoc DirectiveLoc) {
3238 int64_t Offset = 0;
3239 if (getParser().ParseAbsoluteExpression(Offset))
3240 return true;
3241
Rafael Espindola066c2f42011-04-12 23:59:07 +00003242 getStreamer().EmitCFIDefCfaOffset(Offset);
3243 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003244}
3245
3246/// ParseDirectiveCFIAdjustCfaOffset
3247/// ::= .cfi_adjust_cfa_offset adjustment
3248bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3249 SMLoc DirectiveLoc) {
3250 int64_t Adjustment = 0;
3251 if (getParser().ParseAbsoluteExpression(Adjustment))
3252 return true;
3253
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003254 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3255 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003256}
3257
3258/// ParseDirectiveCFIDefCfaRegister
3259/// ::= .cfi_def_cfa_register register
3260bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3261 SMLoc DirectiveLoc) {
3262 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003263 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003264 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003265
Rafael Espindola066c2f42011-04-12 23:59:07 +00003266 getStreamer().EmitCFIDefCfaRegister(Register);
3267 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003268}
3269
3270/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003271/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003272bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3273 int64_t Register = 0;
3274 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003275
3276 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003277 return true;
3278
3279 if (getLexer().isNot(AsmToken::Comma))
3280 return TokError("unexpected token in directive");
3281 Lex();
3282
3283 if (getParser().ParseAbsoluteExpression(Offset))
3284 return true;
3285
Rafael Espindola066c2f42011-04-12 23:59:07 +00003286 getStreamer().EmitCFIOffset(Register, Offset);
3287 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003288}
3289
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003290/// ParseDirectiveCFIRelOffset
3291/// ::= .cfi_rel_offset register, offset
3292bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3293 SMLoc DirectiveLoc) {
3294 int64_t Register = 0;
3295
3296 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3297 return true;
3298
3299 if (getLexer().isNot(AsmToken::Comma))
3300 return TokError("unexpected token in directive");
3301 Lex();
3302
3303 int64_t Offset = 0;
3304 if (getParser().ParseAbsoluteExpression(Offset))
3305 return true;
3306
Rafael Espindola25f492e2011-04-12 16:12:03 +00003307 getStreamer().EmitCFIRelOffset(Register, Offset);
3308 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003309}
3310
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003311static bool isValidEncoding(int64_t Encoding) {
3312 if (Encoding & ~0xff)
3313 return false;
3314
3315 if (Encoding == dwarf::DW_EH_PE_omit)
3316 return true;
3317
3318 const unsigned Format = Encoding & 0xf;
3319 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3320 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3321 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3322 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3323 return false;
3324
Rafael Espindolacaf11582010-12-29 04:31:26 +00003325 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003326 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003327 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003328 return false;
3329
3330 return true;
3331}
3332
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003333/// ParseDirectiveCFIPersonalityOrLsda
3334/// ::= .cfi_personality encoding, [symbol_name]
3335/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003336bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003337 SMLoc DirectiveLoc) {
3338 int64_t Encoding = 0;
3339 if (getParser().ParseAbsoluteExpression(Encoding))
3340 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003341 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003342 return false;
3343
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003344 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003345 return TokError("unsupported encoding.");
3346
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003347 if (getLexer().isNot(AsmToken::Comma))
3348 return TokError("unexpected token in directive");
3349 Lex();
3350
3351 StringRef Name;
3352 if (getParser().ParseIdentifier(Name))
3353 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003354
3355 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3356
3357 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003358 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003359 else {
3360 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003361 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003362 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003363 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003364}
3365
Rafael Espindolafe024d02010-12-28 18:36:23 +00003366/// ParseDirectiveCFIRememberState
3367/// ::= .cfi_remember_state
3368bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3369 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003370 getStreamer().EmitCFIRememberState();
3371 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003372}
3373
3374/// ParseDirectiveCFIRestoreState
3375/// ::= .cfi_remember_state
3376bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3377 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003378 getStreamer().EmitCFIRestoreState();
3379 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003380}
3381
Rafael Espindolac5754392011-04-12 15:31:05 +00003382/// ParseDirectiveCFISameValue
3383/// ::= .cfi_same_value register
3384bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3385 SMLoc DirectiveLoc) {
3386 int64_t Register = 0;
3387
3388 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3389 return true;
3390
3391 getStreamer().EmitCFISameValue(Register);
3392
3393 return false;
3394}
3395
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003396/// ParseDirectiveCFIRestore
3397/// ::= .cfi_restore register
3398bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003399 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003400 int64_t Register = 0;
3401 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3402 return true;
3403
3404 getStreamer().EmitCFIRestore(Register);
3405
3406 return false;
3407}
3408
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003409/// ParseDirectiveCFIEscape
3410/// ::= .cfi_escape expression[,...]
3411bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003412 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003413 std::string Values;
3414 int64_t CurrValue;
3415 if (getParser().ParseAbsoluteExpression(CurrValue))
3416 return true;
3417
3418 Values.push_back((uint8_t)CurrValue);
3419
3420 while (getLexer().is(AsmToken::Comma)) {
3421 Lex();
3422
3423 if (getParser().ParseAbsoluteExpression(CurrValue))
3424 return true;
3425
3426 Values.push_back((uint8_t)CurrValue);
3427 }
3428
3429 getStreamer().EmitCFIEscape(Values);
3430 return false;
3431}
3432
Rafael Espindola16d7d432012-01-23 21:51:52 +00003433/// ParseDirectiveCFISignalFrame
3434/// ::= .cfi_signal_frame
3435bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3436 SMLoc DirectiveLoc) {
3437 if (getLexer().isNot(AsmToken::EndOfStatement))
3438 return Error(getLexer().getLoc(),
3439 "unexpected token in '" + Directive + "' directive");
3440
3441 getStreamer().EmitCFISignalFrame();
3442
3443 return false;
3444}
3445
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003446/// ParseDirectiveCFIUndefined
3447/// ::= .cfi_undefined register
3448bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3449 SMLoc DirectiveLoc) {
3450 int64_t Register = 0;
3451
3452 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3453 return true;
3454
3455 getStreamer().EmitCFIUndefined(Register);
3456
3457 return false;
3458}
3459
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003460/// ParseDirectiveCFIRegister
3461/// ::= .cfi_register register, register
3462bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3463 SMLoc DirectiveLoc) {
3464 int64_t Register1 = 0;
3465
3466 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3467 return true;
3468
3469 if (getLexer().isNot(AsmToken::Comma))
3470 return TokError("unexpected token in directive");
3471 Lex();
3472
3473 int64_t Register2 = 0;
3474
3475 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3476 return true;
3477
3478 getStreamer().EmitCFIRegister(Register1, Register2);
3479
3480 return false;
3481}
3482
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003483/// ParseDirectiveMacrosOnOff
3484/// ::= .macros_on
3485/// ::= .macros_off
3486bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3487 SMLoc DirectiveLoc) {
3488 if (getLexer().isNot(AsmToken::EndOfStatement))
3489 return Error(getLexer().getLoc(),
3490 "unexpected token in '" + Directive + "' directive");
3491
3492 getParser().MacrosEnabled = Directive == ".macros_on";
3493
3494 return false;
3495}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003496
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003497/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003498/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003499bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3500 SMLoc DirectiveLoc) {
3501 StringRef Name;
3502 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003503 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003504
Rafael Espindola8a403d32012-08-08 14:51:03 +00003505 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003506 // Argument delimiter is initially unknown. It will be set by
3507 // ParseMacroArgument()
3508 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003509 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003510 for (;;) {
3511 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003512 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003513 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003514
3515 if (getLexer().is(AsmToken::Equal)) {
3516 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003517 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003518 return true;
3519 }
3520
Rafael Espindola65366442011-06-05 02:43:45 +00003521 Parameters.push_back(Parameter);
3522
Preston Gurd7b6f2032012-09-19 20:36:12 +00003523 if (getLexer().is(AsmToken::Comma))
3524 Lex();
3525 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003526 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003527 }
3528 }
3529
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003530 // Eat the end of statement.
3531 Lex();
3532
3533 AsmToken EndToken, StartToken = getTok();
3534
3535 // Lex the macro definition.
3536 for (;;) {
3537 // Check whether we have reached the end of the file.
3538 if (getLexer().is(AsmToken::Eof))
3539 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3540
3541 // Otherwise, check whether we have reach the .endmacro.
3542 if (getLexer().is(AsmToken::Identifier) &&
3543 (getTok().getIdentifier() == ".endm" ||
3544 getTok().getIdentifier() == ".endmacro")) {
3545 EndToken = getTok();
3546 Lex();
3547 if (getLexer().isNot(AsmToken::EndOfStatement))
3548 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3549 "' directive");
3550 break;
3551 }
3552
3553 // Otherwise, scan til the end of the statement.
3554 getParser().EatToEndOfStatement();
3555 }
3556
3557 if (getParser().MacroMap.lookup(Name)) {
3558 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3559 }
3560
3561 const char *BodyStart = StartToken.getLoc().getPointer();
3562 const char *BodyEnd = EndToken.getLoc().getPointer();
3563 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003564 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003565 return false;
3566}
3567
3568/// ParseDirectiveEndMacro
3569/// ::= .endm
3570/// ::= .endmacro
3571bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003572 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003573 if (getLexer().isNot(AsmToken::EndOfStatement))
3574 return TokError("unexpected token in '" + Directive + "' directive");
3575
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003576 // If we are inside a macro instantiation, terminate the current
3577 // instantiation.
3578 if (!getParser().ActiveMacros.empty()) {
3579 getParser().HandleMacroExit();
3580 return false;
3581 }
3582
3583 // Otherwise, this .endmacro is a stray entry in the file; well formed
3584 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003585 return TokError("unexpected '" + Directive + "' in file, "
3586 "no current macro definition");
3587}
3588
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003589/// ParseDirectivePurgeMacro
3590/// ::= .purgem
3591bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3592 SMLoc DirectiveLoc) {
3593 StringRef Name;
3594 if (getParser().ParseIdentifier(Name))
3595 return TokError("expected identifier in '.purgem' directive");
3596
3597 if (getLexer().isNot(AsmToken::EndOfStatement))
3598 return TokError("unexpected token in '.purgem' directive");
3599
3600 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3601 if (I == getParser().MacroMap.end())
3602 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3603
3604 // Undefine the macro.
3605 delete I->getValue();
3606 getParser().MacroMap.erase(I);
3607 return false;
3608}
3609
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003610bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003611 getParser().CheckForValidSection();
3612
3613 const MCExpr *Value;
3614
3615 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003616 return true;
3617
3618 if (getLexer().isNot(AsmToken::EndOfStatement))
3619 return TokError("unexpected token in directive");
3620
3621 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003622 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003623 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003624 getStreamer().EmitULEB128Value(Value);
3625
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003626 return false;
3627}
3628
Rafael Espindola761cb062012-06-03 23:57:14 +00003629Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003630 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003631
Rafael Espindola761cb062012-06-03 23:57:14 +00003632 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003633 for (;;) {
3634 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003635 if (getLexer().is(AsmToken::Eof)) {
3636 Error(DirectiveLoc, "no matching '.endr' in definition");
3637 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003638 }
3639
Rafael Espindola761cb062012-06-03 23:57:14 +00003640 if (Lexer.is(AsmToken::Identifier) &&
3641 (getTok().getIdentifier() == ".rept")) {
3642 ++NestLevel;
3643 }
3644
3645 // Otherwise, check whether we have reached the .endr.
3646 if (Lexer.is(AsmToken::Identifier) &&
3647 getTok().getIdentifier() == ".endr") {
3648 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003649 EndToken = getTok();
3650 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003651 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3652 TokError("unexpected token in '.endr' directive");
3653 return 0;
3654 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003655 break;
3656 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003657 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003658 }
3659
Rafael Espindola761cb062012-06-03 23:57:14 +00003660 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003661 EatToEndOfStatement();
3662 }
3663
3664 const char *BodyStart = StartToken.getLoc().getPointer();
3665 const char *BodyEnd = EndToken.getLoc().getPointer();
3666 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3667
Rafael Espindola761cb062012-06-03 23:57:14 +00003668 // We Are Anonymous.
3669 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003670 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003671 return new Macro(Name, Body, Parameters);
3672}
3673
3674void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3675 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003676 OS << ".endr\n";
3677
3678 MemoryBuffer *Instantiation =
3679 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3680
Rafael Espindola761cb062012-06-03 23:57:14 +00003681 // Create the macro instantiation object and add to the current macro
3682 // instantiation stack.
3683 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00003684 CurBuffer,
Rafael Espindola761cb062012-06-03 23:57:14 +00003685 getTok().getLoc(),
3686 Instantiation);
3687 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003688
Rafael Espindola761cb062012-06-03 23:57:14 +00003689 // Jump to the macro instantiation and prime the lexer.
3690 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3691 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3692 Lex();
3693}
3694
3695bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3696 int64_t Count;
3697 if (ParseAbsoluteExpression(Count))
3698 return TokError("unexpected token in '.rept' directive");
3699
3700 if (Count < 0)
3701 return TokError("Count is negative");
3702
3703 if (Lexer.isNot(AsmToken::EndOfStatement))
3704 return TokError("unexpected token in '.rept' directive");
3705
3706 // Eat the end of statement.
3707 Lex();
3708
3709 // Lex the rept definition.
3710 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3711 if (!M)
3712 return true;
3713
3714 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3715 // to hold the macro body with substitutions.
3716 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003717 MacroParameters Parameters;
3718 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003719 raw_svector_ostream OS(Buf);
3720 while (Count--) {
3721 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3722 return true;
3723 }
3724 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003725
3726 return false;
3727}
3728
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003729/// ParseDirectiveIrp
3730/// ::= .irp symbol,values
3731bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003732 MacroParameters Parameters;
3733 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003734
Preston Gurd6c9176a2012-09-19 20:29:04 +00003735 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003736 return TokError("expected identifier in '.irp' directive");
3737
3738 Parameters.push_back(Parameter);
3739
3740 if (Lexer.isNot(AsmToken::Comma))
3741 return TokError("expected comma in '.irp' directive");
3742
3743 Lex();
3744
Rafael Espindola8a403d32012-08-08 14:51:03 +00003745 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003746 if (ParseMacroArguments(0, A))
3747 return true;
3748
3749 // Eat the end of statement.
3750 Lex();
3751
3752 // Lex the irp definition.
3753 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3754 if (!M)
3755 return true;
3756
3757 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3758 // to hold the macro body with substitutions.
3759 SmallString<256> Buf;
3760 raw_svector_ostream OS(Buf);
3761
Rafael Espindola7996d042012-08-21 16:06:48 +00003762 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3763 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003764 Args.push_back(*i);
3765
3766 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3767 return true;
3768 }
3769
3770 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3771
3772 return false;
3773}
3774
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003775/// ParseDirectiveIrpc
3776/// ::= .irpc symbol,values
3777bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003778 MacroParameters Parameters;
3779 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003780
Preston Gurd6c9176a2012-09-19 20:29:04 +00003781 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003782 return TokError("expected identifier in '.irpc' directive");
3783
3784 Parameters.push_back(Parameter);
3785
3786 if (Lexer.isNot(AsmToken::Comma))
3787 return TokError("expected comma in '.irpc' directive");
3788
3789 Lex();
3790
Rafael Espindola8a403d32012-08-08 14:51:03 +00003791 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003792 if (ParseMacroArguments(0, A))
3793 return true;
3794
3795 if (A.size() != 1 || A.front().size() != 1)
3796 return TokError("unexpected token in '.irpc' directive");
3797
3798 // Eat the end of statement.
3799 Lex();
3800
3801 // Lex the irpc definition.
3802 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3803 if (!M)
3804 return true;
3805
3806 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3807 // to hold the macro body with substitutions.
3808 SmallString<256> Buf;
3809 raw_svector_ostream OS(Buf);
3810
3811 StringRef Values = A.front().front().getString();
3812 std::size_t I, End = Values.size();
3813 for (I = 0; I < End; ++I) {
3814 MacroArgument Arg;
3815 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3816
Rafael Espindola8a403d32012-08-08 14:51:03 +00003817 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003818 Args.push_back(Arg);
3819
3820 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3821 return true;
3822 }
3823
3824 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3825
3826 return false;
3827}
3828
Rafael Espindola761cb062012-06-03 23:57:14 +00003829bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3830 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003831 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003832
3833 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003834 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003835 assert(getLexer().is(AsmToken::EndOfStatement));
3836
Rafael Espindola761cb062012-06-03 23:57:14 +00003837 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003838 return false;
3839}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003840
Eli Friedman2128aae2012-10-22 23:58:19 +00003841bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) {
3842 const MCExpr *Value;
3843 SMLoc ExprLoc = getLexer().getLoc();
3844 if (ParseExpression(Value))
3845 return true;
3846 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3847 if (!MCE)
3848 return Error(ExprLoc, "unexpected expression in _emit");
3849 uint64_t IntValue = MCE->getValue();
3850 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
3851 return Error(ExprLoc, "literal value out of range for directive");
3852
3853 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5));
3854 return false;
3855}
3856
Chad Rosierb1f8c132012-10-18 15:49:34 +00003857bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3858 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003859 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003860 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003861 SmallVectorImpl<std::string> &Clobbers,
3862 const MCInstrInfo *MII,
3863 const MCInstPrinter *IP,
3864 MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003865 SmallVector<void *, 4> InputDecls;
3866 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003867 SmallVector<bool, 4> InputDeclsAddressOf;
3868 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003869 SmallVector<std::string, 4> InputConstraints;
3870 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003871 std::set<std::string> ClobberRegs;
3872
Chad Rosier4e472d22012-10-20 01:02:45 +00003873 SmallVector<struct AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003874
3875 // Prime the lexer.
3876 Lex();
3877
3878 // While we have input, parse each statement.
3879 unsigned InputIdx = 0;
3880 unsigned OutputIdx = 0;
3881 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00003882 ParseStatementInfo Info(&AsmStrRewrites);
3883 if (ParseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00003884 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003885
Chad Rosier57498012012-12-12 22:45:52 +00003886 if (Info.ParseError)
3887 return true;
3888
Eli Friedman2128aae2012-10-22 23:58:19 +00003889 if (Info.Opcode != ~0U) {
3890 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003891
3892 // Build the list of clobbers, outputs and inputs.
Eli Friedman2128aae2012-10-22 23:58:19 +00003893 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
3894 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003895
3896 // Immediate.
3897 if (Operand->isImm()) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00003898 if (Operand->needAsmRewrite())
3899 AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix,
3900 Operand->getStartLoc()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003901 continue;
3902 }
3903
3904 // Register operand.
Chad Rosierc1ec2072013-01-10 22:10:27 +00003905 if (Operand->isReg() && !Operand->needAddressOf()) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003906 unsigned NumDefs = Desc.getNumDefs();
3907 // Clobber.
3908 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3909 std::string Reg;
3910 raw_string_ostream OS(Reg);
3911 IP->printRegName(OS, Operand->getReg());
3912 ClobberRegs.insert(StringRef(OS.str()));
3913 }
3914 continue;
3915 }
3916
3917 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003918 unsigned Size;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003919 bool IsVarDecl;
Chad Rosier32989592012-10-18 20:27:15 +00003920 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +00003921 Size, IsVarDecl);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003922 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003923 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierc1ec2072013-01-10 22:10:27 +00003924 if (Operand->isMem() && Operand->needSizeDirective())
Chad Rosier4e472d22012-10-20 01:02:45 +00003925 AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective,
Chad Rosierefcb3d92012-10-26 18:04:20 +00003926 Operand->getStartLoc(),
3927 /*Len*/0,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003928 Operand->getMemSize()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003929 if (isOutput) {
3930 std::string Constraint = "=";
3931 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003932 OutputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003933 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003934 Constraint += Operand->getConstraint().str();
3935 OutputConstraints.push_back(Constraint);
Chad Rosier4e472d22012-10-20 01:02:45 +00003936 AsmStrRewrites.push_back(AsmRewrite(AOK_Output,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003937 Operand->getStartLoc(),
3938 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003939 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003940 InputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003941 InputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003942 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosier4e472d22012-10-20 01:02:45 +00003943 AsmStrRewrites.push_back(AsmRewrite(AOK_Input,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003944 Operand->getStartLoc(),
3945 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003946 }
3947 }
3948 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003949 }
3950 }
3951
3952 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003953 NumOutputs = OutputDecls.size();
3954 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003955
3956 // Set the unique clobbers.
3957 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3958 E = ClobberRegs.end(); I != E; ++I)
3959 Clobbers.push_back(*I);
3960
3961 // Merge the various outputs and inputs. Output are expected first.
3962 if (NumOutputs || NumInputs) {
3963 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003964 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003965 Constraints.resize(NumExprs);
Chad Rosier5a719fc2012-10-23 17:43:43 +00003966 // FIXME: Constraints are hard coded to 'm', but we need an 'r'
Chad Rosierc1ec2072013-01-10 22:10:27 +00003967 // constraint for addressof. This needs to be cleaned up!
Chad Rosierb1f8c132012-10-18 15:49:34 +00003968 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003969 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
3970 Constraints[i] = OutputDeclsAddressOf[i] ? "=r" : OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003971 }
3972 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003973 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
3974 Constraints[j] = InputDeclsAddressOf[i] ? "r" : InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003975 }
3976 }
3977
3978 // Build the IR assembly string.
3979 std::string AsmStringIR;
Chad Rosier4e472d22012-10-20 01:02:45 +00003980 AsmRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003981 raw_string_ostream OS(AsmStringIR);
3982 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
Chad Rosier4e472d22012-10-20 01:02:45 +00003983 for (SmallVectorImpl<struct AsmRewrite>::iterator
Chad Rosierb1f8c132012-10-18 15:49:34 +00003984 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3985 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003986
Chad Rosier4e472d22012-10-20 01:02:45 +00003987 AsmRewriteKind Kind = (*I).Kind;
Chad Rosier96d58e62012-10-19 20:57:14 +00003988
3989 // Emit everything up to the immediate/expression. If the previous rewrite
3990 // was a size directive, then this has already been done.
3991 if (PrevKind != AOK_SizeDirective)
3992 OS << StringRef(Start, Loc - Start);
3993 PrevKind = Kind;
3994
Chad Rosier5a719fc2012-10-23 17:43:43 +00003995 // Skip the original expression.
3996 if (Kind == AOK_Skip) {
3997 Start = Loc + (*I).Len;
3998 continue;
3999 }
4000
Chad Rosierb1f8c132012-10-18 15:49:34 +00004001 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004002 switch (Kind) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004003 default: break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004004 case AOK_Imm:
Chad Rosierefcb3d92012-10-26 18:04:20 +00004005 OS << Twine("$$");
4006 OS << (*I).Val;
4007 break;
4008 case AOK_ImmPrefix:
4009 OS << Twine("$$");
Chad Rosierb1f8c132012-10-18 15:49:34 +00004010 break;
4011 case AOK_Input:
4012 OS << '$';
4013 OS << InputIdx++;
4014 break;
4015 case AOK_Output:
4016 OS << '$';
4017 OS << OutputIdx++;
4018 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004019 case AOK_SizeDirective:
Chad Rosier6a020a72012-10-25 20:41:34 +00004020 switch((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004021 default: break;
4022 case 8: OS << "byte ptr "; break;
4023 case 16: OS << "word ptr "; break;
4024 case 32: OS << "dword ptr "; break;
4025 case 64: OS << "qword ptr "; break;
4026 case 80: OS << "xword ptr "; break;
4027 case 128: OS << "xmmword ptr "; break;
4028 case 256: OS << "ymmword ptr "; break;
4029 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004030 break;
4031 case AOK_Emit:
4032 OS << ".byte";
4033 break;
Chad Rosier6a020a72012-10-25 20:41:34 +00004034 case AOK_DotOperator:
4035 OS << (*I).Val;
4036 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004037 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004038
Chad Rosierb1f8c132012-10-18 15:49:34 +00004039 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00004040 if (Kind != AOK_SizeDirective)
4041 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004042 }
4043
4044 // Emit the remainder of the asm string.
4045 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4046 if (Start != AsmEnd)
4047 OS << StringRef(Start, AsmEnd - Start);
4048
4049 AsmString = OS.str();
4050 return false;
4051}
4052
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004053/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004054MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004055 MCContext &C, MCStreamer &Out,
4056 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004057 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004058}