blob: 240c10b5470348536c12a47d0a826ba3580990db [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
Daniel Dunbaraef87e32010-07-18 18:31:38 +000087 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
88 void operator=(const AsmParser &); // DO NOT IMPLEMENT
89private:
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);
136 ~AsmParser();
137
138 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
139
140 void AddDirectiveHandler(MCAsmParserExtension *Object,
141 StringRef Directive,
142 DirectiveHandler Handler) {
143 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
169 const AsmToken &Lex();
170
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
205 /// \brief Reset the current lexer position to that given by \arg Loc. The
206 /// current token is not set; clients should ensure Lex() is called
207 /// subsequently.
208 void JumpToLoc(SMLoc Loc);
209
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000210 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.
218 StringRef ParseStringToEndOfStatement();
219
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
Nico Weber4c4c7322011-01-28 03:04:41 +0000224 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000225
226 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
227 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
228 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000229 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000230
231 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
232 /// and set \arg Res to the identifier contents.
233 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000234
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000235 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000236
237 // ".ascii", ".asciiz", ".string"
238 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000239 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000240 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000241 bool ParseDirectiveFill(); // ".fill"
242 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000243 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000244 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000245 bool ParseDirectiveOrg(); // ".org"
246 // ".align{,32}", ".p2align{,w,l}"
247 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
248
249 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
250 /// accepts a single symbol (which should be a label or an external).
251 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000252
253 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
254
255 bool ParseDirectiveAbort(); // ".abort"
256 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000257 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000258
259 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000260 // ".ifb" or ".ifnb", depending on ExpectBlank.
261 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000262 // ".ifc" or ".ifnc", depending on ExpectEqual.
263 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000264 // ".ifdef" or ".ifndef", depending on expect_defined
265 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000266 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
267 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
268 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
269
270 /// ParseEscapedString - Parse the current token as a string which may include
271 /// escaped characters and return the string contents.
272 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000273
274 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
275 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000276
Rafael Espindola761cb062012-06-03 23:57:14 +0000277 // Macro-like directives
278 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
279 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
280 raw_svector_ostream &OS);
281 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +0000282 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp"
Rafael Espindolafc9216e2012-06-16 18:03:25 +0000283 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
Rafael Espindola761cb062012-06-03 23:57:14 +0000284 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000285};
286
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000287/// \brief Generic implementations of directive handling, etc. which is shared
288/// (or the default, at least) for all assembler parser.
289class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000290 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
291 void AddDirectiveHandler(StringRef Directive) {
292 getParser().AddDirectiveHandler(this, Directive,
293 HandleDirective<GenericAsmParser, Handler>);
294 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000295public:
296 GenericAsmParser() {}
297
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000298 AsmParser &getParser() {
299 return (AsmParser&) this->MCAsmParserExtension::getParser();
300 }
301
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000302 virtual void Initialize(MCAsmParser &Parser) {
303 // Call the base implementation.
304 this->MCAsmParserExtension::Initialize(Parser);
305
306 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
308 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
309 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000310 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000311
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000312 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000313 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
314 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000315 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
316 ".cfi_startproc");
317 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
318 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000319 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
320 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000321 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
322 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000323 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
324 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000325 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
326 ".cfi_def_cfa_register");
327 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
328 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000329 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
330 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000331 AddDirectiveHandler<
332 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
333 AddDirectiveHandler<
334 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000335 AddDirectiveHandler<
336 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
337 AddDirectiveHandler<
338 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000339 AddDirectiveHandler<
340 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000341 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000342 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
343 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000344 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000345 AddDirectiveHandler<
346 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000347
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000348 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000349 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
350 ".macros_on");
351 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
352 ".macros_off");
353 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
354 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
355 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000356 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000357
358 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
359 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000360 }
361
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000362 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
363
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000364 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
365 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
366 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000367 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000368 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000369 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
370 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000371 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000372 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000373 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000374 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
375 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000376 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000377 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000378 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
379 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000380 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000381 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000382 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000383 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000384
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000385 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000386 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
387 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000388 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000389
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000390 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000391};
392
393}
394
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000395namespace llvm {
396
397extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000398extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000399extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000400
401}
402
Chris Lattneraaec2052010-01-19 19:46:13 +0000403enum { DEFAULT_ADDRSPACE = 0 };
404
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000405AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000406 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000407 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000408 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000409 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
410 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000411 // Save the old handler.
412 SavedDiagHandler = SrcMgr.getDiagHandler();
413 SavedDiagContext = SrcMgr.getDiagContext();
414 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000415 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000416 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000417
418 // Initialize the generic parser.
419 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000420
421 // Initialize the platform / file format parser.
422 //
423 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
424 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000425 if (_MAI.hasMicrosoftFastStdCallMangling()) {
426 PlatformParser = createCOFFAsmParser();
427 PlatformParser->Initialize(*this);
428 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000429 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000430 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000431 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000432 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000433 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000434 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000435}
436
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000437AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000438 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
439
440 // Destroy any macros.
441 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
442 ie = MacroMap.end(); it != ie; ++it)
443 delete it->getValue();
444
Daniel Dunbare4749702010-07-12 18:12:02 +0000445 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000446 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000447}
448
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000449void AsmParser::PrintMacroInstantiations() {
450 // Print the active macro instantiation stack.
451 for (std::vector<MacroInstantiation*>::const_reverse_iterator
452 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000453 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
454 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000455}
456
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000457bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000458 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000459 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000460 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000461 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000462 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000463}
464
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000465bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000466 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000467 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000468 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000469 return true;
470}
471
Sean Callananfd0b0282010-01-21 00:19:58 +0000472bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000473 std::string IncludedFile;
474 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000475 if (NewBuf == -1)
476 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000477
Sean Callananfd0b0282010-01-21 00:19:58 +0000478 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000479
Sean Callananfd0b0282010-01-21 00:19:58 +0000480 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000481
Sean Callananfd0b0282010-01-21 00:19:58 +0000482 return false;
483}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000484
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000485/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000486/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000487/// returns true on failure.
488bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
489 std::string IncludedFile;
490 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
491 if (NewBuf == -1)
492 return true;
493
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000494 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000495 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
496 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000497 return false;
498}
499
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000500void AsmParser::JumpToLoc(SMLoc Loc) {
501 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
502 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
503}
504
Sean Callananfd0b0282010-01-21 00:19:58 +0000505const AsmToken &AsmParser::Lex() {
506 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000507
Sean Callananfd0b0282010-01-21 00:19:58 +0000508 if (tok->is(AsmToken::Eof)) {
509 // If this is the end of an included file, pop the parent file off the
510 // include stack.
511 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
512 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000513 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000514 tok = &Lexer.Lex();
515 }
516 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000517
Sean Callananfd0b0282010-01-21 00:19:58 +0000518 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000519 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000520
Sean Callananfd0b0282010-01-21 00:19:58 +0000521 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000522}
523
Chris Lattner79180e22010-04-05 23:15:42 +0000524bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000525 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000526 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000527 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000528
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000529 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000530 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000531
532 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000533 AsmCond StartingCondState = TheCondState;
534
Kevin Enderby613b7572011-11-01 22:27:22 +0000535 // If we are generating dwarf for assembly source files save the initial text
536 // section and generate a .file directive.
537 if (getContext().getGenDwarfForAssembly()) {
538 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000539 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
540 getStreamer().EmitLabel(SectionStartSym);
541 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000542 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
543 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
544 }
545
Chris Lattnerb717fb02009-07-02 21:53:43 +0000546 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000547 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000548 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000549
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000550 // We had an error, validate that one was emitted and recover by skipping to
551 // the next line.
552 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000553 EatToEndOfStatement();
554 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000555
556 if (TheCondState.TheCond != StartingCondState.TheCond ||
557 TheCondState.Ignore != StartingCondState.Ignore)
558 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000559
560 // Check to see there are no empty DwarfFile slots.
561 const std::vector<MCDwarfFile *> &MCDwarfFiles =
562 getContext().getMCDwarfFiles();
563 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000564 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000565 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000566 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000567
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000568 // Check to see that all assembler local symbols were actually defined.
569 // Targets that don't do subsections via symbols may not want this, though,
570 // so conservatively exclude them. Only do this if we're finalizing, though,
571 // as otherwise we won't necessarilly have seen everything yet.
572 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
573 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
574 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
575 e = Symbols.end();
576 i != e; ++i) {
577 MCSymbol *Sym = i->getValue();
578 // Variable symbols may not be marked as defined, so check those
579 // explicitly. If we know it's a variable, we have a definition for
580 // the purposes of this check.
581 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
582 // FIXME: We would really like to refer back to where the symbol was
583 // first referenced for a source location. We need to add something
584 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000585 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
586 "assembler local symbol '" + Sym->getName() +
587 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000588 }
589 }
590
591
Chris Lattner79180e22010-04-05 23:15:42 +0000592 // Finalize the output stream if there are no errors and if the client wants
593 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000594 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 Out.Finish();
596
Chris Lattnerb717fb02009-07-02 21:53:43 +0000597 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000598}
599
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000600void AsmParser::CheckForValidSection() {
601 if (!getStreamer().getCurrentSection()) {
602 TokError("expected section directive before assembly directive");
603 Out.SwitchSection(Ctx.getMachOSection(
604 "__TEXT", "__text",
605 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
606 0, SectionKind::getText()));
607 }
608}
609
Chris Lattner2cf5f142009-06-22 01:29:09 +0000610/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
611void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000612 while (Lexer.isNot(AsmToken::EndOfStatement) &&
613 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000614 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000615
Chris Lattner2cf5f142009-06-22 01:29:09 +0000616 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000617 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000618 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000619}
620
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000621StringRef AsmParser::ParseStringToEndOfStatement() {
622 const char *Start = getTok().getLoc().getPointer();
623
624 while (Lexer.isNot(AsmToken::EndOfStatement) &&
625 Lexer.isNot(AsmToken::Eof))
626 Lex();
627
628 const char *End = getTok().getLoc().getPointer();
629 return StringRef(Start, End - Start);
630}
Chris Lattnerc4193832009-06-22 05:51:26 +0000631
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000632StringRef AsmParser::ParseStringToComma() {
633 const char *Start = getTok().getLoc().getPointer();
634
635 while (Lexer.isNot(AsmToken::EndOfStatement) &&
636 Lexer.isNot(AsmToken::Comma) &&
637 Lexer.isNot(AsmToken::Eof))
638 Lex();
639
640 const char *End = getTok().getLoc().getPointer();
641 return StringRef(Start, End - Start);
642}
643
Chris Lattner74ec1a32009-06-22 06:32:03 +0000644/// ParseParenExpr - Parse a paren expression and return it.
645/// NOTE: This assumes the leading '(' has already been consumed.
646///
647/// parenexpr ::= expr)
648///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000649bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000650 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000651 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000652 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000653 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000654 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000655 return false;
656}
Chris Lattnerc4193832009-06-22 05:51:26 +0000657
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000658/// ParseBracketExpr - Parse a bracket expression and return it.
659/// NOTE: This assumes the leading '[' has already been consumed.
660///
661/// bracketexpr ::= expr]
662///
663bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
664 if (ParseExpression(Res)) return true;
665 if (Lexer.isNot(AsmToken::RBrac))
666 return TokError("expected ']' in brackets expression");
667 EndLoc = Lexer.getLoc();
668 Lex();
669 return false;
670}
671
Chris Lattner74ec1a32009-06-22 06:32:03 +0000672/// ParsePrimaryExpr - Parse a primary expression and return it.
673/// primaryexpr ::= (parenexpr
674/// primaryexpr ::= symbol
675/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000676/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000677/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000678bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000679 switch (Lexer.getKind()) {
680 default:
681 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000682 // If we have an error assume that we've already handled it.
683 case AsmToken::Error:
684 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000685 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000686 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000687 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000688 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000689 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000690 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000691 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000692 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000693 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000694 EndLoc = Lexer.getLoc();
695
696 StringRef Identifier;
697 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000698 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000699
Daniel Dunbarfffff912009-10-16 01:34:54 +0000700 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000701 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000702 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000703
704 // Lookup the symbol variant if used.
705 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000706 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000707 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000708 if (Variant == MCSymbolRefExpr::VK_Invalid) {
709 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000710 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000711 }
712 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000713
Daniel Dunbarfffff912009-10-16 01:34:54 +0000714 // If this is an absolute variable reference, substitute it now to preserve
715 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000716 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000717 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000718 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000719
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000720 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000721 return false;
722 }
723
724 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000725 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000726 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000727 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000728 case AsmToken::Integer: {
729 SMLoc Loc = getTok().getLoc();
730 int64_t IntVal = getTok().getIntVal();
731 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000732 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000733 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000734 // Look for 'b' or 'f' following an Integer as a directional label
735 if (Lexer.getKind() == AsmToken::Identifier) {
736 StringRef IDVal = getTok().getString();
737 if (IDVal == "f" || IDVal == "b"){
738 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
739 IDVal == "f" ? 1 : 0);
740 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
741 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000742 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000743 return Error(Loc, "invalid reference to undefined symbol");
744 EndLoc = Lexer.getLoc();
745 Lex(); // Eat identifier.
746 }
747 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000748 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000749 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000750 case AsmToken::Real: {
751 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000752 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000753 Res = MCConstantExpr::Create(IntVal, getContext());
754 Lex(); // Eat token.
755 return false;
756 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000757 case AsmToken::Dot: {
758 // This is a '.' reference, which references the current PC. Emit a
759 // temporary label to the streamer and refer to it.
760 MCSymbol *Sym = Ctx.CreateTempSymbol();
761 Out.EmitLabel(Sym);
762 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
763 EndLoc = Lexer.getLoc();
764 Lex(); // Eat identifier.
765 return false;
766 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000767 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000768 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000769 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000770 case AsmToken::LBrac:
771 if (!PlatformParser->HasBracketExpressions())
772 return TokError("brackets expression not supported on this target");
773 Lex(); // Eat the '['.
774 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000775 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000776 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000777 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000778 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000779 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000780 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000782 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000783 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000784 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000785 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000788 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000789 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000790 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000791 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000792 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000793 }
794}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000795
Chris Lattnerb4307b32010-01-15 19:28:38 +0000796bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000797 SMLoc EndLoc;
798 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000799}
800
Daniel Dunbarcceba832010-09-17 02:47:07 +0000801const MCExpr *
802AsmParser::ApplyModifierToExpr(const MCExpr *E,
803 MCSymbolRefExpr::VariantKind Variant) {
804 // Recurse over the given expression, rebuilding it to apply the given variant
805 // if there is exactly one symbol.
806 switch (E->getKind()) {
807 case MCExpr::Target:
808 case MCExpr::Constant:
809 return 0;
810
811 case MCExpr::SymbolRef: {
812 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
813
814 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
815 TokError("invalid variant on expression '" +
816 getTok().getIdentifier() + "' (already modified)");
817 return E;
818 }
819
820 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
821 }
822
823 case MCExpr::Unary: {
824 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
825 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
826 if (!Sub)
827 return 0;
828 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
829 }
830
831 case MCExpr::Binary: {
832 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
833 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
834 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
835
836 if (!LHS && !RHS)
837 return 0;
838
839 if (!LHS) LHS = BE->getLHS();
840 if (!RHS) RHS = BE->getRHS();
841
842 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
843 }
844 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000845
Craig Topper85814382012-02-07 05:05:23 +0000846 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000847}
848
Chris Lattner74ec1a32009-06-22 06:32:03 +0000849/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000850///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000851/// expr ::= expr &&,|| expr -> lowest.
852/// expr ::= expr |,^,&,! expr
853/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
854/// expr ::= expr <<,>> expr
855/// expr ::= expr +,- expr
856/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000857/// expr ::= primaryexpr
858///
Chris Lattner54482b42010-01-15 19:39:23 +0000859bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000860 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000861 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000862 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
863 return true;
864
Daniel Dunbarcceba832010-09-17 02:47:07 +0000865 // As a special case, we support 'a op b @ modifier' by rewriting the
866 // expression to include the modifier. This is inefficient, but in general we
867 // expect users to use 'a@modifier op b'.
868 if (Lexer.getKind() == AsmToken::At) {
869 Lex();
870
871 if (Lexer.isNot(AsmToken::Identifier))
872 return TokError("unexpected symbol modifier following '@'");
873
874 MCSymbolRefExpr::VariantKind Variant =
875 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
876 if (Variant == MCSymbolRefExpr::VK_Invalid)
877 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
878
879 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
880 if (!ModifiedRes) {
881 return TokError("invalid modifier '" + getTok().getIdentifier() +
882 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000883 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000884
Daniel Dunbarcceba832010-09-17 02:47:07 +0000885 Res = ModifiedRes;
886 Lex();
887 }
888
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000889 // Try to constant fold it up front, if possible.
890 int64_t Value;
891 if (Res->EvaluateAsAbsolute(Value))
892 Res = MCConstantExpr::Create(Value, getContext());
893
894 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000895}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000896
Chris Lattnerb4307b32010-01-15 19:28:38 +0000897bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000898 Res = 0;
899 return ParseParenExpr(Res, EndLoc) ||
900 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000901}
902
Daniel Dunbar475839e2009-06-29 20:37:27 +0000903bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000904 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000905
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000906 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000907 if (ParseExpression(Expr))
908 return true;
909
Daniel Dunbare00b0112009-10-16 01:57:52 +0000910 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000911 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000912
913 return false;
914}
915
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000916static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000917 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000918 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000919 default:
920 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000921
Jim Grosbachfbe16812011-08-20 16:24:13 +0000922 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000923 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000924 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000925 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000926 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000927 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000928 return 1;
929
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000930
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000931 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000932 //
933 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000934 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000935 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000936 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000937 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000938 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000939 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000940 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000941 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000942 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000943
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000944 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000945 case AsmToken::EqualEqual:
946 Kind = MCBinaryExpr::EQ;
947 return 3;
948 case AsmToken::ExclaimEqual:
949 case AsmToken::LessGreater:
950 Kind = MCBinaryExpr::NE;
951 return 3;
952 case AsmToken::Less:
953 Kind = MCBinaryExpr::LT;
954 return 3;
955 case AsmToken::LessEqual:
956 Kind = MCBinaryExpr::LTE;
957 return 3;
958 case AsmToken::Greater:
959 Kind = MCBinaryExpr::GT;
960 return 3;
961 case AsmToken::GreaterEqual:
962 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000963 return 3;
964
Jim Grosbachfbe16812011-08-20 16:24:13 +0000965 // Intermediate Precedence: <<, >>
966 case AsmToken::LessLess:
967 Kind = MCBinaryExpr::Shl;
968 return 4;
969 case AsmToken::GreaterGreater:
970 Kind = MCBinaryExpr::Shr;
971 return 4;
972
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000973 // High Intermediate Precedence: +, -
974 case AsmToken::Plus:
975 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000976 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000977 case AsmToken::Minus:
978 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000979 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000980
Jim Grosbachfbe16812011-08-20 16:24:13 +0000981 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000982 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000983 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000984 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000985 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000986 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000987 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000988 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000989 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000990 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000991 }
992}
993
994
995/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
996/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000997bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
998 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000999 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001000 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001001 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001002
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001003 // If the next token is lower precedence than we are allowed to eat, return
1004 // successfully with what we ate already.
1005 if (TokPrec < Precedence)
1006 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001007
Sean Callanan79ed1a82010-01-19 20:22:31 +00001008 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001009
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001010 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001011 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001012 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001013
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001014 // If BinOp binds less tightly with RHS than the operator after RHS, let
1015 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001016 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001017 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001018 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001019 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001020 }
1021
Daniel Dunbar475839e2009-06-29 20:37:27 +00001022 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001023 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001024 }
1025}
1026
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001027
1028
1029
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001030/// ParseStatement:
1031/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001032/// ::= Label* Directive ...Operands... EndOfStatement
1033/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001034bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001035 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001036 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001037 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001038 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001039 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001040
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001041 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001042 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001043 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001044 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001045 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001046 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001047 if (Lexer.is(AsmToken::Hash))
1048 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001049
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001050 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001051 if (Lexer.is(AsmToken::Integer)) {
1052 LocalLabelVal = getTok().getIntVal();
1053 if (LocalLabelVal < 0) {
1054 if (!TheCondState.Ignore)
1055 return TokError("unexpected token at start of statement");
1056 IDVal = "";
1057 }
1058 else {
1059 IDVal = getTok().getString();
1060 Lex(); // Consume the integer token to be used as an identifier token.
1061 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001062 if (!TheCondState.Ignore)
1063 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001064 }
1065 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001066
1067 } else if (Lexer.is(AsmToken::Dot)) {
1068 // Treat '.' as a valid identifier in this context.
1069 Lex();
1070 IDVal = ".";
1071
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001072 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001073 if (!TheCondState.Ignore)
1074 return TokError("unexpected token at start of statement");
1075 IDVal = "";
1076 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001077
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001078
Chris Lattner7834fac2010-04-17 18:14:27 +00001079 // Handle conditional assembly here before checking for skipping. We
1080 // have to do this so that .endif isn't skipped in a ".if 0" block for
1081 // example.
1082 if (IDVal == ".if")
1083 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001084 if (IDVal == ".ifb")
1085 return ParseDirectiveIfb(IDLoc, true);
1086 if (IDVal == ".ifnb")
1087 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001088 if (IDVal == ".ifc")
1089 return ParseDirectiveIfc(IDLoc, true);
1090 if (IDVal == ".ifnc")
1091 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001092 if (IDVal == ".ifdef")
1093 return ParseDirectiveIfdef(IDLoc, true);
1094 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1095 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001096 if (IDVal == ".elseif")
1097 return ParseDirectiveElseIf(IDLoc);
1098 if (IDVal == ".else")
1099 return ParseDirectiveElse(IDLoc);
1100 if (IDVal == ".endif")
1101 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001102
Chris Lattner7834fac2010-04-17 18:14:27 +00001103 // If we are in a ".if 0" block, ignore this statement.
1104 if (TheCondState.Ignore) {
1105 EatToEndOfStatement();
1106 return false;
1107 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001108
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001109 // FIXME: Recurse on local labels?
1110
1111 // See what kind of statement we have.
1112 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001113 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001114 CheckForValidSection();
1115
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001116 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001117 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001118
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001119 // Diagnose attempt to use '.' as a label.
1120 if (IDVal == ".")
1121 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1122
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001123 // Diagnose attempt to use a variable as a label.
1124 //
1125 // FIXME: Diagnostics. Note the location of the definition as a label.
1126 // FIXME: This doesn't diagnose assignment to a symbol which has been
1127 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001128 MCSymbol *Sym;
1129 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001130 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001131 else
1132 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001133 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001134 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001135
Daniel Dunbar959fd882009-08-26 22:13:22 +00001136 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001137 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001138
Kevin Enderby94c2e852011-12-09 18:09:40 +00001139 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001140 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001141 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001142 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1143 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001144
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001145 // Consume any end of statement token, if present, to avoid spurious
1146 // AddBlankLine calls().
1147 if (Lexer.is(AsmToken::EndOfStatement)) {
1148 Lex();
1149 if (Lexer.is(AsmToken::Eof))
1150 return false;
1151 }
1152
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001153 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001154 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001155
Daniel Dunbar3f872332009-07-28 16:08:33 +00001156 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001157 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001158 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001159
Nico Weber4c4c7322011-01-28 03:04:41 +00001160 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001161
1162 default: // Normal instruction or directive.
1163 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001164 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001165
1166 // If macros are enabled, check to see if this is a macro instantiation.
1167 if (MacrosEnabled)
1168 if (const Macro *M = MacroMap.lookup(IDVal))
1169 return HandleMacroEntry(IDVal, IDLoc, M);
1170
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001171 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001172 if (IDVal[0] == '.' && IDVal != ".") {
Akira Hatanaka3b02d952012-07-05 19:09:33 +00001173
1174 // Target hook for parsing target specific directives.
1175 if (!getTargetParser().ParseDirective(ID))
1176 return false;
1177
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001178 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001179 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001180 return ParseDirectiveSet(IDVal, true);
1181 if (IDVal == ".equiv")
1182 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001183
Daniel Dunbara0d14262009-06-24 23:30:00 +00001184 // Data directives
1185
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001186 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001187 return ParseDirectiveAscii(IDVal, false);
1188 if (IDVal == ".asciz" || IDVal == ".string")
1189 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001190
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001191 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001192 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001193 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001194 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001195 if (IDVal == ".value")
1196 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001197 if (IDVal == ".2byte")
1198 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001199 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001200 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001201 if (IDVal == ".int")
1202 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001203 if (IDVal == ".4byte")
1204 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001205 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001206 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001207 if (IDVal == ".8byte")
1208 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001209 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001210 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1211 if (IDVal == ".double")
1212 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001213
Eli Friedman5d68ec22010-07-19 04:17:25 +00001214 if (IDVal == ".align") {
1215 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1216 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1217 }
1218 if (IDVal == ".align32") {
1219 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1220 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1221 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001222 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001223 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001224 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001225 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001226 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001227 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001228 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001229 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001230 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001231 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001232 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001233 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1234
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001235 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001236 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001237
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001238 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001239 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001240 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001241 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001242 if (IDVal == ".zero")
1243 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001244
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001245 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001246
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001247 if (IDVal == ".extern") {
1248 EatToEndOfStatement(); // .extern is the default, ignore it.
1249 return false;
1250 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001251 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001252 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001253 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001254 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001255 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001256 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001257 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001258 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001259 if (IDVal == ".symbol_resolver")
1260 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001261 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001262 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001263 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001264 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001265 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001266 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001267 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001268 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001269 if (IDVal == ".weak_def_can_be_hidden")
1270 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001271
Hans Wennborg5cc64912011-06-18 13:51:54 +00001272 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001273 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001274 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001275 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001276
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001277 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001278 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001279 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001280 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001281 if (IDVal == ".incbin")
1282 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001283
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001284 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001285 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001286
Rafael Espindola761cb062012-06-03 23:57:14 +00001287 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001288 if (IDVal == ".rept")
1289 return ParseDirectiveRept(IDLoc);
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001290 if (IDVal == ".irp")
1291 return ParseDirectiveIrp(IDLoc);
Rafael Espindolafc9216e2012-06-16 18:03:25 +00001292 if (IDVal == ".irpc")
1293 return ParseDirectiveIrpc(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001294 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001295 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001296
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001297 // Look up the handler in the handler table.
1298 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1299 DirectiveMap.lookup(IDVal);
1300 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001301 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001302
Kevin Enderby9c656452009-09-10 20:51:44 +00001303
Jim Grosbach686c0182012-05-01 18:38:27 +00001304 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001305 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001306
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001307 CheckForValidSection();
1308
Chris Lattnera7f13542010-05-19 23:34:33 +00001309 // Canonicalize the opcode to lower case.
1310 SmallString<128> Opcode;
1311 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1312 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001313
Chris Lattner98986712010-01-14 22:21:20 +00001314 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001315 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001316 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001317
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001318 // Dump the parsed representation, if requested.
1319 if (getShowParsedOperands()) {
1320 SmallString<256> Str;
1321 raw_svector_ostream OS(Str);
1322 OS << "parsed instruction: [";
1323 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1324 if (i != 0)
1325 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001326 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001327 }
1328 OS << "]";
1329
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001330 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001331 }
1332
Kevin Enderby613b7572011-11-01 22:27:22 +00001333 // If we are generating dwarf for assembly source files and the current
1334 // section is the initial text section then generate a .loc directive for
1335 // the instruction.
1336 if (!HadError && getContext().getGenDwarfForAssembly() &&
1337 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1338 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1339 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1340 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001341 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001342 StringRef());
1343 }
1344
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001345 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001346 if (!HadError)
1347 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1348 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001349
Chris Lattner98986712010-01-14 22:21:20 +00001350 // Free any parsed operands.
1351 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1352 delete ParsedOperands[i];
1353
Chris Lattnercbf8a982010-09-11 16:18:25 +00001354 // Don't skip the rest of the line, the instruction parser is responsible for
1355 // that.
1356 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001357}
Chris Lattner9a023f72009-06-24 04:43:34 +00001358
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001359/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1360/// since they may not be able to be tokenized to get to the end of line token.
1361void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001362 if (!Lexer.is(AsmToken::EndOfStatement))
1363 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001364 // Eat EOL.
1365 Lex();
1366}
1367
1368/// ParseCppHashLineFilenameComment as this:
1369/// ::= # number "filename"
1370/// or just as a full line comment if it doesn't have a number and a string.
1371bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1372 Lex(); // Eat the hash token.
1373
1374 if (getLexer().isNot(AsmToken::Integer)) {
1375 // Consume the line since in cases it is not a well-formed line directive,
1376 // as if were simply a full line comment.
1377 EatToEndOfLine();
1378 return false;
1379 }
1380
1381 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001382 Lex();
1383
1384 if (getLexer().isNot(AsmToken::String)) {
1385 EatToEndOfLine();
1386 return false;
1387 }
1388
1389 StringRef Filename = getTok().getString();
1390 // Get rid of the enclosing quotes.
1391 Filename = Filename.substr(1, Filename.size()-2);
1392
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001393 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1394 CppHashLoc = L;
1395 CppHashFilename = Filename;
1396 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001397
1398 // Ignore any trailing characters, they're just comment.
1399 EatToEndOfLine();
1400 return false;
1401}
1402
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001403/// DiagHandler - will use the last parsed cpp hash line filename comment
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001404/// for the Filename and LineNo if any in the diagnostic.
1405void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1406 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1407 raw_ostream &OS = errs();
1408
1409 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1410 const SMLoc &DiagLoc = Diag.getLoc();
1411 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1412 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1413
1414 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1415 // before printing the message.
1416 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001417 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001418 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1419 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1420 }
1421
1422 // If we have not parsed a cpp hash line filename comment or the source
1423 // manager changed or buffer changed (like in a nested include) then just
1424 // print the normal diagnostic using its Filename and LineNo.
1425 if (!Parser->CppHashLineNumber ||
1426 &DiagSrcMgr != &Parser->SrcMgr ||
1427 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001428 if (Parser->SavedDiagHandler)
1429 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1430 else
1431 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001432 return;
1433 }
1434
1435 // Use the CppHashFilename and calculate a line number based on the
1436 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1437 // the diagnostic.
1438 const std::string Filename = Parser->CppHashFilename;
1439
1440 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1441 int CppHashLocLineNo =
1442 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1443 int LineNo = Parser->CppHashLineNumber - 1 +
1444 (DiagLocLineNo - CppHashLocLineNo);
1445
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001446 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1447 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001448 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001449 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001450
Benjamin Kramer04a04262011-10-16 10:48:29 +00001451 if (Parser->SavedDiagHandler)
1452 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1453 else
1454 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001455}
1456
Rafael Espindola799aacf2012-08-21 18:29:30 +00001457// FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The
1458// difference being that that function accepts '@' as part of identifiers and
1459// we can't do that. AsmLexer.cpp should probably be changed to handle
1460// '@' as a special case when needed.
1461static bool isIdentifierChar(char c) {
1462 return isalnum(c) || c == '_' || c == '$' || c == '.';
1463}
1464
Rafael Espindola761cb062012-06-03 23:57:14 +00001465bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001466 const MacroParameters &Parameters,
1467 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001468 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001469 unsigned NParameters = Parameters.size();
1470 if (NParameters != 0 && NParameters != A.size())
1471 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001472
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001473 while (!Body.empty()) {
1474 // Scan for the next substitution.
1475 std::size_t End = Body.size(), Pos = 0;
1476 for (; Pos != End; ++Pos) {
1477 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001478 if (!NParameters) {
1479 // This macro has no parameters, look for $0, $1, etc.
1480 if (Body[Pos] != '$' || Pos + 1 == End)
1481 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001482
Rafael Espindola65366442011-06-05 02:43:45 +00001483 char Next = Body[Pos + 1];
1484 if (Next == '$' || Next == 'n' || isdigit(Next))
1485 break;
1486 } else {
1487 // This macro has parameters, look for \foo, \bar, etc.
1488 if (Body[Pos] == '\\' && Pos + 1 != End)
1489 break;
1490 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001491 }
1492
1493 // Add the prefix.
1494 OS << Body.slice(0, Pos);
1495
1496 // Check if we reached the end.
1497 if (Pos == End)
1498 break;
1499
Rafael Espindola65366442011-06-05 02:43:45 +00001500 if (!NParameters) {
1501 switch (Body[Pos+1]) {
1502 // $$ => $
1503 case '$':
1504 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001505 break;
1506
Rafael Espindola65366442011-06-05 02:43:45 +00001507 // $n => number of arguments
1508 case 'n':
1509 OS << A.size();
1510 break;
1511
1512 // $[0-9] => argument
1513 default: {
1514 // Missing arguments are ignored.
1515 unsigned Index = Body[Pos+1] - '0';
1516 if (Index >= A.size())
1517 break;
1518
1519 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001520 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001521 ie = A[Index].end(); it != ie; ++it)
1522 OS << it->getString();
1523 break;
1524 }
1525 }
1526 Pos += 2;
1527 } else {
1528 unsigned I = Pos + 1;
Rafael Espindola799aacf2012-08-21 18:29:30 +00001529 while (isIdentifierChar(Body[I]) && I + 1 != End)
Rafael Espindola65366442011-06-05 02:43:45 +00001530 ++I;
1531
1532 const char *Begin = Body.data() + Pos +1;
1533 StringRef Argument(Begin, I - (Pos +1));
1534 unsigned Index = 0;
1535 for (; Index < NParameters; ++Index)
1536 if (Parameters[Index] == Argument)
1537 break;
1538
1539 // FIXME: We should error at the macro definition.
1540 if (Index == NParameters)
1541 return Error(L, "Parameter not found");
1542
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001543 for (MacroArgument::const_iterator it = A[Index].begin(),
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001544 ie = A[Index].end(); it != ie; ++it)
1545 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001546
Rafael Espindola65366442011-06-05 02:43:45 +00001547 Pos += 1 + Argument.size();
1548 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001549 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001550 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001551 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001552
Rafael Espindola65366442011-06-05 02:43:45 +00001553 return false;
1554}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001555
Rafael Espindola65366442011-06-05 02:43:45 +00001556MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1557 MemoryBuffer *I)
1558 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1559{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001560}
1561
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001562/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1563/// This is used for both default macro parameter values and the
1564/// arguments in macro invocations
1565bool AsmParser::ParseMacroArgument(MacroArgument &MA) {
1566 unsigned ParenLevel = 0;
1567
1568 for (;;) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001569 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
1570 return TokError("unexpected token in macro instantiation");
1571
1572 // HandleMacroEntry relies on not advancing the lexer here
1573 // to be able to fill in the remaining default parameter values
1574 if (Lexer.is(AsmToken::EndOfStatement))
1575 break;
1576 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
1577 break;
1578
1579 // Adjust the current parentheses level.
1580 if (Lexer.is(AsmToken::LParen))
1581 ++ParenLevel;
1582 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1583 --ParenLevel;
1584
1585 // Append the token to the current argument list.
1586 MA.push_back(getTok());
1587 Lex();
1588 }
1589 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001590 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001591 return false;
1592}
1593
1594// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001595bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001596 const unsigned NParameters = M ? M->Parameters.size() : 0;
1597
1598 // Parse two kinds of macro invocations:
1599 // - macros defined without any parameters accept an arbitrary number of them
1600 // - macros defined with parameters accept at most that many of them
1601 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1602 ++Parameter) {
1603 MacroArgument MA;
1604
1605 if (ParseMacroArgument(MA))
1606 return true;
1607
Jim Grosbach97146442012-07-30 22:44:17 +00001608 A.push_back(MA);
1609
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001610 if (Lexer.is(AsmToken::EndOfStatement))
1611 return false;
1612
1613 if (Lexer.is(AsmToken::Comma))
1614 Lex();
1615 }
1616 return TokError("Too many arguments");
1617}
1618
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001619bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1620 const Macro *M) {
1621 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1622 // this, although we should protect against infinite loops.
1623 if (ActiveMacros.size() == 20)
1624 return TokError("macros cannot be nested more than 20 levels deep");
1625
Rafael Espindola8a403d32012-08-08 14:51:03 +00001626 MacroArguments A;
1627 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001628 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001629
Jim Grosbach97146442012-07-30 22:44:17 +00001630 // Remove any trailing empty arguments. Do this after-the-fact as we have
1631 // to keep empty arguments in the middle of the list or positionality
1632 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001633 while (!A.empty() && A.back().empty())
1634 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001635
Rafael Espindola65366442011-06-05 02:43:45 +00001636 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1637 // to hold the macro body with substitutions.
1638 SmallString<256> Buf;
1639 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001640 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001641
Rafael Espindola8a403d32012-08-08 14:51:03 +00001642 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001643 return true;
1644
Rafael Espindola761cb062012-06-03 23:57:14 +00001645 // We include the .endmacro in the buffer as our queue to exit the macro
1646 // instantiation.
1647 OS << ".endmacro\n";
1648
Rafael Espindola65366442011-06-05 02:43:45 +00001649 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001650 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001651
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001652 // Create the macro instantiation object and add to the current macro
1653 // instantiation stack.
1654 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001655 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001656 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001657 ActiveMacros.push_back(MI);
1658
1659 // Jump to the macro instantiation and prime the lexer.
1660 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1661 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1662 Lex();
1663
1664 return false;
1665}
1666
1667void AsmParser::HandleMacroExit() {
1668 // Jump to the EndOfStatement we should return to, and consume it.
1669 JumpToLoc(ActiveMacros.back()->ExitLoc);
1670 Lex();
1671
1672 // Pop the instantiation entry.
1673 delete ActiveMacros.back();
1674 ActiveMacros.pop_back();
1675}
1676
Rafael Espindolae71cc862012-01-28 05:57:00 +00001677static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001678 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001679 case MCExpr::Binary: {
1680 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1681 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001682 break;
1683 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001684 case MCExpr::Target:
1685 case MCExpr::Constant:
1686 return false;
1687 case MCExpr::SymbolRef: {
1688 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001689 if (S.isVariable())
1690 return IsUsedIn(Sym, S.getVariableValue());
1691 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001692 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001693 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001694 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001695 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001696
1697 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001698}
1699
Nico Weber4c4c7322011-01-28 03:04:41 +00001700bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001701 // FIXME: Use better location, we should use proper tokens.
1702 SMLoc EqualLoc = Lexer.getLoc();
1703
Daniel Dunbar821e3332009-08-31 08:09:28 +00001704 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001705 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001706 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001707
Rafael Espindolae71cc862012-01-28 05:57:00 +00001708 // Note: we don't count b as used in "a = b". This is to allow
1709 // a = b
1710 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001711
Daniel Dunbar3f872332009-07-28 16:08:33 +00001712 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001713 return TokError("unexpected token in assignment");
1714
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001715 // Error on assignment to '.'.
1716 if (Name == ".") {
1717 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1718 "(use '.space' or '.org').)"));
1719 }
1720
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001721 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001722 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001723
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001724 // Validate that the LHS is allowed to be a variable (either it has not been
1725 // used as a symbol, or it is an absolute symbol).
1726 MCSymbol *Sym = getContext().LookupSymbol(Name);
1727 if (Sym) {
1728 // Diagnose assignment to a label.
1729 //
1730 // FIXME: Diagnostics. Note the location of the definition as a label.
1731 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001732 if (IsUsedIn(Sym, Value))
1733 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1734 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001735 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001736 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1737 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001738 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001739 return Error(EqualLoc, "redefinition of '" + Name + "'");
1740 else if (!Sym->isVariable())
1741 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001742 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001743 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1744 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001745
1746 // Don't count these checks as uses.
1747 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001748 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001749 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001750
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001751 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001752
1753 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001754 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001755
1756 return false;
1757}
1758
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001759/// ParseIdentifier:
1760/// ::= identifier
1761/// ::= string
1762bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001763 // The assembler has relaxed rules for accepting identifiers, in particular we
1764 // allow things like '.globl $foo', which would normally be separate
1765 // tokens. At this level, we have already lexed so we cannot (currently)
1766 // handle this as a context dependent token, instead we detect adjacent tokens
1767 // and return the combined identifier.
1768 if (Lexer.is(AsmToken::Dollar)) {
1769 SMLoc DollarLoc = getLexer().getLoc();
1770
1771 // Consume the dollar sign, and check for a following identifier.
1772 Lex();
1773 if (Lexer.isNot(AsmToken::Identifier))
1774 return true;
1775
1776 // We have a '$' followed by an identifier, make sure they are adjacent.
1777 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1778 return true;
1779
1780 // Construct the joined identifier and consume the token.
1781 Res = StringRef(DollarLoc.getPointer(),
1782 getTok().getIdentifier().size() + 1);
1783 Lex();
1784 return false;
1785 }
1786
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001787 if (Lexer.isNot(AsmToken::Identifier) &&
1788 Lexer.isNot(AsmToken::String))
1789 return true;
1790
Sean Callanan18b83232010-01-19 21:44:56 +00001791 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001792
Sean Callanan79ed1a82010-01-19 20:22:31 +00001793 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001794
1795 return false;
1796}
1797
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001798/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001799/// ::= .equ identifier ',' expression
1800/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001801/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001802bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001803 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001804
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001805 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001806 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001807
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001808 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001809 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001810 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001811
Nico Weber4c4c7322011-01-28 03:04:41 +00001812 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001813}
1814
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001815bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001817
1818 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001819 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001820 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1821 if (Str[i] != '\\') {
1822 Data += Str[i];
1823 continue;
1824 }
1825
1826 // Recognize escaped characters. Note that this escape semantics currently
1827 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1828 ++i;
1829 if (i == e)
1830 return TokError("unexpected backslash at end of string");
1831
1832 // Recognize octal sequences.
1833 if ((unsigned) (Str[i] - '0') <= 7) {
1834 // Consume up to three octal characters.
1835 unsigned Value = Str[i] - '0';
1836
1837 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1838 ++i;
1839 Value = Value * 8 + (Str[i] - '0');
1840
1841 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1842 ++i;
1843 Value = Value * 8 + (Str[i] - '0');
1844 }
1845 }
1846
1847 if (Value > 255)
1848 return TokError("invalid octal escape sequence (out of range)");
1849
1850 Data += (unsigned char) Value;
1851 continue;
1852 }
1853
1854 // Otherwise recognize individual escapes.
1855 switch (Str[i]) {
1856 default:
1857 // Just reject invalid escape sequences for now.
1858 return TokError("invalid escape sequence (unrecognized character)");
1859
1860 case 'b': Data += '\b'; break;
1861 case 'f': Data += '\f'; break;
1862 case 'n': Data += '\n'; break;
1863 case 'r': Data += '\r'; break;
1864 case 't': Data += '\t'; break;
1865 case '"': Data += '"'; break;
1866 case '\\': Data += '\\'; break;
1867 }
1868 }
1869
1870 return false;
1871}
1872
Daniel Dunbara0d14262009-06-24 23:30:00 +00001873/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001874/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1875bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001876 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001877 CheckForValidSection();
1878
Daniel Dunbara0d14262009-06-24 23:30:00 +00001879 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001880 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001881 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001883 std::string Data;
1884 if (ParseEscapedString(Data))
1885 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001886
1887 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001888 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001889 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1890
Sean Callanan79ed1a82010-01-19 20:22:31 +00001891 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001892
1893 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001894 break;
1895
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001896 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001897 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001898 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001899 }
1900 }
1901
Sean Callanan79ed1a82010-01-19 20:22:31 +00001902 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001903 return false;
1904}
1905
1906/// ParseDirectiveValue
1907/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1908bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001909 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001910 CheckForValidSection();
1911
Daniel Dunbara0d14262009-06-24 23:30:00 +00001912 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001913 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001914 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001915 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001916 return true;
1917
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001918 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001919 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1920 assert(Size <= 8 && "Invalid size");
1921 uint64_t IntValue = MCE->getValue();
1922 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1923 return Error(ExprLoc, "literal value out of range for directive");
1924 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1925 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001926 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001927
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001928 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001929 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001930
Daniel Dunbara0d14262009-06-24 23:30:00 +00001931 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001933 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001934 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001935 }
1936 }
1937
Sean Callanan79ed1a82010-01-19 20:22:31 +00001938 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001939 return false;
1940}
1941
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001942/// ParseDirectiveRealValue
1943/// ::= (.single | .double) [ expression (, expression)* ]
1944bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1945 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1946 CheckForValidSection();
1947
1948 for (;;) {
1949 // We don't truly support arithmetic on floating point expressions, so we
1950 // have to manually parse unary prefixes.
1951 bool IsNeg = false;
1952 if (getLexer().is(AsmToken::Minus)) {
1953 Lex();
1954 IsNeg = true;
1955 } else if (getLexer().is(AsmToken::Plus))
1956 Lex();
1957
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001958 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001959 getLexer().isNot(AsmToken::Real) &&
1960 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001961 return TokError("unexpected token in directive");
1962
1963 // Convert to an APFloat.
1964 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001965 StringRef IDVal = getTok().getString();
1966 if (getLexer().is(AsmToken::Identifier)) {
1967 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1968 Value = APFloat::getInf(Semantics);
1969 else if (!IDVal.compare_lower("nan"))
1970 Value = APFloat::getNaN(Semantics, false, ~0);
1971 else
1972 return TokError("invalid floating point literal");
1973 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001974 APFloat::opInvalidOp)
1975 return TokError("invalid floating point literal");
1976 if (IsNeg)
1977 Value.changeSign();
1978
1979 // Consume the numeric token.
1980 Lex();
1981
1982 // Emit the value as an integer.
1983 APInt AsInt = Value.bitcastToAPInt();
1984 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1985 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1986
1987 if (getLexer().is(AsmToken::EndOfStatement))
1988 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001989
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001990 if (getLexer().isNot(AsmToken::Comma))
1991 return TokError("unexpected token in directive");
1992 Lex();
1993 }
1994 }
1995
1996 Lex();
1997 return false;
1998}
1999
Daniel Dunbara0d14262009-06-24 23:30:00 +00002000/// ParseDirectiveSpace
2001/// ::= .space expression [ , expression ]
2002bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002003 CheckForValidSection();
2004
Daniel Dunbara0d14262009-06-24 23:30:00 +00002005 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002006 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002007 return true;
2008
2009 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002010 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2011 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002012 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002013 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002014
Daniel Dunbar475839e2009-06-29 20:37:27 +00002015 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002016 return true;
2017
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002018 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002019 return TokError("unexpected token in '.space' directive");
2020 }
2021
Sean Callanan79ed1a82010-01-19 20:22:31 +00002022 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002023
2024 if (NumBytes <= 0)
2025 return TokError("invalid number of bytes in '.space' directive");
2026
2027 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002028 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002029
2030 return false;
2031}
2032
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002033/// ParseDirectiveZero
2034/// ::= .zero expression
2035bool AsmParser::ParseDirectiveZero() {
2036 CheckForValidSection();
2037
2038 int64_t NumBytes;
2039 if (ParseAbsoluteExpression(NumBytes))
2040 return true;
2041
Rafael Espindolae452b172010-10-05 19:42:57 +00002042 int64_t Val = 0;
2043 if (getLexer().is(AsmToken::Comma)) {
2044 Lex();
2045 if (ParseAbsoluteExpression(Val))
2046 return true;
2047 }
2048
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002049 if (getLexer().isNot(AsmToken::EndOfStatement))
2050 return TokError("unexpected token in '.zero' directive");
2051
2052 Lex();
2053
Rafael Espindolae452b172010-10-05 19:42:57 +00002054 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002055
2056 return false;
2057}
2058
Daniel Dunbara0d14262009-06-24 23:30:00 +00002059/// ParseDirectiveFill
2060/// ::= .fill expression , expression , expression
2061bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002062 CheckForValidSection();
2063
Daniel Dunbara0d14262009-06-24 23:30:00 +00002064 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002065 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002066 return true;
2067
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002068 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002069 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002070 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002071
Daniel Dunbara0d14262009-06-24 23:30:00 +00002072 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002073 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002074 return true;
2075
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002076 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002077 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002078 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002079
Daniel Dunbara0d14262009-06-24 23:30:00 +00002080 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002081 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002082 return true;
2083
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002084 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002085 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002086
Sean Callanan79ed1a82010-01-19 20:22:31 +00002087 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002088
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002089 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2090 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002091
2092 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002093 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002094
2095 return false;
2096}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002097
2098/// ParseDirectiveOrg
2099/// ::= .org expression [ , expression ]
2100bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002101 CheckForValidSection();
2102
Daniel Dunbar821e3332009-08-31 08:09:28 +00002103 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002104 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002105 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002106 return true;
2107
2108 // Parse optional fill expression.
2109 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002110 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2111 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002112 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002113 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002114
Daniel Dunbar475839e2009-06-29 20:37:27 +00002115 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002116 return true;
2117
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002118 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002119 return TokError("unexpected token in '.org' directive");
2120 }
2121
Sean Callanan79ed1a82010-01-19 20:22:31 +00002122 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002123
Jim Grosbachebd4c052012-01-27 00:37:08 +00002124 // Only limited forms of relocatable expressions are accepted here, it
2125 // has to be relative to the current section. The streamer will return
2126 // 'true' if the expression wasn't evaluatable.
2127 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2128 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002129
2130 return false;
2131}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002132
2133/// ParseDirectiveAlign
2134/// ::= {.align, ...} expression [ , expression [ , expression ]]
2135bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002136 CheckForValidSection();
2137
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002138 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002139 int64_t Alignment;
2140 if (ParseAbsoluteExpression(Alignment))
2141 return true;
2142
2143 SMLoc MaxBytesLoc;
2144 bool HasFillExpr = false;
2145 int64_t FillExpr = 0;
2146 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002147 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2148 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002149 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002150 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002151
2152 // The fill expression can be omitted while specifying a maximum number of
2153 // alignment bytes, e.g:
2154 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002155 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002156 HasFillExpr = true;
2157 if (ParseAbsoluteExpression(FillExpr))
2158 return true;
2159 }
2160
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002161 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2162 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002163 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002164 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002165
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002166 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002167 if (ParseAbsoluteExpression(MaxBytesToFill))
2168 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002169
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002170 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002171 return TokError("unexpected token in directive");
2172 }
2173 }
2174
Sean Callanan79ed1a82010-01-19 20:22:31 +00002175 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002176
Daniel Dunbar648ac512010-05-17 21:54:30 +00002177 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002178 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002179
2180 // Compute alignment in bytes.
2181 if (IsPow2) {
2182 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002183 if (Alignment >= 32) {
2184 Error(AlignmentLoc, "invalid alignment value");
2185 Alignment = 31;
2186 }
2187
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002188 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002189 }
2190
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002191 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002192 if (MaxBytesLoc.isValid()) {
2193 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002194 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2195 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002196 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002197 }
2198
2199 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002200 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2201 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002202 MaxBytesToFill = 0;
2203 }
2204 }
2205
Daniel Dunbar648ac512010-05-17 21:54:30 +00002206 // Check whether we should use optimal code alignment for this .align
2207 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002208 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002209 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2210 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002211 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002212 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002213 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002214 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2215 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002216 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002217
2218 return false;
2219}
2220
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002221/// ParseDirectiveSymbolAttribute
2222/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002223bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002224 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002225 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002226 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002227 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002228
2229 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002230 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002231
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002232 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002233
Jim Grosbach10ec6502011-09-15 17:56:49 +00002234 // Assembler local symbols don't make any sense here. Complain loudly.
2235 if (Sym->isTemporary())
2236 return Error(Loc, "non-local symbol required in directive");
2237
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002238 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002239
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002240 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002241 break;
2242
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002243 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002244 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002245 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002246 }
2247 }
2248
Sean Callanan79ed1a82010-01-19 20:22:31 +00002249 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002250 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002251}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002252
2253/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002254/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2255bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002256 CheckForValidSection();
2257
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002258 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002259 StringRef Name;
2260 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002261 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002262
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002263 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002264 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002265
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002266 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002267 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002268 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002269
2270 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002271 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002272 if (ParseAbsoluteExpression(Size))
2273 return true;
2274
2275 int64_t Pow2Alignment = 0;
2276 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002277 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002278 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002279 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002280 if (ParseAbsoluteExpression(Pow2Alignment))
2281 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002282
Chris Lattner258281d2010-01-19 06:22:22 +00002283 // If this target takes alignments in bytes (not log) validate and convert.
2284 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2285 if (!isPowerOf2_64(Pow2Alignment))
2286 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2287 Pow2Alignment = Log2_64(Pow2Alignment);
2288 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002289 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002290
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002291 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002292 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002293
Sean Callanan79ed1a82010-01-19 20:22:31 +00002294 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002295
Chris Lattner1fc3d752009-07-09 17:25:12 +00002296 // NOTE: a size of zero for a .comm should create a undefined symbol
2297 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002298 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002299 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2300 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002301
Eric Christopherc260a3e2010-05-14 01:38:54 +00002302 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002303 // may internally end up wanting an alignment in bytes.
2304 // FIXME: Diagnose overflow.
2305 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002306 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2307 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002308
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002309 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002310 return Error(IDLoc, "invalid symbol redefinition");
2311
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002312 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002313 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002314 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002315 getStreamer().EmitZerofill(Ctx.getMachOSection(
2316 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2317 0, SectionKind::getBSS()),
2318 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002319 return false;
2320 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002321
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002322 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002323 return false;
2324}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002325
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002326/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002327/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002328bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002329 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002330 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002331
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002332 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002333 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002334 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002335
Sean Callanan79ed1a82010-01-19 20:22:31 +00002336 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002337
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002338 if (Str.empty())
2339 Error(Loc, ".abort detected. Assembly stopping.");
2340 else
2341 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002342 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002343
2344 return false;
2345}
Kevin Enderby71148242009-07-14 21:35:03 +00002346
Kevin Enderby1f049b22009-07-14 23:21:55 +00002347/// ParseDirectiveInclude
2348/// ::= .include "filename"
2349bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002350 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002351 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002352
Sean Callanan18b83232010-01-19 21:44:56 +00002353 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002354 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002355 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002356
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002357 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002358 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002359
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002360 // Strip the quotes.
2361 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002362
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002363 // Attempt to switch the lexer to the included file before consuming the end
2364 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002365 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002366 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002367 return true;
2368 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002369
2370 return false;
2371}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002372
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002373/// ParseDirectiveIncbin
2374/// ::= .incbin "filename"
2375bool AsmParser::ParseDirectiveIncbin() {
2376 if (getLexer().isNot(AsmToken::String))
2377 return TokError("expected string in '.incbin' directive");
2378
2379 std::string Filename = getTok().getString();
2380 SMLoc IncbinLoc = getLexer().getLoc();
2381 Lex();
2382
2383 if (getLexer().isNot(AsmToken::EndOfStatement))
2384 return TokError("unexpected token in '.incbin' directive");
2385
2386 // Strip the quotes.
2387 Filename = Filename.substr(1, Filename.size()-2);
2388
2389 // Attempt to process the included file.
2390 if (ProcessIncbinFile(Filename)) {
2391 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2392 return true;
2393 }
2394
2395 return false;
2396}
2397
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002398/// ParseDirectiveIf
2399/// ::= .if expression
2400bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002401 TheCondStack.push_back(TheCondState);
2402 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002403 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002404 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002405 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002406 int64_t ExprValue;
2407 if (ParseAbsoluteExpression(ExprValue))
2408 return true;
2409
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002410 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002411 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002412
Sean Callanan79ed1a82010-01-19 20:22:31 +00002413 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002414
2415 TheCondState.CondMet = ExprValue;
2416 TheCondState.Ignore = !TheCondState.CondMet;
2417 }
2418
2419 return false;
2420}
2421
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002422/// ParseDirectiveIfb
2423/// ::= .ifb string
2424bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2425 TheCondStack.push_back(TheCondState);
2426 TheCondState.TheCond = AsmCond::IfCond;
2427
Benjamin Kramer29739e72012-05-12 16:52:21 +00002428 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002429 EatToEndOfStatement();
2430 } else {
2431 StringRef Str = ParseStringToEndOfStatement();
2432
2433 if (getLexer().isNot(AsmToken::EndOfStatement))
2434 return TokError("unexpected token in '.ifb' directive");
2435
2436 Lex();
2437
2438 TheCondState.CondMet = ExpectBlank == Str.empty();
2439 TheCondState.Ignore = !TheCondState.CondMet;
2440 }
2441
2442 return false;
2443}
2444
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002445/// ParseDirectiveIfc
2446/// ::= .ifc string1, string2
2447bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2448 TheCondStack.push_back(TheCondState);
2449 TheCondState.TheCond = AsmCond::IfCond;
2450
Benjamin Kramer29739e72012-05-12 16:52:21 +00002451 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002452 EatToEndOfStatement();
2453 } else {
2454 StringRef Str1 = ParseStringToComma();
2455
2456 if (getLexer().isNot(AsmToken::Comma))
2457 return TokError("unexpected token in '.ifc' directive");
2458
2459 Lex();
2460
2461 StringRef Str2 = ParseStringToEndOfStatement();
2462
2463 if (getLexer().isNot(AsmToken::EndOfStatement))
2464 return TokError("unexpected token in '.ifc' directive");
2465
2466 Lex();
2467
2468 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2469 TheCondState.Ignore = !TheCondState.CondMet;
2470 }
2471
2472 return false;
2473}
2474
2475/// ParseDirectiveIfdef
2476/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002477bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2478 StringRef Name;
2479 TheCondStack.push_back(TheCondState);
2480 TheCondState.TheCond = AsmCond::IfCond;
2481
2482 if (TheCondState.Ignore) {
2483 EatToEndOfStatement();
2484 } else {
2485 if (ParseIdentifier(Name))
2486 return TokError("expected identifier after '.ifdef'");
2487
2488 Lex();
2489
2490 MCSymbol *Sym = getContext().LookupSymbol(Name);
2491
2492 if (expect_defined)
2493 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2494 else
2495 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2496 TheCondState.Ignore = !TheCondState.CondMet;
2497 }
2498
2499 return false;
2500}
2501
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002502/// ParseDirectiveElseIf
2503/// ::= .elseif expression
2504bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2505 if (TheCondState.TheCond != AsmCond::IfCond &&
2506 TheCondState.TheCond != AsmCond::ElseIfCond)
2507 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2508 " an .elseif");
2509 TheCondState.TheCond = AsmCond::ElseIfCond;
2510
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002511 bool LastIgnoreState = false;
2512 if (!TheCondStack.empty())
2513 LastIgnoreState = TheCondStack.back().Ignore;
2514 if (LastIgnoreState || TheCondState.CondMet) {
2515 TheCondState.Ignore = true;
2516 EatToEndOfStatement();
2517 }
2518 else {
2519 int64_t ExprValue;
2520 if (ParseAbsoluteExpression(ExprValue))
2521 return true;
2522
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002523 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002524 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002525
Sean Callanan79ed1a82010-01-19 20:22:31 +00002526 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002527 TheCondState.CondMet = ExprValue;
2528 TheCondState.Ignore = !TheCondState.CondMet;
2529 }
2530
2531 return false;
2532}
2533
2534/// ParseDirectiveElse
2535/// ::= .else
2536bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002537 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002538 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002539
Sean Callanan79ed1a82010-01-19 20:22:31 +00002540 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002541
2542 if (TheCondState.TheCond != AsmCond::IfCond &&
2543 TheCondState.TheCond != AsmCond::ElseIfCond)
2544 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2545 ".elseif");
2546 TheCondState.TheCond = AsmCond::ElseCond;
2547 bool LastIgnoreState = false;
2548 if (!TheCondStack.empty())
2549 LastIgnoreState = TheCondStack.back().Ignore;
2550 if (LastIgnoreState || TheCondState.CondMet)
2551 TheCondState.Ignore = true;
2552 else
2553 TheCondState.Ignore = false;
2554
2555 return false;
2556}
2557
2558/// ParseDirectiveEndIf
2559/// ::= .endif
2560bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002561 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002562 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002563
Sean Callanan79ed1a82010-01-19 20:22:31 +00002564 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002565
2566 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2567 TheCondStack.empty())
2568 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2569 ".else");
2570 if (!TheCondStack.empty()) {
2571 TheCondState = TheCondStack.back();
2572 TheCondStack.pop_back();
2573 }
2574
2575 return false;
2576}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002577
2578/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002579/// ::= .file [number] filename
2580/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002581bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002582 // FIXME: I'm not sure what this is.
2583 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002584 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002585 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002586 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002587 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002588
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002589 if (FileNumber < 1)
2590 return TokError("file number less than one");
2591 }
2592
Daniel Dunbareceec052010-07-12 17:45:27 +00002593 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002594 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002595
Nick Lewycky44d798d2011-10-17 23:05:28 +00002596 // Usually the directory and filename together, otherwise just the directory.
2597 StringRef Path = getTok().getString();
2598 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002599 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002600
Nick Lewycky44d798d2011-10-17 23:05:28 +00002601 StringRef Directory;
2602 StringRef Filename;
2603 if (getLexer().is(AsmToken::String)) {
2604 if (FileNumber == -1)
2605 return TokError("explicit path specified, but no file number");
2606 Filename = getTok().getString();
2607 Filename = Filename.substr(1, Filename.size()-2);
2608 Directory = Path;
2609 Lex();
2610 } else {
2611 Filename = Path;
2612 }
2613
Daniel Dunbareceec052010-07-12 17:45:27 +00002614 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002615 return TokError("unexpected token in '.file' directive");
2616
Chris Lattnerd32e8032010-01-25 19:02:58 +00002617 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002618 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002619 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002620 if (getContext().getGenDwarfForAssembly() == true)
2621 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2622 "used to generate dwarf debug info for assembly code");
2623
Nick Lewycky44d798d2011-10-17 23:05:28 +00002624 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002625 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002626 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002627
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002628 return false;
2629}
2630
2631/// ParseDirectiveLine
2632/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002633bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002634 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2635 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002636 return TokError("unexpected token in '.line' directive");
2637
Sean Callanan18b83232010-01-19 21:44:56 +00002638 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002639 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002640 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002641
2642 // FIXME: Do something with the .line.
2643 }
2644
Daniel Dunbareceec052010-07-12 17:45:27 +00002645 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002646 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002647
2648 return false;
2649}
2650
2651
2652/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002653/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002654/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2655/// The first number is a file number, must have been previously assigned with
2656/// a .file directive, the second number is the line number and optionally the
2657/// third number is a column position (zero if not specified). The remaining
2658/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002659bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002660
Daniel Dunbareceec052010-07-12 17:45:27 +00002661 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002662 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002663 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002664 if (FileNumber < 1)
2665 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002666 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002667 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002668 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002669
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002670 int64_t LineNumber = 0;
2671 if (getLexer().is(AsmToken::Integer)) {
2672 LineNumber = getTok().getIntVal();
2673 if (LineNumber < 1)
2674 return TokError("line number less than one in '.loc' directive");
2675 Lex();
2676 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002677
2678 int64_t ColumnPos = 0;
2679 if (getLexer().is(AsmToken::Integer)) {
2680 ColumnPos = getTok().getIntVal();
2681 if (ColumnPos < 0)
2682 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002683 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002684 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002685
Kevin Enderbyc0957932010-09-30 16:52:03 +00002686 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002687 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002688 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002689 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2690 for (;;) {
2691 if (getLexer().is(AsmToken::EndOfStatement))
2692 break;
2693
2694 StringRef Name;
2695 SMLoc Loc = getTok().getLoc();
2696 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002697 return TokError("unexpected token in '.loc' directive");
2698
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002699 if (Name == "basic_block")
2700 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2701 else if (Name == "prologue_end")
2702 Flags |= DWARF2_FLAG_PROLOGUE_END;
2703 else if (Name == "epilogue_begin")
2704 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2705 else if (Name == "is_stmt") {
2706 SMLoc Loc = getTok().getLoc();
2707 const MCExpr *Value;
2708 if (getParser().ParseExpression(Value))
2709 return true;
2710 // The expression must be the constant 0 or 1.
2711 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2712 int Value = MCE->getValue();
2713 if (Value == 0)
2714 Flags &= ~DWARF2_FLAG_IS_STMT;
2715 else if (Value == 1)
2716 Flags |= DWARF2_FLAG_IS_STMT;
2717 else
2718 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002719 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002720 else {
2721 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2722 }
2723 }
2724 else if (Name == "isa") {
2725 SMLoc Loc = getTok().getLoc();
2726 const MCExpr *Value;
2727 if (getParser().ParseExpression(Value))
2728 return true;
2729 // The expression must be a constant greater or equal to 0.
2730 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2731 int Value = MCE->getValue();
2732 if (Value < 0)
2733 return Error(Loc, "isa number less than zero");
2734 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002735 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002736 else {
2737 return Error(Loc, "isa number not a constant value");
2738 }
2739 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002740 else if (Name == "discriminator") {
2741 if (getParser().ParseAbsoluteExpression(Discriminator))
2742 return true;
2743 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002744 else {
2745 return Error(Loc, "unknown sub-directive in '.loc' directive");
2746 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002747
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002748 if (getLexer().is(AsmToken::EndOfStatement))
2749 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002750 }
2751 }
2752
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002753 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002754 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002755
2756 return false;
2757}
2758
Daniel Dunbar138abae2010-10-16 04:56:42 +00002759/// ParseDirectiveStabs
2760/// ::= .stabs string, number, number, number
2761bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2762 SMLoc DirectiveLoc) {
2763 return TokError("unsupported directive '" + Directive + "'");
2764}
2765
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002766/// ParseDirectiveCFISections
2767/// ::= .cfi_sections section [, section]
2768bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2769 SMLoc DirectiveLoc) {
2770 StringRef Name;
2771 bool EH = false;
2772 bool Debug = false;
2773
2774 if (getParser().ParseIdentifier(Name))
2775 return TokError("Expected an identifier");
2776
2777 if (Name == ".eh_frame")
2778 EH = true;
2779 else if (Name == ".debug_frame")
2780 Debug = true;
2781
2782 if (getLexer().is(AsmToken::Comma)) {
2783 Lex();
2784
2785 if (getParser().ParseIdentifier(Name))
2786 return TokError("Expected an identifier");
2787
2788 if (Name == ".eh_frame")
2789 EH = true;
2790 else if (Name == ".debug_frame")
2791 Debug = true;
2792 }
2793
2794 getStreamer().EmitCFISections(EH, Debug);
2795
2796 return false;
2797}
2798
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002799/// ParseDirectiveCFIStartProc
2800/// ::= .cfi_startproc
2801bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2802 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002803 getStreamer().EmitCFIStartProc();
2804 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002805}
2806
2807/// ParseDirectiveCFIEndProc
2808/// ::= .cfi_endproc
2809bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002810 getStreamer().EmitCFIEndProc();
2811 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002812}
2813
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002814/// ParseRegisterOrRegisterNumber - parse register name or number.
2815bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2816 SMLoc DirectiveLoc) {
2817 unsigned RegNo;
2818
Jim Grosbach6f888a82011-06-02 17:14:04 +00002819 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002820 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2821 DirectiveLoc))
2822 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002823 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002824 } else
2825 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002826
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002827 return false;
2828}
2829
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002830/// ParseDirectiveCFIDefCfa
2831/// ::= .cfi_def_cfa register, offset
2832bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2833 SMLoc DirectiveLoc) {
2834 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002835 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002836 return true;
2837
2838 if (getLexer().isNot(AsmToken::Comma))
2839 return TokError("unexpected token in directive");
2840 Lex();
2841
2842 int64_t Offset = 0;
2843 if (getParser().ParseAbsoluteExpression(Offset))
2844 return true;
2845
Rafael Espindola066c2f42011-04-12 23:59:07 +00002846 getStreamer().EmitCFIDefCfa(Register, Offset);
2847 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002848}
2849
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002850/// ParseDirectiveCFIDefCfaOffset
2851/// ::= .cfi_def_cfa_offset offset
2852bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2853 SMLoc DirectiveLoc) {
2854 int64_t Offset = 0;
2855 if (getParser().ParseAbsoluteExpression(Offset))
2856 return true;
2857
Rafael Espindola066c2f42011-04-12 23:59:07 +00002858 getStreamer().EmitCFIDefCfaOffset(Offset);
2859 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002860}
2861
2862/// ParseDirectiveCFIAdjustCfaOffset
2863/// ::= .cfi_adjust_cfa_offset adjustment
2864bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2865 SMLoc DirectiveLoc) {
2866 int64_t Adjustment = 0;
2867 if (getParser().ParseAbsoluteExpression(Adjustment))
2868 return true;
2869
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002870 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2871 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002872}
2873
2874/// ParseDirectiveCFIDefCfaRegister
2875/// ::= .cfi_def_cfa_register register
2876bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2877 SMLoc DirectiveLoc) {
2878 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002879 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002880 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002881
Rafael Espindola066c2f42011-04-12 23:59:07 +00002882 getStreamer().EmitCFIDefCfaRegister(Register);
2883 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002884}
2885
2886/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002887/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002888bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2889 int64_t Register = 0;
2890 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002891
2892 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002893 return true;
2894
2895 if (getLexer().isNot(AsmToken::Comma))
2896 return TokError("unexpected token in directive");
2897 Lex();
2898
2899 if (getParser().ParseAbsoluteExpression(Offset))
2900 return true;
2901
Rafael Espindola066c2f42011-04-12 23:59:07 +00002902 getStreamer().EmitCFIOffset(Register, Offset);
2903 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002904}
2905
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002906/// ParseDirectiveCFIRelOffset
2907/// ::= .cfi_rel_offset register, offset
2908bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2909 SMLoc DirectiveLoc) {
2910 int64_t Register = 0;
2911
2912 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2913 return true;
2914
2915 if (getLexer().isNot(AsmToken::Comma))
2916 return TokError("unexpected token in directive");
2917 Lex();
2918
2919 int64_t Offset = 0;
2920 if (getParser().ParseAbsoluteExpression(Offset))
2921 return true;
2922
Rafael Espindola25f492e2011-04-12 16:12:03 +00002923 getStreamer().EmitCFIRelOffset(Register, Offset);
2924 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002925}
2926
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002927static bool isValidEncoding(int64_t Encoding) {
2928 if (Encoding & ~0xff)
2929 return false;
2930
2931 if (Encoding == dwarf::DW_EH_PE_omit)
2932 return true;
2933
2934 const unsigned Format = Encoding & 0xf;
2935 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2936 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2937 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2938 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2939 return false;
2940
Rafael Espindolacaf11582010-12-29 04:31:26 +00002941 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002942 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002943 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002944 return false;
2945
2946 return true;
2947}
2948
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002949/// ParseDirectiveCFIPersonalityOrLsda
2950/// ::= .cfi_personality encoding, [symbol_name]
2951/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002952bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002953 SMLoc DirectiveLoc) {
2954 int64_t Encoding = 0;
2955 if (getParser().ParseAbsoluteExpression(Encoding))
2956 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002957 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002958 return false;
2959
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002960 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002961 return TokError("unsupported encoding.");
2962
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002963 if (getLexer().isNot(AsmToken::Comma))
2964 return TokError("unexpected token in directive");
2965 Lex();
2966
2967 StringRef Name;
2968 if (getParser().ParseIdentifier(Name))
2969 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002970
2971 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2972
2973 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002974 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002975 else {
2976 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002977 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002978 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002979 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002980}
2981
Rafael Espindolafe024d02010-12-28 18:36:23 +00002982/// ParseDirectiveCFIRememberState
2983/// ::= .cfi_remember_state
2984bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2985 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002986 getStreamer().EmitCFIRememberState();
2987 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002988}
2989
2990/// ParseDirectiveCFIRestoreState
2991/// ::= .cfi_remember_state
2992bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2993 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002994 getStreamer().EmitCFIRestoreState();
2995 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002996}
2997
Rafael Espindolac5754392011-04-12 15:31:05 +00002998/// ParseDirectiveCFISameValue
2999/// ::= .cfi_same_value register
3000bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
3001 SMLoc DirectiveLoc) {
3002 int64_t Register = 0;
3003
3004 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3005 return true;
3006
3007 getStreamer().EmitCFISameValue(Register);
3008
3009 return false;
3010}
3011
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003012/// ParseDirectiveCFIRestore
3013/// ::= .cfi_restore register
3014bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003015 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003016 int64_t Register = 0;
3017 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3018 return true;
3019
3020 getStreamer().EmitCFIRestore(Register);
3021
3022 return false;
3023}
3024
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003025/// ParseDirectiveCFIEscape
3026/// ::= .cfi_escape expression[,...]
3027bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003028 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003029 std::string Values;
3030 int64_t CurrValue;
3031 if (getParser().ParseAbsoluteExpression(CurrValue))
3032 return true;
3033
3034 Values.push_back((uint8_t)CurrValue);
3035
3036 while (getLexer().is(AsmToken::Comma)) {
3037 Lex();
3038
3039 if (getParser().ParseAbsoluteExpression(CurrValue))
3040 return true;
3041
3042 Values.push_back((uint8_t)CurrValue);
3043 }
3044
3045 getStreamer().EmitCFIEscape(Values);
3046 return false;
3047}
3048
Rafael Espindola16d7d432012-01-23 21:51:52 +00003049/// ParseDirectiveCFISignalFrame
3050/// ::= .cfi_signal_frame
3051bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3052 SMLoc DirectiveLoc) {
3053 if (getLexer().isNot(AsmToken::EndOfStatement))
3054 return Error(getLexer().getLoc(),
3055 "unexpected token in '" + Directive + "' directive");
3056
3057 getStreamer().EmitCFISignalFrame();
3058
3059 return false;
3060}
3061
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003062/// ParseDirectiveMacrosOnOff
3063/// ::= .macros_on
3064/// ::= .macros_off
3065bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3066 SMLoc DirectiveLoc) {
3067 if (getLexer().isNot(AsmToken::EndOfStatement))
3068 return Error(getLexer().getLoc(),
3069 "unexpected token in '" + Directive + "' directive");
3070
3071 getParser().MacrosEnabled = Directive == ".macros_on";
3072
3073 return false;
3074}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003075
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003076/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003077/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003078bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3079 SMLoc DirectiveLoc) {
3080 StringRef Name;
3081 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003082 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003083
Rafael Espindola8a403d32012-08-08 14:51:03 +00003084 MacroParameters Parameters;
Rafael Espindola65366442011-06-05 02:43:45 +00003085 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003086 for (;;) {
3087 MacroParameter Parameter;
Rafael Espindola65366442011-06-05 02:43:45 +00003088 if (getParser().ParseIdentifier(Parameter))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003089 return TokError("expected identifier in '.macro' directive");
Rafael Espindola65366442011-06-05 02:43:45 +00003090 Parameters.push_back(Parameter);
3091
3092 if (getLexer().isNot(AsmToken::Comma))
3093 break;
3094 Lex();
3095 }
3096 }
3097
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003098 if (getLexer().isNot(AsmToken::EndOfStatement))
3099 return TokError("unexpected token in '.macro' directive");
3100
3101 // Eat the end of statement.
3102 Lex();
3103
3104 AsmToken EndToken, StartToken = getTok();
3105
3106 // Lex the macro definition.
3107 for (;;) {
3108 // Check whether we have reached the end of the file.
3109 if (getLexer().is(AsmToken::Eof))
3110 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3111
3112 // Otherwise, check whether we have reach the .endmacro.
3113 if (getLexer().is(AsmToken::Identifier) &&
3114 (getTok().getIdentifier() == ".endm" ||
3115 getTok().getIdentifier() == ".endmacro")) {
3116 EndToken = getTok();
3117 Lex();
3118 if (getLexer().isNot(AsmToken::EndOfStatement))
3119 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3120 "' directive");
3121 break;
3122 }
3123
3124 // Otherwise, scan til the end of the statement.
3125 getParser().EatToEndOfStatement();
3126 }
3127
3128 if (getParser().MacroMap.lookup(Name)) {
3129 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3130 }
3131
3132 const char *BodyStart = StartToken.getLoc().getPointer();
3133 const char *BodyEnd = EndToken.getLoc().getPointer();
3134 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003135 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003136 return false;
3137}
3138
3139/// ParseDirectiveEndMacro
3140/// ::= .endm
3141/// ::= .endmacro
3142bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003143 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003144 if (getLexer().isNot(AsmToken::EndOfStatement))
3145 return TokError("unexpected token in '" + Directive + "' directive");
3146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003147 // If we are inside a macro instantiation, terminate the current
3148 // instantiation.
3149 if (!getParser().ActiveMacros.empty()) {
3150 getParser().HandleMacroExit();
3151 return false;
3152 }
3153
3154 // Otherwise, this .endmacro is a stray entry in the file; well formed
3155 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003156 return TokError("unexpected '" + Directive + "' in file, "
3157 "no current macro definition");
3158}
3159
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003160/// ParseDirectivePurgeMacro
3161/// ::= .purgem
3162bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3163 SMLoc DirectiveLoc) {
3164 StringRef Name;
3165 if (getParser().ParseIdentifier(Name))
3166 return TokError("expected identifier in '.purgem' directive");
3167
3168 if (getLexer().isNot(AsmToken::EndOfStatement))
3169 return TokError("unexpected token in '.purgem' directive");
3170
3171 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3172 if (I == getParser().MacroMap.end())
3173 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3174
3175 // Undefine the macro.
3176 delete I->getValue();
3177 getParser().MacroMap.erase(I);
3178 return false;
3179}
3180
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003181bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003182 getParser().CheckForValidSection();
3183
3184 const MCExpr *Value;
3185
3186 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003187 return true;
3188
3189 if (getLexer().isNot(AsmToken::EndOfStatement))
3190 return TokError("unexpected token in directive");
3191
3192 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003193 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003194 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003195 getStreamer().EmitULEB128Value(Value);
3196
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003197 return false;
3198}
3199
Rafael Espindola761cb062012-06-03 23:57:14 +00003200Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003201 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003202
Rafael Espindola761cb062012-06-03 23:57:14 +00003203 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003204 for (;;) {
3205 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003206 if (getLexer().is(AsmToken::Eof)) {
3207 Error(DirectiveLoc, "no matching '.endr' in definition");
3208 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003209 }
3210
Rafael Espindola761cb062012-06-03 23:57:14 +00003211 if (Lexer.is(AsmToken::Identifier) &&
3212 (getTok().getIdentifier() == ".rept")) {
3213 ++NestLevel;
3214 }
3215
3216 // Otherwise, check whether we have reached the .endr.
3217 if (Lexer.is(AsmToken::Identifier) &&
3218 getTok().getIdentifier() == ".endr") {
3219 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003220 EndToken = getTok();
3221 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003222 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3223 TokError("unexpected token in '.endr' directive");
3224 return 0;
3225 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003226 break;
3227 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003228 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003229 }
3230
Rafael Espindola761cb062012-06-03 23:57:14 +00003231 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003232 EatToEndOfStatement();
3233 }
3234
3235 const char *BodyStart = StartToken.getLoc().getPointer();
3236 const char *BodyEnd = EndToken.getLoc().getPointer();
3237 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3238
Rafael Espindola761cb062012-06-03 23:57:14 +00003239 // We Are Anonymous.
3240 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003241 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003242 return new Macro(Name, Body, Parameters);
3243}
3244
3245void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3246 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003247 OS << ".endr\n";
3248
3249 MemoryBuffer *Instantiation =
3250 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3251
Rafael Espindola761cb062012-06-03 23:57:14 +00003252 // Create the macro instantiation object and add to the current macro
3253 // instantiation stack.
3254 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3255 getTok().getLoc(),
3256 Instantiation);
3257 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003258
Rafael Espindola761cb062012-06-03 23:57:14 +00003259 // Jump to the macro instantiation and prime the lexer.
3260 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3261 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3262 Lex();
3263}
3264
3265bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3266 int64_t Count;
3267 if (ParseAbsoluteExpression(Count))
3268 return TokError("unexpected token in '.rept' directive");
3269
3270 if (Count < 0)
3271 return TokError("Count is negative");
3272
3273 if (Lexer.isNot(AsmToken::EndOfStatement))
3274 return TokError("unexpected token in '.rept' directive");
3275
3276 // Eat the end of statement.
3277 Lex();
3278
3279 // Lex the rept definition.
3280 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3281 if (!M)
3282 return true;
3283
3284 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3285 // to hold the macro body with substitutions.
3286 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003287 MacroParameters Parameters;
3288 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003289 raw_svector_ostream OS(Buf);
3290 while (Count--) {
3291 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3292 return true;
3293 }
3294 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003295
3296 return false;
3297}
3298
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003299/// ParseDirectiveIrp
3300/// ::= .irp symbol,values
3301bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003302 MacroParameters Parameters;
3303 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003304
3305 if (ParseIdentifier(Parameter))
3306 return TokError("expected identifier in '.irp' directive");
3307
3308 Parameters.push_back(Parameter);
3309
3310 if (Lexer.isNot(AsmToken::Comma))
3311 return TokError("expected comma in '.irp' directive");
3312
3313 Lex();
3314
Rafael Espindola8a403d32012-08-08 14:51:03 +00003315 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003316 if (ParseMacroArguments(0, A))
3317 return true;
3318
3319 // Eat the end of statement.
3320 Lex();
3321
3322 // Lex the irp definition.
3323 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3324 if (!M)
3325 return true;
3326
3327 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3328 // to hold the macro body with substitutions.
3329 SmallString<256> Buf;
3330 raw_svector_ostream OS(Buf);
3331
Rafael Espindola7996d042012-08-21 16:06:48 +00003332 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3333 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003334 Args.push_back(*i);
3335
3336 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3337 return true;
3338 }
3339
3340 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3341
3342 return false;
3343}
3344
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003345/// ParseDirectiveIrpc
3346/// ::= .irpc symbol,values
3347bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003348 MacroParameters Parameters;
3349 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003350
3351 if (ParseIdentifier(Parameter))
3352 return TokError("expected identifier in '.irpc' directive");
3353
3354 Parameters.push_back(Parameter);
3355
3356 if (Lexer.isNot(AsmToken::Comma))
3357 return TokError("expected comma in '.irpc' directive");
3358
3359 Lex();
3360
Rafael Espindola8a403d32012-08-08 14:51:03 +00003361 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003362 if (ParseMacroArguments(0, A))
3363 return true;
3364
3365 if (A.size() != 1 || A.front().size() != 1)
3366 return TokError("unexpected token in '.irpc' directive");
3367
3368 // Eat the end of statement.
3369 Lex();
3370
3371 // Lex the irpc definition.
3372 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3373 if (!M)
3374 return true;
3375
3376 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3377 // to hold the macro body with substitutions.
3378 SmallString<256> Buf;
3379 raw_svector_ostream OS(Buf);
3380
3381 StringRef Values = A.front().front().getString();
3382 std::size_t I, End = Values.size();
3383 for (I = 0; I < End; ++I) {
3384 MacroArgument Arg;
3385 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3386
Rafael Espindola8a403d32012-08-08 14:51:03 +00003387 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003388 Args.push_back(Arg);
3389
3390 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3391 return true;
3392 }
3393
3394 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3395
3396 return false;
3397}
3398
Rafael Espindola761cb062012-06-03 23:57:14 +00003399bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3400 if (ActiveMacros.empty())
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003401 return TokError("unexpected '.endr' directive, no current .rept");
3402
3403 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003404 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003405 assert(getLexer().is(AsmToken::EndOfStatement));
3406
Rafael Espindola761cb062012-06-03 23:57:14 +00003407 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003408 return false;
3409}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003410
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003411/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003412MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003413 MCContext &C, MCStreamer &Out,
3414 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003415 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003416}