blob: ca338abed4e46bf6af9f9438a5c4eb992070b388 [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;
50typedef StringRef MacroParameter;
51typedef 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)
1537 if (Parameters[Index] == Argument)
1538 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
Jim Grosbach97146442012-07-30 22:44:17 +00001609 A.push_back(MA);
1610
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001611 if (Lexer.is(AsmToken::EndOfStatement))
1612 return false;
1613
1614 if (Lexer.is(AsmToken::Comma))
1615 Lex();
1616 }
1617 return TokError("Too many arguments");
1618}
1619
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001620bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1621 const Macro *M) {
1622 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1623 // this, although we should protect against infinite loops.
1624 if (ActiveMacros.size() == 20)
1625 return TokError("macros cannot be nested more than 20 levels deep");
1626
Rafael Espindola8a403d32012-08-08 14:51:03 +00001627 MacroArguments A;
1628 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001629 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001630
Jim Grosbach97146442012-07-30 22:44:17 +00001631 // Remove any trailing empty arguments. Do this after-the-fact as we have
1632 // to keep empty arguments in the middle of the list or positionality
1633 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001634 while (!A.empty() && A.back().empty())
1635 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001636
Rafael Espindola65366442011-06-05 02:43:45 +00001637 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1638 // to hold the macro body with substitutions.
1639 SmallString<256> Buf;
1640 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001641 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001642
Rafael Espindola8a403d32012-08-08 14:51:03 +00001643 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001644 return true;
1645
Rafael Espindola761cb062012-06-03 23:57:14 +00001646 // We include the .endmacro in the buffer as our queue to exit the macro
1647 // instantiation.
1648 OS << ".endmacro\n";
1649
Rafael Espindola65366442011-06-05 02:43:45 +00001650 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001651 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001652
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001653 // Create the macro instantiation object and add to the current macro
1654 // instantiation stack.
1655 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001656 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001657 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001658 ActiveMacros.push_back(MI);
1659
1660 // Jump to the macro instantiation and prime the lexer.
1661 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1662 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1663 Lex();
1664
1665 return false;
1666}
1667
1668void AsmParser::HandleMacroExit() {
1669 // Jump to the EndOfStatement we should return to, and consume it.
1670 JumpToLoc(ActiveMacros.back()->ExitLoc);
1671 Lex();
1672
1673 // Pop the instantiation entry.
1674 delete ActiveMacros.back();
1675 ActiveMacros.pop_back();
1676}
1677
Rafael Espindolae71cc862012-01-28 05:57:00 +00001678static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001679 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001680 case MCExpr::Binary: {
1681 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1682 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001683 break;
1684 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001685 case MCExpr::Target:
1686 case MCExpr::Constant:
1687 return false;
1688 case MCExpr::SymbolRef: {
1689 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001690 if (S.isVariable())
1691 return IsUsedIn(Sym, S.getVariableValue());
1692 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001693 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001694 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001695 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001696 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001697
1698 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001699}
1700
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001701bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef,
1702 bool NoDeadStrip) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001703 // FIXME: Use better location, we should use proper tokens.
1704 SMLoc EqualLoc = Lexer.getLoc();
1705
Daniel Dunbar821e3332009-08-31 08:09:28 +00001706 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001707 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001708 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001709
Rafael Espindolae71cc862012-01-28 05:57:00 +00001710 // Note: we don't count b as used in "a = b". This is to allow
1711 // a = b
1712 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001713
Daniel Dunbar3f872332009-07-28 16:08:33 +00001714 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001715 return TokError("unexpected token in assignment");
1716
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001717 // Error on assignment to '.'.
1718 if (Name == ".") {
1719 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1720 "(use '.space' or '.org').)"));
1721 }
1722
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001723 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001724 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001725
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001726 // Validate that the LHS is allowed to be a variable (either it has not been
1727 // used as a symbol, or it is an absolute symbol).
1728 MCSymbol *Sym = getContext().LookupSymbol(Name);
1729 if (Sym) {
1730 // Diagnose assignment to a label.
1731 //
1732 // FIXME: Diagnostics. Note the location of the definition as a label.
1733 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001734 if (IsUsedIn(Sym, Value))
1735 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1736 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001737 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001738 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1739 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001740 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001741 return Error(EqualLoc, "redefinition of '" + Name + "'");
1742 else if (!Sym->isVariable())
1743 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001744 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001745 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1746 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001747
1748 // Don't count these checks as uses.
1749 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001750 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001751 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001752
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001753 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001754
1755 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001756 Out.EmitAssignment(Sym, Value);
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001757 if (NoDeadStrip)
1758 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip);
1759
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001760
1761 return false;
1762}
1763
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001764/// ParseIdentifier:
1765/// ::= identifier
1766/// ::= string
1767bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001768 // The assembler has relaxed rules for accepting identifiers, in particular we
1769 // allow things like '.globl $foo', which would normally be separate
1770 // tokens. At this level, we have already lexed so we cannot (currently)
1771 // handle this as a context dependent token, instead we detect adjacent tokens
1772 // and return the combined identifier.
1773 if (Lexer.is(AsmToken::Dollar)) {
1774 SMLoc DollarLoc = getLexer().getLoc();
1775
1776 // Consume the dollar sign, and check for a following identifier.
1777 Lex();
1778 if (Lexer.isNot(AsmToken::Identifier))
1779 return true;
1780
1781 // We have a '$' followed by an identifier, make sure they are adjacent.
1782 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1783 return true;
1784
1785 // Construct the joined identifier and consume the token.
1786 Res = StringRef(DollarLoc.getPointer(),
1787 getTok().getIdentifier().size() + 1);
1788 Lex();
1789 return false;
1790 }
1791
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001792 if (Lexer.isNot(AsmToken::Identifier) &&
1793 Lexer.isNot(AsmToken::String))
1794 return true;
1795
Sean Callanan18b83232010-01-19 21:44:56 +00001796 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001797
Sean Callanan79ed1a82010-01-19 20:22:31 +00001798 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001799
1800 return false;
1801}
1802
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001803/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001804/// ::= .equ identifier ',' expression
1805/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001806/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001807bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001808 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001809
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001810 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001811 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001812
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001813 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001814 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001815 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001816
Jim Grosbach3f90a4c2012-09-13 23:11:31 +00001817 return ParseAssignment(Name, allow_redef, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001818}
1819
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001820bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001821 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001822
1823 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001824 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001825 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1826 if (Str[i] != '\\') {
1827 Data += Str[i];
1828 continue;
1829 }
1830
1831 // Recognize escaped characters. Note that this escape semantics currently
1832 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1833 ++i;
1834 if (i == e)
1835 return TokError("unexpected backslash at end of string");
1836
1837 // Recognize octal sequences.
1838 if ((unsigned) (Str[i] - '0') <= 7) {
1839 // Consume up to three octal characters.
1840 unsigned Value = Str[i] - '0';
1841
1842 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1843 ++i;
1844 Value = Value * 8 + (Str[i] - '0');
1845
1846 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1847 ++i;
1848 Value = Value * 8 + (Str[i] - '0');
1849 }
1850 }
1851
1852 if (Value > 255)
1853 return TokError("invalid octal escape sequence (out of range)");
1854
1855 Data += (unsigned char) Value;
1856 continue;
1857 }
1858
1859 // Otherwise recognize individual escapes.
1860 switch (Str[i]) {
1861 default:
1862 // Just reject invalid escape sequences for now.
1863 return TokError("invalid escape sequence (unrecognized character)");
1864
1865 case 'b': Data += '\b'; break;
1866 case 'f': Data += '\f'; break;
1867 case 'n': Data += '\n'; break;
1868 case 'r': Data += '\r'; break;
1869 case 't': Data += '\t'; break;
1870 case '"': Data += '"'; break;
1871 case '\\': Data += '\\'; break;
1872 }
1873 }
1874
1875 return false;
1876}
1877
Daniel Dunbara0d14262009-06-24 23:30:00 +00001878/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001879/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1880bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001881 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001882 CheckForValidSection();
1883
Daniel Dunbara0d14262009-06-24 23:30:00 +00001884 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001885 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001886 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001887
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001888 std::string Data;
1889 if (ParseEscapedString(Data))
1890 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001891
1892 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001893 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001894 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1895
Sean Callanan79ed1a82010-01-19 20:22:31 +00001896 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001897
1898 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001899 break;
1900
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001901 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001902 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001903 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001904 }
1905 }
1906
Sean Callanan79ed1a82010-01-19 20:22:31 +00001907 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001908 return false;
1909}
1910
1911/// ParseDirectiveValue
1912/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1913bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001914 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001915 CheckForValidSection();
1916
Daniel Dunbara0d14262009-06-24 23:30:00 +00001917 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001918 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001919 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001920 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001921 return true;
1922
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001923 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001924 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1925 assert(Size <= 8 && "Invalid size");
1926 uint64_t IntValue = MCE->getValue();
1927 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1928 return Error(ExprLoc, "literal value out of range for directive");
1929 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1930 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001931 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001932
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001933 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001934 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001935
Daniel Dunbara0d14262009-06-24 23:30:00 +00001936 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001937 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001938 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001939 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001940 }
1941 }
1942
Sean Callanan79ed1a82010-01-19 20:22:31 +00001943 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001944 return false;
1945}
1946
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001947/// ParseDirectiveRealValue
1948/// ::= (.single | .double) [ expression (, expression)* ]
1949bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1950 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1951 CheckForValidSection();
1952
1953 for (;;) {
1954 // We don't truly support arithmetic on floating point expressions, so we
1955 // have to manually parse unary prefixes.
1956 bool IsNeg = false;
1957 if (getLexer().is(AsmToken::Minus)) {
1958 Lex();
1959 IsNeg = true;
1960 } else if (getLexer().is(AsmToken::Plus))
1961 Lex();
1962
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001963 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001964 getLexer().isNot(AsmToken::Real) &&
1965 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001966 return TokError("unexpected token in directive");
1967
1968 // Convert to an APFloat.
1969 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001970 StringRef IDVal = getTok().getString();
1971 if (getLexer().is(AsmToken::Identifier)) {
1972 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1973 Value = APFloat::getInf(Semantics);
1974 else if (!IDVal.compare_lower("nan"))
1975 Value = APFloat::getNaN(Semantics, false, ~0);
1976 else
1977 return TokError("invalid floating point literal");
1978 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001979 APFloat::opInvalidOp)
1980 return TokError("invalid floating point literal");
1981 if (IsNeg)
1982 Value.changeSign();
1983
1984 // Consume the numeric token.
1985 Lex();
1986
1987 // Emit the value as an integer.
1988 APInt AsInt = Value.bitcastToAPInt();
1989 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1990 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1991
1992 if (getLexer().is(AsmToken::EndOfStatement))
1993 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001994
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001995 if (getLexer().isNot(AsmToken::Comma))
1996 return TokError("unexpected token in directive");
1997 Lex();
1998 }
1999 }
2000
2001 Lex();
2002 return false;
2003}
2004
Daniel Dunbara0d14262009-06-24 23:30:00 +00002005/// ParseDirectiveSpace
2006/// ::= .space expression [ , expression ]
2007bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002008 CheckForValidSection();
2009
Daniel Dunbara0d14262009-06-24 23:30:00 +00002010 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002011 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002012 return true;
2013
2014 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002015 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2016 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002017 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002018 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002019
Daniel Dunbar475839e2009-06-29 20:37:27 +00002020 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002021 return true;
2022
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002023 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002024 return TokError("unexpected token in '.space' directive");
2025 }
2026
Sean Callanan79ed1a82010-01-19 20:22:31 +00002027 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002028
2029 if (NumBytes <= 0)
2030 return TokError("invalid number of bytes in '.space' directive");
2031
2032 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002033 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002034
2035 return false;
2036}
2037
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002038/// ParseDirectiveZero
2039/// ::= .zero expression
2040bool AsmParser::ParseDirectiveZero() {
2041 CheckForValidSection();
2042
2043 int64_t NumBytes;
2044 if (ParseAbsoluteExpression(NumBytes))
2045 return true;
2046
Rafael Espindolae452b172010-10-05 19:42:57 +00002047 int64_t Val = 0;
2048 if (getLexer().is(AsmToken::Comma)) {
2049 Lex();
2050 if (ParseAbsoluteExpression(Val))
2051 return true;
2052 }
2053
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002054 if (getLexer().isNot(AsmToken::EndOfStatement))
2055 return TokError("unexpected token in '.zero' directive");
2056
2057 Lex();
2058
Rafael Espindolae452b172010-10-05 19:42:57 +00002059 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002060
2061 return false;
2062}
2063
Daniel Dunbara0d14262009-06-24 23:30:00 +00002064/// ParseDirectiveFill
2065/// ::= .fill expression , expression , expression
2066bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002067 CheckForValidSection();
2068
Daniel Dunbara0d14262009-06-24 23:30:00 +00002069 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002070 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002071 return true;
2072
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002073 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002074 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002075 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002076
Daniel Dunbara0d14262009-06-24 23:30:00 +00002077 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002078 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002079 return true;
2080
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002081 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002082 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002083 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002084
Daniel Dunbara0d14262009-06-24 23:30:00 +00002085 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002086 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002087 return true;
2088
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002089 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002090 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002091
Sean Callanan79ed1a82010-01-19 20:22:31 +00002092 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002093
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002094 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2095 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002096
2097 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002098 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002099
2100 return false;
2101}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002102
2103/// ParseDirectiveOrg
2104/// ::= .org expression [ , expression ]
2105bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002106 CheckForValidSection();
2107
Daniel Dunbar821e3332009-08-31 08:09:28 +00002108 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002109 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002110 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002111 return true;
2112
2113 // Parse optional fill expression.
2114 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002115 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2116 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002117 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002118 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002119
Daniel Dunbar475839e2009-06-29 20:37:27 +00002120 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002121 return true;
2122
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002123 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002124 return TokError("unexpected token in '.org' directive");
2125 }
2126
Sean Callanan79ed1a82010-01-19 20:22:31 +00002127 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002128
Jim Grosbachebd4c052012-01-27 00:37:08 +00002129 // Only limited forms of relocatable expressions are accepted here, it
2130 // has to be relative to the current section. The streamer will return
2131 // 'true' if the expression wasn't evaluatable.
2132 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2133 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002134
2135 return false;
2136}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002137
2138/// ParseDirectiveAlign
2139/// ::= {.align, ...} expression [ , expression [ , expression ]]
2140bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002141 CheckForValidSection();
2142
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002143 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002144 int64_t Alignment;
2145 if (ParseAbsoluteExpression(Alignment))
2146 return true;
2147
2148 SMLoc MaxBytesLoc;
2149 bool HasFillExpr = false;
2150 int64_t FillExpr = 0;
2151 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002152 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2153 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002154 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002155 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002156
2157 // The fill expression can be omitted while specifying a maximum number of
2158 // alignment bytes, e.g:
2159 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002160 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002161 HasFillExpr = true;
2162 if (ParseAbsoluteExpression(FillExpr))
2163 return true;
2164 }
2165
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002166 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2167 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002168 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002169 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002170
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002171 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002172 if (ParseAbsoluteExpression(MaxBytesToFill))
2173 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002174
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002175 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002176 return TokError("unexpected token in directive");
2177 }
2178 }
2179
Sean Callanan79ed1a82010-01-19 20:22:31 +00002180 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002181
Daniel Dunbar648ac512010-05-17 21:54:30 +00002182 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002183 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002184
2185 // Compute alignment in bytes.
2186 if (IsPow2) {
2187 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002188 if (Alignment >= 32) {
2189 Error(AlignmentLoc, "invalid alignment value");
2190 Alignment = 31;
2191 }
2192
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002193 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002194 }
2195
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002196 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002197 if (MaxBytesLoc.isValid()) {
2198 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002199 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2200 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002201 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002202 }
2203
2204 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002205 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2206 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002207 MaxBytesToFill = 0;
2208 }
2209 }
2210
Daniel Dunbar648ac512010-05-17 21:54:30 +00002211 // Check whether we should use optimal code alignment for this .align
2212 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002213 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002214 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2215 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002216 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002217 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002218 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002219 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2220 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002221 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002222
2223 return false;
2224}
2225
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002226/// ParseDirectiveSymbolAttribute
2227/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002228bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002229 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002230 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002231 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002232 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002233
2234 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002235 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002236
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002237 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002238
Jim Grosbach10ec6502011-09-15 17:56:49 +00002239 // Assembler local symbols don't make any sense here. Complain loudly.
2240 if (Sym->isTemporary())
2241 return Error(Loc, "non-local symbol required in directive");
2242
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002243 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002244
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002245 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002246 break;
2247
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002248 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002249 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002250 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002251 }
2252 }
2253
Sean Callanan79ed1a82010-01-19 20:22:31 +00002254 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002255 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002256}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002257
2258/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002259/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2260bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002261 CheckForValidSection();
2262
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002263 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002264 StringRef Name;
2265 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002266 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002267
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002268 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002269 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002270
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002271 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002272 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002273 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002274
2275 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002276 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002277 if (ParseAbsoluteExpression(Size))
2278 return true;
2279
2280 int64_t Pow2Alignment = 0;
2281 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002282 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002283 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002284 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002285 if (ParseAbsoluteExpression(Pow2Alignment))
2286 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002287
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002288 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
2289 if (IsLocal && LCOMM == LCOMM::NoAlignment)
Benjamin Kramer39646d92012-09-07 17:25:13 +00002290 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
2291
Chris Lattner258281d2010-01-19 06:22:22 +00002292 // If this target takes alignments in bytes (not log) validate and convert.
Benjamin Kramera9e37c52012-09-07 21:08:01 +00002293 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
2294 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
Chris Lattner258281d2010-01-19 06:22:22 +00002295 if (!isPowerOf2_64(Pow2Alignment))
2296 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2297 Pow2Alignment = Log2_64(Pow2Alignment);
2298 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002299 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002300
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002301 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002302 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002303
Sean Callanan79ed1a82010-01-19 20:22:31 +00002304 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002305
Chris Lattner1fc3d752009-07-09 17:25:12 +00002306 // NOTE: a size of zero for a .comm should create a undefined symbol
2307 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002308 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002309 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2310 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002311
Eric Christopherc260a3e2010-05-14 01:38:54 +00002312 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002313 // may internally end up wanting an alignment in bytes.
2314 // FIXME: Diagnose overflow.
2315 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002316 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2317 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002318
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002319 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002320 return Error(IDLoc, "invalid symbol redefinition");
2321
Chris Lattner1fc3d752009-07-09 17:25:12 +00002322 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002323 if (IsLocal) {
Benjamin Kramer39646d92012-09-07 17:25:13 +00002324 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002325 return false;
2326 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002327
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002328 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002329 return false;
2330}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002331
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002332/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002333/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002334bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002335 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002336 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002337
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002338 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002339 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002340 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002341
Sean Callanan79ed1a82010-01-19 20:22:31 +00002342 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002343
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002344 if (Str.empty())
2345 Error(Loc, ".abort detected. Assembly stopping.");
2346 else
2347 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002348 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002349
2350 return false;
2351}
Kevin Enderby71148242009-07-14 21:35:03 +00002352
Kevin Enderby1f049b22009-07-14 23:21:55 +00002353/// ParseDirectiveInclude
2354/// ::= .include "filename"
2355bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002356 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002357 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002358
Sean Callanan18b83232010-01-19 21:44:56 +00002359 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002360 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002361 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002362
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002363 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002364 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002365
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002366 // Strip the quotes.
2367 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002368
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002369 // Attempt to switch the lexer to the included file before consuming the end
2370 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002371 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002372 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002373 return true;
2374 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002375
2376 return false;
2377}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002378
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002379/// ParseDirectiveIncbin
2380/// ::= .incbin "filename"
2381bool AsmParser::ParseDirectiveIncbin() {
2382 if (getLexer().isNot(AsmToken::String))
2383 return TokError("expected string in '.incbin' directive");
2384
2385 std::string Filename = getTok().getString();
2386 SMLoc IncbinLoc = getLexer().getLoc();
2387 Lex();
2388
2389 if (getLexer().isNot(AsmToken::EndOfStatement))
2390 return TokError("unexpected token in '.incbin' directive");
2391
2392 // Strip the quotes.
2393 Filename = Filename.substr(1, Filename.size()-2);
2394
2395 // Attempt to process the included file.
2396 if (ProcessIncbinFile(Filename)) {
2397 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2398 return true;
2399 }
2400
2401 return false;
2402}
2403
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002404/// ParseDirectiveIf
2405/// ::= .if expression
2406bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002407 TheCondStack.push_back(TheCondState);
2408 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002409 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002410 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002411 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002412 int64_t ExprValue;
2413 if (ParseAbsoluteExpression(ExprValue))
2414 return true;
2415
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002416 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002417 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002418
Sean Callanan79ed1a82010-01-19 20:22:31 +00002419 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002420
2421 TheCondState.CondMet = ExprValue;
2422 TheCondState.Ignore = !TheCondState.CondMet;
2423 }
2424
2425 return false;
2426}
2427
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002428/// ParseDirectiveIfb
2429/// ::= .ifb string
2430bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2431 TheCondStack.push_back(TheCondState);
2432 TheCondState.TheCond = AsmCond::IfCond;
2433
Benjamin Kramer29739e72012-05-12 16:52:21 +00002434 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002435 EatToEndOfStatement();
2436 } else {
2437 StringRef Str = ParseStringToEndOfStatement();
2438
2439 if (getLexer().isNot(AsmToken::EndOfStatement))
2440 return TokError("unexpected token in '.ifb' directive");
2441
2442 Lex();
2443
2444 TheCondState.CondMet = ExpectBlank == Str.empty();
2445 TheCondState.Ignore = !TheCondState.CondMet;
2446 }
2447
2448 return false;
2449}
2450
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002451/// ParseDirectiveIfc
2452/// ::= .ifc string1, string2
2453bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2454 TheCondStack.push_back(TheCondState);
2455 TheCondState.TheCond = AsmCond::IfCond;
2456
Benjamin Kramer29739e72012-05-12 16:52:21 +00002457 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002458 EatToEndOfStatement();
2459 } else {
2460 StringRef Str1 = ParseStringToComma();
2461
2462 if (getLexer().isNot(AsmToken::Comma))
2463 return TokError("unexpected token in '.ifc' directive");
2464
2465 Lex();
2466
2467 StringRef Str2 = ParseStringToEndOfStatement();
2468
2469 if (getLexer().isNot(AsmToken::EndOfStatement))
2470 return TokError("unexpected token in '.ifc' directive");
2471
2472 Lex();
2473
2474 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2475 TheCondState.Ignore = !TheCondState.CondMet;
2476 }
2477
2478 return false;
2479}
2480
2481/// ParseDirectiveIfdef
2482/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002483bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2484 StringRef Name;
2485 TheCondStack.push_back(TheCondState);
2486 TheCondState.TheCond = AsmCond::IfCond;
2487
2488 if (TheCondState.Ignore) {
2489 EatToEndOfStatement();
2490 } else {
2491 if (ParseIdentifier(Name))
2492 return TokError("expected identifier after '.ifdef'");
2493
2494 Lex();
2495
2496 MCSymbol *Sym = getContext().LookupSymbol(Name);
2497
2498 if (expect_defined)
2499 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2500 else
2501 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2502 TheCondState.Ignore = !TheCondState.CondMet;
2503 }
2504
2505 return false;
2506}
2507
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002508/// ParseDirectiveElseIf
2509/// ::= .elseif expression
2510bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2511 if (TheCondState.TheCond != AsmCond::IfCond &&
2512 TheCondState.TheCond != AsmCond::ElseIfCond)
2513 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2514 " an .elseif");
2515 TheCondState.TheCond = AsmCond::ElseIfCond;
2516
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002517 bool LastIgnoreState = false;
2518 if (!TheCondStack.empty())
2519 LastIgnoreState = TheCondStack.back().Ignore;
2520 if (LastIgnoreState || TheCondState.CondMet) {
2521 TheCondState.Ignore = true;
2522 EatToEndOfStatement();
2523 }
2524 else {
2525 int64_t ExprValue;
2526 if (ParseAbsoluteExpression(ExprValue))
2527 return true;
2528
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002529 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002530 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002531
Sean Callanan79ed1a82010-01-19 20:22:31 +00002532 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002533 TheCondState.CondMet = ExprValue;
2534 TheCondState.Ignore = !TheCondState.CondMet;
2535 }
2536
2537 return false;
2538}
2539
2540/// ParseDirectiveElse
2541/// ::= .else
2542bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002543 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002544 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002545
Sean Callanan79ed1a82010-01-19 20:22:31 +00002546 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002547
2548 if (TheCondState.TheCond != AsmCond::IfCond &&
2549 TheCondState.TheCond != AsmCond::ElseIfCond)
2550 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2551 ".elseif");
2552 TheCondState.TheCond = AsmCond::ElseCond;
2553 bool LastIgnoreState = false;
2554 if (!TheCondStack.empty())
2555 LastIgnoreState = TheCondStack.back().Ignore;
2556 if (LastIgnoreState || TheCondState.CondMet)
2557 TheCondState.Ignore = true;
2558 else
2559 TheCondState.Ignore = false;
2560
2561 return false;
2562}
2563
2564/// ParseDirectiveEndIf
2565/// ::= .endif
2566bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002567 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002568 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002569
Sean Callanan79ed1a82010-01-19 20:22:31 +00002570 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002571
2572 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2573 TheCondStack.empty())
2574 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2575 ".else");
2576 if (!TheCondStack.empty()) {
2577 TheCondState = TheCondStack.back();
2578 TheCondStack.pop_back();
2579 }
2580
2581 return false;
2582}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002583
2584/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002585/// ::= .file [number] filename
2586/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002587bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002588 // FIXME: I'm not sure what this is.
2589 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002590 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002591 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002592 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002593 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002594
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002595 if (FileNumber < 1)
2596 return TokError("file number less than one");
2597 }
2598
Daniel Dunbareceec052010-07-12 17:45:27 +00002599 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002600 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002601
Nick Lewycky44d798d2011-10-17 23:05:28 +00002602 // Usually the directory and filename together, otherwise just the directory.
2603 StringRef Path = getTok().getString();
2604 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002605 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002606
Nick Lewycky44d798d2011-10-17 23:05:28 +00002607 StringRef Directory;
2608 StringRef Filename;
2609 if (getLexer().is(AsmToken::String)) {
2610 if (FileNumber == -1)
2611 return TokError("explicit path specified, but no file number");
2612 Filename = getTok().getString();
2613 Filename = Filename.substr(1, Filename.size()-2);
2614 Directory = Path;
2615 Lex();
2616 } else {
2617 Filename = Path;
2618 }
2619
Daniel Dunbareceec052010-07-12 17:45:27 +00002620 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002621 return TokError("unexpected token in '.file' directive");
2622
Chris Lattnerd32e8032010-01-25 19:02:58 +00002623 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002624 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002625 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002626 if (getContext().getGenDwarfForAssembly() == true)
2627 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2628 "used to generate dwarf debug info for assembly code");
2629
Nick Lewycky44d798d2011-10-17 23:05:28 +00002630 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002631 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002632 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002633
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002634 return false;
2635}
2636
2637/// ParseDirectiveLine
2638/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002639bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002640 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2641 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002642 return TokError("unexpected token in '.line' directive");
2643
Sean Callanan18b83232010-01-19 21:44:56 +00002644 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002645 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002646 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002647
2648 // FIXME: Do something with the .line.
2649 }
2650
Daniel Dunbareceec052010-07-12 17:45:27 +00002651 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002652 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002653
2654 return false;
2655}
2656
2657
2658/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002659/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002660/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2661/// The first number is a file number, must have been previously assigned with
2662/// a .file directive, the second number is the line number and optionally the
2663/// third number is a column position (zero if not specified). The remaining
2664/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002665bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002666
Daniel Dunbareceec052010-07-12 17:45:27 +00002667 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002668 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002669 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002670 if (FileNumber < 1)
2671 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002672 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002673 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002674 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002675
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002676 int64_t LineNumber = 0;
2677 if (getLexer().is(AsmToken::Integer)) {
2678 LineNumber = getTok().getIntVal();
2679 if (LineNumber < 1)
2680 return TokError("line number less than one in '.loc' directive");
2681 Lex();
2682 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002683
2684 int64_t ColumnPos = 0;
2685 if (getLexer().is(AsmToken::Integer)) {
2686 ColumnPos = getTok().getIntVal();
2687 if (ColumnPos < 0)
2688 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002689 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002690 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002691
Kevin Enderbyc0957932010-09-30 16:52:03 +00002692 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002693 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002694 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002695 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2696 for (;;) {
2697 if (getLexer().is(AsmToken::EndOfStatement))
2698 break;
2699
2700 StringRef Name;
2701 SMLoc Loc = getTok().getLoc();
2702 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002703 return TokError("unexpected token in '.loc' directive");
2704
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002705 if (Name == "basic_block")
2706 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2707 else if (Name == "prologue_end")
2708 Flags |= DWARF2_FLAG_PROLOGUE_END;
2709 else if (Name == "epilogue_begin")
2710 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2711 else if (Name == "is_stmt") {
2712 SMLoc Loc = getTok().getLoc();
2713 const MCExpr *Value;
2714 if (getParser().ParseExpression(Value))
2715 return true;
2716 // The expression must be the constant 0 or 1.
2717 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2718 int Value = MCE->getValue();
2719 if (Value == 0)
2720 Flags &= ~DWARF2_FLAG_IS_STMT;
2721 else if (Value == 1)
2722 Flags |= DWARF2_FLAG_IS_STMT;
2723 else
2724 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002725 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002726 else {
2727 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2728 }
2729 }
2730 else if (Name == "isa") {
2731 SMLoc Loc = getTok().getLoc();
2732 const MCExpr *Value;
2733 if (getParser().ParseExpression(Value))
2734 return true;
2735 // The expression must be a constant greater or equal to 0.
2736 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2737 int Value = MCE->getValue();
2738 if (Value < 0)
2739 return Error(Loc, "isa number less than zero");
2740 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002741 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002742 else {
2743 return Error(Loc, "isa number not a constant value");
2744 }
2745 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002746 else if (Name == "discriminator") {
2747 if (getParser().ParseAbsoluteExpression(Discriminator))
2748 return true;
2749 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002750 else {
2751 return Error(Loc, "unknown sub-directive in '.loc' directive");
2752 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002753
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002754 if (getLexer().is(AsmToken::EndOfStatement))
2755 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002756 }
2757 }
2758
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002759 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002760 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002761
2762 return false;
2763}
2764
Daniel Dunbar138abae2010-10-16 04:56:42 +00002765/// ParseDirectiveStabs
2766/// ::= .stabs string, number, number, number
2767bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2768 SMLoc DirectiveLoc) {
2769 return TokError("unsupported directive '" + Directive + "'");
2770}
2771
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002772/// ParseDirectiveCFISections
2773/// ::= .cfi_sections section [, section]
2774bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2775 SMLoc DirectiveLoc) {
2776 StringRef Name;
2777 bool EH = false;
2778 bool Debug = false;
2779
2780 if (getParser().ParseIdentifier(Name))
2781 return TokError("Expected an identifier");
2782
2783 if (Name == ".eh_frame")
2784 EH = true;
2785 else if (Name == ".debug_frame")
2786 Debug = true;
2787
2788 if (getLexer().is(AsmToken::Comma)) {
2789 Lex();
2790
2791 if (getParser().ParseIdentifier(Name))
2792 return TokError("Expected an identifier");
2793
2794 if (Name == ".eh_frame")
2795 EH = true;
2796 else if (Name == ".debug_frame")
2797 Debug = true;
2798 }
2799
2800 getStreamer().EmitCFISections(EH, Debug);
2801
2802 return false;
2803}
2804
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002805/// ParseDirectiveCFIStartProc
2806/// ::= .cfi_startproc
2807bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2808 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002809 getStreamer().EmitCFIStartProc();
2810 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002811}
2812
2813/// ParseDirectiveCFIEndProc
2814/// ::= .cfi_endproc
2815bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002816 getStreamer().EmitCFIEndProc();
2817 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002818}
2819
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002820/// ParseRegisterOrRegisterNumber - parse register name or number.
2821bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2822 SMLoc DirectiveLoc) {
2823 unsigned RegNo;
2824
Jim Grosbach6f888a82011-06-02 17:14:04 +00002825 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002826 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2827 DirectiveLoc))
2828 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002829 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002830 } else
2831 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002832
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002833 return false;
2834}
2835
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002836/// ParseDirectiveCFIDefCfa
2837/// ::= .cfi_def_cfa register, offset
2838bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2839 SMLoc DirectiveLoc) {
2840 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002841 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002842 return true;
2843
2844 if (getLexer().isNot(AsmToken::Comma))
2845 return TokError("unexpected token in directive");
2846 Lex();
2847
2848 int64_t Offset = 0;
2849 if (getParser().ParseAbsoluteExpression(Offset))
2850 return true;
2851
Rafael Espindola066c2f42011-04-12 23:59:07 +00002852 getStreamer().EmitCFIDefCfa(Register, Offset);
2853 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002854}
2855
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002856/// ParseDirectiveCFIDefCfaOffset
2857/// ::= .cfi_def_cfa_offset offset
2858bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2859 SMLoc DirectiveLoc) {
2860 int64_t Offset = 0;
2861 if (getParser().ParseAbsoluteExpression(Offset))
2862 return true;
2863
Rafael Espindola066c2f42011-04-12 23:59:07 +00002864 getStreamer().EmitCFIDefCfaOffset(Offset);
2865 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002866}
2867
2868/// ParseDirectiveCFIAdjustCfaOffset
2869/// ::= .cfi_adjust_cfa_offset adjustment
2870bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2871 SMLoc DirectiveLoc) {
2872 int64_t Adjustment = 0;
2873 if (getParser().ParseAbsoluteExpression(Adjustment))
2874 return true;
2875
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002876 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2877 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002878}
2879
2880/// ParseDirectiveCFIDefCfaRegister
2881/// ::= .cfi_def_cfa_register register
2882bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2883 SMLoc DirectiveLoc) {
2884 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002885 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002886 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002887
Rafael Espindola066c2f42011-04-12 23:59:07 +00002888 getStreamer().EmitCFIDefCfaRegister(Register);
2889 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002890}
2891
2892/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002893/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002894bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2895 int64_t Register = 0;
2896 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002897
2898 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002899 return true;
2900
2901 if (getLexer().isNot(AsmToken::Comma))
2902 return TokError("unexpected token in directive");
2903 Lex();
2904
2905 if (getParser().ParseAbsoluteExpression(Offset))
2906 return true;
2907
Rafael Espindola066c2f42011-04-12 23:59:07 +00002908 getStreamer().EmitCFIOffset(Register, Offset);
2909 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002910}
2911
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002912/// ParseDirectiveCFIRelOffset
2913/// ::= .cfi_rel_offset register, offset
2914bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2915 SMLoc DirectiveLoc) {
2916 int64_t Register = 0;
2917
2918 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2919 return true;
2920
2921 if (getLexer().isNot(AsmToken::Comma))
2922 return TokError("unexpected token in directive");
2923 Lex();
2924
2925 int64_t Offset = 0;
2926 if (getParser().ParseAbsoluteExpression(Offset))
2927 return true;
2928
Rafael Espindola25f492e2011-04-12 16:12:03 +00002929 getStreamer().EmitCFIRelOffset(Register, Offset);
2930 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002931}
2932
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002933static bool isValidEncoding(int64_t Encoding) {
2934 if (Encoding & ~0xff)
2935 return false;
2936
2937 if (Encoding == dwarf::DW_EH_PE_omit)
2938 return true;
2939
2940 const unsigned Format = Encoding & 0xf;
2941 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2942 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2943 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2944 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2945 return false;
2946
Rafael Espindolacaf11582010-12-29 04:31:26 +00002947 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002948 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002949 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002950 return false;
2951
2952 return true;
2953}
2954
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002955/// ParseDirectiveCFIPersonalityOrLsda
2956/// ::= .cfi_personality encoding, [symbol_name]
2957/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002958bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002959 SMLoc DirectiveLoc) {
2960 int64_t Encoding = 0;
2961 if (getParser().ParseAbsoluteExpression(Encoding))
2962 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002963 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002964 return false;
2965
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002966 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002967 return TokError("unsupported encoding.");
2968
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002969 if (getLexer().isNot(AsmToken::Comma))
2970 return TokError("unexpected token in directive");
2971 Lex();
2972
2973 StringRef Name;
2974 if (getParser().ParseIdentifier(Name))
2975 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002976
2977 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2978
2979 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002980 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002981 else {
2982 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002983 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002984 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002985 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002986}
2987
Rafael Espindolafe024d02010-12-28 18:36:23 +00002988/// ParseDirectiveCFIRememberState
2989/// ::= .cfi_remember_state
2990bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2991 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002992 getStreamer().EmitCFIRememberState();
2993 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002994}
2995
2996/// ParseDirectiveCFIRestoreState
2997/// ::= .cfi_remember_state
2998bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2999 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00003000 getStreamer().EmitCFIRestoreState();
3001 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00003002}
3003
Rafael Espindolac5754392011-04-12 15:31:05 +00003004/// ParseDirectiveCFISameValue
3005/// ::= .cfi_same_value register
3006bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3007 SMLoc DirectiveLoc) {
3008 int64_t Register = 0;
3009
3010 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3011 return true;
3012
3013 getStreamer().EmitCFISameValue(Register);
3014
3015 return false;
3016}
3017
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003018/// ParseDirectiveCFIRestore
3019/// ::= .cfi_restore register
3020bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003021 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003022 int64_t Register = 0;
3023 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3024 return true;
3025
3026 getStreamer().EmitCFIRestore(Register);
3027
3028 return false;
3029}
3030
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003031/// ParseDirectiveCFIEscape
3032/// ::= .cfi_escape expression[,...]
3033bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003034 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003035 std::string Values;
3036 int64_t CurrValue;
3037 if (getParser().ParseAbsoluteExpression(CurrValue))
3038 return true;
3039
3040 Values.push_back((uint8_t)CurrValue);
3041
3042 while (getLexer().is(AsmToken::Comma)) {
3043 Lex();
3044
3045 if (getParser().ParseAbsoluteExpression(CurrValue))
3046 return true;
3047
3048 Values.push_back((uint8_t)CurrValue);
3049 }
3050
3051 getStreamer().EmitCFIEscape(Values);
3052 return false;
3053}
3054
Rafael Espindola16d7d432012-01-23 21:51:52 +00003055/// ParseDirectiveCFISignalFrame
3056/// ::= .cfi_signal_frame
3057bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3058 SMLoc DirectiveLoc) {
3059 if (getLexer().isNot(AsmToken::EndOfStatement))
3060 return Error(getLexer().getLoc(),
3061 "unexpected token in '" + Directive + "' directive");
3062
3063 getStreamer().EmitCFISignalFrame();
3064
3065 return false;
3066}
3067
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003068/// ParseDirectiveMacrosOnOff
3069/// ::= .macros_on
3070/// ::= .macros_off
3071bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3072 SMLoc DirectiveLoc) {
3073 if (getLexer().isNot(AsmToken::EndOfStatement))
3074 return Error(getLexer().getLoc(),
3075 "unexpected token in '" + Directive + "' directive");
3076
3077 getParser().MacrosEnabled = Directive == ".macros_on";
3078
3079 return false;
3080}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003081
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003082/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003083/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003084bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3085 SMLoc DirectiveLoc) {
3086 StringRef Name;
3087 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003088 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003089
Rafael Espindola8a403d32012-08-08 14:51:03 +00003090 MacroParameters Parameters;
Rafael Espindola65366442011-06-05 02:43:45 +00003091 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003092 for (;;) {
3093 MacroParameter Parameter;
Rafael Espindola65366442011-06-05 02:43:45 +00003094 if (getParser().ParseIdentifier(Parameter))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003095 return TokError("expected identifier in '.macro' directive");
Rafael Espindola65366442011-06-05 02:43:45 +00003096 Parameters.push_back(Parameter);
3097
3098 if (getLexer().isNot(AsmToken::Comma))
3099 break;
3100 Lex();
3101 }
3102 }
3103
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003104 if (getLexer().isNot(AsmToken::EndOfStatement))
3105 return TokError("unexpected token in '.macro' directive");
3106
3107 // Eat the end of statement.
3108 Lex();
3109
3110 AsmToken EndToken, StartToken = getTok();
3111
3112 // Lex the macro definition.
3113 for (;;) {
3114 // Check whether we have reached the end of the file.
3115 if (getLexer().is(AsmToken::Eof))
3116 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3117
3118 // Otherwise, check whether we have reach the .endmacro.
3119 if (getLexer().is(AsmToken::Identifier) &&
3120 (getTok().getIdentifier() == ".endm" ||
3121 getTok().getIdentifier() == ".endmacro")) {
3122 EndToken = getTok();
3123 Lex();
3124 if (getLexer().isNot(AsmToken::EndOfStatement))
3125 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3126 "' directive");
3127 break;
3128 }
3129
3130 // Otherwise, scan til the end of the statement.
3131 getParser().EatToEndOfStatement();
3132 }
3133
3134 if (getParser().MacroMap.lookup(Name)) {
3135 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3136 }
3137
3138 const char *BodyStart = StartToken.getLoc().getPointer();
3139 const char *BodyEnd = EndToken.getLoc().getPointer();
3140 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003141 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003142 return false;
3143}
3144
3145/// ParseDirectiveEndMacro
3146/// ::= .endm
3147/// ::= .endmacro
3148bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003149 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003150 if (getLexer().isNot(AsmToken::EndOfStatement))
3151 return TokError("unexpected token in '" + Directive + "' directive");
3152
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003153 // If we are inside a macro instantiation, terminate the current
3154 // instantiation.
3155 if (!getParser().ActiveMacros.empty()) {
3156 getParser().HandleMacroExit();
3157 return false;
3158 }
3159
3160 // Otherwise, this .endmacro is a stray entry in the file; well formed
3161 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003162 return TokError("unexpected '" + Directive + "' in file, "
3163 "no current macro definition");
3164}
3165
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003166/// ParseDirectivePurgeMacro
3167/// ::= .purgem
3168bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3169 SMLoc DirectiveLoc) {
3170 StringRef Name;
3171 if (getParser().ParseIdentifier(Name))
3172 return TokError("expected identifier in '.purgem' directive");
3173
3174 if (getLexer().isNot(AsmToken::EndOfStatement))
3175 return TokError("unexpected token in '.purgem' directive");
3176
3177 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3178 if (I == getParser().MacroMap.end())
3179 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3180
3181 // Undefine the macro.
3182 delete I->getValue();
3183 getParser().MacroMap.erase(I);
3184 return false;
3185}
3186
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003187bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003188 getParser().CheckForValidSection();
3189
3190 const MCExpr *Value;
3191
3192 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003193 return true;
3194
3195 if (getLexer().isNot(AsmToken::EndOfStatement))
3196 return TokError("unexpected token in directive");
3197
3198 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003199 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003200 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003201 getStreamer().EmitULEB128Value(Value);
3202
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003203 return false;
3204}
3205
Rafael Espindola761cb062012-06-03 23:57:14 +00003206Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003207 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003208
Rafael Espindola761cb062012-06-03 23:57:14 +00003209 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003210 for (;;) {
3211 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003212 if (getLexer().is(AsmToken::Eof)) {
3213 Error(DirectiveLoc, "no matching '.endr' in definition");
3214 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003215 }
3216
Rafael Espindola761cb062012-06-03 23:57:14 +00003217 if (Lexer.is(AsmToken::Identifier) &&
3218 (getTok().getIdentifier() == ".rept")) {
3219 ++NestLevel;
3220 }
3221
3222 // Otherwise, check whether we have reached the .endr.
3223 if (Lexer.is(AsmToken::Identifier) &&
3224 getTok().getIdentifier() == ".endr") {
3225 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003226 EndToken = getTok();
3227 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003228 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3229 TokError("unexpected token in '.endr' directive");
3230 return 0;
3231 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003232 break;
3233 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003234 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003235 }
3236
Rafael Espindola761cb062012-06-03 23:57:14 +00003237 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003238 EatToEndOfStatement();
3239 }
3240
3241 const char *BodyStart = StartToken.getLoc().getPointer();
3242 const char *BodyEnd = EndToken.getLoc().getPointer();
3243 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3244
Rafael Espindola761cb062012-06-03 23:57:14 +00003245 // We Are Anonymous.
3246 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003247 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003248 return new Macro(Name, Body, Parameters);
3249}
3250
3251void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3252 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003253 OS << ".endr\n";
3254
3255 MemoryBuffer *Instantiation =
3256 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3257
Rafael Espindola761cb062012-06-03 23:57:14 +00003258 // Create the macro instantiation object and add to the current macro
3259 // instantiation stack.
3260 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3261 getTok().getLoc(),
3262 Instantiation);
3263 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003264
Rafael Espindola761cb062012-06-03 23:57:14 +00003265 // Jump to the macro instantiation and prime the lexer.
3266 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3267 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3268 Lex();
3269}
3270
3271bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3272 int64_t Count;
3273 if (ParseAbsoluteExpression(Count))
3274 return TokError("unexpected token in '.rept' directive");
3275
3276 if (Count < 0)
3277 return TokError("Count is negative");
3278
3279 if (Lexer.isNot(AsmToken::EndOfStatement))
3280 return TokError("unexpected token in '.rept' directive");
3281
3282 // Eat the end of statement.
3283 Lex();
3284
3285 // Lex the rept definition.
3286 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3287 if (!M)
3288 return true;
3289
3290 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3291 // to hold the macro body with substitutions.
3292 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003293 MacroParameters Parameters;
3294 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003295 raw_svector_ostream OS(Buf);
3296 while (Count--) {
3297 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3298 return true;
3299 }
3300 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003301
3302 return false;
3303}
3304
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003305/// ParseDirectiveIrp
3306/// ::= .irp symbol,values
3307bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003308 MacroParameters Parameters;
3309 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003310
3311 if (ParseIdentifier(Parameter))
3312 return TokError("expected identifier in '.irp' directive");
3313
3314 Parameters.push_back(Parameter);
3315
3316 if (Lexer.isNot(AsmToken::Comma))
3317 return TokError("expected comma in '.irp' directive");
3318
3319 Lex();
3320
Rafael Espindola8a403d32012-08-08 14:51:03 +00003321 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003322 if (ParseMacroArguments(0, A))
3323 return true;
3324
3325 // Eat the end of statement.
3326 Lex();
3327
3328 // Lex the irp definition.
3329 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3330 if (!M)
3331 return true;
3332
3333 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3334 // to hold the macro body with substitutions.
3335 SmallString<256> Buf;
3336 raw_svector_ostream OS(Buf);
3337
Rafael Espindola7996d042012-08-21 16:06:48 +00003338 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3339 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003340 Args.push_back(*i);
3341
3342 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3343 return true;
3344 }
3345
3346 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3347
3348 return false;
3349}
3350
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003351/// ParseDirectiveIrpc
3352/// ::= .irpc symbol,values
3353bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003354 MacroParameters Parameters;
3355 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003356
3357 if (ParseIdentifier(Parameter))
3358 return TokError("expected identifier in '.irpc' directive");
3359
3360 Parameters.push_back(Parameter);
3361
3362 if (Lexer.isNot(AsmToken::Comma))
3363 return TokError("expected comma in '.irpc' directive");
3364
3365 Lex();
3366
Rafael Espindola8a403d32012-08-08 14:51:03 +00003367 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003368 if (ParseMacroArguments(0, A))
3369 return true;
3370
3371 if (A.size() != 1 || A.front().size() != 1)
3372 return TokError("unexpected token in '.irpc' directive");
3373
3374 // Eat the end of statement.
3375 Lex();
3376
3377 // Lex the irpc definition.
3378 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3379 if (!M)
3380 return true;
3381
3382 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3383 // to hold the macro body with substitutions.
3384 SmallString<256> Buf;
3385 raw_svector_ostream OS(Buf);
3386
3387 StringRef Values = A.front().front().getString();
3388 std::size_t I, End = Values.size();
3389 for (I = 0; I < End; ++I) {
3390 MacroArgument Arg;
3391 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3392
Rafael Espindola8a403d32012-08-08 14:51:03 +00003393 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003394 Args.push_back(Arg);
3395
3396 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3397 return true;
3398 }
3399
3400 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3401
3402 return false;
3403}
3404
Rafael Espindola761cb062012-06-03 23:57:14 +00003405bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3406 if (ActiveMacros.empty())
Preston Gurd6579eea2012-09-19 20:23:43 +00003407 return TokError("unmatched '.endr' directive");
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003408
3409 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003410 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003411 assert(getLexer().is(AsmToken::EndOfStatement));
3412
Rafael Espindola761cb062012-06-03 23:57:14 +00003413 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003414 return false;
3415}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003416
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003417/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003418MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003419 MCContext &C, MCStreamer &Out,
3420 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003421 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003422}