blob: 266d87e149001b1d0ab3150648c364b275c442ed [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"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000027#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000028#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000031#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000032#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000033#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000034#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000036#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000037#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000038#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000039using namespace llvm;
40
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000041static cl::opt<bool>
42FatalAssemblerWarnings("fatal-assembler-warnings",
43 cl::desc("Consider warnings as error"));
44
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000045namespace {
46
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000047/// \brief Helper class for tracking macro definitions.
Rafael Espindola28c1f6662012-06-03 22:41:23 +000048typedef std::vector<AsmToken> MacroArgument;
Rafael Espindola8a403d32012-08-08 14:51:03 +000049typedef std::vector<MacroArgument> MacroArguments;
Preston Gurd6c9176a2012-09-19 20:29:04 +000050typedef std::pair<StringRef, MacroArgument> MacroParameter;
Rafael Espindola8a403d32012-08-08 14:51:03 +000051typedef std::vector<MacroParameter> MacroParameters;
Rafael Espindola28c1f6662012-06-03 22:41:23 +000052
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053struct Macro {
54 StringRef Name;
55 StringRef Body;
Rafael Espindola8a403d32012-08-08 14:51:03 +000056 MacroParameters Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000057
58public:
Rafael Espindola8a403d32012-08-08 14:51:03 +000059 Macro(StringRef N, StringRef B, const MacroParameters &P) :
Rafael Espindola65366442011-06-05 02:43:45 +000060 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000061};
62
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000063/// \brief Helper class for storing information about an active macro
64/// instantiation.
65struct MacroInstantiation {
66 /// The macro being instantiated.
67 const Macro *TheMacro;
68
69 /// The macro instantiation with substitutions.
70 MemoryBuffer *Instantiation;
71
72 /// The location of the instantiation.
73 SMLoc InstantiationLoc;
74
75 /// The location where parsing should resume upon instantiation completion.
76 SMLoc ExitLoc;
77
78public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000079 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000080 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000081};
82
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083/// \brief The concrete assembly parser instance.
84class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000085 friend class GenericAsmParser;
86
Craig Topper85aadc02012-09-15 16:23:52 +000087 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION;
88 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089private:
90 AsmLexer Lexer;
91 MCContext &Ctx;
92 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000093 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000094 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000095 SourceMgr::DiagHandlerTy SavedDiagHandler;
96 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000097 MCAsmParserExtension *GenericParser;
98 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000099
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000100 /// This is the current buffer index we're lexing from as managed by the
101 /// SourceMgr object.
102 int CurBuffer;
103
104 AsmCond TheCondState;
105 std::vector<AsmCond> TheCondStack;
106
107 /// DirectiveMap - This is a table handlers for directives. Each handler is
108 /// invoked after the directive identifier is read and is responsible for
109 /// parsing and validating the rest of the directive. The handler is passed
110 /// in the directive name and the location of the directive keyword.
111 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000112
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000113 /// MacroMap - Map of currently defined macros.
114 StringMap<Macro*> MacroMap;
115
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000116 /// ActiveMacros - Stack of active macro instantiations.
117 std::vector<MacroInstantiation*> ActiveMacros;
118
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000119 /// Boolean tracking whether macro substitution is enabled.
120 unsigned MacrosEnabled : 1;
121
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000122 /// Flag tracking whether any errors have been encountered.
123 unsigned HadError : 1;
124
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000125 /// The values from the last parsed cpp hash file line comment if any.
126 StringRef CppHashFilename;
127 int64_t CppHashLineNumber;
128 SMLoc CppHashLoc;
129
Devang Patel0db58bf2012-01-31 18:14:05 +0000130 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
131 unsigned AssemblerDialect;
132
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000133public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000134 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000135 const MCAsmInfo &MAI);
Craig Topper345d16d2012-08-29 05:48:09 +0000136 virtual ~AsmParser();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000137
138 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
139
Craig Topper345d16d2012-08-29 05:48:09 +0000140 virtual void AddDirectiveHandler(MCAsmParserExtension *Object,
141 StringRef Directive,
142 DirectiveHandler Handler) {
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000143 DirectiveMap[Directive] = std::make_pair(Object, Handler);
144 }
145
146public:
147 /// @name MCAsmParser Interface
148 /// {
149
150 virtual SourceMgr &getSourceManager() { return SrcMgr; }
151 virtual MCAsmLexer &getLexer() { return Lexer; }
152 virtual MCContext &getContext() { return Ctx; }
153 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000154 virtual unsigned getAssemblerDialect() {
155 if (AssemblerDialect == ~0U)
156 return MAI.getAssemblerDialect();
157 else
158 return AssemblerDialect;
159 }
160 virtual void setAssemblerDialect(unsigned i) {
161 AssemblerDialect = i;
162 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000164 virtual bool Warning(SMLoc L, const Twine &Msg,
165 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
166 virtual bool Error(SMLoc L, const Twine &Msg,
167 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000168
Craig Topper345d16d2012-08-29 05:48:09 +0000169 virtual const AsmToken &Lex();
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000170
171 bool ParseExpression(const MCExpr *&Res);
172 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
173 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
174 virtual bool ParseAbsoluteExpression(int64_t &Res);
175
176 /// }
177
178private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000179 void CheckForValidSection();
180
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000181 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000182 void EatToEndOfLine();
183 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000185 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000186 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +0000187 const MacroParameters &Parameters,
188 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000189 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000190 void HandleMacroExit();
191
192 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000193 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000194 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
195 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000196 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000197 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000198
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000199 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
200 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000201 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
202 /// This returns true on failure.
203 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000204
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000205 /// \brief Reset the current lexer position to that given by \p Loc. The
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000206 /// current token is not set; clients should ensure Lex() is called
207 /// subsequently.
208 void JumpToLoc(SMLoc Loc);
209
Craig Topper345d16d2012-08-29 05:48:09 +0000210 virtual void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000211
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000212 bool ParseMacroArgument(MacroArgument &MA);
Rafael Espindola8a403d32012-08-08 14:51:03 +0000213 bool ParseMacroArguments(const Macro *M, MacroArguments &A);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000214
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000215 /// \brief Parse up to the end of statement and a return the contents from the
216 /// current token until the end of the statement; the current token on exit
217 /// will be either the EndOfStatement or EOF.
Craig Topper345d16d2012-08-29 05:48:09 +0000218 virtual StringRef ParseStringToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000219
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000220 /// \brief Parse until the end of a statement or a comma is encountered,
221 /// return the contents from the current token up to the end or comma.
222 StringRef ParseStringToComma();
223
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000224 bool ParseAssignment(StringRef Name, bool allow_redef,
225 bool NoDeadStrip = false);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000226
227 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
228 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
229 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000230 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000231
232 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000233 /// and set \p Res to the identifier contents.
Craig Topper345d16d2012-08-29 05:48:09 +0000234 virtual bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000235
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000236 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000237
238 // ".ascii", ".asciiz", ".string"
239 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000240 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000241 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000242 bool ParseDirectiveFill(); // ".fill"
243 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000244 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000245 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000246 bool ParseDirectiveOrg(); // ".org"
247 // ".align{,32}", ".p2align{,w,l}"
248 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
249
250 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
251 /// accepts a single symbol (which should be a label or an external).
252 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000253
254 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
255
256 bool ParseDirectiveAbort(); // ".abort"
257 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000258 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000259
260 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000261 // ".ifb" or ".ifnb", depending on ExpectBlank.
262 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000263 // ".ifc" or ".ifnc", depending on ExpectEqual.
264 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000265 // ".ifdef" or ".ifndef", depending on expect_defined
266 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000267 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
268 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
269 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
270
271 /// ParseEscapedString - Parse the current token as a string which may include
272 /// escaped characters and return the string contents.
273 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000274
275 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
276 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000277
Rafael Espindola761cb062012-06-03 23:57:14 +0000278 // Macro-like directives
279 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
280 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
281 raw_svector_ostream &OS);
282 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000283 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000284 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000285 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000286};
287
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000288/// \brief Generic implementations of directive handling, etc. which is shared
289/// (or the default, at least) for all assembler parser.
290class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000291 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
292 void AddDirectiveHandler(StringRef Directive) {
293 getParser().AddDirectiveHandler(this, Directive,
294 HandleDirective<GenericAsmParser, Handler>);
295 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000296public:
297 GenericAsmParser() {}
298
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000299 AsmParser &getParser() {
300 return (AsmParser&) this->MCAsmParserExtension::getParser();
301 }
302
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000303 virtual void Initialize(MCAsmParser &Parser) {
304 // Call the base implementation.
305 this->MCAsmParserExtension::Initialize(Parser);
306
307 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
309 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000312
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000313 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000314 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
315 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000316 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
317 ".cfi_startproc");
318 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
319 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000320 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
321 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000322 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
323 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000324 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
325 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000326 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
327 ".cfi_def_cfa_register");
328 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
329 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000330 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
331 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000332 AddDirectiveHandler<
333 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
334 AddDirectiveHandler<
335 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000336 AddDirectiveHandler<
337 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
338 AddDirectiveHandler<
339 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000340 AddDirectiveHandler<
341 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000342 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000343 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
344 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000345 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000346 AddDirectiveHandler<
347 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000348
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000349 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000350 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
351 ".macros_on");
352 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
353 ".macros_off");
354 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
355 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
356 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000357 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000358
359 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
360 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000361 }
362
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000363 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
364
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000365 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
366 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
367 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000368 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000369 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000370 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
371 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000372 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000373 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000374 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000375 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
376 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000377 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000378 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000379 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
380 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000381 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000382 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000383 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000384 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000385
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000386 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000387 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
388 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000389 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000390
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000391 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000392};
393
394}
395
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000396namespace llvm {
397
398extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000399extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000400extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000401
402}
403
Chris Lattneraaec2052010-01-19 19:46:13 +0000404enum { DEFAULT_ADDRSPACE = 0 };
405
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000406AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000407 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000408 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000409 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000410 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
411 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000412 // Save the old handler.
413 SavedDiagHandler = SrcMgr.getDiagHandler();
414 SavedDiagContext = SrcMgr.getDiagContext();
415 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000416 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000417 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000418
419 // Initialize the generic parser.
420 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000421
422 // Initialize the platform / file format parser.
423 //
424 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
425 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000426 if (_MAI.hasMicrosoftFastStdCallMangling()) {
427 PlatformParser = createCOFFAsmParser();
428 PlatformParser->Initialize(*this);
429 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000430 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000431 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000432 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000433 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000434 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000435 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000436}
437
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000438AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000439 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
440
441 // Destroy any macros.
442 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
443 ie = MacroMap.end(); it != ie; ++it)
444 delete it->getValue();
445
Daniel Dunbare4749702010-07-12 18:12:02 +0000446 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000447 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000448}
449
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000450void AsmParser::PrintMacroInstantiations() {
451 // Print the active macro instantiation stack.
452 for (std::vector<MacroInstantiation*>::const_reverse_iterator
453 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000454 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
455 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000456}
457
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000458bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000459 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000460 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000461 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000462 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000463 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000464}
465
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000466bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000467 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000468 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000469 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000470 return true;
471}
472
Sean Callananfd0b0282010-01-21 00:19:58 +0000473bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000474 std::string IncludedFile;
475 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000476 if (NewBuf == -1)
477 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000478
Sean Callananfd0b0282010-01-21 00:19:58 +0000479 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000480
Sean Callananfd0b0282010-01-21 00:19:58 +0000481 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000482
Sean Callananfd0b0282010-01-21 00:19:58 +0000483 return false;
484}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000485
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000486/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000487/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000488/// returns true on failure.
489bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
490 std::string IncludedFile;
491 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
492 if (NewBuf == -1)
493 return true;
494
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000495 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000496 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
497 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000498 return false;
499}
500
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000501void AsmParser::JumpToLoc(SMLoc Loc) {
502 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
503 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
504}
505
Sean Callananfd0b0282010-01-21 00:19:58 +0000506const AsmToken &AsmParser::Lex() {
507 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000508
Sean Callananfd0b0282010-01-21 00:19:58 +0000509 if (tok->is(AsmToken::Eof)) {
510 // If this is the end of an included file, pop the parent file off the
511 // include stack.
512 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
513 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000514 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000515 tok = &Lexer.Lex();
516 }
517 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000518
Sean Callananfd0b0282010-01-21 00:19:58 +0000519 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000520 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000521
Sean Callananfd0b0282010-01-21 00:19:58 +0000522 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000523}
524
Chris Lattner79180e22010-04-05 23:15:42 +0000525bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000526 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000527 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000528 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000529
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000530 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000531 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000532
533 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000534 AsmCond StartingCondState = TheCondState;
535
Kevin Enderby613b7572011-11-01 22:27:22 +0000536 // If we are generating dwarf for assembly source files save the initial text
537 // section and generate a .file directive.
538 if (getContext().getGenDwarfForAssembly()) {
539 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000540 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
541 getStreamer().EmitLabel(SectionStartSym);
542 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000543 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
544 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
545 }
546
Chris Lattnerb717fb02009-07-02 21:53:43 +0000547 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000548 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000549 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000550
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000551 // We had an error, validate that one was emitted and recover by skipping to
552 // the next line.
553 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000554 EatToEndOfStatement();
555 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000556
557 if (TheCondState.TheCond != StartingCondState.TheCond ||
558 TheCondState.Ignore != StartingCondState.Ignore)
559 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000560
561 // Check to see there are no empty DwarfFile slots.
562 const std::vector<MCDwarfFile *> &MCDwarfFiles =
563 getContext().getMCDwarfFiles();
564 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000565 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000566 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000567 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000568
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000569 // Check to see that all assembler local symbols were actually defined.
570 // Targets that don't do subsections via symbols may not want this, though,
571 // so conservatively exclude them. Only do this if we're finalizing, though,
572 // as otherwise we won't necessarilly have seen everything yet.
573 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
574 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
575 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
576 e = Symbols.end();
577 i != e; ++i) {
578 MCSymbol *Sym = i->getValue();
579 // Variable symbols may not be marked as defined, so check those
580 // explicitly. If we know it's a variable, we have a definition for
581 // the purposes of this check.
582 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
583 // FIXME: We would really like to refer back to where the symbol was
584 // first referenced for a source location. We need to add something
585 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000586 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
587 "assembler local symbol '" + Sym->getName() +
588 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000589 }
590 }
591
592
Chris Lattner79180e22010-04-05 23:15:42 +0000593 // Finalize the output stream if there are no errors and if the client wants
594 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000595 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000596 Out.Finish();
597
Chris Lattnerb717fb02009-07-02 21:53:43 +0000598 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000599}
600
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000601void AsmParser::CheckForValidSection() {
602 if (!getStreamer().getCurrentSection()) {
603 TokError("expected section directive before assembly directive");
604 Out.SwitchSection(Ctx.getMachOSection(
605 "__TEXT", "__text",
606 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
607 0, SectionKind::getText()));
608 }
609}
610
Chris Lattner2cf5f142009-06-22 01:29:09 +0000611/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
612void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000613 while (Lexer.isNot(AsmToken::EndOfStatement) &&
614 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000615 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000616
Chris Lattner2cf5f142009-06-22 01:29:09 +0000617 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000618 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000619 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000620}
621
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000622StringRef AsmParser::ParseStringToEndOfStatement() {
623 const char *Start = getTok().getLoc().getPointer();
624
625 while (Lexer.isNot(AsmToken::EndOfStatement) &&
626 Lexer.isNot(AsmToken::Eof))
627 Lex();
628
629 const char *End = getTok().getLoc().getPointer();
630 return StringRef(Start, End - Start);
631}
Chris Lattnerc4193832009-06-22 05:51:26 +0000632
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000633StringRef AsmParser::ParseStringToComma() {
634 const char *Start = getTok().getLoc().getPointer();
635
636 while (Lexer.isNot(AsmToken::EndOfStatement) &&
637 Lexer.isNot(AsmToken::Comma) &&
638 Lexer.isNot(AsmToken::Eof))
639 Lex();
640
641 const char *End = getTok().getLoc().getPointer();
642 return StringRef(Start, End - Start);
643}
644
Chris Lattner74ec1a32009-06-22 06:32:03 +0000645/// ParseParenExpr - Parse a paren expression and return it.
646/// NOTE: This assumes the leading '(' has already been consumed.
647///
648/// parenexpr ::= expr)
649///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000650bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000651 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000652 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000653 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000654 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000655 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000656 return false;
657}
Chris Lattnerc4193832009-06-22 05:51:26 +0000658
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000659/// ParseBracketExpr - Parse a bracket expression and return it.
660/// NOTE: This assumes the leading '[' has already been consumed.
661///
662/// bracketexpr ::= expr]
663///
664bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
665 if (ParseExpression(Res)) return true;
666 if (Lexer.isNot(AsmToken::RBrac))
667 return TokError("expected ']' in brackets expression");
668 EndLoc = Lexer.getLoc();
669 Lex();
670 return false;
671}
672
Chris Lattner74ec1a32009-06-22 06:32:03 +0000673/// ParsePrimaryExpr - Parse a primary expression and return it.
674/// primaryexpr ::= (parenexpr
675/// primaryexpr ::= symbol
676/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000677/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000678/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000679bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000680 switch (Lexer.getKind()) {
681 default:
682 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000683 // If we have an error assume that we've already handled it.
684 case AsmToken::Error:
685 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000686 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000687 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000688 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000689 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000690 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000691 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000692 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000693 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000694 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000695 EndLoc = Lexer.getLoc();
696
697 StringRef Identifier;
698 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000699 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000700
Daniel Dunbarfffff912009-10-16 01:34:54 +0000701 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000702 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000703 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000704
705 // Lookup the symbol variant if used.
706 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000707 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000708 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000709 if (Variant == MCSymbolRefExpr::VK_Invalid) {
710 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000711 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000712 }
713 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000714
Daniel Dunbarfffff912009-10-16 01:34:54 +0000715 // If this is an absolute variable reference, substitute it now to preserve
716 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000717 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000718 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000719 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000720
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000721 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000722 return false;
723 }
724
725 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000726 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000727 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000728 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000729 case AsmToken::Integer: {
730 SMLoc Loc = getTok().getLoc();
731 int64_t IntVal = getTok().getIntVal();
732 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000733 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000734 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000735 // Look for 'b' or 'f' following an Integer as a directional label
736 if (Lexer.getKind() == AsmToken::Identifier) {
737 StringRef IDVal = getTok().getString();
738 if (IDVal == "f" || IDVal == "b"){
739 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
740 IDVal == "f" ? 1 : 0);
741 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
742 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000743 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000744 return Error(Loc, "invalid reference to undefined symbol");
745 EndLoc = Lexer.getLoc();
746 Lex(); // Eat identifier.
747 }
748 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000749 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000750 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000751 case AsmToken::Real: {
752 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000753 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000754 Res = MCConstantExpr::Create(IntVal, getContext());
755 Lex(); // Eat token.
756 return false;
757 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000758 case AsmToken::Dot: {
759 // This is a '.' reference, which references the current PC. Emit a
760 // temporary label to the streamer and refer to it.
761 MCSymbol *Sym = Ctx.CreateTempSymbol();
762 Out.EmitLabel(Sym);
763 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
764 EndLoc = Lexer.getLoc();
765 Lex(); // Eat identifier.
766 return false;
767 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000768 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000769 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000770 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000771 case AsmToken::LBrac:
772 if (!PlatformParser->HasBracketExpressions())
773 return TokError("brackets expression not supported on this target");
774 Lex(); // Eat the '['.
775 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000776 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000777 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000778 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000779 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000780 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000781 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000782 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000783 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000784 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000785 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000786 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000787 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000788 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000789 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000790 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000791 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000792 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000793 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000794 }
795}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000796
Chris Lattnerb4307b32010-01-15 19:28:38 +0000797bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000798 SMLoc EndLoc;
799 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000800}
801
Daniel Dunbarcceba832010-09-17 02:47:07 +0000802const MCExpr *
803AsmParser::ApplyModifierToExpr(const MCExpr *E,
804 MCSymbolRefExpr::VariantKind Variant) {
805 // Recurse over the given expression, rebuilding it to apply the given variant
806 // if there is exactly one symbol.
807 switch (E->getKind()) {
808 case MCExpr::Target:
809 case MCExpr::Constant:
810 return 0;
811
812 case MCExpr::SymbolRef: {
813 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
814
815 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
816 TokError("invalid variant on expression '" +
817 getTok().getIdentifier() + "' (already modified)");
818 return E;
819 }
820
821 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
822 }
823
824 case MCExpr::Unary: {
825 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
826 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
827 if (!Sub)
828 return 0;
829 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
830 }
831
832 case MCExpr::Binary: {
833 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
834 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
835 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
836
837 if (!LHS && !RHS)
838 return 0;
839
840 if (!LHS) LHS = BE->getLHS();
841 if (!RHS) RHS = BE->getRHS();
842
843 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
844 }
845 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000846
Craig Topper85814382012-02-07 05:05:23 +0000847 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000848}
849
Chris Lattner74ec1a32009-06-22 06:32:03 +0000850/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000851///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000852/// expr ::= expr &&,|| expr -> lowest.
853/// expr ::= expr |,^,&,! expr
854/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
855/// expr ::= expr <<,>> expr
856/// expr ::= expr +,- expr
857/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000858/// expr ::= primaryexpr
859///
Chris Lattner54482b42010-01-15 19:39:23 +0000860bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000861 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000862 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000863 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
864 return true;
865
Daniel Dunbarcceba832010-09-17 02:47:07 +0000866 // As a special case, we support 'a op b @ modifier' by rewriting the
867 // expression to include the modifier. This is inefficient, but in general we
868 // expect users to use 'a@modifier op b'.
869 if (Lexer.getKind() == AsmToken::At) {
870 Lex();
871
872 if (Lexer.isNot(AsmToken::Identifier))
873 return TokError("unexpected symbol modifier following '@'");
874
875 MCSymbolRefExpr::VariantKind Variant =
876 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
877 if (Variant == MCSymbolRefExpr::VK_Invalid)
878 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
879
880 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
881 if (!ModifiedRes) {
882 return TokError("invalid modifier '" + getTok().getIdentifier() +
883 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000884 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000885
Daniel Dunbarcceba832010-09-17 02:47:07 +0000886 Res = ModifiedRes;
887 Lex();
888 }
889
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000890 // Try to constant fold it up front, if possible.
891 int64_t Value;
892 if (Res->EvaluateAsAbsolute(Value))
893 Res = MCConstantExpr::Create(Value, getContext());
894
895 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000896}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000897
Chris Lattnerb4307b32010-01-15 19:28:38 +0000898bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000899 Res = 0;
900 return ParseParenExpr(Res, EndLoc) ||
901 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000902}
903
Daniel Dunbar475839e2009-06-29 20:37:27 +0000904bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000905 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000906
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000907 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000908 if (ParseExpression(Expr))
909 return true;
910
Daniel Dunbare00b0112009-10-16 01:57:52 +0000911 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000912 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000913
914 return false;
915}
916
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000917static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000918 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000919 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000920 default:
921 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000922
Jim Grosbachfbe16812011-08-20 16:24:13 +0000923 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000924 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000925 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000926 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000927 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000928 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000929 return 1;
930
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000931
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000932 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000933 //
934 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000935 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000936 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000937 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000938 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000939 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000940 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000941 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000942 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000943 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000944
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000945 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000946 case AsmToken::EqualEqual:
947 Kind = MCBinaryExpr::EQ;
948 return 3;
949 case AsmToken::ExclaimEqual:
950 case AsmToken::LessGreater:
951 Kind = MCBinaryExpr::NE;
952 return 3;
953 case AsmToken::Less:
954 Kind = MCBinaryExpr::LT;
955 return 3;
956 case AsmToken::LessEqual:
957 Kind = MCBinaryExpr::LTE;
958 return 3;
959 case AsmToken::Greater:
960 Kind = MCBinaryExpr::GT;
961 return 3;
962 case AsmToken::GreaterEqual:
963 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000964 return 3;
965
Jim Grosbachfbe16812011-08-20 16:24:13 +0000966 // Intermediate Precedence: <<, >>
967 case AsmToken::LessLess:
968 Kind = MCBinaryExpr::Shl;
969 return 4;
970 case AsmToken::GreaterGreater:
971 Kind = MCBinaryExpr::Shr;
972 return 4;
973
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000974 // High Intermediate Precedence: +, -
975 case AsmToken::Plus:
976 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000977 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000978 case AsmToken::Minus:
979 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000980 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000981
Jim Grosbachfbe16812011-08-20 16:24:13 +0000982 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000983 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000984 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000985 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000986 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000987 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000988 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000989 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000990 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000991 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000992 }
993}
994
995
996/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
997/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000998bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
999 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001000 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001001 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001002 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001003
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001004 // If the next token is lower precedence than we are allowed to eat, return
1005 // successfully with what we ate already.
1006 if (TokPrec < Precedence)
1007 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001008
Sean Callanan79ed1a82010-01-19 20:22:31 +00001009 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001010
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001011 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001012 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001013 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001014
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001015 // If BinOp binds less tightly with RHS than the operator after RHS, let
1016 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001017 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001018 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001019 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001020 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001021 }
1022
Daniel Dunbar475839e2009-06-29 20:37:27 +00001023 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001024 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001025 }
1026}
1027
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001028
1029
1030
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001031/// ParseStatement:
1032/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001033/// ::= Label* Directive ...Operands... EndOfStatement
1034/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001035bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001036 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001037 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001038 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001039 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001040 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001041
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001042 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001043 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001044 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001045 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001046 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001047 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001048 if (Lexer.is(AsmToken::Hash))
1049 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001050
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001051 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001052 if (Lexer.is(AsmToken::Integer)) {
1053 LocalLabelVal = getTok().getIntVal();
1054 if (LocalLabelVal < 0) {
1055 if (!TheCondState.Ignore)
1056 return TokError("unexpected token at start of statement");
1057 IDVal = "";
1058 }
1059 else {
1060 IDVal = getTok().getString();
1061 Lex(); // Consume the integer token to be used as an identifier token.
1062 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001063 if (!TheCondState.Ignore)
1064 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001065 }
1066 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001067
1068 } else if (Lexer.is(AsmToken::Dot)) {
1069 // Treat '.' as a valid identifier in this context.
1070 Lex();
1071 IDVal = ".";
1072
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001073 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001074 if (!TheCondState.Ignore)
1075 return TokError("unexpected token at start of statement");
1076 IDVal = "";
1077 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001078
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001079
Chris Lattner7834fac2010-04-17 18:14:27 +00001080 // Handle conditional assembly here before checking for skipping. We
1081 // have to do this so that .endif isn't skipped in a ".if 0" block for
1082 // example.
1083 if (IDVal == ".if")
1084 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001085 if (IDVal == ".ifb")
1086 return ParseDirectiveIfb(IDLoc, true);
1087 if (IDVal == ".ifnb")
1088 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001089 if (IDVal == ".ifc")
1090 return ParseDirectiveIfc(IDLoc, true);
1091 if (IDVal == ".ifnc")
1092 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001093 if (IDVal == ".ifdef")
1094 return ParseDirectiveIfdef(IDLoc, true);
1095 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1096 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001097 if (IDVal == ".elseif")
1098 return ParseDirectiveElseIf(IDLoc);
1099 if (IDVal == ".else")
1100 return ParseDirectiveElse(IDLoc);
1101 if (IDVal == ".endif")
1102 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001103
Chris Lattner7834fac2010-04-17 18:14:27 +00001104 // If we are in a ".if 0" block, ignore this statement.
1105 if (TheCondState.Ignore) {
1106 EatToEndOfStatement();
1107 return false;
1108 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001109
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001110 // FIXME: Recurse on local labels?
1111
1112 // See what kind of statement we have.
1113 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001114 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001115 CheckForValidSection();
1116
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001117 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001119
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001120 // Diagnose attempt to use '.' as a label.
1121 if (IDVal == ".")
1122 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1123
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001124 // Diagnose attempt to use a variable as a label.
1125 //
1126 // FIXME: Diagnostics. Note the location of the definition as a label.
1127 // FIXME: This doesn't diagnose assignment to a symbol which has been
1128 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001129 MCSymbol *Sym;
1130 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001131 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001132 else
1133 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001134 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001135 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001136
Daniel Dunbar959fd882009-08-26 22:13:22 +00001137 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001138 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001139
Kevin Enderby94c2e852011-12-09 18:09:40 +00001140 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001141 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001142 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001143 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1144 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001145
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001146 // Consume any end of statement token, if present, to avoid spurious
1147 // AddBlankLine calls().
1148 if (Lexer.is(AsmToken::EndOfStatement)) {
1149 Lex();
1150 if (Lexer.is(AsmToken::Eof))
1151 return false;
1152 }
1153
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001154 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001155 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001156
Daniel Dunbar3f872332009-07-28 16:08:33 +00001157 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001158 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001159 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001160
Nico Weber4c4c7322011-01-28 03:04:41 +00001161 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001162
1163 default: // Normal instruction or directive.
1164 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001165 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001166
1167 // If macros are enabled, check to see if this is a macro instantiation.
1168 if (MacrosEnabled)
1169 if (const Macro *M = MacroMap.lookup(IDVal))
1170 return HandleMacroEntry(IDVal, IDLoc, M);
1171
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001172 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001173 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001174
1175 // Target hook for parsing target specific directives.
1176 if (!getTargetParser().ParseDirective(ID))
1177 return false;
1178
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001179 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001180 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001181 return ParseDirectiveSet(IDVal, true);
1182 if (IDVal == ".equiv")
1183 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001184
Daniel Dunbara0d14262009-06-24 23:30:00 +00001185 // Data directives
1186
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001187 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001188 return ParseDirectiveAscii(IDVal, false);
1189 if (IDVal == ".asciz" || IDVal == ".string")
1190 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001191
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001192 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001193 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001194 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001195 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001196 if (IDVal == ".value")
1197 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001198 if (IDVal == ".2byte")
1199 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001200 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001201 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001202 if (IDVal == ".int")
1203 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001204 if (IDVal == ".4byte")
1205 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001206 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001207 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001208 if (IDVal == ".8byte")
1209 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001210 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001211 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1212 if (IDVal == ".double")
1213 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001214
Eli Friedman5d68ec22010-07-19 04:17:25 +00001215 if (IDVal == ".align") {
1216 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1217 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1218 }
1219 if (IDVal == ".align32") {
1220 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1221 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1222 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001223 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001225 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001226 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001227 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001228 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001229 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001230 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001231 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001232 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001233 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001234 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1235
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001236 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001237 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001238
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001239 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001240 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001241 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001242 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001243 if (IDVal == ".zero")
1244 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001245
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001246 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001247
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001248 if (IDVal == ".extern") {
1249 EatToEndOfStatement(); // .extern is the default, ignore it.
1250 return false;
1251 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001252 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001253 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001254 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001255 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001256 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001257 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001258 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001259 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001260 if (IDVal == ".symbol_resolver")
1261 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001262 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001263 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001264 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001265 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001266 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001267 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001268 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001269 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001270 if (IDVal == ".weak_def_can_be_hidden")
1271 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001272
Hans Wennborg5cc64912011-06-18 13:51:54 +00001273 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001274 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001275 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001276 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001277
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001278 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001279 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001280 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001281 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001282 if (IDVal == ".incbin")
1283 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001284
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001285 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001286 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001287
Rafael Espindola761cb062012-06-03 23:57:14 +00001288 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001289 if (IDVal == ".rept")
1290 return ParseDirectiveRept(IDLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001291 if (IDVal == ".irp")
1292 return ParseDirectiveIrp(IDLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00001293 if (IDVal == ".irpc")
1294 return ParseDirectiveIrpc(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001295 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001296 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001297
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001298 // Look up the handler in the handler table.
1299 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1300 DirectiveMap.lookup(IDVal);
1301 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001302 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001303
Kevin Enderby9c656452009-09-10 20:51:44 +00001304
Jim Grosbach686c0182012-05-01 18:38:27 +00001305 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001306 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001307
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001308 CheckForValidSection();
1309
Chris Lattnera7f13542010-05-19 23:34:33 +00001310 // Canonicalize the opcode to lower case.
1311 SmallString<128> Opcode;
1312 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1313 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001314
Chris Lattner98986712010-01-14 22:21:20 +00001315 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001316 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001317 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001318
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001319 // Dump the parsed representation, if requested.
1320 if (getShowParsedOperands()) {
1321 SmallString<256> Str;
1322 raw_svector_ostream OS(Str);
1323 OS << "parsed instruction: [";
1324 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1325 if (i != 0)
1326 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001327 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001328 }
1329 OS << "]";
1330
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001331 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001332 }
1333
Kevin Enderby613b7572011-11-01 22:27:22 +00001334 // If we are generating dwarf for assembly source files and the current
1335 // section is the initial text section then generate a .loc directive for
1336 // the instruction.
1337 if (!HadError && getContext().getGenDwarfForAssembly() &&
1338 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1339 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1340 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1341 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001342 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001343 StringRef());
1344 }
1345
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001346 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001347 if (!HadError)
1348 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1349 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001350
Chris Lattner98986712010-01-14 22:21:20 +00001351 // Free any parsed operands.
1352 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1353 delete ParsedOperands[i];
1354
Chris Lattnercbf8a982010-09-11 16:18:25 +00001355 // Don't skip the rest of the line, the instruction parser is responsible for
1356 // that.
1357 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001358}
Chris Lattner9a023f72009-06-24 04:43:34 +00001359
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001360/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1361/// since they may not be able to be tokenized to get to the end of line token.
1362void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001363 if (!Lexer.is(AsmToken::EndOfStatement))
1364 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001365 // Eat EOL.
1366 Lex();
1367}
1368
1369/// ParseCppHashLineFilenameComment as this:
1370/// ::= # number "filename"
1371/// or just as a full line comment if it doesn't have a number and a string.
1372bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1373 Lex(); // Eat the hash token.
1374
1375 if (getLexer().isNot(AsmToken::Integer)) {
1376 // Consume the line since in cases it is not a well-formed line directive,
1377 // as if were simply a full line comment.
1378 EatToEndOfLine();
1379 return false;
1380 }
1381
1382 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001383 Lex();
1384
1385 if (getLexer().isNot(AsmToken::String)) {
1386 EatToEndOfLine();
1387 return false;
1388 }
1389
1390 StringRef Filename = getTok().getString();
1391 // Get rid of the enclosing quotes.
1392 Filename = Filename.substr(1, Filename.size()-2);
1393
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001394 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1395 CppHashLoc = L;
1396 CppHashFilename = Filename;
1397 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001398
1399 // Ignore any trailing characters, they're just comment.
1400 EatToEndOfLine();
1401 return false;
1402}
1403
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001404/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001405/// for the Filename and LineNo if any in the diagnostic.
1406void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1407 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1408 raw_ostream &OS = errs();
1409
1410 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1411 const SMLoc &DiagLoc = Diag.getLoc();
1412 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1413 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1414
1415 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1416 // before printing the message.
1417 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001418 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001419 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1420 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1421 }
1422
1423 // If we have not parsed a cpp hash line filename comment or the source
1424 // manager changed or buffer changed (like in a nested include) then just
1425 // print the normal diagnostic using its Filename and LineNo.
1426 if (!Parser->CppHashLineNumber ||
1427 &DiagSrcMgr != &Parser->SrcMgr ||
1428 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001429 if (Parser->SavedDiagHandler)
1430 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1431 else
1432 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001433 return;
1434 }
1435
1436 // Use the CppHashFilename and calculate a line number based on the
1437 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1438 // the diagnostic.
1439 const std::string Filename = Parser->CppHashFilename;
1440
1441 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1442 int CppHashLocLineNo =
1443 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1444 int LineNo = Parser->CppHashLineNumber - 1 +
1445 (DiagLocLineNo - CppHashLocLineNo);
1446
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001447 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1448 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001449 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001450 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001451
Benjamin Kramer04a04262011-10-16 10:48:29 +00001452 if (Parser->SavedDiagHandler)
1453 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1454 else
1455 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001456}
1457
Rafael Espindola799aacf2012-08-21 18:29:30 +00001458// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1459// difference being that that function accepts '@' as part of identifiers and
1460// we can't do that. AsmLexer.cpp should probably be changed to handle
1461// '@' as a special case when needed.
1462static bool isIdentifierChar(char c) {
1463 return isalnum(c) || c == '_' || c == '$' || c == '.';
1464}
1465
Rafael Espindola761cb062012-06-03 23:57:14 +00001466bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001467 const MacroParameters &Parameters,
1468 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001469 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001470 unsigned NParameters = Parameters.size();
1471 if (NParameters != 0 && NParameters != A.size())
1472 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001473
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001474 while (!Body.empty()) {
1475 // Scan for the next substitution.
1476 std::size_t End = Body.size(), Pos = 0;
1477 for (; Pos != End; ++Pos) {
1478 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001479 if (!NParameters) {
1480 // This macro has no parameters, look for $0, $1, etc.
1481 if (Body[Pos] != '$' || Pos + 1 == End)
1482 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001483
Rafael Espindola65366442011-06-05 02:43:45 +00001484 char Next = Body[Pos + 1];
1485 if (Next == '$' || Next == 'n' || isdigit(Next))
1486 break;
1487 } else {
1488 // This macro has parameters, look for \foo, \bar, etc.
1489 if (Body[Pos] == '\\' && Pos + 1 != End)
1490 break;
1491 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001492 }
1493
1494 // Add the prefix.
1495 OS << Body.slice(0, Pos);
1496
1497 // Check if we reached the end.
1498 if (Pos == End)
1499 break;
1500
Rafael Espindola65366442011-06-05 02:43:45 +00001501 if (!NParameters) {
1502 switch (Body[Pos+1]) {
1503 // $$ => $
1504 case '$':
1505 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001506 break;
1507
Rafael Espindola65366442011-06-05 02:43:45 +00001508 // $n => number of arguments
1509 case 'n':
1510 OS << A.size();
1511 break;
1512
1513 // $[0-9] => argument
1514 default: {
1515 // Missing arguments are ignored.
1516 unsigned Index = Body[Pos+1] - '0';
1517 if (Index >= A.size())
1518 break;
1519
1520 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001521 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001522 ie = A[Index].end(); it != ie; ++it)
1523 OS << it->getString();
1524 break;
1525 }
1526 }
1527 Pos += 2;
1528 } else {
1529 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001530 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001531 ++I;
1532
1533 const char *Begin = Body.data() + Pos +1;
1534 StringRef Argument(Begin, I - (Pos +1));
1535 unsigned Index = 0;
1536 for (; Index < NParameters; ++Index)
Preston Gurd6c9176a2012-09-19 20:29:04 +00001537 if (Parameters[Index].first == Argument)
Rafael Espindola65366442011-06-05 02:43:45 +00001538 break;
1539
1540 // FIXME: We should error at the macro definition.
1541 if (Index == NParameters)
1542 return Error(L, "Parameter not found");
1543
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001544 for (MacroArgument::const_iterator it = A[Index].begin(),
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001545 ie = A[Index].end(); it != ie; ++it)
1546 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001547
Rafael Espindola65366442011-06-05 02:43:45 +00001548 Pos += 1 + Argument.size();
1549 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001550 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001551 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001552 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001553
Rafael Espindola65366442011-06-05 02:43:45 +00001554 return false;
1555}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001556
Rafael Espindola65366442011-06-05 02:43:45 +00001557MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1558 MemoryBuffer *I)
1559 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1560{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001561}
1562
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001563/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1564/// This is used for both default macro parameter values and the
1565/// arguments in macro invocations
1566bool AsmParser::ParseMacroArgument(MacroArgument &MA) {
1567 unsigned ParenLevel = 0;
1568
1569 for (;;) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001570 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
1571 return TokError("unexpected token in macro instantiation");
1572
1573 // HandleMacroEntry relies on not advancing the lexer here
1574 // to be able to fill in the remaining default parameter values
1575 if (Lexer.is(AsmToken::EndOfStatement))
1576 break;
1577 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
1578 break;
1579
1580 // Adjust the current parentheses level.
1581 if (Lexer.is(AsmToken::LParen))
1582 ++ParenLevel;
1583 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1584 --ParenLevel;
1585
1586 // Append the token to the current argument list.
1587 MA.push_back(getTok());
1588 Lex();
1589 }
1590 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001591 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001592 return false;
1593}
1594
1595// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001596bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001597 const unsigned NParameters = M ? M->Parameters.size() : 0;
1598
1599 // Parse two kinds of macro invocations:
1600 // - macros defined without any parameters accept an arbitrary number of them
1601 // - macros defined with parameters accept at most that many of them
1602 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1603 ++Parameter) {
1604 MacroArgument MA;
1605
1606 if (ParseMacroArgument(MA))
1607 return true;
1608
Preston Gurd6c9176a2012-09-19 20:29:04 +00001609 if (!MA.empty() || !NParameters)
1610 A.push_back(MA);
1611 else if (NParameters) {
1612 if (!M->Parameters[Parameter].second.empty())
1613 A.push_back(M->Parameters[Parameter].second);
1614 }
Jim Grosbach97146442012-07-30 22:44:17 +00001615
Preston Gurd6c9176a2012-09-19 20:29:04 +00001616 // At the end of the statement, fill in remaining arguments that have
1617 // default values. If there aren't any, then the next argument is
1618 // required but missing
1619 if (Lexer.is(AsmToken::EndOfStatement)) {
1620 if (NParameters && Parameter < NParameters - 1) {
1621 if (M->Parameters[Parameter + 1].second.empty())
1622 return TokError("macro argument '" +
1623 Twine(M->Parameters[Parameter + 1].first) +
1624 "' is missing");
1625 else
1626 continue;
1627 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001628 return false;
Preston Gurd6c9176a2012-09-19 20:29:04 +00001629 }
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001630
1631 if (Lexer.is(AsmToken::Comma))
1632 Lex();
1633 }
1634 return TokError("Too many arguments");
1635}
1636
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001637bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1638 const Macro *M) {
1639 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1640 // this, although we should protect against infinite loops.
1641 if (ActiveMacros.size() == 20)
1642 return TokError("macros cannot be nested more than 20 levels deep");
1643
Rafael Espindola8a403d32012-08-08 14:51:03 +00001644 MacroArguments A;
1645 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001646 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001647
Jim Grosbach97146442012-07-30 22:44:17 +00001648 // Remove any trailing empty arguments. Do this after-the-fact as we have
1649 // to keep empty arguments in the middle of the list or positionality
1650 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001651 while (!A.empty() && A.back().empty())
1652 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001653
Rafael Espindola65366442011-06-05 02:43:45 +00001654 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1655 // to hold the macro body with substitutions.
1656 SmallString<256> Buf;
1657 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001658 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001659
Rafael Espindola8a403d32012-08-08 14:51:03 +00001660 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001661 return true;
1662
Rafael Espindola761cb062012-06-03 23:57:14 +00001663 // We include the .endmacro in the buffer as our queue to exit the macro
1664 // instantiation.
1665 OS << ".endmacro\n";
1666
Rafael Espindola65366442011-06-05 02:43:45 +00001667 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001668 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001669
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001670 // Create the macro instantiation object and add to the current macro
1671 // instantiation stack.
1672 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001673 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001674 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001675 ActiveMacros.push_back(MI);
1676
1677 // Jump to the macro instantiation and prime the lexer.
1678 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1679 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1680 Lex();
1681
1682 return false;
1683}
1684
1685void AsmParser::HandleMacroExit() {
1686 // Jump to the EndOfStatement we should return to, and consume it.
1687 JumpToLoc(ActiveMacros.back()->ExitLoc);
1688 Lex();
1689
1690 // Pop the instantiation entry.
1691 delete ActiveMacros.back();
1692 ActiveMacros.pop_back();
1693}
1694
Rafael Espindolae71cc862012-01-28 05:57:00 +00001695static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001696 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001697 case MCExpr::Binary: {
1698 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1699 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001700 break;
1701 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001702 case MCExpr::Target:
1703 case MCExpr::Constant:
1704 return false;
1705 case MCExpr::SymbolRef: {
1706 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001707 if (S.isVariable())
1708 return IsUsedIn(Sym, S.getVariableValue());
1709 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001710 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001711 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001712 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001713 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001714
1715 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001716}
1717
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001718bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1719 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001720 // FIXME: Use better location, we should use proper tokens.
1721 SMLoc EqualLoc = Lexer.getLoc();
1722
Daniel Dunbar821e3332009-08-31 08:09:28 +00001723 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001724 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001725 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001726
Rafael Espindolae71cc862012-01-28 05:57:00 +00001727 // Note: we don't count b as used in "a = b". This is to allow
1728 // a = b
1729 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001730
Daniel Dunbar3f872332009-07-28 16:08:33 +00001731 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001732 return TokError("unexpected token in assignment");
1733
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001734 // Error on assignment to '.'.
1735 if (Name == ".") {
1736 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1737 "(use '.space' or '.org').)"));
1738 }
1739
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001740 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001741 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001742
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001743 // Validate that the LHS is allowed to be a variable (either it has not been
1744 // used as a symbol, or it is an absolute symbol).
1745 MCSymbol *Sym = getContext().LookupSymbol(Name);
1746 if (Sym) {
1747 // Diagnose assignment to a label.
1748 //
1749 // FIXME: Diagnostics. Note the location of the definition as a label.
1750 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001751 if (IsUsedIn(Sym, Value))
1752 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1753 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001754 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001755 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1756 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001757 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001758 return Error(EqualLoc, "redefinition of '" + Name + "'");
1759 else if (!Sym->isVariable())
1760 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001761 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001762 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1763 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001764
1765 // Don't count these checks as uses.
1766 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001767 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001768 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001769
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001770 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001771
1772 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001773 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001774 if (NoDeadStrip)
1775 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1776
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001777
1778 return false;
1779}
1780
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001781/// ParseIdentifier:
1782/// ::= identifier
1783/// ::= string
1784bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001785 // The assembler has relaxed rules for accepting identifiers, in particular we
1786 // allow things like '.globl $foo', which would normally be separate
1787 // tokens. At this level, we have already lexed so we cannot (currently)
1788 // handle this as a context dependent token, instead we detect adjacent tokens
1789 // and return the combined identifier.
1790 if (Lexer.is(AsmToken::Dollar)) {
1791 SMLoc DollarLoc = getLexer().getLoc();
1792
1793 // Consume the dollar sign, and check for a following identifier.
1794 Lex();
1795 if (Lexer.isNot(AsmToken::Identifier))
1796 return true;
1797
1798 // We have a '$' followed by an identifier, make sure they are adjacent.
1799 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1800 return true;
1801
1802 // Construct the joined identifier and consume the token.
1803 Res = StringRef(DollarLoc.getPointer(),
1804 getTok().getIdentifier().size() + 1);
1805 Lex();
1806 return false;
1807 }
1808
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001809 if (Lexer.isNot(AsmToken::Identifier) &&
1810 Lexer.isNot(AsmToken::String))
1811 return true;
1812
Sean Callanan18b83232010-01-19 21:44:56 +00001813 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001814
Sean Callanan79ed1a82010-01-19 20:22:31 +00001815 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001816
1817 return false;
1818}
1819
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001820/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001821/// ::= .equ identifier ',' expression
1822/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001823/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001824bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001825 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001826
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001827 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001828 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001829
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001830 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001831 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001832 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001833
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001834 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001835}
1836
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001837bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001839
1840 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001841 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001842 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1843 if (Str[i] != '\\') {
1844 Data += Str[i];
1845 continue;
1846 }
1847
1848 // Recognize escaped characters. Note that this escape semantics currently
1849 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1850 ++i;
1851 if (i == e)
1852 return TokError("unexpected backslash at end of string");
1853
1854 // Recognize octal sequences.
1855 if ((unsigned) (Str[i] - '0') <= 7) {
1856 // Consume up to three octal characters.
1857 unsigned Value = Str[i] - '0';
1858
1859 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1860 ++i;
1861 Value = Value * 8 + (Str[i] - '0');
1862
1863 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1864 ++i;
1865 Value = Value * 8 + (Str[i] - '0');
1866 }
1867 }
1868
1869 if (Value > 255)
1870 return TokError("invalid octal escape sequence (out of range)");
1871
1872 Data += (unsigned char) Value;
1873 continue;
1874 }
1875
1876 // Otherwise recognize individual escapes.
1877 switch (Str[i]) {
1878 default:
1879 // Just reject invalid escape sequences for now.
1880 return TokError("invalid escape sequence (unrecognized character)");
1881
1882 case 'b': Data += '\b'; break;
1883 case 'f': Data += '\f'; break;
1884 case 'n': Data += '\n'; break;
1885 case 'r': Data += '\r'; break;
1886 case 't': Data += '\t'; break;
1887 case '"': Data += '"'; break;
1888 case '\\': Data += '\\'; break;
1889 }
1890 }
1891
1892 return false;
1893}
1894
Daniel Dunbara0d14262009-06-24 23:30:00 +00001895/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001896/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1897bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001898 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001899 CheckForValidSection();
1900
Daniel Dunbara0d14262009-06-24 23:30:00 +00001901 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001902 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001903 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001904
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001905 std::string Data;
1906 if (ParseEscapedString(Data))
1907 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001908
1909 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001910 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001911 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1912
Sean Callanan79ed1a82010-01-19 20:22:31 +00001913 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001914
1915 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001916 break;
1917
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001918 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001919 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001920 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001921 }
1922 }
1923
Sean Callanan79ed1a82010-01-19 20:22:31 +00001924 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001925 return false;
1926}
1927
1928/// ParseDirectiveValue
1929/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1930bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001931 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001932 CheckForValidSection();
1933
Daniel Dunbara0d14262009-06-24 23:30:00 +00001934 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001935 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001936 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001937 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001938 return true;
1939
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001940 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001941 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1942 assert(Size <= 8 && "Invalid size");
1943 uint64_t IntValue = MCE->getValue();
1944 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1945 return Error(ExprLoc, "literal value out of range for directive");
1946 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1947 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001948 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001949
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001950 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001951 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001952
Daniel Dunbara0d14262009-06-24 23:30:00 +00001953 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001954 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001955 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001956 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001957 }
1958 }
1959
Sean Callanan79ed1a82010-01-19 20:22:31 +00001960 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001961 return false;
1962}
1963
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001964/// ParseDirectiveRealValue
1965/// ::= (.single | .double) [ expression (, expression)* ]
1966bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1967 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1968 CheckForValidSection();
1969
1970 for (;;) {
1971 // We don't truly support arithmetic on floating point expressions, so we
1972 // have to manually parse unary prefixes.
1973 bool IsNeg = false;
1974 if (getLexer().is(AsmToken::Minus)) {
1975 Lex();
1976 IsNeg = true;
1977 } else if (getLexer().is(AsmToken::Plus))
1978 Lex();
1979
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001980 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001981 getLexer().isNot(AsmToken::Real) &&
1982 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001983 return TokError("unexpected token in directive");
1984
1985 // Convert to an APFloat.
1986 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001987 StringRef IDVal = getTok().getString();
1988 if (getLexer().is(AsmToken::Identifier)) {
1989 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1990 Value = APFloat::getInf(Semantics);
1991 else if (!IDVal.compare_lower("nan"))
1992 Value = APFloat::getNaN(Semantics, false, ~0);
1993 else
1994 return TokError("invalid floating point literal");
1995 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001996 APFloat::opInvalidOp)
1997 return TokError("invalid floating point literal");
1998 if (IsNeg)
1999 Value.changeSign();
2000
2001 // Consume the numeric token.
2002 Lex();
2003
2004 // Emit the value as an integer.
2005 APInt AsInt = Value.bitcastToAPInt();
2006 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
2007 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
2008
2009 if (getLexer().is(AsmToken::EndOfStatement))
2010 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002011
Daniel Dunbarb95a0792010-09-24 01:59:56 +00002012 if (getLexer().isNot(AsmToken::Comma))
2013 return TokError("unexpected token in directive");
2014 Lex();
2015 }
2016 }
2017
2018 Lex();
2019 return false;
2020}
2021
Daniel Dunbara0d14262009-06-24 23:30:00 +00002022/// ParseDirectiveSpace
2023/// ::= .space expression [ , expression ]
2024bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002025 CheckForValidSection();
2026
Daniel Dunbara0d14262009-06-24 23:30:00 +00002027 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002028 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002029 return true;
2030
2031 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002032 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2033 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002034 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002035 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002036
Daniel Dunbar475839e2009-06-29 20:37:27 +00002037 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002038 return true;
2039
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002040 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002041 return TokError("unexpected token in '.space' directive");
2042 }
2043
Sean Callanan79ed1a82010-01-19 20:22:31 +00002044 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002045
2046 if (NumBytes <= 0)
2047 return TokError("invalid number of bytes in '.space' directive");
2048
2049 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002050 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002051
2052 return false;
2053}
2054
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002055/// ParseDirectiveZero
2056/// ::= .zero expression
2057bool AsmParser::ParseDirectiveZero() {
2058 CheckForValidSection();
2059
2060 int64_t NumBytes;
2061 if (ParseAbsoluteExpression(NumBytes))
2062 return true;
2063
Rafael Espindolae452b172010-10-05 19:42:57 +00002064 int64_t Val = 0;
2065 if (getLexer().is(AsmToken::Comma)) {
2066 Lex();
2067 if (ParseAbsoluteExpression(Val))
2068 return true;
2069 }
2070
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002071 if (getLexer().isNot(AsmToken::EndOfStatement))
2072 return TokError("unexpected token in '.zero' directive");
2073
2074 Lex();
2075
Rafael Espindolae452b172010-10-05 19:42:57 +00002076 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002077
2078 return false;
2079}
2080
Daniel Dunbara0d14262009-06-24 23:30:00 +00002081/// ParseDirectiveFill
2082/// ::= .fill expression , expression , expression
2083bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002084 CheckForValidSection();
2085
Daniel Dunbara0d14262009-06-24 23:30:00 +00002086 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002087 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002088 return true;
2089
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002090 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002091 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002092 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002093
Daniel Dunbara0d14262009-06-24 23:30:00 +00002094 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002095 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002096 return true;
2097
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002098 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002099 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002100 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002101
Daniel Dunbara0d14262009-06-24 23:30:00 +00002102 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002103 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002104 return true;
2105
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002106 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002107 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002108
Sean Callanan79ed1a82010-01-19 20:22:31 +00002109 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002110
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002111 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2112 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002113
2114 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002115 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002116
2117 return false;
2118}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002119
2120/// ParseDirectiveOrg
2121/// ::= .org expression [ , expression ]
2122bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002123 CheckForValidSection();
2124
Daniel Dunbar821e3332009-08-31 08:09:28 +00002125 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002126 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002127 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002128 return true;
2129
2130 // Parse optional fill expression.
2131 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002132 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2133 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002134 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002135 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002136
Daniel Dunbar475839e2009-06-29 20:37:27 +00002137 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002138 return true;
2139
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002140 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002141 return TokError("unexpected token in '.org' directive");
2142 }
2143
Sean Callanan79ed1a82010-01-19 20:22:31 +00002144 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002145
Jim Grosbachebd4c052012-01-27 00:37:08 +00002146 // Only limited forms of relocatable expressions are accepted here, it
2147 // has to be relative to the current section. The streamer will return
2148 // 'true' if the expression wasn't evaluatable.
2149 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2150 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002151
2152 return false;
2153}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002154
2155/// ParseDirectiveAlign
2156/// ::= {.align, ...} expression [ , expression [ , expression ]]
2157bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002158 CheckForValidSection();
2159
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002160 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002161 int64_t Alignment;
2162 if (ParseAbsoluteExpression(Alignment))
2163 return true;
2164
2165 SMLoc MaxBytesLoc;
2166 bool HasFillExpr = false;
2167 int64_t FillExpr = 0;
2168 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002169 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2170 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002171 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002172 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002173
2174 // The fill expression can be omitted while specifying a maximum number of
2175 // alignment bytes, e.g:
2176 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002177 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002178 HasFillExpr = true;
2179 if (ParseAbsoluteExpression(FillExpr))
2180 return true;
2181 }
2182
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002183 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2184 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002185 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002186 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002187
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002188 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002189 if (ParseAbsoluteExpression(MaxBytesToFill))
2190 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002191
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002192 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002193 return TokError("unexpected token in directive");
2194 }
2195 }
2196
Sean Callanan79ed1a82010-01-19 20:22:31 +00002197 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002198
Daniel Dunbar648ac512010-05-17 21:54:30 +00002199 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002200 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002201
2202 // Compute alignment in bytes.
2203 if (IsPow2) {
2204 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002205 if (Alignment >= 32) {
2206 Error(AlignmentLoc, "invalid alignment value");
2207 Alignment = 31;
2208 }
2209
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002210 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002211 }
2212
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002213 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002214 if (MaxBytesLoc.isValid()) {
2215 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002216 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2217 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002218 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002219 }
2220
2221 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002222 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2223 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002224 MaxBytesToFill = 0;
2225 }
2226 }
2227
Daniel Dunbar648ac512010-05-17 21:54:30 +00002228 // Check whether we should use optimal code alignment for this .align
2229 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002230 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002231 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2232 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002233 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002234 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002235 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002236 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2237 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002238 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002239
2240 return false;
2241}
2242
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002243/// ParseDirectiveSymbolAttribute
2244/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002245bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002246 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002247 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002248 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002249 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002250
2251 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002252 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002253
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002254 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002255
Jim Grosbach10ec6502011-09-15 17:56:49 +00002256 // Assembler local symbols don't make any sense here. Complain loudly.
2257 if (Sym->isTemporary())
2258 return Error(Loc, "non-local symbol required in directive");
2259
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002260 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002261
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002262 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002263 break;
2264
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002265 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002266 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002267 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002268 }
2269 }
2270
Sean Callanan79ed1a82010-01-19 20:22:31 +00002271 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002272 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002273}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002274
2275/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002276/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2277bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002278 CheckForValidSection();
2279
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002280 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002281 StringRef Name;
2282 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002283 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002284
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002285 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002286 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002287
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002288 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002289 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002290 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002291
2292 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002293 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002294 if (ParseAbsoluteExpression(Size))
2295 return true;
2296
2297 int64_t Pow2Alignment = 0;
2298 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002299 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002300 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002301 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002302 if (ParseAbsoluteExpression(Pow2Alignment))
2303 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002304
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002305 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2306 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002307 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2308
Chris Lattner258281d2010-01-19 06:22:22 +00002309 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002310 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2311 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002312 if (!isPowerOf2_64(Pow2Alignment))
2313 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2314 Pow2Alignment = Log2_64(Pow2Alignment);
2315 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002316 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002317
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002318 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002319 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002320
Sean Callanan79ed1a82010-01-19 20:22:31 +00002321 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002322
Chris Lattner1fc3d752009-07-09 17:25:12 +00002323 // NOTE: a size of zero for a .comm should create a undefined symbol
2324 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002325 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002326 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2327 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002328
Eric Christopherc260a3e2010-05-14 01:38:54 +00002329 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002330 // may internally end up wanting an alignment in bytes.
2331 // FIXME: Diagnose overflow.
2332 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002333 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2334 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002335
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002336 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002337 return Error(IDLoc, "invalid symbol redefinition");
2338
Chris Lattner1fc3d752009-07-09 17:25:12 +00002339 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002340 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002341 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002342 return false;
2343 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002344
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002345 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002346 return false;
2347}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002348
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002349/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002350/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002351bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002352 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002353 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002354
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002355 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002356 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002357 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002358
Sean Callanan79ed1a82010-01-19 20:22:31 +00002359 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002360
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002361 if (Str.empty())
2362 Error(Loc, ".abort detected. Assembly stopping.");
2363 else
2364 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002365 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002366
2367 return false;
2368}
Kevin Enderby71148242009-07-14 21:35:03 +00002369
Kevin Enderby1f049b22009-07-14 23:21:55 +00002370/// ParseDirectiveInclude
2371/// ::= .include "filename"
2372bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002373 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002374 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002375
Sean Callanan18b83232010-01-19 21:44:56 +00002376 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002377 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002378 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002379
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002380 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002381 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002382
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002383 // Strip the quotes.
2384 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002385
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002386 // Attempt to switch the lexer to the included file before consuming the end
2387 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002388 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002389 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002390 return true;
2391 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002392
2393 return false;
2394}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002395
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002396/// ParseDirectiveIncbin
2397/// ::= .incbin "filename"
2398bool AsmParser::ParseDirectiveIncbin() {
2399 if (getLexer().isNot(AsmToken::String))
2400 return TokError("expected string in '.incbin' directive");
2401
2402 std::string Filename = getTok().getString();
2403 SMLoc IncbinLoc = getLexer().getLoc();
2404 Lex();
2405
2406 if (getLexer().isNot(AsmToken::EndOfStatement))
2407 return TokError("unexpected token in '.incbin' directive");
2408
2409 // Strip the quotes.
2410 Filename = Filename.substr(1, Filename.size()-2);
2411
2412 // Attempt to process the included file.
2413 if (ProcessIncbinFile(Filename)) {
2414 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2415 return true;
2416 }
2417
2418 return false;
2419}
2420
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002421/// ParseDirectiveIf
2422/// ::= .if expression
2423bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002424 TheCondStack.push_back(TheCondState);
2425 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002426 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002427 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002428 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002429 int64_t ExprValue;
2430 if (ParseAbsoluteExpression(ExprValue))
2431 return true;
2432
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002433 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002434 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002435
Sean Callanan79ed1a82010-01-19 20:22:31 +00002436 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002437
2438 TheCondState.CondMet = ExprValue;
2439 TheCondState.Ignore = !TheCondState.CondMet;
2440 }
2441
2442 return false;
2443}
2444
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002445/// ParseDirectiveIfb
2446/// ::= .ifb string
2447bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2448 TheCondStack.push_back(TheCondState);
2449 TheCondState.TheCond = AsmCond::IfCond;
2450
Benjamin Kramer29739e72012-05-12 16:52:21 +00002451 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002452 EatToEndOfStatement();
2453 } else {
2454 StringRef Str = ParseStringToEndOfStatement();
2455
2456 if (getLexer().isNot(AsmToken::EndOfStatement))
2457 return TokError("unexpected token in '.ifb' directive");
2458
2459 Lex();
2460
2461 TheCondState.CondMet = ExpectBlank == Str.empty();
2462 TheCondState.Ignore = !TheCondState.CondMet;
2463 }
2464
2465 return false;
2466}
2467
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002468/// ParseDirectiveIfc
2469/// ::= .ifc string1, string2
2470bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2471 TheCondStack.push_back(TheCondState);
2472 TheCondState.TheCond = AsmCond::IfCond;
2473
Benjamin Kramer29739e72012-05-12 16:52:21 +00002474 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002475 EatToEndOfStatement();
2476 } else {
2477 StringRef Str1 = ParseStringToComma();
2478
2479 if (getLexer().isNot(AsmToken::Comma))
2480 return TokError("unexpected token in '.ifc' directive");
2481
2482 Lex();
2483
2484 StringRef Str2 = ParseStringToEndOfStatement();
2485
2486 if (getLexer().isNot(AsmToken::EndOfStatement))
2487 return TokError("unexpected token in '.ifc' directive");
2488
2489 Lex();
2490
2491 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2492 TheCondState.Ignore = !TheCondState.CondMet;
2493 }
2494
2495 return false;
2496}
2497
2498/// ParseDirectiveIfdef
2499/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002500bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2501 StringRef Name;
2502 TheCondStack.push_back(TheCondState);
2503 TheCondState.TheCond = AsmCond::IfCond;
2504
2505 if (TheCondState.Ignore) {
2506 EatToEndOfStatement();
2507 } else {
2508 if (ParseIdentifier(Name))
2509 return TokError("expected identifier after '.ifdef'");
2510
2511 Lex();
2512
2513 MCSymbol *Sym = getContext().LookupSymbol(Name);
2514
2515 if (expect_defined)
2516 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2517 else
2518 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2519 TheCondState.Ignore = !TheCondState.CondMet;
2520 }
2521
2522 return false;
2523}
2524
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002525/// ParseDirectiveElseIf
2526/// ::= .elseif expression
2527bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2528 if (TheCondState.TheCond != AsmCond::IfCond &&
2529 TheCondState.TheCond != AsmCond::ElseIfCond)
2530 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2531 " an .elseif");
2532 TheCondState.TheCond = AsmCond::ElseIfCond;
2533
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002534 bool LastIgnoreState = false;
2535 if (!TheCondStack.empty())
2536 LastIgnoreState = TheCondStack.back().Ignore;
2537 if (LastIgnoreState || TheCondState.CondMet) {
2538 TheCondState.Ignore = true;
2539 EatToEndOfStatement();
2540 }
2541 else {
2542 int64_t ExprValue;
2543 if (ParseAbsoluteExpression(ExprValue))
2544 return true;
2545
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002546 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002547 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002548
Sean Callanan79ed1a82010-01-19 20:22:31 +00002549 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002550 TheCondState.CondMet = ExprValue;
2551 TheCondState.Ignore = !TheCondState.CondMet;
2552 }
2553
2554 return false;
2555}
2556
2557/// ParseDirectiveElse
2558/// ::= .else
2559bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002560 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002561 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002562
Sean Callanan79ed1a82010-01-19 20:22:31 +00002563 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002564
2565 if (TheCondState.TheCond != AsmCond::IfCond &&
2566 TheCondState.TheCond != AsmCond::ElseIfCond)
2567 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2568 ".elseif");
2569 TheCondState.TheCond = AsmCond::ElseCond;
2570 bool LastIgnoreState = false;
2571 if (!TheCondStack.empty())
2572 LastIgnoreState = TheCondStack.back().Ignore;
2573 if (LastIgnoreState || TheCondState.CondMet)
2574 TheCondState.Ignore = true;
2575 else
2576 TheCondState.Ignore = false;
2577
2578 return false;
2579}
2580
2581/// ParseDirectiveEndIf
2582/// ::= .endif
2583bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002584 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002585 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002586
Sean Callanan79ed1a82010-01-19 20:22:31 +00002587 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002588
2589 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2590 TheCondStack.empty())
2591 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2592 ".else");
2593 if (!TheCondStack.empty()) {
2594 TheCondState = TheCondStack.back();
2595 TheCondStack.pop_back();
2596 }
2597
2598 return false;
2599}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002600
2601/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002602/// ::= .file [number] filename
2603/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002604bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002605 // FIXME: I'm not sure what this is.
2606 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002607 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002608 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002609 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002610 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002611
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002612 if (FileNumber < 1)
2613 return TokError("file number less than one");
2614 }
2615
Daniel Dunbareceec052010-07-12 17:45:27 +00002616 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002617 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002618
Nick Lewycky44d798d2011-10-17 23:05:28 +00002619 // Usually the directory and filename together, otherwise just the directory.
2620 StringRef Path = getTok().getString();
2621 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002622 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002623
Nick Lewycky44d798d2011-10-17 23:05:28 +00002624 StringRef Directory;
2625 StringRef Filename;
2626 if (getLexer().is(AsmToken::String)) {
2627 if (FileNumber == -1)
2628 return TokError("explicit path specified, but no file number");
2629 Filename = getTok().getString();
2630 Filename = Filename.substr(1, Filename.size()-2);
2631 Directory = Path;
2632 Lex();
2633 } else {
2634 Filename = Path;
2635 }
2636
Daniel Dunbareceec052010-07-12 17:45:27 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002638 return TokError("unexpected token in '.file' directive");
2639
Chris Lattnerd32e8032010-01-25 19:02:58 +00002640 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002641 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002642 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002643 if (getContext().getGenDwarfForAssembly() == true)
2644 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2645 "used to generate dwarf debug info for assembly code");
2646
Nick Lewycky44d798d2011-10-17 23:05:28 +00002647 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002648 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002649 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002650
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002651 return false;
2652}
2653
2654/// ParseDirectiveLine
2655/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002656bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002657 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2658 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002659 return TokError("unexpected token in '.line' directive");
2660
Sean Callanan18b83232010-01-19 21:44:56 +00002661 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002662 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002663 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002664
2665 // FIXME: Do something with the .line.
2666 }
2667
Daniel Dunbareceec052010-07-12 17:45:27 +00002668 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002669 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002670
2671 return false;
2672}
2673
2674
2675/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002676/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002677/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2678/// The first number is a file number, must have been previously assigned with
2679/// a .file directive, the second number is the line number and optionally the
2680/// third number is a column position (zero if not specified). The remaining
2681/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002682bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002683
Daniel Dunbareceec052010-07-12 17:45:27 +00002684 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002685 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002686 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002687 if (FileNumber < 1)
2688 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002689 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002690 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002691 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002692
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002693 int64_t LineNumber = 0;
2694 if (getLexer().is(AsmToken::Integer)) {
2695 LineNumber = getTok().getIntVal();
2696 if (LineNumber < 1)
2697 return TokError("line number less than one in '.loc' directive");
2698 Lex();
2699 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002700
2701 int64_t ColumnPos = 0;
2702 if (getLexer().is(AsmToken::Integer)) {
2703 ColumnPos = getTok().getIntVal();
2704 if (ColumnPos < 0)
2705 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002706 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002707 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002708
Kevin Enderbyc0957932010-09-30 16:52:03 +00002709 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002710 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002711 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002712 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2713 for (;;) {
2714 if (getLexer().is(AsmToken::EndOfStatement))
2715 break;
2716
2717 StringRef Name;
2718 SMLoc Loc = getTok().getLoc();
2719 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002720 return TokError("unexpected token in '.loc' directive");
2721
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002722 if (Name == "basic_block")
2723 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2724 else if (Name == "prologue_end")
2725 Flags |= DWARF2_FLAG_PROLOGUE_END;
2726 else if (Name == "epilogue_begin")
2727 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2728 else if (Name == "is_stmt") {
2729 SMLoc Loc = getTok().getLoc();
2730 const MCExpr *Value;
2731 if (getParser().ParseExpression(Value))
2732 return true;
2733 // The expression must be the constant 0 or 1.
2734 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2735 int Value = MCE->getValue();
2736 if (Value == 0)
2737 Flags &= ~DWARF2_FLAG_IS_STMT;
2738 else if (Value == 1)
2739 Flags |= DWARF2_FLAG_IS_STMT;
2740 else
2741 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002742 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002743 else {
2744 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2745 }
2746 }
2747 else if (Name == "isa") {
2748 SMLoc Loc = getTok().getLoc();
2749 const MCExpr *Value;
2750 if (getParser().ParseExpression(Value))
2751 return true;
2752 // The expression must be a constant greater or equal to 0.
2753 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2754 int Value = MCE->getValue();
2755 if (Value < 0)
2756 return Error(Loc, "isa number less than zero");
2757 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002758 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002759 else {
2760 return Error(Loc, "isa number not a constant value");
2761 }
2762 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002763 else if (Name == "discriminator") {
2764 if (getParser().ParseAbsoluteExpression(Discriminator))
2765 return true;
2766 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002767 else {
2768 return Error(Loc, "unknown sub-directive in '.loc' directive");
2769 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002770
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002771 if (getLexer().is(AsmToken::EndOfStatement))
2772 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002773 }
2774 }
2775
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002776 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002777 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002778
2779 return false;
2780}
2781
Daniel Dunbar138abae2010-10-16 04:56:42 +00002782/// ParseDirectiveStabs
2783/// ::= .stabs string, number, number, number
2784bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2785 SMLoc DirectiveLoc) {
2786 return TokError("unsupported directive '" + Directive + "'");
2787}
2788
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002789/// ParseDirectiveCFISections
2790/// ::= .cfi_sections section [, section]
2791bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2792 SMLoc DirectiveLoc) {
2793 StringRef Name;
2794 bool EH = false;
2795 bool Debug = false;
2796
2797 if (getParser().ParseIdentifier(Name))
2798 return TokError("Expected an identifier");
2799
2800 if (Name == ".eh_frame")
2801 EH = true;
2802 else if (Name == ".debug_frame")
2803 Debug = true;
2804
2805 if (getLexer().is(AsmToken::Comma)) {
2806 Lex();
2807
2808 if (getParser().ParseIdentifier(Name))
2809 return TokError("Expected an identifier");
2810
2811 if (Name == ".eh_frame")
2812 EH = true;
2813 else if (Name == ".debug_frame")
2814 Debug = true;
2815 }
2816
2817 getStreamer().EmitCFISections(EH, Debug);
2818
2819 return false;
2820}
2821
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002822/// ParseDirectiveCFIStartProc
2823/// ::= .cfi_startproc
2824bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2825 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002826 getStreamer().EmitCFIStartProc();
2827 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002828}
2829
2830/// ParseDirectiveCFIEndProc
2831/// ::= .cfi_endproc
2832bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002833 getStreamer().EmitCFIEndProc();
2834 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002835}
2836
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002837/// ParseRegisterOrRegisterNumber - parse register name or number.
2838bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2839 SMLoc DirectiveLoc) {
2840 unsigned RegNo;
2841
Jim Grosbach6f888a82011-06-02 17:14:04 +00002842 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002843 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2844 DirectiveLoc))
2845 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002846 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002847 } else
2848 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002849
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002850 return false;
2851}
2852
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002853/// ParseDirectiveCFIDefCfa
2854/// ::= .cfi_def_cfa register, offset
2855bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2856 SMLoc DirectiveLoc) {
2857 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002858 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002859 return true;
2860
2861 if (getLexer().isNot(AsmToken::Comma))
2862 return TokError("unexpected token in directive");
2863 Lex();
2864
2865 int64_t Offset = 0;
2866 if (getParser().ParseAbsoluteExpression(Offset))
2867 return true;
2868
Rafael Espindola066c2f42011-04-12 23:59:07 +00002869 getStreamer().EmitCFIDefCfa(Register, Offset);
2870 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002871}
2872
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002873/// ParseDirectiveCFIDefCfaOffset
2874/// ::= .cfi_def_cfa_offset offset
2875bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2876 SMLoc DirectiveLoc) {
2877 int64_t Offset = 0;
2878 if (getParser().ParseAbsoluteExpression(Offset))
2879 return true;
2880
Rafael Espindola066c2f42011-04-12 23:59:07 +00002881 getStreamer().EmitCFIDefCfaOffset(Offset);
2882 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002883}
2884
2885/// ParseDirectiveCFIAdjustCfaOffset
2886/// ::= .cfi_adjust_cfa_offset adjustment
2887bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2888 SMLoc DirectiveLoc) {
2889 int64_t Adjustment = 0;
2890 if (getParser().ParseAbsoluteExpression(Adjustment))
2891 return true;
2892
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002893 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2894 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002895}
2896
2897/// ParseDirectiveCFIDefCfaRegister
2898/// ::= .cfi_def_cfa_register register
2899bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2900 SMLoc DirectiveLoc) {
2901 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002902 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002903 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002904
Rafael Espindola066c2f42011-04-12 23:59:07 +00002905 getStreamer().EmitCFIDefCfaRegister(Register);
2906 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002907}
2908
2909/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002910/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002911bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2912 int64_t Register = 0;
2913 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002914
2915 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002916 return true;
2917
2918 if (getLexer().isNot(AsmToken::Comma))
2919 return TokError("unexpected token in directive");
2920 Lex();
2921
2922 if (getParser().ParseAbsoluteExpression(Offset))
2923 return true;
2924
Rafael Espindola066c2f42011-04-12 23:59:07 +00002925 getStreamer().EmitCFIOffset(Register, Offset);
2926 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002927}
2928
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002929/// ParseDirectiveCFIRelOffset
2930/// ::= .cfi_rel_offset register, offset
2931bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2932 SMLoc DirectiveLoc) {
2933 int64_t Register = 0;
2934
2935 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2936 return true;
2937
2938 if (getLexer().isNot(AsmToken::Comma))
2939 return TokError("unexpected token in directive");
2940 Lex();
2941
2942 int64_t Offset = 0;
2943 if (getParser().ParseAbsoluteExpression(Offset))
2944 return true;
2945
Rafael Espindola25f492e2011-04-12 16:12:03 +00002946 getStreamer().EmitCFIRelOffset(Register, Offset);
2947 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002948}
2949
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002950static bool isValidEncoding(int64_t Encoding) {
2951 if (Encoding & ~0xff)
2952 return false;
2953
2954 if (Encoding == dwarf::DW_EH_PE_omit)
2955 return true;
2956
2957 const unsigned Format = Encoding & 0xf;
2958 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2959 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2960 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2961 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2962 return false;
2963
Rafael Espindolacaf11582010-12-29 04:31:26 +00002964 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002965 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002966 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002967 return false;
2968
2969 return true;
2970}
2971
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002972/// ParseDirectiveCFIPersonalityOrLsda
2973/// ::= .cfi_personality encoding, [symbol_name]
2974/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002975bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002976 SMLoc DirectiveLoc) {
2977 int64_t Encoding = 0;
2978 if (getParser().ParseAbsoluteExpression(Encoding))
2979 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002980 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002981 return false;
2982
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002983 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002984 return TokError("unsupported encoding.");
2985
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002986 if (getLexer().isNot(AsmToken::Comma))
2987 return TokError("unexpected token in directive");
2988 Lex();
2989
2990 StringRef Name;
2991 if (getParser().ParseIdentifier(Name))
2992 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002993
2994 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2995
2996 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002997 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002998 else {
2999 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00003000 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00003001 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00003002 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00003003}
3004
Rafael Espindolafe024d02010-12-28 18:36:23 +00003005/// ParseDirectiveCFIRememberState
3006/// ::= .cfi_remember_state
3007bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
3008 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003009 getStreamer().EmitCFIRememberState();
3010 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003011}
3012
3013/// ParseDirectiveCFIRestoreState
3014/// ::= .cfi_remember_state
3015bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
3016 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003017 getStreamer().EmitCFIRestoreState();
3018 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003019}
3020
Rafael Espindolac5754392011-04-12 15:31:05 +00003021/// ParseDirectiveCFISameValue
3022/// ::= .cfi_same_value register
3023bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3024 SMLoc DirectiveLoc) {
3025 int64_t Register = 0;
3026
3027 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3028 return true;
3029
3030 getStreamer().EmitCFISameValue(Register);
3031
3032 return false;
3033}
3034
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003035/// ParseDirectiveCFIRestore
3036/// ::= .cfi_restore register
3037bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003038 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003039 int64_t Register = 0;
3040 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3041 return true;
3042
3043 getStreamer().EmitCFIRestore(Register);
3044
3045 return false;
3046}
3047
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003048/// ParseDirectiveCFIEscape
3049/// ::= .cfi_escape expression[,...]
3050bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003051 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003052 std::string Values;
3053 int64_t CurrValue;
3054 if (getParser().ParseAbsoluteExpression(CurrValue))
3055 return true;
3056
3057 Values.push_back((uint8_t)CurrValue);
3058
3059 while (getLexer().is(AsmToken::Comma)) {
3060 Lex();
3061
3062 if (getParser().ParseAbsoluteExpression(CurrValue))
3063 return true;
3064
3065 Values.push_back((uint8_t)CurrValue);
3066 }
3067
3068 getStreamer().EmitCFIEscape(Values);
3069 return false;
3070}
3071
Rafael Espindola16d7d432012-01-23 21:51:52 +00003072/// ParseDirectiveCFISignalFrame
3073/// ::= .cfi_signal_frame
3074bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3075 SMLoc DirectiveLoc) {
3076 if (getLexer().isNot(AsmToken::EndOfStatement))
3077 return Error(getLexer().getLoc(),
3078 "unexpected token in '" + Directive + "' directive");
3079
3080 getStreamer().EmitCFISignalFrame();
3081
3082 return false;
3083}
3084
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003085/// ParseDirectiveMacrosOnOff
3086/// ::= .macros_on
3087/// ::= .macros_off
3088bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3089 SMLoc DirectiveLoc) {
3090 if (getLexer().isNot(AsmToken::EndOfStatement))
3091 return Error(getLexer().getLoc(),
3092 "unexpected token in '" + Directive + "' directive");
3093
3094 getParser().MacrosEnabled = Directive == ".macros_on";
3095
3096 return false;
3097}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003098
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003099/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003100/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003101bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3102 SMLoc DirectiveLoc) {
3103 StringRef Name;
3104 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003105 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003106
Rafael Espindola8a403d32012-08-08 14:51:03 +00003107 MacroParameters Parameters;
Rafael Espindola65366442011-06-05 02:43:45 +00003108 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003109 for (;;) {
3110 MacroParameter Parameter;
Preston Gurd6c9176a2012-09-19 20:29:04 +00003111 if (getParser().ParseIdentifier(Parameter.first))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003112 return TokError("expected identifier in '.macro' directive");
Preston Gurd6c9176a2012-09-19 20:29:04 +00003113
3114 if (getLexer().is(AsmToken::Equal)) {
3115 Lex();
3116 if (getParser().ParseMacroArgument(Parameter.second))
3117 return true;
3118 }
3119
Rafael Espindola65366442011-06-05 02:43:45 +00003120 Parameters.push_back(Parameter);
3121
3122 if (getLexer().isNot(AsmToken::Comma))
3123 break;
3124 Lex();
3125 }
3126 }
3127
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003128 if (getLexer().isNot(AsmToken::EndOfStatement))
3129 return TokError("unexpected token in '.macro' directive");
3130
3131 // Eat the end of statement.
3132 Lex();
3133
3134 AsmToken EndToken, StartToken = getTok();
3135
3136 // Lex the macro definition.
3137 for (;;) {
3138 // Check whether we have reached the end of the file.
3139 if (getLexer().is(AsmToken::Eof))
3140 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3141
3142 // Otherwise, check whether we have reach the .endmacro.
3143 if (getLexer().is(AsmToken::Identifier) &&
3144 (getTok().getIdentifier() == ".endm" ||
3145 getTok().getIdentifier() == ".endmacro")) {
3146 EndToken = getTok();
3147 Lex();
3148 if (getLexer().isNot(AsmToken::EndOfStatement))
3149 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3150 "' directive");
3151 break;
3152 }
3153
3154 // Otherwise, scan til the end of the statement.
3155 getParser().EatToEndOfStatement();
3156 }
3157
3158 if (getParser().MacroMap.lookup(Name)) {
3159 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3160 }
3161
3162 const char *BodyStart = StartToken.getLoc().getPointer();
3163 const char *BodyEnd = EndToken.getLoc().getPointer();
3164 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003165 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003166 return false;
3167}
3168
3169/// ParseDirectiveEndMacro
3170/// ::= .endm
3171/// ::= .endmacro
3172bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003173 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003174 if (getLexer().isNot(AsmToken::EndOfStatement))
3175 return TokError("unexpected token in '" + Directive + "' directive");
3176
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003177 // If we are inside a macro instantiation, terminate the current
3178 // instantiation.
3179 if (!getParser().ActiveMacros.empty()) {
3180 getParser().HandleMacroExit();
3181 return false;
3182 }
3183
3184 // Otherwise, this .endmacro is a stray entry in the file; well formed
3185 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003186 return TokError("unexpected '" + Directive + "' in file, "
3187 "no current macro definition");
3188}
3189
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003190/// ParseDirectivePurgeMacro
3191/// ::= .purgem
3192bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3193 SMLoc DirectiveLoc) {
3194 StringRef Name;
3195 if (getParser().ParseIdentifier(Name))
3196 return TokError("expected identifier in '.purgem' directive");
3197
3198 if (getLexer().isNot(AsmToken::EndOfStatement))
3199 return TokError("unexpected token in '.purgem' directive");
3200
3201 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3202 if (I == getParser().MacroMap.end())
3203 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3204
3205 // Undefine the macro.
3206 delete I->getValue();
3207 getParser().MacroMap.erase(I);
3208 return false;
3209}
3210
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003211bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003212 getParser().CheckForValidSection();
3213
3214 const MCExpr *Value;
3215
3216 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003217 return true;
3218
3219 if (getLexer().isNot(AsmToken::EndOfStatement))
3220 return TokError("unexpected token in directive");
3221
3222 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003223 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003224 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003225 getStreamer().EmitULEB128Value(Value);
3226
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003227 return false;
3228}
3229
Rafael Espindola761cb062012-06-03 23:57:14 +00003230Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003231 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003232
Rafael Espindola761cb062012-06-03 23:57:14 +00003233 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003234 for (;;) {
3235 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003236 if (getLexer().is(AsmToken::Eof)) {
3237 Error(DirectiveLoc, "no matching '.endr' in definition");
3238 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003239 }
3240
Rafael Espindola761cb062012-06-03 23:57:14 +00003241 if (Lexer.is(AsmToken::Identifier) &&
3242 (getTok().getIdentifier() == ".rept")) {
3243 ++NestLevel;
3244 }
3245
3246 // Otherwise, check whether we have reached the .endr.
3247 if (Lexer.is(AsmToken::Identifier) &&
3248 getTok().getIdentifier() == ".endr") {
3249 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003250 EndToken = getTok();
3251 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003252 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3253 TokError("unexpected token in '.endr' directive");
3254 return 0;
3255 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003256 break;
3257 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003258 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003259 }
3260
Rafael Espindola761cb062012-06-03 23:57:14 +00003261 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003262 EatToEndOfStatement();
3263 }
3264
3265 const char *BodyStart = StartToken.getLoc().getPointer();
3266 const char *BodyEnd = EndToken.getLoc().getPointer();
3267 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3268
Rafael Espindola761cb062012-06-03 23:57:14 +00003269 // We Are Anonymous.
3270 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003271 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003272 return new Macro(Name, Body, Parameters);
3273}
3274
3275void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3276 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003277 OS << ".endr\n";
3278
3279 MemoryBuffer *Instantiation =
3280 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3281
Rafael Espindola761cb062012-06-03 23:57:14 +00003282 // Create the macro instantiation object and add to the current macro
3283 // instantiation stack.
3284 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3285 getTok().getLoc(),
3286 Instantiation);
3287 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003288
Rafael Espindola761cb062012-06-03 23:57:14 +00003289 // Jump to the macro instantiation and prime the lexer.
3290 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3291 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3292 Lex();
3293}
3294
3295bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3296 int64_t Count;
3297 if (ParseAbsoluteExpression(Count))
3298 return TokError("unexpected token in '.rept' directive");
3299
3300 if (Count < 0)
3301 return TokError("Count is negative");
3302
3303 if (Lexer.isNot(AsmToken::EndOfStatement))
3304 return TokError("unexpected token in '.rept' directive");
3305
3306 // Eat the end of statement.
3307 Lex();
3308
3309 // Lex the rept definition.
3310 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3311 if (!M)
3312 return true;
3313
3314 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3315 // to hold the macro body with substitutions.
3316 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003317 MacroParameters Parameters;
3318 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003319 raw_svector_ostream OS(Buf);
3320 while (Count--) {
3321 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3322 return true;
3323 }
3324 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003325
3326 return false;
3327}
3328
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003329/// ParseDirectiveIrp
3330/// ::= .irp symbol,values
3331bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003332 MacroParameters Parameters;
3333 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003334
Preston Gurd6c9176a2012-09-19 20:29:04 +00003335 if (ParseIdentifier(Parameter.first))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003336 return TokError("expected identifier in '.irp' directive");
3337
3338 Parameters.push_back(Parameter);
3339
3340 if (Lexer.isNot(AsmToken::Comma))
3341 return TokError("expected comma in '.irp' directive");
3342
3343 Lex();
3344
Rafael Espindola8a403d32012-08-08 14:51:03 +00003345 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003346 if (ParseMacroArguments(0, A))
3347 return true;
3348
3349 // Eat the end of statement.
3350 Lex();
3351
3352 // Lex the irp definition.
3353 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3354 if (!M)
3355 return true;
3356
3357 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3358 // to hold the macro body with substitutions.
3359 SmallString<256> Buf;
3360 raw_svector_ostream OS(Buf);
3361
Rafael Espindola7996d042012-08-21 16:06:48 +00003362 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3363 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003364 Args.push_back(*i);
3365
3366 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3367 return true;
3368 }
3369
3370 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3371
3372 return false;
3373}
3374
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003375/// ParseDirectiveIrpc
3376/// ::= .irpc symbol,values
3377bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003378 MacroParameters Parameters;
3379 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003380
Preston Gurd6c9176a2012-09-19 20:29:04 +00003381 if (ParseIdentifier(Parameter.first))
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003382 return TokError("expected identifier in '.irpc' directive");
3383
3384 Parameters.push_back(Parameter);
3385
3386 if (Lexer.isNot(AsmToken::Comma))
3387 return TokError("expected comma in '.irpc' directive");
3388
3389 Lex();
3390
Rafael Espindola8a403d32012-08-08 14:51:03 +00003391 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003392 if (ParseMacroArguments(0, A))
3393 return true;
3394
3395 if (A.size() != 1 || A.front().size() != 1)
3396 return TokError("unexpected token in '.irpc' directive");
3397
3398 // Eat the end of statement.
3399 Lex();
3400
3401 // Lex the irpc definition.
3402 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3403 if (!M)
3404 return true;
3405
3406 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3407 // to hold the macro body with substitutions.
3408 SmallString<256> Buf;
3409 raw_svector_ostream OS(Buf);
3410
3411 StringRef Values = A.front().front().getString();
3412 std::size_t I, End = Values.size();
3413 for (I = 0; I < End; ++I) {
3414 MacroArgument Arg;
3415 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3416
Rafael Espindola8a403d32012-08-08 14:51:03 +00003417 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003418 Args.push_back(Arg);
3419
3420 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3421 return true;
3422 }
3423
3424 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3425
3426 return false;
3427}
3428
Rafael Espindola761cb062012-06-03 23:57:14 +00003429bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3430 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003431 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003432
3433 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003434 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003435 assert(getLexer().is(AsmToken::EndOfStatement));
3436
Rafael Espindola761cb062012-06-03 23:57:14 +00003437 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003438 return false;
3439}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003440
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003441/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003442MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003443 MCContext &C, MCStreamer &Out,
3444 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003445 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003446}