blob: 752cc19132cbb25d1e19e988cf5b0a0e85d4579a [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.
Eli Bendersky9bac6b22013-01-14 19:00:26 +000054typedef std::vector<MCAsmMacroArgument> MacroArguments;
55typedef std::pair<StringRef, MCAsmMacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000056typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000057
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000058struct Macro {
59 StringRef Name;
60 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000061 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000062
63public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000064 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000065 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000066};
67
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000068/// \brief Helper class for storing information about an active macro
69/// instantiation.
70struct MacroInstantiation {
71 /// The macro being instantiated.
72 const Macro *TheMacro;
73
74 /// The macro instantiation with substitutions.
75 MemoryBuffer *Instantiation;
76
77 /// The location of the instantiation.
78 SMLoc InstantiationLoc;
79
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000080 /// The buffer where parsing should resume upon instantiation completion.
81 int ExitBuffer;
82
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000083 /// The location where parsing should resume upon instantiation completion.
84 SMLoc ExitLoc;
85
86public:
Daniel Dunbar4259a1a2012-12-01 01:38:48 +000087 MacroInstantiation(const Macro *M, SMLoc IL, int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000088 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000089};
90
Chad Rosier6a020a72012-10-25 20:41:34 +000091//struct AsmRewrite;
Eli Friedman2128aae2012-10-22 23:58:19 +000092struct ParseStatementInfo {
93 /// ParsedOperands - The parsed operands from the last parsed statement.
94 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
95
96 /// Opcode - The opcode from the last parsed instruction.
97 unsigned Opcode;
98
Chad Rosier57498012012-12-12 22:45:52 +000099 /// Error - Was there an error parsing the inline assembly?
100 bool ParseError;
101
Eli Friedman2128aae2012-10-22 23:58:19 +0000102 SmallVectorImpl<AsmRewrite> *AsmRewrites;
103
Chad Rosier57498012012-12-12 22:45:52 +0000104 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000105 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
Chad Rosier57498012012-12-12 22:45:52 +0000106 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {}
Eli Friedman2128aae2012-10-22 23:58:19 +0000107
108 ~ParseStatementInfo() {
109 // Free any parsed operands.
110 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
111 delete ParsedOperands[i];
112 ParsedOperands.clear();
113 }
114};
115
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000116/// \brief The concrete assembly parser instance.
117class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000118 friend class GenericAsmParser;
119
Craig Topper85aadc02012-09-15 16:23:52 +0000120 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
121 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000122private:
123 AsmLexer Lexer;
124 MCContext &Ctx;
125 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000126 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000127 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +0000128 SourceMgr::DiagHandlerTy SavedDiagHandler;
129 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000130 MCAsmParserExtension *GenericParser;
131 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000132
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000133 /// This is the current buffer index we're lexing from as managed by the
134 /// SourceMgr object.
135 int CurBuffer;
136
137 AsmCond TheCondState;
138 std::vector<AsmCond> TheCondStack;
139
140 /// DirectiveMap - This is a table handlers for directives. Each handler is
141 /// invoked after the directive identifier is read and is responsible for
142 /// parsing and validating the rest of the directive. The handler is passed
143 /// in the directive name and the location of the directive keyword.
144 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000145
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000146 /// MacroMap - Map of currently defined macros.
147 StringMap<Macro*> MacroMap;
148
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000149 /// ActiveMacros - Stack of active macro instantiations.
150 std::vector<MacroInstantiation*> ActiveMacros;
151
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000152 /// Boolean tracking whether macro substitution is enabled.
Eli Bendersky733c3362013-01-14 18:08:41 +0000153 unsigned MacrosEnabledFlag : 1;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000154
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000155 /// Flag tracking whether any errors have been encountered.
156 unsigned HadError : 1;
157
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000158 /// The values from the last parsed cpp hash file line comment if any.
159 StringRef CppHashFilename;
160 int64_t CppHashLineNumber;
161 SMLoc CppHashLoc;
Kevin Enderby32c1a822012-11-05 21:55:41 +0000162 int CppHashBuf;
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000163
Devang Patel0db58bf2012-01-31 18:14:05 +0000164 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
165 unsigned AssemblerDialect;
166
Preston Gurd7b6f2032012-09-19 20:36:12 +0000167 /// IsDarwin - is Darwin compatibility enabled?
168 bool IsDarwin;
169
Chad Rosier8f138d12012-10-15 17:19:13 +0000170 /// ParsingInlineAsm - Are we parsing ms-style inline assembly?
Chad Rosier84125ca2012-10-13 00:26:04 +0000171 bool ParsingInlineAsm;
172
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000173public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000174 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000175 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000176 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177
178 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
179
Craig Topper345d16d2012-08-29 05:48:09 +0000180 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
181 StringRef Directive,
182 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183 DirectiveMap[Directive] = std::make_pair(Object, Handler);
184 }
185
186public:
187 /// @name MCAsmParser Interface
188 /// {
189
190 virtual SourceMgr &getSourceManager() { return SrcMgr; }
191 virtual MCAsmLexer &getLexer() { return Lexer; }
192 virtual MCContext &getContext() { return Ctx; }
193 virtual MCStreamer &getStreamer() { return Out; }
Eric Christopher2318ba12012-12-18 00:30:54 +0000194 virtual unsigned getAssemblerDialect() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000195 if (AssemblerDialect == ~0U)
Eric Christopher2318ba12012-12-18 00:30:54 +0000196 return MAI.getAssemblerDialect();
Devang Patel0db58bf2012-01-31 18:14:05 +0000197 else
198 return AssemblerDialect;
199 }
200 virtual void setAssemblerDialect(unsigned i) {
201 AssemblerDialect = i;
202 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000203
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000204 virtual bool Warning(SMLoc L, const Twine &Msg,
205 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
206 virtual bool Error(SMLoc L, const Twine &Msg,
207 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000208
Craig Topper345d16d2012-08-29 05:48:09 +0000209 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210
Chad Rosier84125ca2012-10-13 00:26:04 +0000211 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; }
Chad Rosierc5ac87d2012-10-16 20:16:20 +0000212 bool isParsingInlineAsm() { return ParsingInlineAsm; }
Chad Rosierb1f8c132012-10-18 15:49:34 +0000213
214 bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
215 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000216 SmallVectorImpl<std::pair<void *,bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000217 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +0000218 SmallVectorImpl<std::string> &Clobbers,
219 const MCInstrInfo *MII,
220 const MCInstPrinter *IP,
221 MCAsmParserSemaCallback &SI);
Chad Rosier84125ca2012-10-13 00:26:04 +0000222
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000223 bool ParseExpression(const MCExpr *&Res);
224 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
225 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
226 virtual bool ParseAbsoluteExpression(int64_t &Res);
227
Eli Benderskybf706b32013-01-12 00:05:00 +0000228 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
229 /// and set \p Res to the identifier contents.
230 virtual bool ParseIdentifier(StringRef &Res);
Eli Benderskyb2f0b592013-01-12 00:23:24 +0000231 virtual void EatToEndOfStatement();
Eli Benderskybf706b32013-01-12 00:05:00 +0000232
Eli Bendersky733c3362013-01-14 18:08:41 +0000233 virtual bool MacrosEnabled() {return MacrosEnabledFlag;}
234 virtual void SetMacrosEnabled(bool flag) {MacrosEnabledFlag = flag;}
235
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000236 /// }
237
238private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000239 void CheckForValidSection();
240
Eli Friedman2128aae2012-10-22 23:58:19 +0000241 bool ParseStatement(ParseStatementInfo &Info);
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000242 void EatToEndOfLine();
243 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000244
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000245 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000246 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000247 const MacroParameters &Parameters,
248 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000249 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000250 void HandleMacroExit();
251
252 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000253 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000254 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
255 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000256 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000257 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000258
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000259 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
260 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000261 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
262 /// This returns true on failure.
263 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000264
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000265 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000266 /// current token is not set; clients should ensure Lex() is called
267 /// subsequently.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000268 ///
269 /// \param InBuffer If not -1, should be the known buffer id that contains the
270 /// location.
271 void JumpToLoc(SMLoc Loc, int InBuffer=-1);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000272
Eli Bendersky9bac6b22013-01-14 19:00:26 +0000273 bool ParseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +0000274 AsmToken::TokenKind &ArgumentDelimiter);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000275 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000276
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000277 /// \brief Parse up to the end of statement and a return the contents from the
278 /// current token until the end of the statement; the current token on exit
279 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000280 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000281
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000282 /// \brief Parse until the end of a statement or a comma is encountered,
283 /// return the contents from the current token up to the end or comma.
284 StringRef ParseStringToComma();
285
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000286 bool ParseAssignment(StringRef Name, bool allow_redef,
287 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000288
289 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
290 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
291 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000292 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000293
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000294 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000295
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000296 enum DirectiveKind {
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000297 DK_NO_DIRECTIVE, // Placeholder
298 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT,
299 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE,
300 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW,
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000301 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR,
Eli Bendersky7eef9c12013-01-10 23:40:56 +0000302 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK,
303 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL,
304 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN,
305 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE,
306 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT,
307 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC,
308 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF,
309 DK_ELSEIF, DK_ELSE, DK_ENDIF
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000310 };
311
312 StringMap<DirectiveKind> DirectiveKindMapping;
313
314 // ".ascii", ".asciz", ".string"
Rafael Espindola787c3372010-10-28 20:02:27 +0000315 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000316 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000317 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000318 bool ParseDirectiveFill(); // ".fill"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000319 bool ParseDirectiveZero(); // ".zero"
Eric Christopher2318ba12012-12-18 00:30:54 +0000320 // ".set", ".equ", ".equiv"
321 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000322 bool ParseDirectiveOrg(); // ".org"
323 // ".align{,32}", ".p2align{,w,l}"
324 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
325
Eli Bendersky4766ef42012-12-20 19:05:53 +0000326 // ".bundle_align_mode"
327 bool ParseDirectiveBundleAlignMode();
328 // ".bundle_lock"
329 bool ParseDirectiveBundleLock();
330 // ".bundle_unlock"
331 bool ParseDirectiveBundleUnlock();
332
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000333 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
334 /// accepts a single symbol (which should be a label or an external).
335 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000336
337 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
338
339 bool ParseDirectiveAbort(); // ".abort"
340 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000341 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000342
343 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000344 // ".ifb" or ".ifnb", depending on ExpectBlank.
345 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000346 // ".ifc" or ".ifnc", depending on ExpectEqual.
347 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000348 // ".ifdef" or ".ifndef", depending on expect_defined
349 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000350 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
351 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
352 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
353
354 /// ParseEscapedString - Parse the current token as a string which may include
355 /// escaped characters and return the string contents.
356 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000357
358 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
359 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000360
Rafael Espindola761cb062012-06-03 23:57:14 +0000361 // Macro-like directives
362 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
363 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
364 raw_svector_ostream &OS);
365 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000366 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000367 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000368 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Chad Rosierb1f8c132012-10-18 15:49:34 +0000369
Eli Friedman2128aae2012-10-22 23:58:19 +0000370 // "_emit"
371 bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info);
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000372
373 void initializeDirectiveKindMapping();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000374};
375
Eli Bendersky63e6f482013-01-10 23:32:57 +0000376/// \brief Generic implementation of directive handling, etc. which is shared
377/// (or the default, at least) for all assembler parsers.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000378class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000379 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
380 void AddDirectiveHandler(StringRef Directive) {
381 getParser().AddDirectiveHandler(this, Directive,
382 HandleDirective<GenericAsmParser, Handler>);
383 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000384public:
385 GenericAsmParser() {}
386
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000387 AsmParser &getParser() {
388 return (AsmParser&) this->MCAsmParserExtension::getParser();
389 }
390
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000391 virtual void Initialize(MCAsmParser &Parser) {
392 // Call the base implementation.
393 this->MCAsmParserExtension::Initialize(Parser);
394
395 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000396 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
397 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
398 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000399 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000400
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000401 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".space");
402 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveSpace>(".skip");
403
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000404 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000405 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
406 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000407 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
408 ".cfi_startproc");
409 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
410 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000411 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
412 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000413 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
414 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000415 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
416 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000417 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
418 ".cfi_def_cfa_register");
419 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
420 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000421 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
422 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000423 AddDirectiveHandler<
424 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
425 AddDirectiveHandler<
426 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000427 AddDirectiveHandler<
428 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
429 AddDirectiveHandler<
430 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000431 AddDirectiveHandler<
432 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000433 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000434 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
435 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000436 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000437 AddDirectiveHandler<
438 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000439 AddDirectiveHandler<
440 &GenericAsmParser::ParseDirectiveCFIUndefined>(".cfi_undefined");
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000441 AddDirectiveHandler<
442 &GenericAsmParser::ParseDirectiveCFIRegister>(".cfi_register");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000443
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000444 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000445 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
446 ".macros_on");
447 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
448 ".macros_off");
449 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
450 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
451 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000452 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000453
454 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
455 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000456 }
457
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000458 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
459
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000460 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
461 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
462 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000463 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Eli Bendersky9b1bb052013-01-11 22:55:28 +0000464 bool ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000465 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000466 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
467 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000468 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000469 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000470 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000471 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
472 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000473 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000474 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000475 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
476 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000477 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000478 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000479 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000480 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000481 bool ParseDirectiveCFIUndefined(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000482 bool ParseDirectiveCFIRegister(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000483
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000484 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000485 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
486 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000487 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000488
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000489 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000490};
491
492}
493
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000494namespace llvm {
495
496extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000497extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000498extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000499
500}
501
Chris Lattneraaec2052010-01-19 19:46:13 +0000502enum { DEFAULT_ADDRSPACE = 0 };
503
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000504AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000505 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000506 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000507 GenericParser(new GenericAsmParser), PlatformParser(0),
Eli Bendersky733c3362013-01-14 18:08:41 +0000508 CurBuffer(0), MacrosEnabledFlag(true), CppHashLineNumber(0),
Eli Friedman2128aae2012-10-22 23:58:19 +0000509 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000510 // Save the old handler.
511 SavedDiagHandler = SrcMgr.getDiagHandler();
512 SavedDiagContext = SrcMgr.getDiagContext();
513 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000514 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000515 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000516
517 // Initialize the generic parser.
518 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000519
520 // Initialize the platform / file format parser.
521 //
522 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
523 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000524 if (_MAI.hasMicrosoftFastStdCallMangling()) {
525 PlatformParser = createCOFFAsmParser();
526 PlatformParser->Initialize(*this);
527 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000528 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000529 PlatformParser->Initialize(*this);
Preston Gurd7b6f2032012-09-19 20:36:12 +0000530 IsDarwin = true;
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000531 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000532 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000533 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000534 }
Eli Bendersky5d0f0612013-01-10 22:44:57 +0000535
536 initializeDirectiveKindMapping();
Chris Lattnerebb89b42009-09-27 21:16:52 +0000537}
538
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000539AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000540 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
541
542 // Destroy any macros.
543 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
544 ie = MacroMap.end(); it != ie; ++it)
545 delete it->getValue();
546
Daniel Dunbare4749702010-07-12 18:12:02 +0000547 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000548 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000549}
550
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000551void AsmParser::PrintMacroInstantiations() {
552 // Print the active macro instantiation stack.
553 for (std::vector<MacroInstantiation*>::const_reverse_iterator
554 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000555 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
556 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000557}
558
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000559bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000560 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000561 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000562 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000563 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000564 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000565}
566
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000567bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000568 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000569 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000570 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000571 return true;
572}
573
Sean Callananfd0b0282010-01-21 00:19:58 +0000574bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000575 std::string IncludedFile;
576 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000577 if (NewBuf == -1)
578 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000579
Sean Callananfd0b0282010-01-21 00:19:58 +0000580 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000581
Sean Callananfd0b0282010-01-21 00:19:58 +0000582 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000583
Sean Callananfd0b0282010-01-21 00:19:58 +0000584 return false;
585}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000586
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000587/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000588/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000589/// returns true on failure.
590bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
591 std::string IncludedFile;
592 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
593 if (NewBuf == -1)
594 return true;
595
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000596 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000597 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
598 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000599 return false;
600}
601
Daniel Dunbar4259a1a2012-12-01 01:38:48 +0000602void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) {
603 if (InBuffer != -1) {
604 CurBuffer = InBuffer;
605 } else {
606 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
607 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000608 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
609}
610
Sean Callananfd0b0282010-01-21 00:19:58 +0000611const AsmToken &AsmParser::Lex() {
612 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000613
Sean Callananfd0b0282010-01-21 00:19:58 +0000614 if (tok->is(AsmToken::Eof)) {
615 // If this is the end of an included file, pop the parent file off the
616 // include stack.
617 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
618 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000619 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000620 tok = &Lexer.Lex();
621 }
622 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000623
Sean Callananfd0b0282010-01-21 00:19:58 +0000624 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000625 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000626
Sean Callananfd0b0282010-01-21 00:19:58 +0000627 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000628}
629
Chris Lattner79180e22010-04-05 23:15:42 +0000630bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000631 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000632 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000633 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000634
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000635 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000636 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000637
638 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000639 AsmCond StartingCondState = TheCondState;
640
Kevin Enderby613b7572011-11-01 22:27:22 +0000641 // If we are generating dwarf for assembly source files save the initial text
642 // section and generate a .file directive.
643 if (getContext().getGenDwarfForAssembly()) {
644 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000645 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
646 getStreamer().EmitLabel(SectionStartSym);
647 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000648 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
Eric Christopher6c583142012-12-18 00:31:01 +0000649 StringRef(),
650 getContext().getMainFileName());
Kevin Enderby613b7572011-11-01 22:27:22 +0000651 }
652
Chris Lattnerb717fb02009-07-02 21:53:43 +0000653 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000654 while (Lexer.isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +0000655 ParseStatementInfo Info;
656 if (!ParseStatement(Info)) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000657
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000658 // We had an error, validate that one was emitted and recover by skipping to
659 // the next line.
660 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000661 EatToEndOfStatement();
662 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000663
664 if (TheCondState.TheCond != StartingCondState.TheCond ||
665 TheCondState.Ignore != StartingCondState.Ignore)
666 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000667
668 // Check to see there are no empty DwarfFile slots.
669 const std::vector<MCDwarfFile *> &MCDwarfFiles =
670 getContext().getMCDwarfFiles();
671 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000672 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000673 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000674 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000675
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000676 // Check to see that all assembler local symbols were actually defined.
677 // Targets that don't do subsections via symbols may not want this, though,
678 // so conservatively exclude them. Only do this if we're finalizing, though,
679 // as otherwise we won't necessarilly have seen everything yet.
680 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
681 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
682 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
683 e = Symbols.end();
684 i != e; ++i) {
685 MCSymbol *Sym = i->getValue();
686 // Variable symbols may not be marked as defined, so check those
687 // explicitly. If we know it's a variable, we have a definition for
688 // the purposes of this check.
689 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
690 // FIXME: We would really like to refer back to where the symbol was
691 // first referenced for a source location. We need to add something
692 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000693 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
694 "assembler local symbol '" + Sym->getName() +
695 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000696 }
697 }
698
699
Chris Lattner79180e22010-04-05 23:15:42 +0000700 // Finalize the output stream if there are no errors and if the client wants
701 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000702 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000703 Out.Finish();
704
Chris Lattnerb717fb02009-07-02 21:53:43 +0000705 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000706}
707
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000708void AsmParser::CheckForValidSection() {
Chad Rosier84125ca2012-10-13 00:26:04 +0000709 if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000710 TokError("expected section directive before assembly directive");
711 Out.SwitchSection(Ctx.getMachOSection(
712 "__TEXT", "__text",
713 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
714 0, SectionKind::getText()));
715 }
716}
717
Chris Lattner2cf5f142009-06-22 01:29:09 +0000718/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
719void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000720 while (Lexer.isNot(AsmToken::EndOfStatement) &&
721 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000722 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000723
Chris Lattner2cf5f142009-06-22 01:29:09 +0000724 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000725 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000726 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000727}
728
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000729StringRef AsmParser::ParseStringToEndOfStatement() {
730 const char *Start = getTok().getLoc().getPointer();
731
732 while (Lexer.isNot(AsmToken::EndOfStatement) &&
733 Lexer.isNot(AsmToken::Eof))
734 Lex();
735
736 const char *End = getTok().getLoc().getPointer();
737 return StringRef(Start, End - Start);
738}
Chris Lattnerc4193832009-06-22 05:51:26 +0000739
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000740StringRef AsmParser::ParseStringToComma() {
741 const char *Start = getTok().getLoc().getPointer();
742
743 while (Lexer.isNot(AsmToken::EndOfStatement) &&
744 Lexer.isNot(AsmToken::Comma) &&
745 Lexer.isNot(AsmToken::Eof))
746 Lex();
747
748 const char *End = getTok().getLoc().getPointer();
749 return StringRef(Start, End - Start);
750}
751
Chris Lattner74ec1a32009-06-22 06:32:03 +0000752/// ParseParenExpr - Parse a paren expression and return it.
753/// NOTE: This assumes the leading '(' has already been consumed.
754///
755/// parenexpr ::= expr)
756///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000757bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000758 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000759 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000760 return TokError("expected ')' in parentheses expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000761 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000762 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000763 return false;
764}
Chris Lattnerc4193832009-06-22 05:51:26 +0000765
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000766/// ParseBracketExpr - Parse a bracket expression and return it.
767/// NOTE: This assumes the leading '[' has already been consumed.
768///
769/// bracketexpr ::= expr]
770///
771bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
772 if (ParseExpression(Res)) return true;
773 if (Lexer.isNot(AsmToken::RBrac))
774 return TokError("expected ']' in brackets expression");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000775 EndLoc = Lexer.getTok().getEndLoc();
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000776 Lex();
777 return false;
778}
779
Chris Lattner74ec1a32009-06-22 06:32:03 +0000780/// ParsePrimaryExpr - Parse a primary expression and return it.
781/// primaryexpr ::= (parenexpr
782/// primaryexpr ::= symbol
783/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000784/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000785/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000786bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000787 switch (Lexer.getKind()) {
788 default:
789 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000790 // If we have an error assume that we've already handled it.
791 case AsmToken::Error:
792 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000793 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000794 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000795 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000796 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000797 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000798 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000799 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000800 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000801 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000802 StringRef Identifier;
803 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000804 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000805
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000806 EndLoc = SMLoc::getFromPointer(Identifier.end());
807
Daniel Dunbarfffff912009-10-16 01:34:54 +0000808 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000809 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000810 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000811
812 // Lookup the symbol variant if used.
813 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000814 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000815 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000816 if (Variant == MCSymbolRefExpr::VK_Invalid) {
817 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000818 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000819 }
820 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000821
Daniel Dunbarfffff912009-10-16 01:34:54 +0000822 // If this is an absolute variable reference, substitute it now to preserve
823 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000824 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000825 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000826 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000827
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000828 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000829 return false;
830 }
831
832 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000833 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000834 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000835 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000836 case AsmToken::Integer: {
837 SMLoc Loc = getTok().getLoc();
838 int64_t IntVal = getTok().getIntVal();
839 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000840 EndLoc = Lexer.getTok().getEndLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000841 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000842 // Look for 'b' or 'f' following an Integer as a directional label
843 if (Lexer.getKind() == AsmToken::Identifier) {
844 StringRef IDVal = getTok().getString();
845 if (IDVal == "f" || IDVal == "b"){
846 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
847 IDVal == "f" ? 1 : 0);
848 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
849 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000850 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000851 return Error(Loc, "invalid reference to undefined symbol");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000852 EndLoc = Lexer.getTok().getEndLoc();
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000853 Lex(); // Eat identifier.
854 }
855 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000856 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000857 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000858 case AsmToken::Real: {
859 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000860 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000861 Res = MCConstantExpr::Create(IntVal, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000862 EndLoc = Lexer.getTok().getEndLoc();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000863 Lex(); // Eat token.
864 return false;
865 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000866 case AsmToken::Dot: {
867 // This is a '.' reference, which references the current PC. Emit a
868 // temporary label to the streamer and refer to it.
869 MCSymbol *Sym = Ctx.CreateTempSymbol();
870 Out.EmitLabel(Sym);
871 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000872 EndLoc = Lexer.getTok().getEndLoc();
Chris Lattnerd3050352010-04-14 04:40:28 +0000873 Lex(); // Eat identifier.
874 return false;
875 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000876 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000877 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000878 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000879 case AsmToken::LBrac:
880 if (!PlatformParser->HasBracketExpressions())
881 return TokError("brackets expression not supported on this target");
882 Lex(); // Eat the '['.
883 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000885 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000886 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000887 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000888 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000889 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000890 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000891 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000892 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000893 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000894 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000895 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000896 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000897 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000898 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000899 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000900 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000901 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000902 }
903}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000904
Chris Lattnerb4307b32010-01-15 19:28:38 +0000905bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000906 SMLoc EndLoc;
907 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000908}
909
Daniel Dunbarcceba832010-09-17 02:47:07 +0000910const MCExpr *
911AsmParser::ApplyModifierToExpr(const MCExpr *E,
912 MCSymbolRefExpr::VariantKind Variant) {
913 // Recurse over the given expression, rebuilding it to apply the given variant
914 // if there is exactly one symbol.
915 switch (E->getKind()) {
916 case MCExpr::Target:
917 case MCExpr::Constant:
918 return 0;
919
920 case MCExpr::SymbolRef: {
921 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
922
923 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
924 TokError("invalid variant on expression '" +
925 getTok().getIdentifier() + "' (already modified)");
926 return E;
927 }
928
929 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
930 }
931
932 case MCExpr::Unary: {
933 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
934 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
935 if (!Sub)
936 return 0;
937 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
938 }
939
940 case MCExpr::Binary: {
941 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
942 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
943 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
944
945 if (!LHS && !RHS)
946 return 0;
947
948 if (!LHS) LHS = BE->getLHS();
949 if (!RHS) RHS = BE->getRHS();
950
951 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
952 }
953 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000954
Craig Topper85814382012-02-07 05:05:23 +0000955 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000956}
957
Chris Lattner74ec1a32009-06-22 06:32:03 +0000958/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000959///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000960/// expr ::= expr &&,|| expr -> lowest.
961/// expr ::= expr |,^,&,! expr
962/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
963/// expr ::= expr <<,>> expr
964/// expr ::= expr +,- expr
965/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000966/// expr ::= primaryexpr
967///
Chris Lattner54482b42010-01-15 19:39:23 +0000968bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000969 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000970 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000971 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
972 return true;
973
Daniel Dunbarcceba832010-09-17 02:47:07 +0000974 // As a special case, we support 'a op b @ modifier' by rewriting the
975 // expression to include the modifier. This is inefficient, but in general we
976 // expect users to use 'a@modifier op b'.
977 if (Lexer.getKind() == AsmToken::At) {
978 Lex();
979
980 if (Lexer.isNot(AsmToken::Identifier))
981 return TokError("unexpected symbol modifier following '@'");
982
983 MCSymbolRefExpr::VariantKind Variant =
984 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
985 if (Variant == MCSymbolRefExpr::VK_Invalid)
986 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
987
988 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
989 if (!ModifiedRes) {
990 return TokError("invalid modifier '" + getTok().getIdentifier() +
991 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000992 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000993
Daniel Dunbarcceba832010-09-17 02:47:07 +0000994 Res = ModifiedRes;
995 Lex();
996 }
997
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000998 // Try to constant fold it up front, if possible.
999 int64_t Value;
1000 if (Res->EvaluateAsAbsolute(Value))
1001 Res = MCConstantExpr::Create(Value, getContext());
1002
1003 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +00001004}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001005
Chris Lattnerb4307b32010-01-15 19:28:38 +00001006bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +00001007 Res = 0;
1008 return ParseParenExpr(Res, EndLoc) ||
1009 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +00001010}
1011
Daniel Dunbar475839e2009-06-29 20:37:27 +00001012bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001013 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001014
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001015 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +00001016 if (ParseExpression(Expr))
1017 return true;
1018
Daniel Dunbare00b0112009-10-16 01:57:52 +00001019 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001020 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +00001021
1022 return false;
1023}
1024
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001025static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001026 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001027 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001028 default:
1029 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +00001030
Jim Grosbachfbe16812011-08-20 16:24:13 +00001031 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +00001032 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001033 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001034 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001035 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001036 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001037 return 1;
1038
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001039
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001040 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +00001041 //
1042 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +00001043 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001044 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001045 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001046 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001047 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001048 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001050 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001051 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001052
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001053 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +00001054 case AsmToken::EqualEqual:
1055 Kind = MCBinaryExpr::EQ;
1056 return 3;
1057 case AsmToken::ExclaimEqual:
1058 case AsmToken::LessGreater:
1059 Kind = MCBinaryExpr::NE;
1060 return 3;
1061 case AsmToken::Less:
1062 Kind = MCBinaryExpr::LT;
1063 return 3;
1064 case AsmToken::LessEqual:
1065 Kind = MCBinaryExpr::LTE;
1066 return 3;
1067 case AsmToken::Greater:
1068 Kind = MCBinaryExpr::GT;
1069 return 3;
1070 case AsmToken::GreaterEqual:
1071 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001072 return 3;
1073
Jim Grosbachfbe16812011-08-20 16:24:13 +00001074 // Intermediate Precedence: <<, >>
1075 case AsmToken::LessLess:
1076 Kind = MCBinaryExpr::Shl;
1077 return 4;
1078 case AsmToken::GreaterGreater:
1079 Kind = MCBinaryExpr::Shr;
1080 return 4;
1081
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001082 // High Intermediate Precedence: +, -
1083 case AsmToken::Plus:
1084 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001085 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001086 case AsmToken::Minus:
1087 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001088 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +00001089
Jim Grosbachfbe16812011-08-20 16:24:13 +00001090 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +00001091 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001092 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001093 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001094 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001095 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001096 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001097 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001098 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +00001099 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001100 }
1101}
1102
1103
1104/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
1105/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001106bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1107 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001108 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001109 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001110 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001111
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001112 // If the next token is lower precedence than we are allowed to eat, return
1113 // successfully with what we ate already.
1114 if (TokPrec < Precedence)
1115 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001116
Sean Callanan79ed1a82010-01-19 20:22:31 +00001117 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001118
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001119 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001120 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001121 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001122
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001123 // If BinOp binds less tightly with RHS than the operator after RHS, let
1124 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001125 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001126 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001127 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001128 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001129 }
1130
Daniel Dunbar475839e2009-06-29 20:37:27 +00001131 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001132 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001133 }
1134}
1135
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001136/// ParseStatement:
1137/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001138/// ::= Label* Directive ...Operands... EndOfStatement
1139/// ::= Label* Identifier OperandList* EndOfStatement
Eli Friedman2128aae2012-10-22 23:58:19 +00001140bool AsmParser::ParseStatement(ParseStatementInfo &Info) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001141 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001142 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001143 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001144 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001145 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001146
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001147 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001148 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001149 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001150 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001151 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001152 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001153 if (Lexer.is(AsmToken::Hash))
1154 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001155
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001156 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001157 if (Lexer.is(AsmToken::Integer)) {
1158 LocalLabelVal = getTok().getIntVal();
1159 if (LocalLabelVal < 0) {
1160 if (!TheCondState.Ignore)
1161 return TokError("unexpected token at start of statement");
1162 IDVal = "";
1163 }
1164 else {
1165 IDVal = getTok().getString();
1166 Lex(); // Consume the integer token to be used as an identifier token.
1167 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001168 if (!TheCondState.Ignore)
1169 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001170 }
1171 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001172
1173 } else if (Lexer.is(AsmToken::Dot)) {
1174 // Treat '.' as a valid identifier in this context.
1175 Lex();
1176 IDVal = ".";
1177
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001178 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001179 if (!TheCondState.Ignore)
1180 return TokError("unexpected token at start of statement");
1181 IDVal = "";
1182 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001183
Chris Lattner7834fac2010-04-17 18:14:27 +00001184 // Handle conditional assembly here before checking for skipping. We
1185 // have to do this so that .endif isn't skipped in a ".if 0" block for
1186 // example.
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001187 StringMap<DirectiveKind>::const_iterator DirKindIt =
1188 DirectiveKindMapping.find(IDVal);
1189 DirectiveKind DirKind =
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001190 (DirKindIt == DirectiveKindMapping.end()) ? DK_NO_DIRECTIVE :
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001191 DirKindIt->getValue();
1192 switch (DirKind) {
1193 default:
1194 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001195 case DK_IF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001196 return ParseDirectiveIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001197 case DK_IFB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001198 return ParseDirectiveIfb(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001199 case DK_IFNB:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001200 return ParseDirectiveIfb(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001201 case DK_IFC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001202 return ParseDirectiveIfc(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001203 case DK_IFNC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001204 return ParseDirectiveIfc(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001205 case DK_IFDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001206 return ParseDirectiveIfdef(IDLoc, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001207 case DK_IFNDEF:
1208 case DK_IFNOTDEF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001209 return ParseDirectiveIfdef(IDLoc, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001210 case DK_ELSEIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001211 return ParseDirectiveElseIf(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001212 case DK_ELSE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001213 return ParseDirectiveElse(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001214 case DK_ENDIF:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001215 return ParseDirectiveEndIf(IDLoc);
1216 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001217
Chris Lattner7834fac2010-04-17 18:14:27 +00001218 // If we are in a ".if 0" block, ignore this statement.
Chad Rosier17feeec2012-10-20 00:47:08 +00001219 if (TheCondState.Ignore) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001220 EatToEndOfStatement();
1221 return false;
1222 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001223
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001224 // FIXME: Recurse on local labels?
1225
1226 // See what kind of statement we have.
1227 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001228 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001229 CheckForValidSection();
1230
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001231 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001232 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001233
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001234 // Diagnose attempt to use '.' as a label.
1235 if (IDVal == ".")
1236 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1237
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001238 // Diagnose attempt to use a variable as a label.
1239 //
1240 // FIXME: Diagnostics. Note the location of the definition as a label.
1241 // FIXME: This doesn't diagnose assignment to a symbol which has been
1242 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001243 MCSymbol *Sym;
1244 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001245 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001246 else
1247 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001248 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001249 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001250
Daniel Dunbar959fd882009-08-26 22:13:22 +00001251 // Emit the label.
Chad Rosierdeb1bab2013-01-07 20:34:12 +00001252 if (!ParsingInlineAsm)
1253 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001254
Kevin Enderby94c2e852011-12-09 18:09:40 +00001255 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001256 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001257 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001258 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1259 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001260
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001261 // Consume any end of statement token, if present, to avoid spurious
1262 // AddBlankLine calls().
1263 if (Lexer.is(AsmToken::EndOfStatement)) {
1264 Lex();
1265 if (Lexer.is(AsmToken::Eof))
1266 return false;
1267 }
1268
Eli Friedman2128aae2012-10-22 23:58:19 +00001269 return false;
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001270 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001271
Daniel Dunbar3f872332009-07-28 16:08:33 +00001272 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001273 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001274 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001275
Nico Weber4c4c7322011-01-28 03:04:41 +00001276 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001277
1278 default: // Normal instruction or directive.
1279 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001280 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001281
1282 // If macros are enabled, check to see if this is a macro instantiation.
Eli Bendersky733c3362013-01-14 18:08:41 +00001283 if (MacrosEnabled())
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001284 if (const Macro *M = MacroMap.lookup(IDVal))
1285 return HandleMacroEntry(IDVal, IDLoc, M);
1286
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001287 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001288 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001289
1290 // Target hook for parsing target specific directives.
1291 if (!getTargetParser().ParseDirective(ID))
1292 return false;
1293
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001294 switch (DirKind) {
1295 default:
1296 break;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001297 case DK_SET:
1298 case DK_EQU:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001299 return ParseDirectiveSet(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001300 case DK_EQUIV:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001301 return ParseDirectiveSet(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001302 case DK_ASCII:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001303 return ParseDirectiveAscii(IDVal, false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001304 case DK_ASCIZ:
1305 case DK_STRING:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001306 return ParseDirectiveAscii(IDVal, true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001307 case DK_BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001308 return ParseDirectiveValue(1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001309 case DK_SHORT:
1310 case DK_VALUE:
1311 case DK_2BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001312 return ParseDirectiveValue(2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001313 case DK_LONG:
1314 case DK_INT:
1315 case DK_4BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001316 return ParseDirectiveValue(4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001317 case DK_QUAD:
1318 case DK_8BYTE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001319 return ParseDirectiveValue(8);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001320 case DK_SINGLE:
1321 case DK_FLOAT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001322 return ParseDirectiveRealValue(APFloat::IEEEsingle);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001323 case DK_DOUBLE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001324 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001325 case DK_ALIGN: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001326 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1327 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1328 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001329 case DK_ALIGN32: {
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001330 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1331 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1332 }
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001333 case DK_BALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001334 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001335 case DK_BALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001336 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001337 case DK_BALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001338 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001339 case DK_P2ALIGN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001340 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001341 case DK_P2ALIGNW:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001342 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001343 case DK_P2ALIGNL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001344 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001345 case DK_ORG:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001346 return ParseDirectiveOrg();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001347 case DK_FILL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001348 return ParseDirectiveFill();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001349 case DK_ZERO:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001350 return ParseDirectiveZero();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001351 case DK_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001352 EatToEndOfStatement(); // .extern is the default, ignore it.
1353 return false;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001354 case DK_GLOBL:
1355 case DK_GLOBAL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001356 return ParseDirectiveSymbolAttribute(MCSA_Global);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001357 case DK_INDIRECT_SYMBOL:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001358 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001359 case DK_LAZY_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001360 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001361 case DK_NO_DEAD_STRIP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001362 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001363 case DK_SYMBOL_RESOLVER:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001364 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001365 case DK_PRIVATE_EXTERN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001366 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001367 case DK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001368 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001369 case DK_WEAK_DEFINITION:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001370 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001371 case DK_WEAK_REFERENCE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001372 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001373 case DK_WEAK_DEF_CAN_BE_HIDDEN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001374 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001375 case DK_COMM:
1376 case DK_COMMON:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001377 return ParseDirectiveComm(/*IsLocal=*/false);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001378 case DK_LCOMM:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001379 return ParseDirectiveComm(/*IsLocal=*/true);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001380 case DK_ABORT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001381 return ParseDirectiveAbort();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001382 case DK_INCLUDE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001383 return ParseDirectiveInclude();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001384 case DK_INCBIN:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001385 return ParseDirectiveIncbin();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001386 case DK_CODE16:
1387 case DK_CODE16GCC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001388 return TokError(Twine(IDVal) + " not supported yet");
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001389 case DK_REPT:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001390 return ParseDirectiveRept(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001391 case DK_IRP:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001392 return ParseDirectiveIrp(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001393 case DK_IRPC:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001394 return ParseDirectiveIrpc(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001395 case DK_ENDR:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001396 return ParseDirectiveEndr(IDLoc);
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001397 case DK_BUNDLE_ALIGN_MODE:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001398 return ParseDirectiveBundleAlignMode();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001399 case DK_BUNDLE_LOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001400 return ParseDirectiveBundleLock();
Eli Bendersky7eef9c12013-01-10 23:40:56 +00001401 case DK_BUNDLE_UNLOCK:
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001402 return ParseDirectiveBundleUnlock();
Eli Friedman5d68ec22010-07-19 04:17:25 +00001403 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001404
Eli Bendersky5d0f0612013-01-10 22:44:57 +00001405 // Look up the handler in the extension handler table.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001406 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1407 DirectiveMap.lookup(IDVal);
1408 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001409 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001410
Jim Grosbach686c0182012-05-01 18:38:27 +00001411 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001412 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001413
Eli Friedman2128aae2012-10-22 23:58:19 +00001414 // _emit
1415 if (ParsingInlineAsm && IDVal == "_emit")
1416 return ParseDirectiveEmit(IDLoc, Info);
1417
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001418 CheckForValidSection();
1419
Chris Lattnera7f13542010-05-19 23:34:33 +00001420 // Canonicalize the opcode to lower case.
Chad Rosier8f138d12012-10-15 17:19:13 +00001421 SmallString<128> OpcodeStr;
Chris Lattnera7f13542010-05-19 23:34:33 +00001422 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
Chad Rosier8f138d12012-10-15 17:19:13 +00001423 OpcodeStr.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001424
Chad Rosier6a020a72012-10-25 20:41:34 +00001425 ParseInstructionInfo IInfo(Info.AsmRewrites);
1426 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr.str(),
1427 IDLoc,Info.ParsedOperands);
Chad Rosier57498012012-12-12 22:45:52 +00001428 Info.ParseError = HadError;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001429
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001430 // Dump the parsed representation, if requested.
1431 if (getShowParsedOperands()) {
1432 SmallString<256> Str;
1433 raw_svector_ostream OS(Str);
1434 OS << "parsed instruction: [";
Eli Friedman2128aae2012-10-22 23:58:19 +00001435 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001436 if (i != 0)
1437 OS << ", ";
Eli Friedman2128aae2012-10-22 23:58:19 +00001438 Info.ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001439 }
1440 OS << "]";
1441
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001442 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001443 }
1444
Kevin Enderby613b7572011-11-01 22:27:22 +00001445 // If we are generating dwarf for assembly source files and the current
1446 // section is the initial text section then generate a .loc directive for
1447 // the instruction.
1448 if (!HadError && getContext().getGenDwarfForAssembly() &&
Eric Christopher2318ba12012-12-18 00:30:54 +00001449 getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) {
Kevin Enderby938482f2012-11-01 17:31:35 +00001450
1451 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer);
1452
1453 // If we previously parsed a cpp hash file line comment then make sure the
1454 // current Dwarf File is for the CppHashFilename if not then emit the
1455 // Dwarf File table for it and adjust the line number for the .loc.
1456 const std::vector<MCDwarfFile *> &MCDwarfFiles =
1457 getContext().getMCDwarfFiles();
1458 if (CppHashFilename.size() != 0) {
1459 if(MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() !=
1460 CppHashFilename)
Eric Christopher2318ba12012-12-18 00:30:54 +00001461 getStreamer().EmitDwarfFileDirective(
1462 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename);
Kevin Enderby938482f2012-11-01 17:31:35 +00001463
Kevin Enderby32c1a822012-11-05 21:55:41 +00001464 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf);
Kevin Enderby938482f2012-11-01 17:31:35 +00001465 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo);
1466 }
1467
Kevin Enderby613b7572011-11-01 22:27:22 +00001468 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
Kevin Enderby938482f2012-11-01 17:31:35 +00001469 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001470 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001471 StringRef());
1472 }
1473
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001474 // If parsing succeeded, match the instruction.
Chad Rosier84125ca2012-10-13 00:26:04 +00001475 if (!HadError) {
Chad Rosier84125ca2012-10-13 00:26:04 +00001476 unsigned ErrorInfo;
Eli Friedman2128aae2012-10-22 23:58:19 +00001477 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode,
1478 Info.ParsedOperands,
1479 Out, ErrorInfo,
Chad Rosier84125ca2012-10-13 00:26:04 +00001480 ParsingInlineAsm);
1481 }
Chris Lattner98986712010-01-14 22:21:20 +00001482
Chris Lattnercbf8a982010-09-11 16:18:25 +00001483 // Don't skip the rest of the line, the instruction parser is responsible for
1484 // that.
1485 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001486}
Chris Lattner9a023f72009-06-24 04:43:34 +00001487
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001488/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1489/// since they may not be able to be tokenized to get to the end of line token.
1490void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001491 if (!Lexer.is(AsmToken::EndOfStatement))
1492 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001493 // Eat EOL.
1494 Lex();
1495}
1496
1497/// ParseCppHashLineFilenameComment as this:
1498/// ::= # number "filename"
1499/// or just as a full line comment if it doesn't have a number and a string.
1500bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1501 Lex(); // Eat the hash token.
1502
1503 if (getLexer().isNot(AsmToken::Integer)) {
1504 // Consume the line since in cases it is not a well-formed line directive,
1505 // as if were simply a full line comment.
1506 EatToEndOfLine();
1507 return false;
1508 }
1509
1510 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001511 Lex();
1512
1513 if (getLexer().isNot(AsmToken::String)) {
1514 EatToEndOfLine();
1515 return false;
1516 }
1517
1518 StringRef Filename = getTok().getString();
1519 // Get rid of the enclosing quotes.
1520 Filename = Filename.substr(1, Filename.size()-2);
1521
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001522 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1523 CppHashLoc = L;
1524 CppHashFilename = Filename;
1525 CppHashLineNumber = LineNumber;
Kevin Enderby32c1a822012-11-05 21:55:41 +00001526 CppHashBuf = CurBuffer;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001527
1528 // Ignore any trailing characters, they're just comment.
1529 EatToEndOfLine();
1530 return false;
1531}
1532
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001533/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001534/// for the Filename and LineNo if any in the diagnostic.
1535void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1536 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1537 raw_ostream &OS = errs();
1538
1539 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1540 const SMLoc &DiagLoc = Diag.getLoc();
1541 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1542 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1543
1544 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1545 // before printing the message.
1546 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001547 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001548 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1549 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1550 }
1551
Eric Christopher2318ba12012-12-18 00:30:54 +00001552 // If we have not parsed a cpp hash line filename comment or the source
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001553 // manager changed or buffer changed (like in a nested include) then just
1554 // print the normal diagnostic using its Filename and LineNo.
1555 if (!Parser->CppHashLineNumber ||
1556 &DiagSrcMgr != &Parser->SrcMgr ||
1557 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001558 if (Parser->SavedDiagHandler)
1559 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1560 else
1561 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001562 return;
1563 }
1564
Eric Christopher2318ba12012-12-18 00:30:54 +00001565 // Use the CppHashFilename and calculate a line number based on the
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001566 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1567 // the diagnostic.
1568 const std::string Filename = Parser->CppHashFilename;
1569
1570 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1571 int CppHashLocLineNo =
1572 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1573 int LineNo = Parser->CppHashLineNumber - 1 +
1574 (DiagLocLineNo - CppHashLocLineNo);
1575
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001576 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1577 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001578 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001579 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001580
Benjamin Kramer04a04262011-10-16 10:48:29 +00001581 if (Parser->SavedDiagHandler)
1582 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1583 else
1584 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001585}
1586
Rafael Espindola799aacf2012-08-21 18:29:30 +00001587// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1588// difference being that that function accepts '@' as part of identifiers and
1589// we can't do that. AsmLexer.cpp should probably be changed to handle
1590// '@' as a special case when needed.
1591static bool isIdentifierChar(char c) {
1592 return isalnum(c) || c == '_' || c == '$' || c == '.';
1593}
1594
Rafael Espindola761cb062012-06-03 23:57:14 +00001595bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001596 const MacroParameters &Parameters,
1597 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001598 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001599 unsigned NParameters = Parameters.size();
1600 if (NParameters != 0 && NParameters != A.size())
1601 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001602
Preston Gurd7b6f2032012-09-19 20:36:12 +00001603 // A macro without parameters is handled differently on Darwin:
1604 // gas accepts no arguments and does no substitutions
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001605 while (!Body.empty()) {
1606 // Scan for the next substitution.
1607 std::size_t End = Body.size(), Pos = 0;
1608 for (; Pos != End; ++Pos) {
1609 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001610 if (!NParameters) {
1611 // This macro has no parameters, look for $0, $1, etc.
1612 if (Body[Pos] != '$' || Pos + 1 == End)
1613 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001614
Rafael Espindola65366442011-06-05 02:43:45 +00001615 char Next = Body[Pos + 1];
1616 if (Next == '$' || Next == 'n' || isdigit(Next))
1617 break;
1618 } else {
1619 // This macro has parameters, look for \foo, \bar, etc.
1620 if (Body[Pos] == '\\' && Pos + 1 != End)
1621 break;
1622 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001623 }
1624
1625 // Add the prefix.
1626 OS << Body.slice(0, Pos);
1627
1628 // Check if we reached the end.
1629 if (Pos == End)
1630 break;
1631
Rafael Espindola65366442011-06-05 02:43:45 +00001632 if (!NParameters) {
1633 switch (Body[Pos+1]) {
1634 // $$ => $
1635 case '$':
1636 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001637 break;
1638
Rafael Espindola65366442011-06-05 02:43:45 +00001639 // $n => number of arguments
1640 case 'n':
1641 OS << A.size();
1642 break;
1643
1644 // $[0-9] => argument
1645 default: {
1646 // Missing arguments are ignored.
1647 unsigned Index = Body[Pos+1] - '0';
1648 if (Index >= A.size())
1649 break;
1650
1651 // Otherwise substitute with the token values, with spaces eliminated.
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001652 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001653 ie = A[Index].end(); it != ie; ++it)
1654 OS << it->getString();
1655 break;
1656 }
1657 }
1658 Pos += 2;
1659 } else {
1660 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001661 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001662 ++I;
1663
1664 const char *Begin = Body.data() + Pos +1;
1665 StringRef Argument(Begin, I - (Pos +1));
1666 unsigned Index = 0;
1667 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001668 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001669 break;
1670
Preston Gurd7b6f2032012-09-19 20:36:12 +00001671 if (Index == NParameters) {
1672 if (Body[Pos+1] == '(' && Body[Pos+2] == ')')
1673 Pos += 3;
1674 else {
1675 OS << '\\' << Argument;
1676 Pos = I;
1677 }
1678 } else {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001679 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(),
Preston Gurd7b6f2032012-09-19 20:36:12 +00001680 ie = A[Index].end(); it != ie; ++it)
1681 if (it->getKind() == AsmToken::String)
1682 OS << it->getStringContents();
1683 else
1684 OS << it->getString();
Rafael Espindola65366442011-06-05 02:43:45 +00001685
Preston Gurd7b6f2032012-09-19 20:36:12 +00001686 Pos += 1 + Argument.size();
1687 }
Rafael Espindola65366442011-06-05 02:43:45 +00001688 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001689 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001690 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001691 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001692
Rafael Espindola65366442011-06-05 02:43:45 +00001693 return false;
1694}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001695
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001696MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL,
1697 int EB, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +00001698 MemoryBuffer *I)
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001699 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB),
1700 ExitLoc(EL)
Rafael Espindola65366442011-06-05 02:43:45 +00001701{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001702}
1703
Preston Gurd7b6f2032012-09-19 20:36:12 +00001704static bool IsOperator(AsmToken::TokenKind kind)
1705{
1706 switch (kind)
1707 {
1708 default:
1709 return false;
1710 case AsmToken::Plus:
1711 case AsmToken::Minus:
1712 case AsmToken::Tilde:
1713 case AsmToken::Slash:
1714 case AsmToken::Star:
1715 case AsmToken::Dot:
1716 case AsmToken::Equal:
1717 case AsmToken::EqualEqual:
1718 case AsmToken::Pipe:
1719 case AsmToken::PipePipe:
1720 case AsmToken::Caret:
1721 case AsmToken::Amp:
1722 case AsmToken::AmpAmp:
1723 case AsmToken::Exclaim:
1724 case AsmToken::ExclaimEqual:
1725 case AsmToken::Percent:
1726 case AsmToken::Less:
1727 case AsmToken::LessEqual:
1728 case AsmToken::LessLess:
1729 case AsmToken::LessGreater:
1730 case AsmToken::Greater:
1731 case AsmToken::GreaterEqual:
1732 case AsmToken::GreaterGreater:
1733 return true;
1734 }
1735}
1736
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001737bool AsmParser::ParseMacroArgument(MCAsmMacroArgument &MA,
Preston Gurd7b6f2032012-09-19 20:36:12 +00001738 AsmToken::TokenKind &ArgumentDelimiter) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001739 unsigned ParenLevel = 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001740 unsigned AddTokens = 0;
1741
1742 // gas accepts arguments separated by whitespace, except on Darwin
1743 if (!IsDarwin)
1744 Lexer.setSkipSpace(false);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001745
1746 for (;;) {
Preston Gurd7b6f2032012-09-19 20:36:12 +00001747 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) {
1748 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001749 return TokError("unexpected token in macro instantiation");
Preston Gurd7b6f2032012-09-19 20:36:12 +00001750 }
1751
1752 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1753 // Spaces and commas cannot be mixed to delimit parameters
1754 if (ArgumentDelimiter == AsmToken::Eof)
1755 ArgumentDelimiter = AsmToken::Comma;
1756 else if (ArgumentDelimiter != AsmToken::Comma) {
1757 Lexer.setSkipSpace(true);
1758 return TokError("expected ' ' for macro argument separator");
1759 }
1760 break;
1761 }
1762
1763 if (Lexer.is(AsmToken::Space)) {
1764 Lex(); // Eat spaces
1765
1766 // Spaces can delimit parameters, but could also be part an expression.
1767 // If the token after a space is an operator, add the token and the next
1768 // one into this argument
1769 if (ArgumentDelimiter == AsmToken::Space ||
1770 ArgumentDelimiter == AsmToken::Eof) {
1771 if (IsOperator(Lexer.getKind())) {
1772 // Check to see whether the token is used as an operator,
1773 // or part of an identifier
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001774 const char *NextChar = getTok().getEndLoc().getPointer();
Preston Gurd7b6f2032012-09-19 20:36:12 +00001775 if (*NextChar == ' ')
1776 AddTokens = 2;
1777 }
1778
1779 if (!AddTokens && ParenLevel == 0) {
1780 if (ArgumentDelimiter == AsmToken::Eof &&
1781 !IsOperator(Lexer.getKind()))
1782 ArgumentDelimiter = AsmToken::Space;
1783 break;
1784 }
1785 }
1786 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001787
1788 // HandleMacroEntry relies on not advancing the lexer here
1789 // to be able to fill in the remaining default parameter values
1790 if (Lexer.is(AsmToken::EndOfStatement))
1791 break;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001792
1793 // Adjust the current parentheses level.
1794 if (Lexer.is(AsmToken::LParen))
1795 ++ParenLevel;
1796 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1797 --ParenLevel;
1798
1799 // Append the token to the current argument list.
1800 MA.push_back(getTok());
Preston Gurd7b6f2032012-09-19 20:36:12 +00001801 if (AddTokens)
1802 AddTokens--;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001803 Lex();
1804 }
Preston Gurd7b6f2032012-09-19 20:36:12 +00001805
1806 Lexer.setSkipSpace(true);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001807 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001808 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001809 return false;
1810}
1811
1812// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001813bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001814 const unsigned NParameters = M ? M->Parameters.size() : 0;
Preston Gurd7b6f2032012-09-19 20:36:12 +00001815 // Argument delimiter is initially unknown. It will be set by
1816 // ParseMacroArgument()
1817 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001818
1819 // Parse two kinds of macro invocations:
1820 // - macros defined without any parameters accept an arbitrary number of them
1821 // - macros defined with parameters accept at most that many of them
1822 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1823 ++Parameter) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00001824 MCAsmMacroArgument MA;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001825
Preston Gurd7b6f2032012-09-19 20:36:12 +00001826 if (ParseMacroArgument(MA, ArgumentDelimiter))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001827 return true;
1828
Preston Gurd6c9176a2012-09-19 20:29:04 +00001829 if (!MA.empty() || !NParameters)
1830 A.push_back(MA);
1831 else if (NParameters) {
1832 if (!M->Parameters[Parameter].second.empty())
1833 A.push_back(M->Parameters[Parameter].second);
1834 }
Jim Grosbach97146442012-07-30 22:44:17 +00001835
Preston Gurd6c9176a2012-09-19 20:29:04 +00001836 // At the end of the statement, fill in remaining arguments that have
1837 // default values. If there aren't any, then the next argument is
1838 // required but missing
1839 if (Lexer.is(AsmToken::EndOfStatement)) {
1840 if (NParameters && Parameter < NParameters - 1) {
1841 if (M->Parameters[Parameter + 1].second.empty())
1842 return TokError("macro argument '" +
1843 Twine(M->Parameters[Parameter + 1].first) +
1844 "' is missing");
1845 else
1846 continue;
1847 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001848 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001849 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001850
1851 if (Lexer.is(AsmToken::Comma))
1852 Lex();
1853 }
1854 return TokError("Too many arguments");
1855}
1856
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001857bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1858 const Macro *M) {
1859 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1860 // this, although we should protect against infinite loops.
1861 if (ActiveMacros.size() == 20)
1862 return TokError("macros cannot be nested more than 20 levels deep");
1863
Rafael Espindola8a403d32012-08-08 14:51:03 +00001864 MacroArguments A;
1865 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001866 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001867
Jim Grosbach97146442012-07-30 22:44:17 +00001868 // Remove any trailing empty arguments. Do this after-the-fact as we have
1869 // to keep empty arguments in the middle of the list or positionality
1870 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001871 while (!A.empty() && A.back().empty())
1872 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001873
Rafael Espindola65366442011-06-05 02:43:45 +00001874 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1875 // to hold the macro body with substitutions.
1876 SmallString<256> Buf;
1877 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001878 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001879
Rafael Espindola8a403d32012-08-08 14:51:03 +00001880 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001881 return true;
1882
Rafael Espindola761cb062012-06-03 23:57:14 +00001883 // We include the .endmacro in the buffer as our queue to exit the macro
1884 // instantiation.
1885 OS << ".endmacro\n";
1886
Rafael Espindola65366442011-06-05 02:43:45 +00001887 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001888 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001889
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001890 // Create the macro instantiation object and add to the current macro
1891 // instantiation stack.
1892 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001893 CurBuffer,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001894 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001895 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001896 ActiveMacros.push_back(MI);
1897
1898 // Jump to the macro instantiation and prime the lexer.
1899 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1900 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1901 Lex();
1902
1903 return false;
1904}
1905
1906void AsmParser::HandleMacroExit() {
1907 // Jump to the EndOfStatement we should return to, and consume it.
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00001908 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001909 Lex();
1910
1911 // Pop the instantiation entry.
1912 delete ActiveMacros.back();
1913 ActiveMacros.pop_back();
1914}
1915
Rafael Espindolae71cc862012-01-28 05:57:00 +00001916static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001917 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001918 case MCExpr::Binary: {
1919 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1920 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001921 break;
1922 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001923 case MCExpr::Target:
1924 case MCExpr::Constant:
1925 return false;
1926 case MCExpr::SymbolRef: {
1927 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001928 if (S.isVariable())
1929 return IsUsedIn(Sym, S.getVariableValue());
1930 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001931 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001932 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001933 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001934 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001935
1936 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001937}
1938
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001939bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1940 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001941 // FIXME: Use better location, we should use proper tokens.
1942 SMLoc EqualLoc = Lexer.getLoc();
1943
Daniel Dunbar821e3332009-08-31 08:09:28 +00001944 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001945 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001946 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001947
Rafael Espindolae71cc862012-01-28 05:57:00 +00001948 // Note: we don't count b as used in "a = b". This is to allow
1949 // a = b
1950 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001951
Daniel Dunbar3f872332009-07-28 16:08:33 +00001952 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001953 return TokError("unexpected token in assignment");
1954
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001955 // Error on assignment to '.'.
1956 if (Name == ".") {
1957 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1958 "(use '.space' or '.org').)"));
1959 }
1960
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001961 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001962 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001963
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001964 // Validate that the LHS is allowed to be a variable (either it has not been
1965 // used as a symbol, or it is an absolute symbol).
1966 MCSymbol *Sym = getContext().LookupSymbol(Name);
1967 if (Sym) {
1968 // Diagnose assignment to a label.
1969 //
1970 // FIXME: Diagnostics. Note the location of the definition as a label.
1971 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001972 if (IsUsedIn(Sym, Value))
1973 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1974 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001975 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001976 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1977 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001978 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001979 return Error(EqualLoc, "redefinition of '" + Name + "'");
1980 else if (!Sym->isVariable())
1981 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001982 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001983 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1984 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001985
1986 // Don't count these checks as uses.
1987 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001988 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001989 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001990
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001991 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001992
1993 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001994 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001995 if (NoDeadStrip)
1996 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1997
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001998
1999 return false;
2000}
2001
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002002/// ParseIdentifier:
2003/// ::= identifier
2004/// ::= string
2005bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00002006 // The assembler has relaxed rules for accepting identifiers, in particular we
2007 // allow things like '.globl $foo', which would normally be separate
2008 // tokens. At this level, we have already lexed so we cannot (currently)
2009 // handle this as a context dependent token, instead we detect adjacent tokens
2010 // and return the combined identifier.
2011 if (Lexer.is(AsmToken::Dollar)) {
2012 SMLoc DollarLoc = getLexer().getLoc();
2013
2014 // Consume the dollar sign, and check for a following identifier.
2015 Lex();
2016 if (Lexer.isNot(AsmToken::Identifier))
2017 return true;
2018
2019 // We have a '$' followed by an identifier, make sure they are adjacent.
2020 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2021 return true;
2022
2023 // Construct the joined identifier and consume the token.
2024 Res = StringRef(DollarLoc.getPointer(),
2025 getTok().getIdentifier().size() + 1);
2026 Lex();
2027 return false;
2028 }
2029
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002030 if (Lexer.isNot(AsmToken::Identifier) &&
2031 Lexer.isNot(AsmToken::String))
2032 return true;
2033
Sean Callanan18b83232010-01-19 21:44:56 +00002034 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002035
Sean Callanan79ed1a82010-01-19 20:22:31 +00002036 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002037
2038 return false;
2039}
2040
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002041/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00002042/// ::= .equ identifier ',' expression
2043/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002044/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00002045bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002046 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002047
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002048 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00002049 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002050
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002051 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00002052 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002053 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002054
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00002055 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00002056}
2057
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002058bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002059 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002060
2061 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00002062 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002063 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
2064 if (Str[i] != '\\') {
2065 Data += Str[i];
2066 continue;
2067 }
2068
2069 // Recognize escaped characters. Note that this escape semantics currently
2070 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
2071 ++i;
2072 if (i == e)
2073 return TokError("unexpected backslash at end of string");
2074
2075 // Recognize octal sequences.
2076 if ((unsigned) (Str[i] - '0') <= 7) {
2077 // Consume up to three octal characters.
2078 unsigned Value = Str[i] - '0';
2079
2080 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2081 ++i;
2082 Value = Value * 8 + (Str[i] - '0');
2083
2084 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
2085 ++i;
2086 Value = Value * 8 + (Str[i] - '0');
2087 }
2088 }
2089
2090 if (Value > 255)
2091 return TokError("invalid octal escape sequence (out of range)");
2092
2093 Data += (unsigned char) Value;
2094 continue;
2095 }
2096
2097 // Otherwise recognize individual escapes.
2098 switch (Str[i]) {
2099 default:
2100 // Just reject invalid escape sequences for now.
2101 return TokError("invalid escape sequence (unrecognized character)");
2102
2103 case 'b': Data += '\b'; break;
2104 case 'f': Data += '\f'; break;
2105 case 'n': Data += '\n'; break;
2106 case 'r': Data += '\r'; break;
2107 case 't': Data += '\t'; break;
2108 case '"': Data += '"'; break;
2109 case '\\': Data += '\\'; break;
2110 }
2111 }
2112
2113 return false;
2114}
2115
Daniel Dunbara0d14262009-06-24 23:30:00 +00002116/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00002117/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
2118bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002119 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002120 CheckForValidSection();
2121
Daniel Dunbara0d14262009-06-24 23:30:00 +00002122 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002123 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00002124 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002125
Daniel Dunbar1ab75942009-08-14 18:19:52 +00002126 std::string Data;
2127 if (ParseEscapedString(Data))
2128 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002129
2130 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002131 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002132 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
2133
Sean Callanan79ed1a82010-01-19 20:22:31 +00002134 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002135
2136 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002137 break;
2138
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002139 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00002140 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002141 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002142 }
2143 }
2144
Sean Callanan79ed1a82010-01-19 20:22:31 +00002145 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002146 return false;
2147}
2148
2149/// ParseDirectiveValue
2150/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
2151bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002152 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002153 CheckForValidSection();
2154
Daniel Dunbara0d14262009-06-24 23:30:00 +00002155 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00002156 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00002157 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002158 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002159 return true;
2160
Daniel Dunbar414c0c42010-05-23 18:36:38 +00002161 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00002162 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2163 assert(Size <= 8 && "Invalid size");
2164 uint64_t IntValue = MCE->getValue();
2165 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
2166 return Error(ExprLoc, "literal value out of range for directive");
2167 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
2168 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002169 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002170
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002171 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002172 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002173
Daniel Dunbara0d14262009-06-24 23:30:00 +00002174 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002175 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002176 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002177 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002178 }
2179 }
2180
Sean Callanan79ed1a82010-01-19 20:22:31 +00002181 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002182 return false;
2183}
2184
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002185/// ParseDirectiveRealValue
2186/// ::= (.single | .double) [ expression (, expression)* ]
2187bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
2188 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2189 CheckForValidSection();
2190
2191 for (;;) {
2192 // We don't truly support arithmetic on floating point expressions, so we
2193 // have to manually parse unary prefixes.
2194 bool IsNeg = false;
2195 if (getLexer().is(AsmToken::Minus)) {
2196 Lex();
2197 IsNeg = true;
2198 } else if (getLexer().is(AsmToken::Plus))
2199 Lex();
2200
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002201 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00002202 getLexer().isNot(AsmToken::Real) &&
2203 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002204 return TokError("unexpected token in directive");
2205
2206 // Convert to an APFloat.
2207 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00002208 StringRef IDVal = getTok().getString();
2209 if (getLexer().is(AsmToken::Identifier)) {
2210 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
2211 Value = APFloat::getInf(Semantics);
2212 else if (!IDVal.compare_lower("nan"))
2213 Value = APFloat::getNaN(Semantics, false, ~0);
2214 else
2215 return TokError("invalid floating point literal");
2216 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002217 APFloat::opInvalidOp)
2218 return TokError("invalid floating point literal");
2219 if (IsNeg)
2220 Value.changeSign();
2221
2222 // Consume the numeric token.
2223 Lex();
2224
2225 // Emit the value as an integer.
2226 APInt AsInt = Value.bitcastToAPInt();
2227 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2228 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2229
2230 if (getLexer().is(AsmToken::EndOfStatement))
2231 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002232
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002233 if (getLexer().isNot(AsmToken::Comma))
2234 return TokError("unexpected token in directive");
2235 Lex();
2236 }
2237 }
2238
2239 Lex();
2240 return false;
2241}
2242
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002243/// ParseDirectiveZero
2244/// ::= .zero expression
2245bool AsmParser::ParseDirectiveZero() {
2246 CheckForValidSection();
2247
2248 int64_t NumBytes;
2249 if (ParseAbsoluteExpression(NumBytes))
2250 return true;
2251
Rafael Espindolae452b172010-10-05 19:42:57 +00002252 int64_t Val = 0;
2253 if (getLexer().is(AsmToken::Comma)) {
2254 Lex();
2255 if (ParseAbsoluteExpression(Val))
2256 return true;
2257 }
2258
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002259 if (getLexer().isNot(AsmToken::EndOfStatement))
2260 return TokError("unexpected token in '.zero' directive");
2261
2262 Lex();
2263
Rafael Espindolae452b172010-10-05 19:42:57 +00002264 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002265
2266 return false;
2267}
2268
Daniel Dunbara0d14262009-06-24 23:30:00 +00002269/// ParseDirectiveFill
2270/// ::= .fill expression , expression , expression
2271bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002272 CheckForValidSection();
2273
Daniel Dunbara0d14262009-06-24 23:30:00 +00002274 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002275 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002276 return true;
2277
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002278 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002279 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002280 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002281
Daniel Dunbara0d14262009-06-24 23:30:00 +00002282 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002283 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002284 return true;
2285
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002286 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002287 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002288 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002289
Daniel Dunbara0d14262009-06-24 23:30:00 +00002290 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002291 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002292 return true;
2293
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002294 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002295 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002296
Sean Callanan79ed1a82010-01-19 20:22:31 +00002297 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002298
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002299 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2300 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002301
2302 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002303 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002304
2305 return false;
2306}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002307
2308/// ParseDirectiveOrg
2309/// ::= .org expression [ , expression ]
2310bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002311 CheckForValidSection();
2312
Daniel Dunbar821e3332009-08-31 08:09:28 +00002313 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002314 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002315 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002316 return true;
2317
2318 // Parse optional fill expression.
2319 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002320 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2321 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002322 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002323 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002324
Daniel Dunbar475839e2009-06-29 20:37:27 +00002325 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002326 return true;
2327
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002328 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002329 return TokError("unexpected token in '.org' directive");
2330 }
2331
Sean Callanan79ed1a82010-01-19 20:22:31 +00002332 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002333
Jim Grosbachebd4c052012-01-27 00:37:08 +00002334 // Only limited forms of relocatable expressions are accepted here, it
2335 // has to be relative to the current section. The streamer will return
2336 // 'true' if the expression wasn't evaluatable.
2337 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2338 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002339
2340 return false;
2341}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002342
2343/// ParseDirectiveAlign
2344/// ::= {.align, ...} expression [ , expression [ , expression ]]
2345bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002346 CheckForValidSection();
2347
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002348 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002349 int64_t Alignment;
2350 if (ParseAbsoluteExpression(Alignment))
2351 return true;
2352
2353 SMLoc MaxBytesLoc;
2354 bool HasFillExpr = false;
2355 int64_t FillExpr = 0;
2356 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002357 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2358 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002359 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002360 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002361
2362 // The fill expression can be omitted while specifying a maximum number of
2363 // alignment bytes, e.g:
2364 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002365 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002366 HasFillExpr = true;
2367 if (ParseAbsoluteExpression(FillExpr))
2368 return true;
2369 }
2370
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002371 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2372 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002373 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002374 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002375
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002376 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002377 if (ParseAbsoluteExpression(MaxBytesToFill))
2378 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002379
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002381 return TokError("unexpected token in directive");
2382 }
2383 }
2384
Sean Callanan79ed1a82010-01-19 20:22:31 +00002385 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002386
Daniel Dunbar648ac512010-05-17 21:54:30 +00002387 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002388 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002389
2390 // Compute alignment in bytes.
2391 if (IsPow2) {
2392 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002393 if (Alignment >= 32) {
2394 Error(AlignmentLoc, "invalid alignment value");
2395 Alignment = 31;
2396 }
2397
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002398 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002399 }
2400
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002401 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002402 if (MaxBytesLoc.isValid()) {
2403 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002404 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2405 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002406 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002407 }
2408
2409 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002410 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2411 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002412 MaxBytesToFill = 0;
2413 }
2414 }
2415
Daniel Dunbar648ac512010-05-17 21:54:30 +00002416 // Check whether we should use optimal code alignment for this .align
2417 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002418 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002419 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2420 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002421 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002422 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002423 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002424 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2425 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002426 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002427
2428 return false;
2429}
2430
Eli Bendersky4766ef42012-12-20 19:05:53 +00002431
2432/// ParseDirectiveBundleAlignMode
2433/// ::= {.bundle_align_mode} expression
2434bool AsmParser::ParseDirectiveBundleAlignMode() {
2435 CheckForValidSection();
2436
2437 // Expect a single argument: an expression that evaluates to a constant
2438 // in the inclusive range 0-30.
2439 SMLoc ExprLoc = getLexer().getLoc();
2440 int64_t AlignSizePow2;
2441 if (ParseAbsoluteExpression(AlignSizePow2))
2442 return true;
2443 else if (getLexer().isNot(AsmToken::EndOfStatement))
2444 return TokError("unexpected token after expression in"
2445 " '.bundle_align_mode' directive");
2446 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30)
2447 return Error(ExprLoc,
2448 "invalid bundle alignment size (expected between 0 and 30)");
2449
2450 Lex();
2451
2452 // Because of AlignSizePow2's verified range we can safely truncate it to
2453 // unsigned.
2454 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2));
2455 return false;
2456}
2457
2458/// ParseDirectiveBundleLock
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002459/// ::= {.bundle_lock} [align_to_end]
Eli Bendersky4766ef42012-12-20 19:05:53 +00002460bool AsmParser::ParseDirectiveBundleLock() {
2461 CheckForValidSection();
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002462 bool AlignToEnd = false;
Eli Bendersky4766ef42012-12-20 19:05:53 +00002463
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002464 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2465 StringRef Option;
2466 SMLoc Loc = getTok().getLoc();
2467 const char *kInvalidOptionError =
2468 "invalid option for '.bundle_lock' directive";
2469
2470 if (ParseIdentifier(Option))
2471 return Error(Loc, kInvalidOptionError);
2472
2473 if (Option != "align_to_end")
2474 return Error(Loc, kInvalidOptionError);
2475 else if (getLexer().isNot(AsmToken::EndOfStatement))
2476 return Error(Loc,
2477 "unexpected token after '.bundle_lock' directive option");
2478 AlignToEnd = true;
2479 }
2480
Eli Bendersky4766ef42012-12-20 19:05:53 +00002481 Lex();
2482
Eli Bendersky6c1d4972013-01-07 21:51:08 +00002483 getStreamer().EmitBundleLock(AlignToEnd);
Eli Bendersky4766ef42012-12-20 19:05:53 +00002484 return false;
2485}
2486
2487/// ParseDirectiveBundleLock
2488/// ::= {.bundle_lock}
2489bool AsmParser::ParseDirectiveBundleUnlock() {
2490 CheckForValidSection();
2491
2492 if (getLexer().isNot(AsmToken::EndOfStatement))
2493 return TokError("unexpected token in '.bundle_unlock' directive");
2494 Lex();
2495
2496 getStreamer().EmitBundleUnlock();
2497 return false;
2498}
2499
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002500/// ParseDirectiveSymbolAttribute
2501/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002502bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002503 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002504 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002505 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002506 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002507
2508 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002509 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002510
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002511 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002512
Jim Grosbach10ec6502011-09-15 17:56:49 +00002513 // Assembler local symbols don't make any sense here. Complain loudly.
2514 if (Sym->isTemporary())
2515 return Error(Loc, "non-local symbol required in directive");
2516
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002517 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002518
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002519 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002520 break;
2521
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002522 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002523 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002524 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002525 }
2526 }
2527
Sean Callanan79ed1a82010-01-19 20:22:31 +00002528 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002529 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002530}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002531
2532/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002533/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2534bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002535 CheckForValidSection();
2536
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002537 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002538 StringRef Name;
2539 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002540 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002541
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002542 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002543 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002544
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002545 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002546 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002547 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002548
2549 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002550 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002551 if (ParseAbsoluteExpression(Size))
2552 return true;
2553
2554 int64_t Pow2Alignment = 0;
2555 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002556 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002557 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002558 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002559 if (ParseAbsoluteExpression(Pow2Alignment))
2560 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002561
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002562 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2563 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002564 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2565
Chris Lattner258281d2010-01-19 06:22:22 +00002566 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002567 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2568 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002569 if (!isPowerOf2_64(Pow2Alignment))
2570 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2571 Pow2Alignment = Log2_64(Pow2Alignment);
2572 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002573 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002574
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002575 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002576 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002577
Sean Callanan79ed1a82010-01-19 20:22:31 +00002578 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002579
Chris Lattner1fc3d752009-07-09 17:25:12 +00002580 // NOTE: a size of zero for a .comm should create a undefined symbol
2581 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002582 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002583 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2584 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002585
Eric Christopherc260a3e2010-05-14 01:38:54 +00002586 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002587 // may internally end up wanting an alignment in bytes.
2588 // FIXME: Diagnose overflow.
2589 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002590 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2591 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002592
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002593 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002594 return Error(IDLoc, "invalid symbol redefinition");
2595
Chris Lattner1fc3d752009-07-09 17:25:12 +00002596 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002597 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002598 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002599 return false;
2600 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002601
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002602 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002603 return false;
2604}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002605
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002606/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002607/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002608bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002609 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002610 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002611
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002612 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002613 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002614 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002615
Sean Callanan79ed1a82010-01-19 20:22:31 +00002616 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002617
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002618 if (Str.empty())
2619 Error(Loc, ".abort detected. Assembly stopping.");
2620 else
2621 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002622 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002623
2624 return false;
2625}
Kevin Enderby71148242009-07-14 21:35:03 +00002626
Kevin Enderby1f049b22009-07-14 23:21:55 +00002627/// ParseDirectiveInclude
2628/// ::= .include "filename"
2629bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002630 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002631 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002632
Sean Callanan18b83232010-01-19 21:44:56 +00002633 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002634 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002635 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002636
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002638 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002639
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002640 // Strip the quotes.
2641 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002642
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002643 // Attempt to switch the lexer to the included file before consuming the end
2644 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002645 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002646 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002647 return true;
2648 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002649
2650 return false;
2651}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002652
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002653/// ParseDirectiveIncbin
2654/// ::= .incbin "filename"
2655bool AsmParser::ParseDirectiveIncbin() {
2656 if (getLexer().isNot(AsmToken::String))
2657 return TokError("expected string in '.incbin' directive");
2658
2659 std::string Filename = getTok().getString();
2660 SMLoc IncbinLoc = getLexer().getLoc();
2661 Lex();
2662
2663 if (getLexer().isNot(AsmToken::EndOfStatement))
2664 return TokError("unexpected token in '.incbin' directive");
2665
2666 // Strip the quotes.
2667 Filename = Filename.substr(1, Filename.size()-2);
2668
2669 // Attempt to process the included file.
2670 if (ProcessIncbinFile(Filename)) {
2671 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2672 return true;
2673 }
2674
2675 return false;
2676}
2677
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002678/// ParseDirectiveIf
2679/// ::= .if expression
2680bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002681 TheCondStack.push_back(TheCondState);
2682 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002683 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002684 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002685 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002686 int64_t ExprValue;
2687 if (ParseAbsoluteExpression(ExprValue))
2688 return true;
2689
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002690 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002691 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002692
Sean Callanan79ed1a82010-01-19 20:22:31 +00002693 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002694
2695 TheCondState.CondMet = ExprValue;
2696 TheCondState.Ignore = !TheCondState.CondMet;
2697 }
2698
2699 return false;
2700}
2701
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002702/// ParseDirectiveIfb
2703/// ::= .ifb string
2704bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2705 TheCondStack.push_back(TheCondState);
2706 TheCondState.TheCond = AsmCond::IfCond;
2707
Benjamin Kramer29739e72012-05-12 16:52:21 +00002708 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002709 EatToEndOfStatement();
2710 } else {
2711 StringRef Str = ParseStringToEndOfStatement();
2712
2713 if (getLexer().isNot(AsmToken::EndOfStatement))
2714 return TokError("unexpected token in '.ifb' directive");
2715
2716 Lex();
2717
2718 TheCondState.CondMet = ExpectBlank == Str.empty();
2719 TheCondState.Ignore = !TheCondState.CondMet;
2720 }
2721
2722 return false;
2723}
2724
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002725/// ParseDirectiveIfc
2726/// ::= .ifc string1, string2
2727bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2728 TheCondStack.push_back(TheCondState);
2729 TheCondState.TheCond = AsmCond::IfCond;
2730
Benjamin Kramer29739e72012-05-12 16:52:21 +00002731 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002732 EatToEndOfStatement();
2733 } else {
2734 StringRef Str1 = ParseStringToComma();
2735
2736 if (getLexer().isNot(AsmToken::Comma))
2737 return TokError("unexpected token in '.ifc' directive");
2738
2739 Lex();
2740
2741 StringRef Str2 = ParseStringToEndOfStatement();
2742
2743 if (getLexer().isNot(AsmToken::EndOfStatement))
2744 return TokError("unexpected token in '.ifc' directive");
2745
2746 Lex();
2747
2748 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2749 TheCondState.Ignore = !TheCondState.CondMet;
2750 }
2751
2752 return false;
2753}
2754
2755/// ParseDirectiveIfdef
2756/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002757bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2758 StringRef Name;
2759 TheCondStack.push_back(TheCondState);
2760 TheCondState.TheCond = AsmCond::IfCond;
2761
2762 if (TheCondState.Ignore) {
2763 EatToEndOfStatement();
2764 } else {
2765 if (ParseIdentifier(Name))
2766 return TokError("expected identifier after '.ifdef'");
2767
2768 Lex();
2769
2770 MCSymbol *Sym = getContext().LookupSymbol(Name);
2771
2772 if (expect_defined)
2773 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2774 else
2775 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2776 TheCondState.Ignore = !TheCondState.CondMet;
2777 }
2778
2779 return false;
2780}
2781
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002782/// ParseDirectiveElseIf
2783/// ::= .elseif expression
2784bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2785 if (TheCondState.TheCond != AsmCond::IfCond &&
2786 TheCondState.TheCond != AsmCond::ElseIfCond)
2787 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2788 " an .elseif");
2789 TheCondState.TheCond = AsmCond::ElseIfCond;
2790
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002791 bool LastIgnoreState = false;
2792 if (!TheCondStack.empty())
2793 LastIgnoreState = TheCondStack.back().Ignore;
2794 if (LastIgnoreState || TheCondState.CondMet) {
2795 TheCondState.Ignore = true;
2796 EatToEndOfStatement();
2797 }
2798 else {
2799 int64_t ExprValue;
2800 if (ParseAbsoluteExpression(ExprValue))
2801 return true;
2802
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002803 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002804 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002805
Sean Callanan79ed1a82010-01-19 20:22:31 +00002806 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002807 TheCondState.CondMet = ExprValue;
2808 TheCondState.Ignore = !TheCondState.CondMet;
2809 }
2810
2811 return false;
2812}
2813
2814/// ParseDirectiveElse
2815/// ::= .else
2816bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002817 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002818 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002819
Sean Callanan79ed1a82010-01-19 20:22:31 +00002820 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002821
2822 if (TheCondState.TheCond != AsmCond::IfCond &&
2823 TheCondState.TheCond != AsmCond::ElseIfCond)
2824 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2825 ".elseif");
2826 TheCondState.TheCond = AsmCond::ElseCond;
2827 bool LastIgnoreState = false;
2828 if (!TheCondStack.empty())
2829 LastIgnoreState = TheCondStack.back().Ignore;
2830 if (LastIgnoreState || TheCondState.CondMet)
2831 TheCondState.Ignore = true;
2832 else
2833 TheCondState.Ignore = false;
2834
2835 return false;
2836}
2837
2838/// ParseDirectiveEndIf
2839/// ::= .endif
2840bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002841 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002842 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002843
Sean Callanan79ed1a82010-01-19 20:22:31 +00002844 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002845
2846 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2847 TheCondStack.empty())
2848 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2849 ".else");
2850 if (!TheCondStack.empty()) {
2851 TheCondState = TheCondStack.back();
2852 TheCondStack.pop_back();
2853 }
2854
2855 return false;
2856}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002857
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002858void AsmParser::initializeDirectiveKindMapping() {
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002859 DirectiveKindMapping[".set"] = DK_SET;
2860 DirectiveKindMapping[".equ"] = DK_EQU;
2861 DirectiveKindMapping[".equiv"] = DK_EQUIV;
2862 DirectiveKindMapping[".ascii"] = DK_ASCII;
2863 DirectiveKindMapping[".asciz"] = DK_ASCIZ;
2864 DirectiveKindMapping[".string"] = DK_STRING;
2865 DirectiveKindMapping[".byte"] = DK_BYTE;
2866 DirectiveKindMapping[".short"] = DK_SHORT;
2867 DirectiveKindMapping[".value"] = DK_VALUE;
2868 DirectiveKindMapping[".2byte"] = DK_2BYTE;
2869 DirectiveKindMapping[".long"] = DK_LONG;
2870 DirectiveKindMapping[".int"] = DK_INT;
2871 DirectiveKindMapping[".4byte"] = DK_4BYTE;
2872 DirectiveKindMapping[".quad"] = DK_QUAD;
2873 DirectiveKindMapping[".8byte"] = DK_8BYTE;
2874 DirectiveKindMapping[".single"] = DK_SINGLE;
2875 DirectiveKindMapping[".float"] = DK_FLOAT;
2876 DirectiveKindMapping[".double"] = DK_DOUBLE;
2877 DirectiveKindMapping[".align"] = DK_ALIGN;
2878 DirectiveKindMapping[".align32"] = DK_ALIGN32;
2879 DirectiveKindMapping[".balign"] = DK_BALIGN;
2880 DirectiveKindMapping[".balignw"] = DK_BALIGNW;
2881 DirectiveKindMapping[".balignl"] = DK_BALIGNL;
2882 DirectiveKindMapping[".p2align"] = DK_P2ALIGN;
2883 DirectiveKindMapping[".p2alignw"] = DK_P2ALIGNW;
2884 DirectiveKindMapping[".p2alignl"] = DK_P2ALIGNL;
2885 DirectiveKindMapping[".org"] = DK_ORG;
2886 DirectiveKindMapping[".fill"] = DK_FILL;
Eli Bendersky7eef9c12013-01-10 23:40:56 +00002887 DirectiveKindMapping[".zero"] = DK_ZERO;
2888 DirectiveKindMapping[".extern"] = DK_EXTERN;
2889 DirectiveKindMapping[".globl"] = DK_GLOBL;
2890 DirectiveKindMapping[".global"] = DK_GLOBAL;
2891 DirectiveKindMapping[".indirect_symbol"] = DK_INDIRECT_SYMBOL;
2892 DirectiveKindMapping[".lazy_reference"] = DK_LAZY_REFERENCE;
2893 DirectiveKindMapping[".no_dead_strip"] = DK_NO_DEAD_STRIP;
2894 DirectiveKindMapping[".symbol_resolver"] = DK_SYMBOL_RESOLVER;
2895 DirectiveKindMapping[".private_extern"] = DK_PRIVATE_EXTERN;
2896 DirectiveKindMapping[".reference"] = DK_REFERENCE;
2897 DirectiveKindMapping[".weak_definition"] = DK_WEAK_DEFINITION;
2898 DirectiveKindMapping[".weak_reference"] = DK_WEAK_REFERENCE;
2899 DirectiveKindMapping[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN;
2900 DirectiveKindMapping[".comm"] = DK_COMM;
2901 DirectiveKindMapping[".common"] = DK_COMMON;
2902 DirectiveKindMapping[".lcomm"] = DK_LCOMM;
2903 DirectiveKindMapping[".abort"] = DK_ABORT;
2904 DirectiveKindMapping[".include"] = DK_INCLUDE;
2905 DirectiveKindMapping[".incbin"] = DK_INCBIN;
2906 DirectiveKindMapping[".code16"] = DK_CODE16;
2907 DirectiveKindMapping[".code16gcc"] = DK_CODE16GCC;
2908 DirectiveKindMapping[".rept"] = DK_REPT;
2909 DirectiveKindMapping[".irp"] = DK_IRP;
2910 DirectiveKindMapping[".irpc"] = DK_IRPC;
2911 DirectiveKindMapping[".endr"] = DK_ENDR;
2912 DirectiveKindMapping[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE;
2913 DirectiveKindMapping[".bundle_lock"] = DK_BUNDLE_LOCK;
2914 DirectiveKindMapping[".bundle_unlock"] = DK_BUNDLE_UNLOCK;
2915 DirectiveKindMapping[".if"] = DK_IF;
2916 DirectiveKindMapping[".ifb"] = DK_IFB;
2917 DirectiveKindMapping[".ifnb"] = DK_IFNB;
2918 DirectiveKindMapping[".ifc"] = DK_IFC;
2919 DirectiveKindMapping[".ifnc"] = DK_IFNC;
2920 DirectiveKindMapping[".ifdef"] = DK_IFDEF;
2921 DirectiveKindMapping[".ifndef"] = DK_IFNDEF;
2922 DirectiveKindMapping[".ifnotdef"] = DK_IFNOTDEF;
2923 DirectiveKindMapping[".elseif"] = DK_ELSEIF;
2924 DirectiveKindMapping[".else"] = DK_ELSE;
2925 DirectiveKindMapping[".endif"] = DK_ENDIF;
Eli Bendersky5d0f0612013-01-10 22:44:57 +00002926}
2927
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002928/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002929/// ::= .file [number] filename
2930/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002931bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002932 // FIXME: I'm not sure what this is.
2933 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002934 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002935 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002936 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002937 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002938
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002939 if (FileNumber < 1)
2940 return TokError("file number less than one");
2941 }
2942
Daniel Dunbareceec052010-07-12 17:45:27 +00002943 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002944 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002945
Nick Lewycky44d798d2011-10-17 23:05:28 +00002946 // Usually the directory and filename together, otherwise just the directory.
2947 StringRef Path = getTok().getString();
2948 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002949 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002950
Nick Lewycky44d798d2011-10-17 23:05:28 +00002951 StringRef Directory;
2952 StringRef Filename;
2953 if (getLexer().is(AsmToken::String)) {
2954 if (FileNumber == -1)
2955 return TokError("explicit path specified, but no file number");
2956 Filename = getTok().getString();
2957 Filename = Filename.substr(1, Filename.size()-2);
2958 Directory = Path;
2959 Lex();
2960 } else {
2961 Filename = Path;
2962 }
2963
Daniel Dunbareceec052010-07-12 17:45:27 +00002964 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002965 return TokError("unexpected token in '.file' directive");
2966
Chris Lattnerd32e8032010-01-25 19:02:58 +00002967 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002968 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002969 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002970 if (getContext().getGenDwarfForAssembly() == true)
2971 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2972 "used to generate dwarf debug info for assembly code");
2973
Nick Lewycky44d798d2011-10-17 23:05:28 +00002974 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002975 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002976 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002977
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002978 return false;
2979}
2980
2981/// ParseDirectiveLine
2982/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002983bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002984 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2985 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002986 return TokError("unexpected token in '.line' directive");
2987
Sean Callanan18b83232010-01-19 21:44:56 +00002988 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002989 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002990 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002991
2992 // FIXME: Do something with the .line.
2993 }
2994
Daniel Dunbareceec052010-07-12 17:45:27 +00002995 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002996 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002997
2998 return false;
2999}
3000
3001
3002/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003003/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003004/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
3005/// The first number is a file number, must have been previously assigned with
3006/// a .file directive, the second number is the line number and optionally the
3007/// third number is a column position (zero if not specified). The remaining
3008/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00003009bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003010
Daniel Dunbareceec052010-07-12 17:45:27 +00003011 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003012 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00003013 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003014 if (FileNumber < 1)
3015 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00003016 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003017 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003018 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003019
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00003020 int64_t LineNumber = 0;
3021 if (getLexer().is(AsmToken::Integer)) {
3022 LineNumber = getTok().getIntVal();
3023 if (LineNumber < 1)
3024 return TokError("line number less than one in '.loc' directive");
3025 Lex();
3026 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003027
3028 int64_t ColumnPos = 0;
3029 if (getLexer().is(AsmToken::Integer)) {
3030 ColumnPos = getTok().getIntVal();
3031 if (ColumnPos < 0)
3032 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00003033 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003034 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003035
Kevin Enderbyc0957932010-09-30 16:52:03 +00003036 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003037 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003038 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003039 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3040 for (;;) {
3041 if (getLexer().is(AsmToken::EndOfStatement))
3042 break;
3043
3044 StringRef Name;
3045 SMLoc Loc = getTok().getLoc();
3046 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003047 return TokError("unexpected token in '.loc' directive");
3048
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003049 if (Name == "basic_block")
3050 Flags |= DWARF2_FLAG_BASIC_BLOCK;
3051 else if (Name == "prologue_end")
3052 Flags |= DWARF2_FLAG_PROLOGUE_END;
3053 else if (Name == "epilogue_begin")
3054 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
3055 else if (Name == "is_stmt") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003056 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003057 const MCExpr *Value;
3058 if (getParser().ParseExpression(Value))
3059 return true;
3060 // The expression must be the constant 0 or 1.
3061 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3062 int Value = MCE->getValue();
3063 if (Value == 0)
3064 Flags &= ~DWARF2_FLAG_IS_STMT;
3065 else if (Value == 1)
3066 Flags |= DWARF2_FLAG_IS_STMT;
3067 else
3068 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003069 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003070 else {
3071 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
3072 }
3073 }
3074 else if (Name == "isa") {
Jordan Rose3ebe59c2013-01-07 19:00:49 +00003075 Loc = getTok().getLoc();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003076 const MCExpr *Value;
3077 if (getParser().ParseExpression(Value))
3078 return true;
3079 // The expression must be a constant greater or equal to 0.
3080 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3081 int Value = MCE->getValue();
3082 if (Value < 0)
3083 return Error(Loc, "isa number less than zero");
3084 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00003085 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003086 else {
3087 return Error(Loc, "isa number not a constant value");
3088 }
3089 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00003090 else if (Name == "discriminator") {
3091 if (getParser().ParseAbsoluteExpression(Discriminator))
3092 return true;
3093 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003094 else {
3095 return Error(Loc, "unknown sub-directive in '.loc' directive");
3096 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003097
Kevin Enderbyc1840b32010-08-24 20:32:42 +00003098 if (getLexer().is(AsmToken::EndOfStatement))
3099 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003100 }
3101 }
3102
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00003103 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00003104 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00003105
3106 return false;
3107}
3108
Daniel Dunbar138abae2010-10-16 04:56:42 +00003109/// ParseDirectiveStabs
3110/// ::= .stabs string, number, number, number
3111bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
3112 SMLoc DirectiveLoc) {
3113 return TokError("unsupported directive '" + Directive + "'");
3114}
3115
Eli Bendersky9b1bb052013-01-11 22:55:28 +00003116/// ParseDirectiveSpace
3117/// ::= .space expression [ , expression ]
3118bool GenericAsmParser::ParseDirectiveSpace(StringRef, SMLoc DirectiveLoc) {
3119 getParser().CheckForValidSection();
3120
3121 int64_t NumBytes;
3122 if (getParser().ParseAbsoluteExpression(NumBytes))
3123 return true;
3124
3125 int64_t FillExpr = 0;
3126 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3127 if (getLexer().isNot(AsmToken::Comma))
3128 return TokError("unexpected token in '.space' directive");
3129 Lex();
3130
3131 if (getParser().ParseAbsoluteExpression(FillExpr))
3132 return true;
3133
3134 if (getLexer().isNot(AsmToken::EndOfStatement))
3135 return TokError("unexpected token in '.space' directive");
3136 }
3137
3138 Lex();
3139
3140 if (NumBytes <= 0)
3141 return TokError("invalid number of bytes in '.space' directive");
3142
3143 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
3144 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
3145
3146 return false;
3147}
3148
Rafael Espindolaf9efd832011-05-10 01:10:18 +00003149/// ParseDirectiveCFISections
3150/// ::= .cfi_sections section [, section]
3151bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
3152 SMLoc DirectiveLoc) {
3153 StringRef Name;
3154 bool EH = false;
3155 bool Debug = false;
3156
3157 if (getParser().ParseIdentifier(Name))
3158 return TokError("Expected an identifier");
3159
3160 if (Name == ".eh_frame")
3161 EH = true;
3162 else if (Name == ".debug_frame")
3163 Debug = true;
3164
3165 if (getLexer().is(AsmToken::Comma)) {
3166 Lex();
3167
3168 if (getParser().ParseIdentifier(Name))
3169 return TokError("Expected an identifier");
3170
3171 if (Name == ".eh_frame")
3172 EH = true;
3173 else if (Name == ".debug_frame")
3174 Debug = true;
3175 }
3176
3177 getStreamer().EmitCFISections(EH, Debug);
3178
3179 return false;
3180}
3181
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003182/// ParseDirectiveCFIStartProc
3183/// ::= .cfi_startproc
3184bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
3185 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003186 getStreamer().EmitCFIStartProc();
3187 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003188}
3189
3190/// ParseDirectiveCFIEndProc
3191/// ::= .cfi_endproc
3192bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003193 getStreamer().EmitCFIEndProc();
3194 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003195}
3196
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003197/// ParseRegisterOrRegisterNumber - parse register name or number.
3198bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
3199 SMLoc DirectiveLoc) {
3200 unsigned RegNo;
3201
Jim Grosbach6f888a82011-06-02 17:14:04 +00003202 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003203 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
3204 DirectiveLoc))
3205 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00003206 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003207 } else
3208 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00003209
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003210 return false;
3211}
3212
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003213/// ParseDirectiveCFIDefCfa
3214/// ::= .cfi_def_cfa register, offset
3215bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
3216 SMLoc DirectiveLoc) {
3217 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003218 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003219 return true;
3220
3221 if (getLexer().isNot(AsmToken::Comma))
3222 return TokError("unexpected token in directive");
3223 Lex();
3224
3225 int64_t Offset = 0;
3226 if (getParser().ParseAbsoluteExpression(Offset))
3227 return true;
3228
Rafael Espindola066c2f42011-04-12 23:59:07 +00003229 getStreamer().EmitCFIDefCfa(Register, Offset);
3230 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00003231}
3232
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003233/// ParseDirectiveCFIDefCfaOffset
3234/// ::= .cfi_def_cfa_offset offset
3235bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
3236 SMLoc DirectiveLoc) {
3237 int64_t Offset = 0;
3238 if (getParser().ParseAbsoluteExpression(Offset))
3239 return true;
3240
Rafael Espindola066c2f42011-04-12 23:59:07 +00003241 getStreamer().EmitCFIDefCfaOffset(Offset);
3242 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00003243}
3244
3245/// ParseDirectiveCFIAdjustCfaOffset
3246/// ::= .cfi_adjust_cfa_offset adjustment
3247bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
3248 SMLoc DirectiveLoc) {
3249 int64_t Adjustment = 0;
3250 if (getParser().ParseAbsoluteExpression(Adjustment))
3251 return true;
3252
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00003253 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
3254 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003255}
3256
3257/// ParseDirectiveCFIDefCfaRegister
3258/// ::= .cfi_def_cfa_register register
3259bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
3260 SMLoc DirectiveLoc) {
3261 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003262 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003263 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003264
Rafael Espindola066c2f42011-04-12 23:59:07 +00003265 getStreamer().EmitCFIDefCfaRegister(Register);
3266 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003267}
3268
3269/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003270/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003271bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
3272 int64_t Register = 0;
3273 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00003274
3275 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003276 return true;
3277
3278 if (getLexer().isNot(AsmToken::Comma))
3279 return TokError("unexpected token in directive");
3280 Lex();
3281
3282 if (getParser().ParseAbsoluteExpression(Offset))
3283 return true;
3284
Rafael Espindola066c2f42011-04-12 23:59:07 +00003285 getStreamer().EmitCFIOffset(Register, Offset);
3286 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003287}
3288
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003289/// ParseDirectiveCFIRelOffset
3290/// ::= .cfi_rel_offset register, offset
3291bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
3292 SMLoc DirectiveLoc) {
3293 int64_t Register = 0;
3294
3295 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3296 return true;
3297
3298 if (getLexer().isNot(AsmToken::Comma))
3299 return TokError("unexpected token in directive");
3300 Lex();
3301
3302 int64_t Offset = 0;
3303 if (getParser().ParseAbsoluteExpression(Offset))
3304 return true;
3305
Rafael Espindola25f492e2011-04-12 16:12:03 +00003306 getStreamer().EmitCFIRelOffset(Register, Offset);
3307 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00003308}
3309
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003310static bool isValidEncoding(int64_t Encoding) {
3311 if (Encoding & ~0xff)
3312 return false;
3313
3314 if (Encoding == dwarf::DW_EH_PE_omit)
3315 return true;
3316
3317 const unsigned Format = Encoding & 0xf;
3318 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
3319 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
3320 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
3321 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
3322 return false;
3323
Rafael Espindolacaf11582010-12-29 04:31:26 +00003324 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003325 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00003326 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003327 return false;
3328
3329 return true;
3330}
3331
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003332/// ParseDirectiveCFIPersonalityOrLsda
3333/// ::= .cfi_personality encoding, [symbol_name]
3334/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003335bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003336 SMLoc DirectiveLoc) {
3337 int64_t Encoding = 0;
3338 if (getParser().ParseAbsoluteExpression(Encoding))
3339 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003340 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003341 return false;
3342
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00003343 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003344 return TokError("unsupported encoding.");
3345
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003346 if (getLexer().isNot(AsmToken::Comma))
3347 return TokError("unexpected token in directive");
3348 Lex();
3349
3350 StringRef Name;
3351 if (getParser().ParseIdentifier(Name))
3352 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003353
3354 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3355
3356 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00003357 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003358 else {
3359 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003360 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003361 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003362 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003363}
3364
Rafael Espindolafe024d02010-12-28 18:36:23 +00003365/// ParseDirectiveCFIRememberState
3366/// ::= .cfi_remember_state
3367bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3368 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003369 getStreamer().EmitCFIRememberState();
3370 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003371}
3372
3373/// ParseDirectiveCFIRestoreState
3374/// ::= .cfi_remember_state
3375bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3376 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003377 getStreamer().EmitCFIRestoreState();
3378 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003379}
3380
Rafael Espindolac5754392011-04-12 15:31:05 +00003381/// ParseDirectiveCFISameValue
3382/// ::= .cfi_same_value register
3383bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3384 SMLoc DirectiveLoc) {
3385 int64_t Register = 0;
3386
3387 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3388 return true;
3389
3390 getStreamer().EmitCFISameValue(Register);
3391
3392 return false;
3393}
3394
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003395/// ParseDirectiveCFIRestore
3396/// ::= .cfi_restore register
3397bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003398 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003399 int64_t Register = 0;
3400 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3401 return true;
3402
3403 getStreamer().EmitCFIRestore(Register);
3404
3405 return false;
3406}
3407
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003408/// ParseDirectiveCFIEscape
3409/// ::= .cfi_escape expression[,...]
3410bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003411 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003412 std::string Values;
3413 int64_t CurrValue;
3414 if (getParser().ParseAbsoluteExpression(CurrValue))
3415 return true;
3416
3417 Values.push_back((uint8_t)CurrValue);
3418
3419 while (getLexer().is(AsmToken::Comma)) {
3420 Lex();
3421
3422 if (getParser().ParseAbsoluteExpression(CurrValue))
3423 return true;
3424
3425 Values.push_back((uint8_t)CurrValue);
3426 }
3427
3428 getStreamer().EmitCFIEscape(Values);
3429 return false;
3430}
3431
Rafael Espindola16d7d432012-01-23 21:51:52 +00003432/// ParseDirectiveCFISignalFrame
3433/// ::= .cfi_signal_frame
3434bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3435 SMLoc DirectiveLoc) {
3436 if (getLexer().isNot(AsmToken::EndOfStatement))
3437 return Error(getLexer().getLoc(),
3438 "unexpected token in '" + Directive + "' directive");
3439
3440 getStreamer().EmitCFISignalFrame();
3441
3442 return false;
3443}
3444
Rafael Espindolac8fec7e2012-11-23 16:59:41 +00003445/// ParseDirectiveCFIUndefined
3446/// ::= .cfi_undefined register
3447bool GenericAsmParser::ParseDirectiveCFIUndefined(StringRef Directive,
3448 SMLoc DirectiveLoc) {
3449 int64_t Register = 0;
3450
3451 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3452 return true;
3453
3454 getStreamer().EmitCFIUndefined(Register);
3455
3456 return false;
3457}
3458
Rafael Espindolaf4f14f62012-11-25 15:14:49 +00003459/// ParseDirectiveCFIRegister
3460/// ::= .cfi_register register, register
3461bool GenericAsmParser::ParseDirectiveCFIRegister(StringRef Directive,
3462 SMLoc DirectiveLoc) {
3463 int64_t Register1 = 0;
3464
3465 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc))
3466 return true;
3467
3468 if (getLexer().isNot(AsmToken::Comma))
3469 return TokError("unexpected token in directive");
3470 Lex();
3471
3472 int64_t Register2 = 0;
3473
3474 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc))
3475 return true;
3476
3477 getStreamer().EmitCFIRegister(Register1, Register2);
3478
3479 return false;
3480}
3481
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003482/// ParseDirectiveMacrosOnOff
3483/// ::= .macros_on
3484/// ::= .macros_off
3485bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3486 SMLoc DirectiveLoc) {
3487 if (getLexer().isNot(AsmToken::EndOfStatement))
3488 return Error(getLexer().getLoc(),
3489 "unexpected token in '" + Directive + "' directive");
3490
Eli Bendersky733c3362013-01-14 18:08:41 +00003491 getParser().SetMacrosEnabled(Directive == ".macros_on");
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003492
3493 return false;
3494}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003495
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003496/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003497/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003498bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3499 SMLoc DirectiveLoc) {
3500 StringRef Name;
3501 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003502 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003503
Rafael Espindola8a403d32012-08-08 14:51:03 +00003504 MacroParameters Parameters;
Preston Gurd7b6f2032012-09-19 20:36:12 +00003505 // Argument delimiter is initially unknown. It will be set by
3506 // ParseMacroArgument()
3507 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof;
Rafael Espindola65366442011-06-05 02:43:45 +00003508 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003509 for (;;) {
3510 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003511 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003512 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003513
3514 if (getLexer().is(AsmToken::Equal)) {
3515 Lex();
Preston Gurd7b6f2032012-09-19 20:36:12 +00003516 if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter))
Preston Gurd6c9176a2012-09-19 20:29:04 +00003517 return true;
3518 }
3519
Rafael Espindola65366442011-06-05 02:43:45 +00003520 Parameters.push_back(Parameter);
3521
Preston Gurd7b6f2032012-09-19 20:36:12 +00003522 if (getLexer().is(AsmToken::Comma))
3523 Lex();
3524 else if (getLexer().is(AsmToken::EndOfStatement))
Rafael Espindola65366442011-06-05 02:43:45 +00003525 break;
Rafael Espindola65366442011-06-05 02:43:45 +00003526 }
3527 }
3528
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003529 // Eat the end of statement.
3530 Lex();
3531
3532 AsmToken EndToken, StartToken = getTok();
3533
3534 // Lex the macro definition.
3535 for (;;) {
3536 // Check whether we have reached the end of the file.
3537 if (getLexer().is(AsmToken::Eof))
3538 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3539
3540 // Otherwise, check whether we have reach the .endmacro.
3541 if (getLexer().is(AsmToken::Identifier) &&
3542 (getTok().getIdentifier() == ".endm" ||
3543 getTok().getIdentifier() == ".endmacro")) {
3544 EndToken = getTok();
3545 Lex();
3546 if (getLexer().isNot(AsmToken::EndOfStatement))
3547 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3548 "' directive");
3549 break;
3550 }
3551
3552 // Otherwise, scan til the end of the statement.
3553 getParser().EatToEndOfStatement();
3554 }
3555
3556 if (getParser().MacroMap.lookup(Name)) {
3557 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3558 }
3559
3560 const char *BodyStart = StartToken.getLoc().getPointer();
3561 const char *BodyEnd = EndToken.getLoc().getPointer();
3562 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003563 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003564 return false;
3565}
3566
3567/// ParseDirectiveEndMacro
3568/// ::= .endm
3569/// ::= .endmacro
3570bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003571 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003572 if (getLexer().isNot(AsmToken::EndOfStatement))
3573 return TokError("unexpected token in '" + Directive + "' directive");
3574
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003575 // If we are inside a macro instantiation, terminate the current
3576 // instantiation.
3577 if (!getParser().ActiveMacros.empty()) {
3578 getParser().HandleMacroExit();
3579 return false;
3580 }
3581
3582 // Otherwise, this .endmacro is a stray entry in the file; well formed
3583 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003584 return TokError("unexpected '" + Directive + "' in file, "
3585 "no current macro definition");
3586}
3587
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003588/// ParseDirectivePurgeMacro
3589/// ::= .purgem
3590bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3591 SMLoc DirectiveLoc) {
3592 StringRef Name;
3593 if (getParser().ParseIdentifier(Name))
3594 return TokError("expected identifier in '.purgem' directive");
3595
3596 if (getLexer().isNot(AsmToken::EndOfStatement))
3597 return TokError("unexpected token in '.purgem' directive");
3598
3599 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3600 if (I == getParser().MacroMap.end())
3601 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3602
3603 // Undefine the macro.
3604 delete I->getValue();
3605 getParser().MacroMap.erase(I);
3606 return false;
3607}
3608
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003609bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003610 getParser().CheckForValidSection();
3611
3612 const MCExpr *Value;
3613
3614 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003615 return true;
3616
3617 if (getLexer().isNot(AsmToken::EndOfStatement))
3618 return TokError("unexpected token in directive");
3619
3620 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003621 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003622 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003623 getStreamer().EmitULEB128Value(Value);
3624
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003625 return false;
3626}
3627
Rafael Espindola761cb062012-06-03 23:57:14 +00003628Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003629 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003630
Rafael Espindola761cb062012-06-03 23:57:14 +00003631 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003632 for (;;) {
3633 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003634 if (getLexer().is(AsmToken::Eof)) {
3635 Error(DirectiveLoc, "no matching '.endr' in definition");
3636 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003637 }
3638
Rafael Espindola761cb062012-06-03 23:57:14 +00003639 if (Lexer.is(AsmToken::Identifier) &&
3640 (getTok().getIdentifier() == ".rept")) {
3641 ++NestLevel;
3642 }
3643
3644 // Otherwise, check whether we have reached the .endr.
3645 if (Lexer.is(AsmToken::Identifier) &&
3646 getTok().getIdentifier() == ".endr") {
3647 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003648 EndToken = getTok();
3649 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003650 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3651 TokError("unexpected token in '.endr' directive");
3652 return 0;
3653 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003654 break;
3655 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003656 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003657 }
3658
Rafael Espindola761cb062012-06-03 23:57:14 +00003659 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003660 EatToEndOfStatement();
3661 }
3662
3663 const char *BodyStart = StartToken.getLoc().getPointer();
3664 const char *BodyEnd = EndToken.getLoc().getPointer();
3665 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3666
Rafael Espindola761cb062012-06-03 23:57:14 +00003667 // We Are Anonymous.
3668 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003669 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003670 return new Macro(Name, Body, Parameters);
3671}
3672
3673void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3674 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003675 OS << ".endr\n";
3676
3677 MemoryBuffer *Instantiation =
3678 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3679
Rafael Espindola761cb062012-06-03 23:57:14 +00003680 // Create the macro instantiation object and add to the current macro
3681 // instantiation stack.
3682 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
Daniel Dunbar4259a1a2012-12-01 01:38:48 +00003683 CurBuffer,
Rafael Espindola761cb062012-06-03 23:57:14 +00003684 getTok().getLoc(),
3685 Instantiation);
3686 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003687
Rafael Espindola761cb062012-06-03 23:57:14 +00003688 // Jump to the macro instantiation and prime the lexer.
3689 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3690 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3691 Lex();
3692}
3693
3694bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3695 int64_t Count;
3696 if (ParseAbsoluteExpression(Count))
3697 return TokError("unexpected token in '.rept' directive");
3698
3699 if (Count < 0)
3700 return TokError("Count is negative");
3701
3702 if (Lexer.isNot(AsmToken::EndOfStatement))
3703 return TokError("unexpected token in '.rept' directive");
3704
3705 // Eat the end of statement.
3706 Lex();
3707
3708 // Lex the rept definition.
3709 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3710 if (!M)
3711 return true;
3712
3713 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3714 // to hold the macro body with substitutions.
3715 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003716 MacroParameters Parameters;
3717 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003718 raw_svector_ostream OS(Buf);
3719 while (Count--) {
3720 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3721 return true;
3722 }
3723 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003724
3725 return false;
3726}
3727
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003728/// ParseDirectiveIrp
3729/// ::= .irp symbol,values
3730bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003731 MacroParameters Parameters;
3732 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003733
Preston Gurd6c9176a2012-09-19 20:29:04 +00003734 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003735 return TokError("expected identifier in '.irp' directive");
3736
3737 Parameters.push_back(Parameter);
3738
3739 if (Lexer.isNot(AsmToken::Comma))
3740 return TokError("expected comma in '.irp' directive");
3741
3742 Lex();
3743
Rafael Espindola8a403d32012-08-08 14:51:03 +00003744 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003745 if (ParseMacroArguments(0, A))
3746 return true;
3747
3748 // Eat the end of statement.
3749 Lex();
3750
3751 // Lex the irp definition.
3752 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3753 if (!M)
3754 return true;
3755
3756 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3757 // to hold the macro body with substitutions.
3758 SmallString<256> Buf;
3759 raw_svector_ostream OS(Buf);
3760
Rafael Espindola7996d042012-08-21 16:06:48 +00003761 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3762 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003763 Args.push_back(*i);
3764
3765 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3766 return true;
3767 }
3768
3769 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3770
3771 return false;
3772}
3773
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003774/// ParseDirectiveIrpc
3775/// ::= .irpc symbol,values
3776bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003777 MacroParameters Parameters;
3778 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003779
Preston Gurd6c9176a2012-09-19 20:29:04 +00003780 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003781 return TokError("expected identifier in '.irpc' directive");
3782
3783 Parameters.push_back(Parameter);
3784
3785 if (Lexer.isNot(AsmToken::Comma))
3786 return TokError("expected comma in '.irpc' directive");
3787
3788 Lex();
3789
Rafael Espindola8a403d32012-08-08 14:51:03 +00003790 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003791 if (ParseMacroArguments(0, A))
3792 return true;
3793
3794 if (A.size() != 1 || A.front().size() != 1)
3795 return TokError("unexpected token in '.irpc' directive");
3796
3797 // Eat the end of statement.
3798 Lex();
3799
3800 // Lex the irpc definition.
3801 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3802 if (!M)
3803 return true;
3804
3805 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3806 // to hold the macro body with substitutions.
3807 SmallString<256> Buf;
3808 raw_svector_ostream OS(Buf);
3809
3810 StringRef Values = A.front().front().getString();
3811 std::size_t I, End = Values.size();
3812 for (I = 0; I < End; ++I) {
Eli Bendersky9bac6b22013-01-14 19:00:26 +00003813 MCAsmMacroArgument Arg;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003814 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3815
Rafael Espindola8a403d32012-08-08 14:51:03 +00003816 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003817 Args.push_back(Arg);
3818
3819 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3820 return true;
3821 }
3822
3823 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3824
3825 return false;
3826}
3827
Rafael Espindola761cb062012-06-03 23:57:14 +00003828bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3829 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003830 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003831
3832 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003833 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003834 assert(getLexer().is(AsmToken::EndOfStatement));
3835
Rafael Espindola761cb062012-06-03 23:57:14 +00003836 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003837 return false;
3838}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003839
Eli Friedman2128aae2012-10-22 23:58:19 +00003840bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) {
3841 const MCExpr *Value;
3842 SMLoc ExprLoc = getLexer().getLoc();
3843 if (ParseExpression(Value))
3844 return true;
3845 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3846 if (!MCE)
3847 return Error(ExprLoc, "unexpected expression in _emit");
3848 uint64_t IntValue = MCE->getValue();
3849 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue))
3850 return Error(ExprLoc, "literal value out of range for directive");
3851
3852 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5));
3853 return false;
3854}
3855
Chad Rosierb1f8c132012-10-18 15:49:34 +00003856bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString,
3857 unsigned &NumOutputs, unsigned &NumInputs,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003858 SmallVectorImpl<std::pair<void *, bool> > &OpDecls,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003859 SmallVectorImpl<std::string> &Constraints,
Chad Rosierb1f8c132012-10-18 15:49:34 +00003860 SmallVectorImpl<std::string> &Clobbers,
3861 const MCInstrInfo *MII,
3862 const MCInstPrinter *IP,
3863 MCAsmParserSemaCallback &SI) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00003864 SmallVector<void *, 4> InputDecls;
3865 SmallVector<void *, 4> OutputDecls;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003866 SmallVector<bool, 4> InputDeclsAddressOf;
3867 SmallVector<bool, 4> OutputDeclsAddressOf;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003868 SmallVector<std::string, 4> InputConstraints;
3869 SmallVector<std::string, 4> OutputConstraints;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003870 std::set<std::string> ClobberRegs;
3871
Chad Rosier4e472d22012-10-20 01:02:45 +00003872 SmallVector<struct AsmRewrite, 4> AsmStrRewrites;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003873
3874 // Prime the lexer.
3875 Lex();
3876
3877 // While we have input, parse each statement.
3878 unsigned InputIdx = 0;
3879 unsigned OutputIdx = 0;
3880 while (getLexer().isNot(AsmToken::Eof)) {
Eli Friedman2128aae2012-10-22 23:58:19 +00003881 ParseStatementInfo Info(&AsmStrRewrites);
3882 if (ParseStatement(Info))
Chad Rosierab450e42012-10-19 22:57:33 +00003883 return true;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003884
Chad Rosier57498012012-12-12 22:45:52 +00003885 if (Info.ParseError)
3886 return true;
3887
Eli Friedman2128aae2012-10-22 23:58:19 +00003888 if (Info.Opcode != ~0U) {
3889 const MCInstrDesc &Desc = MII->get(Info.Opcode);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003890
3891 // Build the list of clobbers, outputs and inputs.
Eli Friedman2128aae2012-10-22 23:58:19 +00003892 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
3893 MCParsedAsmOperand *Operand = Info.ParsedOperands[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003894
3895 // Immediate.
3896 if (Operand->isImm()) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00003897 if (Operand->needAsmRewrite())
3898 AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix,
3899 Operand->getStartLoc()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003900 continue;
3901 }
3902
3903 // Register operand.
Chad Rosierc1ec2072013-01-10 22:10:27 +00003904 if (Operand->isReg() && !Operand->needAddressOf()) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003905 unsigned NumDefs = Desc.getNumDefs();
3906 // Clobber.
3907 if (NumDefs && Operand->getMCOperandNum() < NumDefs) {
3908 std::string Reg;
3909 raw_string_ostream OS(Reg);
3910 IP->printRegName(OS, Operand->getReg());
3911 ClobberRegs.insert(StringRef(OS.str()));
3912 }
3913 continue;
3914 }
3915
3916 // Expr/Input or Output.
Chad Rosier32989592012-10-18 20:27:15 +00003917 unsigned Size;
Chad Rosierc1ec2072013-01-10 22:10:27 +00003918 bool IsVarDecl;
Chad Rosier32989592012-10-18 20:27:15 +00003919 void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +00003920 Size, IsVarDecl);
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003921 if (OpDecl) {
Chad Rosierb1f8c132012-10-18 15:49:34 +00003922 bool isOutput = (i == 1) && Desc.mayStore();
Chad Rosierc1ec2072013-01-10 22:10:27 +00003923 if (Operand->isMem() && Operand->needSizeDirective())
Chad Rosier4e472d22012-10-20 01:02:45 +00003924 AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective,
Chad Rosierefcb3d92012-10-26 18:04:20 +00003925 Operand->getStartLoc(),
3926 /*Len*/0,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003927 Operand->getMemSize()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003928 if (isOutput) {
3929 std::string Constraint = "=";
3930 ++InputIdx;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003931 OutputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003932 OutputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003933 Constraint += Operand->getConstraint().str();
3934 OutputConstraints.push_back(Constraint);
Chad Rosier4e472d22012-10-20 01:02:45 +00003935 AsmStrRewrites.push_back(AsmRewrite(AOK_Output,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003936 Operand->getStartLoc(),
3937 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003938 } else {
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003939 InputDecls.push_back(OpDecl);
NAKAMURA Takumib956ec12013-01-11 02:50:09 +00003940 InputDeclsAddressOf.push_back(Operand->needAddressOf());
Chad Rosierb1f8c132012-10-18 15:49:34 +00003941 InputConstraints.push_back(Operand->getConstraint().str());
Chad Rosier4e472d22012-10-20 01:02:45 +00003942 AsmStrRewrites.push_back(AsmRewrite(AOK_Input,
Chad Rosier5a719fc2012-10-23 17:43:43 +00003943 Operand->getStartLoc(),
3944 Operand->getNameLen()));
Chad Rosierb1f8c132012-10-18 15:49:34 +00003945 }
3946 }
3947 }
Chad Rosierb1f8c132012-10-18 15:49:34 +00003948 }
3949 }
3950
3951 // Set the number of Outputs and Inputs.
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003952 NumOutputs = OutputDecls.size();
3953 NumInputs = InputDecls.size();
Chad Rosierb1f8c132012-10-18 15:49:34 +00003954
3955 // Set the unique clobbers.
3956 for (std::set<std::string>::iterator I = ClobberRegs.begin(),
3957 E = ClobberRegs.end(); I != E; ++I)
3958 Clobbers.push_back(*I);
3959
3960 // Merge the various outputs and inputs. Output are expected first.
3961 if (NumOutputs || NumInputs) {
3962 unsigned NumExprs = NumOutputs + NumInputs;
Chad Rosierc8dd27e2012-10-18 19:39:30 +00003963 OpDecls.resize(NumExprs);
Chad Rosierb1f8c132012-10-18 15:49:34 +00003964 Constraints.resize(NumExprs);
Chad Rosier5a719fc2012-10-23 17:43:43 +00003965 // FIXME: Constraints are hard coded to 'm', but we need an 'r'
Chad Rosierc1ec2072013-01-10 22:10:27 +00003966 // constraint for addressof. This needs to be cleaned up!
Chad Rosierb1f8c132012-10-18 15:49:34 +00003967 for (unsigned i = 0; i < NumOutputs; ++i) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003968 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
3969 Constraints[i] = OutputDeclsAddressOf[i] ? "=r" : OutputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003970 }
3971 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
Chad Rosierc1ec2072013-01-10 22:10:27 +00003972 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
3973 Constraints[j] = InputDeclsAddressOf[i] ? "r" : InputConstraints[i];
Chad Rosierb1f8c132012-10-18 15:49:34 +00003974 }
3975 }
3976
3977 // Build the IR assembly string.
3978 std::string AsmStringIR;
Chad Rosier4e472d22012-10-20 01:02:45 +00003979 AsmRewriteKind PrevKind = AOK_Imm;
Chad Rosierb1f8c132012-10-18 15:49:34 +00003980 raw_string_ostream OS(AsmStringIR);
3981 const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart();
Chad Rosier4e472d22012-10-20 01:02:45 +00003982 for (SmallVectorImpl<struct AsmRewrite>::iterator
Chad Rosierb1f8c132012-10-18 15:49:34 +00003983 I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
3984 const char *Loc = (*I).Loc.getPointer();
Chad Rosier96d58e62012-10-19 20:57:14 +00003985
Chad Rosier4e472d22012-10-20 01:02:45 +00003986 AsmRewriteKind Kind = (*I).Kind;
Chad Rosier96d58e62012-10-19 20:57:14 +00003987
3988 // Emit everything up to the immediate/expression. If the previous rewrite
3989 // was a size directive, then this has already been done.
3990 if (PrevKind != AOK_SizeDirective)
3991 OS << StringRef(Start, Loc - Start);
3992 PrevKind = Kind;
3993
Chad Rosier5a719fc2012-10-23 17:43:43 +00003994 // Skip the original expression.
3995 if (Kind == AOK_Skip) {
3996 Start = Loc + (*I).Len;
3997 continue;
3998 }
3999
Chad Rosierb1f8c132012-10-18 15:49:34 +00004000 // Rewrite expressions in $N notation.
Chad Rosier96d58e62012-10-19 20:57:14 +00004001 switch (Kind) {
Chad Rosier5a719fc2012-10-23 17:43:43 +00004002 default: break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004003 case AOK_Imm:
Chad Rosierefcb3d92012-10-26 18:04:20 +00004004 OS << Twine("$$");
4005 OS << (*I).Val;
4006 break;
4007 case AOK_ImmPrefix:
4008 OS << Twine("$$");
Chad Rosierb1f8c132012-10-18 15:49:34 +00004009 break;
4010 case AOK_Input:
4011 OS << '$';
4012 OS << InputIdx++;
4013 break;
4014 case AOK_Output:
4015 OS << '$';
4016 OS << OutputIdx++;
4017 break;
Chad Rosier96d58e62012-10-19 20:57:14 +00004018 case AOK_SizeDirective:
Chad Rosier6a020a72012-10-25 20:41:34 +00004019 switch((*I).Val) {
Chad Rosier96d58e62012-10-19 20:57:14 +00004020 default: break;
4021 case 8: OS << "byte ptr "; break;
4022 case 16: OS << "word ptr "; break;
4023 case 32: OS << "dword ptr "; break;
4024 case 64: OS << "qword ptr "; break;
4025 case 80: OS << "xword ptr "; break;
4026 case 128: OS << "xmmword ptr "; break;
4027 case 256: OS << "ymmword ptr "; break;
4028 }
Eli Friedman2128aae2012-10-22 23:58:19 +00004029 break;
4030 case AOK_Emit:
4031 OS << ".byte";
4032 break;
Chad Rosier6a020a72012-10-25 20:41:34 +00004033 case AOK_DotOperator:
4034 OS << (*I).Val;
4035 break;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004036 }
Chad Rosier96d58e62012-10-19 20:57:14 +00004037
Chad Rosierb1f8c132012-10-18 15:49:34 +00004038 // Skip the original expression.
Chad Rosier96d58e62012-10-19 20:57:14 +00004039 if (Kind != AOK_SizeDirective)
4040 Start = Loc + (*I).Len;
Chad Rosierb1f8c132012-10-18 15:49:34 +00004041 }
4042
4043 // Emit the remainder of the asm string.
4044 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd();
4045 if (Start != AsmEnd)
4046 OS << StringRef(Start, AsmEnd - Start);
4047
4048 AsmString = OS.str();
4049 return false;
4050}
4051
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004052/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004053MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004054 MCContext &C, MCStreamer &Out,
4055 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00004056 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00004057}