blob: f7236627d5df8102b106a350242715133a526de3 [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 Espindola761cb062012-06-03 23:57:14 +00001457bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola8a403d32012-08-08 14:51:03 +00001458 const MacroParameters &Parameters,
1459 const MacroArguments &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001460 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001461 unsigned NParameters = Parameters.size();
1462 if (NParameters != 0 && NParameters != A.size())
1463 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001464
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001465 while (!Body.empty()) {
1466 // Scan for the next substitution.
1467 std::size_t End = Body.size(), Pos = 0;
1468 for (; Pos != End; ++Pos) {
1469 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001470 if (!NParameters) {
1471 // This macro has no parameters, look for $0, $1, etc.
1472 if (Body[Pos] != '$' || Pos + 1 == End)
1473 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001474
Rafael Espindola65366442011-06-05 02:43:45 +00001475 char Next = Body[Pos + 1];
1476 if (Next == '$' || Next == 'n' || isdigit(Next))
1477 break;
1478 } else {
1479 // This macro has parameters, look for \foo, \bar, etc.
1480 if (Body[Pos] == '\\' && Pos + 1 != End)
1481 break;
1482 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001483 }
1484
1485 // Add the prefix.
1486 OS << Body.slice(0, Pos);
1487
1488 // Check if we reached the end.
1489 if (Pos == End)
1490 break;
1491
Rafael Espindola65366442011-06-05 02:43:45 +00001492 if (!NParameters) {
1493 switch (Body[Pos+1]) {
1494 // $$ => $
1495 case '$':
1496 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001497 break;
1498
Rafael Espindola65366442011-06-05 02:43:45 +00001499 // $n => number of arguments
1500 case 'n':
1501 OS << A.size();
1502 break;
1503
1504 // $[0-9] => argument
1505 default: {
1506 // Missing arguments are ignored.
1507 unsigned Index = Body[Pos+1] - '0';
1508 if (Index >= A.size())
1509 break;
1510
1511 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001512 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001513 ie = A[Index].end(); it != ie; ++it)
1514 OS << it->getString();
1515 break;
1516 }
1517 }
1518 Pos += 2;
1519 } else {
1520 unsigned I = Pos + 1;
1521 while (isalnum(Body[I]) && I + 1 != End)
1522 ++I;
1523
1524 const char *Begin = Body.data() + Pos +1;
1525 StringRef Argument(Begin, I - (Pos +1));
1526 unsigned Index = 0;
1527 for (; Index < NParameters; ++Index)
1528 if (Parameters[Index] == Argument)
1529 break;
1530
1531 // FIXME: We should error at the macro definition.
1532 if (Index == NParameters)
1533 return Error(L, "Parameter not found");
1534
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001535 for (MacroArgument::const_iterator it = A[Index].begin(),
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001536 ie = A[Index].end(); it != ie; ++it)
1537 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001538
Rafael Espindola65366442011-06-05 02:43:45 +00001539 Pos += 1 + Argument.size();
1540 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001541 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001542 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001543 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001544
Rafael Espindola65366442011-06-05 02:43:45 +00001545 return false;
1546}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001547
Rafael Espindola65366442011-06-05 02:43:45 +00001548MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1549 MemoryBuffer *I)
1550 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1551{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001552}
1553
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001554/// ParseMacroArgument - Extract AsmTokens for a macro argument.
1555/// This is used for both default macro parameter values and the
1556/// arguments in macro invocations
1557bool AsmParser::ParseMacroArgument(MacroArgument &MA) {
1558 unsigned ParenLevel = 0;
1559
1560 for (;;) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001561 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
1562 return TokError("unexpected token in macro instantiation");
1563
1564 // HandleMacroEntry relies on not advancing the lexer here
1565 // to be able to fill in the remaining default parameter values
1566 if (Lexer.is(AsmToken::EndOfStatement))
1567 break;
1568 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
1569 break;
1570
1571 // Adjust the current parentheses level.
1572 if (Lexer.is(AsmToken::LParen))
1573 ++ParenLevel;
1574 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1575 --ParenLevel;
1576
1577 // Append the token to the current argument list.
1578 MA.push_back(getTok());
1579 Lex();
1580 }
1581 if (ParenLevel != 0)
Rafael Espindola76ac2002012-08-21 15:55:04 +00001582 return TokError("unbalanced parentheses in macro argument");
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001583 return false;
1584}
1585
1586// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001587bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001588 const unsigned NParameters = M ? M->Parameters.size() : 0;
1589
1590 // Parse two kinds of macro invocations:
1591 // - macros defined without any parameters accept an arbitrary number of them
1592 // - macros defined with parameters accept at most that many of them
1593 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1594 ++Parameter) {
1595 MacroArgument MA;
1596
1597 if (ParseMacroArgument(MA))
1598 return true;
1599
Jim Grosbach97146442012-07-30 22:44:17 +00001600 A.push_back(MA);
1601
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001602 if (Lexer.is(AsmToken::EndOfStatement))
1603 return false;
1604
1605 if (Lexer.is(AsmToken::Comma))
1606 Lex();
1607 }
1608 return TokError("Too many arguments");
1609}
1610
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001611bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1612 const Macro *M) {
1613 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1614 // this, although we should protect against infinite loops.
1615 if (ActiveMacros.size() == 20)
1616 return TokError("macros cannot be nested more than 20 levels deep");
1617
Rafael Espindola8a403d32012-08-08 14:51:03 +00001618 MacroArguments A;
1619 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001620 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001621
Jim Grosbach97146442012-07-30 22:44:17 +00001622 // Remove any trailing empty arguments. Do this after-the-fact as we have
1623 // to keep empty arguments in the middle of the list or positionality
1624 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001625 while (!A.empty() && A.back().empty())
1626 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001627
Rafael Espindola65366442011-06-05 02:43:45 +00001628 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1629 // to hold the macro body with substitutions.
1630 SmallString<256> Buf;
1631 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001632 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001633
Rafael Espindola8a403d32012-08-08 14:51:03 +00001634 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001635 return true;
1636
Rafael Espindola761cb062012-06-03 23:57:14 +00001637 // We include the .endmacro in the buffer as our queue to exit the macro
1638 // instantiation.
1639 OS << ".endmacro\n";
1640
Rafael Espindola65366442011-06-05 02:43:45 +00001641 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001642 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001643
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001644 // Create the macro instantiation object and add to the current macro
1645 // instantiation stack.
1646 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001647 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001648 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001649 ActiveMacros.push_back(MI);
1650
1651 // Jump to the macro instantiation and prime the lexer.
1652 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1653 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1654 Lex();
1655
1656 return false;
1657}
1658
1659void AsmParser::HandleMacroExit() {
1660 // Jump to the EndOfStatement we should return to, and consume it.
1661 JumpToLoc(ActiveMacros.back()->ExitLoc);
1662 Lex();
1663
1664 // Pop the instantiation entry.
1665 delete ActiveMacros.back();
1666 ActiveMacros.pop_back();
1667}
1668
Rafael Espindolae71cc862012-01-28 05:57:00 +00001669static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001670 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001671 case MCExpr::Binary: {
1672 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1673 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001674 break;
1675 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001676 case MCExpr::Target:
1677 case MCExpr::Constant:
1678 return false;
1679 case MCExpr::SymbolRef: {
1680 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001681 if (S.isVariable())
1682 return IsUsedIn(Sym, S.getVariableValue());
1683 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001684 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001685 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001686 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001687 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001688
1689 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001690}
1691
Nico Weber4c4c7322011-01-28 03:04:41 +00001692bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001693 // FIXME: Use better location, we should use proper tokens.
1694 SMLoc EqualLoc = Lexer.getLoc();
1695
Daniel Dunbar821e3332009-08-31 08:09:28 +00001696 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001697 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001698 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001699
Rafael Espindolae71cc862012-01-28 05:57:00 +00001700 // Note: we don't count b as used in "a = b". This is to allow
1701 // a = b
1702 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001703
Daniel Dunbar3f872332009-07-28 16:08:33 +00001704 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001705 return TokError("unexpected token in assignment");
1706
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001707 // Error on assignment to '.'.
1708 if (Name == ".") {
1709 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1710 "(use '.space' or '.org').)"));
1711 }
1712
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001713 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001714 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001715
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001716 // Validate that the LHS is allowed to be a variable (either it has not been
1717 // used as a symbol, or it is an absolute symbol).
1718 MCSymbol *Sym = getContext().LookupSymbol(Name);
1719 if (Sym) {
1720 // Diagnose assignment to a label.
1721 //
1722 // FIXME: Diagnostics. Note the location of the definition as a label.
1723 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001724 if (IsUsedIn(Sym, Value))
1725 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1726 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001727 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001728 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1729 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001730 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001731 return Error(EqualLoc, "redefinition of '" + Name + "'");
1732 else if (!Sym->isVariable())
1733 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001734 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001735 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1736 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001737
1738 // Don't count these checks as uses.
1739 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001740 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001741 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001742
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001743 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001744
1745 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001746 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001747
1748 return false;
1749}
1750
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001751/// ParseIdentifier:
1752/// ::= identifier
1753/// ::= string
1754bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001755 // The assembler has relaxed rules for accepting identifiers, in particular we
1756 // allow things like '.globl $foo', which would normally be separate
1757 // tokens. At this level, we have already lexed so we cannot (currently)
1758 // handle this as a context dependent token, instead we detect adjacent tokens
1759 // and return the combined identifier.
1760 if (Lexer.is(AsmToken::Dollar)) {
1761 SMLoc DollarLoc = getLexer().getLoc();
1762
1763 // Consume the dollar sign, and check for a following identifier.
1764 Lex();
1765 if (Lexer.isNot(AsmToken::Identifier))
1766 return true;
1767
1768 // We have a '$' followed by an identifier, make sure they are adjacent.
1769 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1770 return true;
1771
1772 // Construct the joined identifier and consume the token.
1773 Res = StringRef(DollarLoc.getPointer(),
1774 getTok().getIdentifier().size() + 1);
1775 Lex();
1776 return false;
1777 }
1778
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001779 if (Lexer.isNot(AsmToken::Identifier) &&
1780 Lexer.isNot(AsmToken::String))
1781 return true;
1782
Sean Callanan18b83232010-01-19 21:44:56 +00001783 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001784
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001786
1787 return false;
1788}
1789
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001790/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001791/// ::= .equ identifier ',' expression
1792/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001793/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001794bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001795 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001796
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001797 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001798 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001799
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001800 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001801 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001802 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001803
Nico Weber4c4c7322011-01-28 03:04:41 +00001804 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001805}
1806
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001807bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001808 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001809
1810 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001811 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001812 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1813 if (Str[i] != '\\') {
1814 Data += Str[i];
1815 continue;
1816 }
1817
1818 // Recognize escaped characters. Note that this escape semantics currently
1819 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1820 ++i;
1821 if (i == e)
1822 return TokError("unexpected backslash at end of string");
1823
1824 // Recognize octal sequences.
1825 if ((unsigned) (Str[i] - '0') <= 7) {
1826 // Consume up to three octal characters.
1827 unsigned Value = Str[i] - '0';
1828
1829 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1830 ++i;
1831 Value = Value * 8 + (Str[i] - '0');
1832
1833 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1834 ++i;
1835 Value = Value * 8 + (Str[i] - '0');
1836 }
1837 }
1838
1839 if (Value > 255)
1840 return TokError("invalid octal escape sequence (out of range)");
1841
1842 Data += (unsigned char) Value;
1843 continue;
1844 }
1845
1846 // Otherwise recognize individual escapes.
1847 switch (Str[i]) {
1848 default:
1849 // Just reject invalid escape sequences for now.
1850 return TokError("invalid escape sequence (unrecognized character)");
1851
1852 case 'b': Data += '\b'; break;
1853 case 'f': Data += '\f'; break;
1854 case 'n': Data += '\n'; break;
1855 case 'r': Data += '\r'; break;
1856 case 't': Data += '\t'; break;
1857 case '"': Data += '"'; break;
1858 case '\\': Data += '\\'; break;
1859 }
1860 }
1861
1862 return false;
1863}
1864
Daniel Dunbara0d14262009-06-24 23:30:00 +00001865/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001866/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1867bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001868 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001869 CheckForValidSection();
1870
Daniel Dunbara0d14262009-06-24 23:30:00 +00001871 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001872 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001873 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001875 std::string Data;
1876 if (ParseEscapedString(Data))
1877 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001878
1879 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001880 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001881 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1882
Sean Callanan79ed1a82010-01-19 20:22:31 +00001883 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001884
1885 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001886 break;
1887
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001888 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001889 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001890 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001891 }
1892 }
1893
Sean Callanan79ed1a82010-01-19 20:22:31 +00001894 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001895 return false;
1896}
1897
1898/// ParseDirectiveValue
1899/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1900bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001901 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001902 CheckForValidSection();
1903
Daniel Dunbara0d14262009-06-24 23:30:00 +00001904 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001905 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001906 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001907 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001908 return true;
1909
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001910 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001911 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1912 assert(Size <= 8 && "Invalid size");
1913 uint64_t IntValue = MCE->getValue();
1914 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1915 return Error(ExprLoc, "literal value out of range for directive");
1916 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1917 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001918 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001919
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001920 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001921 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001922
Daniel Dunbara0d14262009-06-24 23:30:00 +00001923 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001924 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001925 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001926 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001927 }
1928 }
1929
Sean Callanan79ed1a82010-01-19 20:22:31 +00001930 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001931 return false;
1932}
1933
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001934/// ParseDirectiveRealValue
1935/// ::= (.single | .double) [ expression (, expression)* ]
1936bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1937 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1938 CheckForValidSection();
1939
1940 for (;;) {
1941 // We don't truly support arithmetic on floating point expressions, so we
1942 // have to manually parse unary prefixes.
1943 bool IsNeg = false;
1944 if (getLexer().is(AsmToken::Minus)) {
1945 Lex();
1946 IsNeg = true;
1947 } else if (getLexer().is(AsmToken::Plus))
1948 Lex();
1949
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001950 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001951 getLexer().isNot(AsmToken::Real) &&
1952 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001953 return TokError("unexpected token in directive");
1954
1955 // Convert to an APFloat.
1956 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001957 StringRef IDVal = getTok().getString();
1958 if (getLexer().is(AsmToken::Identifier)) {
1959 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1960 Value = APFloat::getInf(Semantics);
1961 else if (!IDVal.compare_lower("nan"))
1962 Value = APFloat::getNaN(Semantics, false, ~0);
1963 else
1964 return TokError("invalid floating point literal");
1965 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001966 APFloat::opInvalidOp)
1967 return TokError("invalid floating point literal");
1968 if (IsNeg)
1969 Value.changeSign();
1970
1971 // Consume the numeric token.
1972 Lex();
1973
1974 // Emit the value as an integer.
1975 APInt AsInt = Value.bitcastToAPInt();
1976 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1977 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1978
1979 if (getLexer().is(AsmToken::EndOfStatement))
1980 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001981
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001982 if (getLexer().isNot(AsmToken::Comma))
1983 return TokError("unexpected token in directive");
1984 Lex();
1985 }
1986 }
1987
1988 Lex();
1989 return false;
1990}
1991
Daniel Dunbara0d14262009-06-24 23:30:00 +00001992/// ParseDirectiveSpace
1993/// ::= .space expression [ , expression ]
1994bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001995 CheckForValidSection();
1996
Daniel Dunbara0d14262009-06-24 23:30:00 +00001997 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001998 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001999 return true;
2000
2001 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002002 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2003 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002004 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002005 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002006
Daniel Dunbar475839e2009-06-29 20:37:27 +00002007 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002008 return true;
2009
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002010 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002011 return TokError("unexpected token in '.space' directive");
2012 }
2013
Sean Callanan79ed1a82010-01-19 20:22:31 +00002014 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002015
2016 if (NumBytes <= 0)
2017 return TokError("invalid number of bytes in '.space' directive");
2018
2019 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002020 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002021
2022 return false;
2023}
2024
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002025/// ParseDirectiveZero
2026/// ::= .zero expression
2027bool AsmParser::ParseDirectiveZero() {
2028 CheckForValidSection();
2029
2030 int64_t NumBytes;
2031 if (ParseAbsoluteExpression(NumBytes))
2032 return true;
2033
Rafael Espindolae452b172010-10-05 19:42:57 +00002034 int64_t Val = 0;
2035 if (getLexer().is(AsmToken::Comma)) {
2036 Lex();
2037 if (ParseAbsoluteExpression(Val))
2038 return true;
2039 }
2040
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002041 if (getLexer().isNot(AsmToken::EndOfStatement))
2042 return TokError("unexpected token in '.zero' directive");
2043
2044 Lex();
2045
Rafael Espindolae452b172010-10-05 19:42:57 +00002046 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002047
2048 return false;
2049}
2050
Daniel Dunbara0d14262009-06-24 23:30:00 +00002051/// ParseDirectiveFill
2052/// ::= .fill expression , expression , expression
2053bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002054 CheckForValidSection();
2055
Daniel Dunbara0d14262009-06-24 23:30:00 +00002056 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002057 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002058 return true;
2059
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002061 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002062 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002063
Daniel Dunbara0d14262009-06-24 23:30:00 +00002064 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002065 if (ParseAbsoluteExpression(FillSize))
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 FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002073 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002074 return true;
2075
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002076 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002077 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002078
Sean Callanan79ed1a82010-01-19 20:22:31 +00002079 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002080
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002081 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2082 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002083
2084 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002085 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002086
2087 return false;
2088}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002089
2090/// ParseDirectiveOrg
2091/// ::= .org expression [ , expression ]
2092bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002093 CheckForValidSection();
2094
Daniel Dunbar821e3332009-08-31 08:09:28 +00002095 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002096 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002097 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002098 return true;
2099
2100 // Parse optional fill expression.
2101 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002102 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2103 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002104 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002105 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002106
Daniel Dunbar475839e2009-06-29 20:37:27 +00002107 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002108 return true;
2109
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002110 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002111 return TokError("unexpected token in '.org' directive");
2112 }
2113
Sean Callanan79ed1a82010-01-19 20:22:31 +00002114 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002115
Jim Grosbachebd4c052012-01-27 00:37:08 +00002116 // Only limited forms of relocatable expressions are accepted here, it
2117 // has to be relative to the current section. The streamer will return
2118 // 'true' if the expression wasn't evaluatable.
2119 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2120 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002121
2122 return false;
2123}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002124
2125/// ParseDirectiveAlign
2126/// ::= {.align, ...} expression [ , expression [ , expression ]]
2127bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002128 CheckForValidSection();
2129
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002130 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002131 int64_t Alignment;
2132 if (ParseAbsoluteExpression(Alignment))
2133 return true;
2134
2135 SMLoc MaxBytesLoc;
2136 bool HasFillExpr = false;
2137 int64_t FillExpr = 0;
2138 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002139 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2140 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002141 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002143
2144 // The fill expression can be omitted while specifying a maximum number of
2145 // alignment bytes, e.g:
2146 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002147 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002148 HasFillExpr = true;
2149 if (ParseAbsoluteExpression(FillExpr))
2150 return true;
2151 }
2152
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002153 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2154 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002155 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002156 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002157
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002158 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002159 if (ParseAbsoluteExpression(MaxBytesToFill))
2160 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002161
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002162 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002163 return TokError("unexpected token in directive");
2164 }
2165 }
2166
Sean Callanan79ed1a82010-01-19 20:22:31 +00002167 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002168
Daniel Dunbar648ac512010-05-17 21:54:30 +00002169 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002170 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002171
2172 // Compute alignment in bytes.
2173 if (IsPow2) {
2174 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002175 if (Alignment >= 32) {
2176 Error(AlignmentLoc, "invalid alignment value");
2177 Alignment = 31;
2178 }
2179
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002180 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002181 }
2182
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002183 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002184 if (MaxBytesLoc.isValid()) {
2185 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002186 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2187 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002188 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002189 }
2190
2191 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002192 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2193 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002194 MaxBytesToFill = 0;
2195 }
2196 }
2197
Daniel Dunbar648ac512010-05-17 21:54:30 +00002198 // Check whether we should use optimal code alignment for this .align
2199 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002200 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002201 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2202 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002203 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002204 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002205 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002206 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2207 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002208 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002209
2210 return false;
2211}
2212
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002213/// ParseDirectiveSymbolAttribute
2214/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002215bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002216 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002217 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002218 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002219 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002220
2221 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002222 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002223
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002224 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002225
Jim Grosbach10ec6502011-09-15 17:56:49 +00002226 // Assembler local symbols don't make any sense here. Complain loudly.
2227 if (Sym->isTemporary())
2228 return Error(Loc, "non-local symbol required in directive");
2229
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002230 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002231
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002232 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002233 break;
2234
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002235 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002236 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002237 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002238 }
2239 }
2240
Sean Callanan79ed1a82010-01-19 20:22:31 +00002241 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002242 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002243}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002244
2245/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002246/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2247bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002248 CheckForValidSection();
2249
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002250 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002251 StringRef Name;
2252 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002253 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002254
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002255 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002256 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002257
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002258 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002259 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002260 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002261
2262 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002263 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002264 if (ParseAbsoluteExpression(Size))
2265 return true;
2266
2267 int64_t Pow2Alignment = 0;
2268 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002269 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002270 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002271 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002272 if (ParseAbsoluteExpression(Pow2Alignment))
2273 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002274
Chris Lattner258281d2010-01-19 06:22:22 +00002275 // If this target takes alignments in bytes (not log) validate and convert.
2276 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2277 if (!isPowerOf2_64(Pow2Alignment))
2278 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2279 Pow2Alignment = Log2_64(Pow2Alignment);
2280 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002281 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002282
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002283 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002284 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002285
Sean Callanan79ed1a82010-01-19 20:22:31 +00002286 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002287
Chris Lattner1fc3d752009-07-09 17:25:12 +00002288 // NOTE: a size of zero for a .comm should create a undefined symbol
2289 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002290 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002291 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2292 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002293
Eric Christopherc260a3e2010-05-14 01:38:54 +00002294 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002295 // may internally end up wanting an alignment in bytes.
2296 // FIXME: Diagnose overflow.
2297 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002298 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2299 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002300
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002301 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002302 return Error(IDLoc, "invalid symbol redefinition");
2303
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002304 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002305 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002306 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002307 getStreamer().EmitZerofill(Ctx.getMachOSection(
2308 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2309 0, SectionKind::getBSS()),
2310 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002311 return false;
2312 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002313
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002314 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002315 return false;
2316}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002317
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002318/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002319/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002320bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002321 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002322 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002323
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002324 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002325 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002326 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002327
Sean Callanan79ed1a82010-01-19 20:22:31 +00002328 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002329
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002330 if (Str.empty())
2331 Error(Loc, ".abort detected. Assembly stopping.");
2332 else
2333 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002334 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002335
2336 return false;
2337}
Kevin Enderby71148242009-07-14 21:35:03 +00002338
Kevin Enderby1f049b22009-07-14 23:21:55 +00002339/// ParseDirectiveInclude
2340/// ::= .include "filename"
2341bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002342 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002343 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002344
Sean Callanan18b83232010-01-19 21:44:56 +00002345 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002346 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002347 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002348
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002349 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002350 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002351
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002352 // Strip the quotes.
2353 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002354
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002355 // Attempt to switch the lexer to the included file before consuming the end
2356 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002357 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002358 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002359 return true;
2360 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002361
2362 return false;
2363}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002364
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002365/// ParseDirectiveIncbin
2366/// ::= .incbin "filename"
2367bool AsmParser::ParseDirectiveIncbin() {
2368 if (getLexer().isNot(AsmToken::String))
2369 return TokError("expected string in '.incbin' directive");
2370
2371 std::string Filename = getTok().getString();
2372 SMLoc IncbinLoc = getLexer().getLoc();
2373 Lex();
2374
2375 if (getLexer().isNot(AsmToken::EndOfStatement))
2376 return TokError("unexpected token in '.incbin' directive");
2377
2378 // Strip the quotes.
2379 Filename = Filename.substr(1, Filename.size()-2);
2380
2381 // Attempt to process the included file.
2382 if (ProcessIncbinFile(Filename)) {
2383 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2384 return true;
2385 }
2386
2387 return false;
2388}
2389
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002390/// ParseDirectiveIf
2391/// ::= .if expression
2392bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002393 TheCondStack.push_back(TheCondState);
2394 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002395 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002396 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002397 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002398 int64_t ExprValue;
2399 if (ParseAbsoluteExpression(ExprValue))
2400 return true;
2401
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002402 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002403 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002404
Sean Callanan79ed1a82010-01-19 20:22:31 +00002405 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002406
2407 TheCondState.CondMet = ExprValue;
2408 TheCondState.Ignore = !TheCondState.CondMet;
2409 }
2410
2411 return false;
2412}
2413
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002414/// ParseDirectiveIfb
2415/// ::= .ifb string
2416bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2417 TheCondStack.push_back(TheCondState);
2418 TheCondState.TheCond = AsmCond::IfCond;
2419
Benjamin Kramer29739e72012-05-12 16:52:21 +00002420 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002421 EatToEndOfStatement();
2422 } else {
2423 StringRef Str = ParseStringToEndOfStatement();
2424
2425 if (getLexer().isNot(AsmToken::EndOfStatement))
2426 return TokError("unexpected token in '.ifb' directive");
2427
2428 Lex();
2429
2430 TheCondState.CondMet = ExpectBlank == Str.empty();
2431 TheCondState.Ignore = !TheCondState.CondMet;
2432 }
2433
2434 return false;
2435}
2436
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002437/// ParseDirectiveIfc
2438/// ::= .ifc string1, string2
2439bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2440 TheCondStack.push_back(TheCondState);
2441 TheCondState.TheCond = AsmCond::IfCond;
2442
Benjamin Kramer29739e72012-05-12 16:52:21 +00002443 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002444 EatToEndOfStatement();
2445 } else {
2446 StringRef Str1 = ParseStringToComma();
2447
2448 if (getLexer().isNot(AsmToken::Comma))
2449 return TokError("unexpected token in '.ifc' directive");
2450
2451 Lex();
2452
2453 StringRef Str2 = ParseStringToEndOfStatement();
2454
2455 if (getLexer().isNot(AsmToken::EndOfStatement))
2456 return TokError("unexpected token in '.ifc' directive");
2457
2458 Lex();
2459
2460 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2461 TheCondState.Ignore = !TheCondState.CondMet;
2462 }
2463
2464 return false;
2465}
2466
2467/// ParseDirectiveIfdef
2468/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002469bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2470 StringRef Name;
2471 TheCondStack.push_back(TheCondState);
2472 TheCondState.TheCond = AsmCond::IfCond;
2473
2474 if (TheCondState.Ignore) {
2475 EatToEndOfStatement();
2476 } else {
2477 if (ParseIdentifier(Name))
2478 return TokError("expected identifier after '.ifdef'");
2479
2480 Lex();
2481
2482 MCSymbol *Sym = getContext().LookupSymbol(Name);
2483
2484 if (expect_defined)
2485 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2486 else
2487 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2488 TheCondState.Ignore = !TheCondState.CondMet;
2489 }
2490
2491 return false;
2492}
2493
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002494/// ParseDirectiveElseIf
2495/// ::= .elseif expression
2496bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2497 if (TheCondState.TheCond != AsmCond::IfCond &&
2498 TheCondState.TheCond != AsmCond::ElseIfCond)
2499 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2500 " an .elseif");
2501 TheCondState.TheCond = AsmCond::ElseIfCond;
2502
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002503 bool LastIgnoreState = false;
2504 if (!TheCondStack.empty())
2505 LastIgnoreState = TheCondStack.back().Ignore;
2506 if (LastIgnoreState || TheCondState.CondMet) {
2507 TheCondState.Ignore = true;
2508 EatToEndOfStatement();
2509 }
2510 else {
2511 int64_t ExprValue;
2512 if (ParseAbsoluteExpression(ExprValue))
2513 return true;
2514
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002515 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002516 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002517
Sean Callanan79ed1a82010-01-19 20:22:31 +00002518 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002519 TheCondState.CondMet = ExprValue;
2520 TheCondState.Ignore = !TheCondState.CondMet;
2521 }
2522
2523 return false;
2524}
2525
2526/// ParseDirectiveElse
2527/// ::= .else
2528bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002529 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002530 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002531
Sean Callanan79ed1a82010-01-19 20:22:31 +00002532 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002533
2534 if (TheCondState.TheCond != AsmCond::IfCond &&
2535 TheCondState.TheCond != AsmCond::ElseIfCond)
2536 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2537 ".elseif");
2538 TheCondState.TheCond = AsmCond::ElseCond;
2539 bool LastIgnoreState = false;
2540 if (!TheCondStack.empty())
2541 LastIgnoreState = TheCondStack.back().Ignore;
2542 if (LastIgnoreState || TheCondState.CondMet)
2543 TheCondState.Ignore = true;
2544 else
2545 TheCondState.Ignore = false;
2546
2547 return false;
2548}
2549
2550/// ParseDirectiveEndIf
2551/// ::= .endif
2552bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002553 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002554 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002555
Sean Callanan79ed1a82010-01-19 20:22:31 +00002556 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002557
2558 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2559 TheCondStack.empty())
2560 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2561 ".else");
2562 if (!TheCondStack.empty()) {
2563 TheCondState = TheCondStack.back();
2564 TheCondStack.pop_back();
2565 }
2566
2567 return false;
2568}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002569
2570/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002571/// ::= .file [number] filename
2572/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002573bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002574 // FIXME: I'm not sure what this is.
2575 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002576 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002577 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002578 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002579 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002580
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002581 if (FileNumber < 1)
2582 return TokError("file number less than one");
2583 }
2584
Daniel Dunbareceec052010-07-12 17:45:27 +00002585 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002586 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002587
Nick Lewycky44d798d2011-10-17 23:05:28 +00002588 // Usually the directory and filename together, otherwise just the directory.
2589 StringRef Path = getTok().getString();
2590 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002591 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002592
Nick Lewycky44d798d2011-10-17 23:05:28 +00002593 StringRef Directory;
2594 StringRef Filename;
2595 if (getLexer().is(AsmToken::String)) {
2596 if (FileNumber == -1)
2597 return TokError("explicit path specified, but no file number");
2598 Filename = getTok().getString();
2599 Filename = Filename.substr(1, Filename.size()-2);
2600 Directory = Path;
2601 Lex();
2602 } else {
2603 Filename = Path;
2604 }
2605
Daniel Dunbareceec052010-07-12 17:45:27 +00002606 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002607 return TokError("unexpected token in '.file' directive");
2608
Chris Lattnerd32e8032010-01-25 19:02:58 +00002609 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002610 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002611 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002612 if (getContext().getGenDwarfForAssembly() == true)
2613 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2614 "used to generate dwarf debug info for assembly code");
2615
Nick Lewycky44d798d2011-10-17 23:05:28 +00002616 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002617 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002618 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002619
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002620 return false;
2621}
2622
2623/// ParseDirectiveLine
2624/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002625bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002626 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2627 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002628 return TokError("unexpected token in '.line' directive");
2629
Sean Callanan18b83232010-01-19 21:44:56 +00002630 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002631 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002632 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002633
2634 // FIXME: Do something with the .line.
2635 }
2636
Daniel Dunbareceec052010-07-12 17:45:27 +00002637 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002638 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002639
2640 return false;
2641}
2642
2643
2644/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002645/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002646/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2647/// The first number is a file number, must have been previously assigned with
2648/// a .file directive, the second number is the line number and optionally the
2649/// third number is a column position (zero if not specified). The remaining
2650/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002651bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002652
Daniel Dunbareceec052010-07-12 17:45:27 +00002653 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002654 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002655 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002656 if (FileNumber < 1)
2657 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002658 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002659 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002660 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002661
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002662 int64_t LineNumber = 0;
2663 if (getLexer().is(AsmToken::Integer)) {
2664 LineNumber = getTok().getIntVal();
2665 if (LineNumber < 1)
2666 return TokError("line number less than one in '.loc' directive");
2667 Lex();
2668 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002669
2670 int64_t ColumnPos = 0;
2671 if (getLexer().is(AsmToken::Integer)) {
2672 ColumnPos = getTok().getIntVal();
2673 if (ColumnPos < 0)
2674 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002675 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002676 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002677
Kevin Enderbyc0957932010-09-30 16:52:03 +00002678 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002679 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002680 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002681 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2682 for (;;) {
2683 if (getLexer().is(AsmToken::EndOfStatement))
2684 break;
2685
2686 StringRef Name;
2687 SMLoc Loc = getTok().getLoc();
2688 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002689 return TokError("unexpected token in '.loc' directive");
2690
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002691 if (Name == "basic_block")
2692 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2693 else if (Name == "prologue_end")
2694 Flags |= DWARF2_FLAG_PROLOGUE_END;
2695 else if (Name == "epilogue_begin")
2696 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2697 else if (Name == "is_stmt") {
2698 SMLoc Loc = getTok().getLoc();
2699 const MCExpr *Value;
2700 if (getParser().ParseExpression(Value))
2701 return true;
2702 // The expression must be the constant 0 or 1.
2703 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2704 int Value = MCE->getValue();
2705 if (Value == 0)
2706 Flags &= ~DWARF2_FLAG_IS_STMT;
2707 else if (Value == 1)
2708 Flags |= DWARF2_FLAG_IS_STMT;
2709 else
2710 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002711 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002712 else {
2713 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2714 }
2715 }
2716 else if (Name == "isa") {
2717 SMLoc Loc = getTok().getLoc();
2718 const MCExpr *Value;
2719 if (getParser().ParseExpression(Value))
2720 return true;
2721 // The expression must be a constant greater or equal to 0.
2722 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2723 int Value = MCE->getValue();
2724 if (Value < 0)
2725 return Error(Loc, "isa number less than zero");
2726 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002727 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002728 else {
2729 return Error(Loc, "isa number not a constant value");
2730 }
2731 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002732 else if (Name == "discriminator") {
2733 if (getParser().ParseAbsoluteExpression(Discriminator))
2734 return true;
2735 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002736 else {
2737 return Error(Loc, "unknown sub-directive in '.loc' directive");
2738 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002739
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002740 if (getLexer().is(AsmToken::EndOfStatement))
2741 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002742 }
2743 }
2744
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002745 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002746 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002747
2748 return false;
2749}
2750
Daniel Dunbar138abae2010-10-16 04:56:42 +00002751/// ParseDirectiveStabs
2752/// ::= .stabs string, number, number, number
2753bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2754 SMLoc DirectiveLoc) {
2755 return TokError("unsupported directive '" + Directive + "'");
2756}
2757
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002758/// ParseDirectiveCFISections
2759/// ::= .cfi_sections section [, section]
2760bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2761 SMLoc DirectiveLoc) {
2762 StringRef Name;
2763 bool EH = false;
2764 bool Debug = false;
2765
2766 if (getParser().ParseIdentifier(Name))
2767 return TokError("Expected an identifier");
2768
2769 if (Name == ".eh_frame")
2770 EH = true;
2771 else if (Name == ".debug_frame")
2772 Debug = true;
2773
2774 if (getLexer().is(AsmToken::Comma)) {
2775 Lex();
2776
2777 if (getParser().ParseIdentifier(Name))
2778 return TokError("Expected an identifier");
2779
2780 if (Name == ".eh_frame")
2781 EH = true;
2782 else if (Name == ".debug_frame")
2783 Debug = true;
2784 }
2785
2786 getStreamer().EmitCFISections(EH, Debug);
2787
2788 return false;
2789}
2790
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002791/// ParseDirectiveCFIStartProc
2792/// ::= .cfi_startproc
2793bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2794 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002795 getStreamer().EmitCFIStartProc();
2796 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002797}
2798
2799/// ParseDirectiveCFIEndProc
2800/// ::= .cfi_endproc
2801bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002802 getStreamer().EmitCFIEndProc();
2803 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002804}
2805
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002806/// ParseRegisterOrRegisterNumber - parse register name or number.
2807bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2808 SMLoc DirectiveLoc) {
2809 unsigned RegNo;
2810
Jim Grosbach6f888a82011-06-02 17:14:04 +00002811 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002812 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2813 DirectiveLoc))
2814 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002815 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002816 } else
2817 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002818
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002819 return false;
2820}
2821
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002822/// ParseDirectiveCFIDefCfa
2823/// ::= .cfi_def_cfa register, offset
2824bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2825 SMLoc DirectiveLoc) {
2826 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002827 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002828 return true;
2829
2830 if (getLexer().isNot(AsmToken::Comma))
2831 return TokError("unexpected token in directive");
2832 Lex();
2833
2834 int64_t Offset = 0;
2835 if (getParser().ParseAbsoluteExpression(Offset))
2836 return true;
2837
Rafael Espindola066c2f42011-04-12 23:59:07 +00002838 getStreamer().EmitCFIDefCfa(Register, Offset);
2839 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002840}
2841
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002842/// ParseDirectiveCFIDefCfaOffset
2843/// ::= .cfi_def_cfa_offset offset
2844bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2845 SMLoc DirectiveLoc) {
2846 int64_t Offset = 0;
2847 if (getParser().ParseAbsoluteExpression(Offset))
2848 return true;
2849
Rafael Espindola066c2f42011-04-12 23:59:07 +00002850 getStreamer().EmitCFIDefCfaOffset(Offset);
2851 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002852}
2853
2854/// ParseDirectiveCFIAdjustCfaOffset
2855/// ::= .cfi_adjust_cfa_offset adjustment
2856bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2857 SMLoc DirectiveLoc) {
2858 int64_t Adjustment = 0;
2859 if (getParser().ParseAbsoluteExpression(Adjustment))
2860 return true;
2861
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002862 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2863 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002864}
2865
2866/// ParseDirectiveCFIDefCfaRegister
2867/// ::= .cfi_def_cfa_register register
2868bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2869 SMLoc DirectiveLoc) {
2870 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002871 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002872 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002873
Rafael Espindola066c2f42011-04-12 23:59:07 +00002874 getStreamer().EmitCFIDefCfaRegister(Register);
2875 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002876}
2877
2878/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002879/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002880bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2881 int64_t Register = 0;
2882 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002883
2884 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002885 return true;
2886
2887 if (getLexer().isNot(AsmToken::Comma))
2888 return TokError("unexpected token in directive");
2889 Lex();
2890
2891 if (getParser().ParseAbsoluteExpression(Offset))
2892 return true;
2893
Rafael Espindola066c2f42011-04-12 23:59:07 +00002894 getStreamer().EmitCFIOffset(Register, Offset);
2895 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002896}
2897
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002898/// ParseDirectiveCFIRelOffset
2899/// ::= .cfi_rel_offset register, offset
2900bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2901 SMLoc DirectiveLoc) {
2902 int64_t Register = 0;
2903
2904 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2905 return true;
2906
2907 if (getLexer().isNot(AsmToken::Comma))
2908 return TokError("unexpected token in directive");
2909 Lex();
2910
2911 int64_t Offset = 0;
2912 if (getParser().ParseAbsoluteExpression(Offset))
2913 return true;
2914
Rafael Espindola25f492e2011-04-12 16:12:03 +00002915 getStreamer().EmitCFIRelOffset(Register, Offset);
2916 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002917}
2918
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002919static bool isValidEncoding(int64_t Encoding) {
2920 if (Encoding & ~0xff)
2921 return false;
2922
2923 if (Encoding == dwarf::DW_EH_PE_omit)
2924 return true;
2925
2926 const unsigned Format = Encoding & 0xf;
2927 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2928 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2929 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2930 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2931 return false;
2932
Rafael Espindolacaf11582010-12-29 04:31:26 +00002933 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002934 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002935 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002936 return false;
2937
2938 return true;
2939}
2940
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002941/// ParseDirectiveCFIPersonalityOrLsda
2942/// ::= .cfi_personality encoding, [symbol_name]
2943/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002944bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002945 SMLoc DirectiveLoc) {
2946 int64_t Encoding = 0;
2947 if (getParser().ParseAbsoluteExpression(Encoding))
2948 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002949 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002950 return false;
2951
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002952 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002953 return TokError("unsupported encoding.");
2954
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002955 if (getLexer().isNot(AsmToken::Comma))
2956 return TokError("unexpected token in directive");
2957 Lex();
2958
2959 StringRef Name;
2960 if (getParser().ParseIdentifier(Name))
2961 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002962
2963 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2964
2965 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002966 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002967 else {
2968 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002969 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002970 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002971 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002972}
2973
Rafael Espindolafe024d02010-12-28 18:36:23 +00002974/// ParseDirectiveCFIRememberState
2975/// ::= .cfi_remember_state
2976bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2977 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002978 getStreamer().EmitCFIRememberState();
2979 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002980}
2981
2982/// ParseDirectiveCFIRestoreState
2983/// ::= .cfi_remember_state
2984bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2985 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002986 getStreamer().EmitCFIRestoreState();
2987 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002988}
2989
Rafael Espindolac5754392011-04-12 15:31:05 +00002990/// ParseDirectiveCFISameValue
2991/// ::= .cfi_same_value register
2992bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2993 SMLoc DirectiveLoc) {
2994 int64_t Register = 0;
2995
2996 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2997 return true;
2998
2999 getStreamer().EmitCFISameValue(Register);
3000
3001 return false;
3002}
3003
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003004/// ParseDirectiveCFIRestore
3005/// ::= .cfi_restore register
3006bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003007 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003008 int64_t Register = 0;
3009 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3010 return true;
3011
3012 getStreamer().EmitCFIRestore(Register);
3013
3014 return false;
3015}
3016
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003017/// ParseDirectiveCFIEscape
3018/// ::= .cfi_escape expression[,...]
3019bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003020 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003021 std::string Values;
3022 int64_t CurrValue;
3023 if (getParser().ParseAbsoluteExpression(CurrValue))
3024 return true;
3025
3026 Values.push_back((uint8_t)CurrValue);
3027
3028 while (getLexer().is(AsmToken::Comma)) {
3029 Lex();
3030
3031 if (getParser().ParseAbsoluteExpression(CurrValue))
3032 return true;
3033
3034 Values.push_back((uint8_t)CurrValue);
3035 }
3036
3037 getStreamer().EmitCFIEscape(Values);
3038 return false;
3039}
3040
Rafael Espindola16d7d432012-01-23 21:51:52 +00003041/// ParseDirectiveCFISignalFrame
3042/// ::= .cfi_signal_frame
3043bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3044 SMLoc DirectiveLoc) {
3045 if (getLexer().isNot(AsmToken::EndOfStatement))
3046 return Error(getLexer().getLoc(),
3047 "unexpected token in '" + Directive + "' directive");
3048
3049 getStreamer().EmitCFISignalFrame();
3050
3051 return false;
3052}
3053
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003054/// ParseDirectiveMacrosOnOff
3055/// ::= .macros_on
3056/// ::= .macros_off
3057bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3058 SMLoc DirectiveLoc) {
3059 if (getLexer().isNot(AsmToken::EndOfStatement))
3060 return Error(getLexer().getLoc(),
3061 "unexpected token in '" + Directive + "' directive");
3062
3063 getParser().MacrosEnabled = Directive == ".macros_on";
3064
3065 return false;
3066}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003067
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003068/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003069/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003070bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3071 SMLoc DirectiveLoc) {
3072 StringRef Name;
3073 if (getParser().ParseIdentifier(Name))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003074 return TokError("expected identifier in '.macro' directive");
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003075
Rafael Espindola8a403d32012-08-08 14:51:03 +00003076 MacroParameters Parameters;
Rafael Espindola65366442011-06-05 02:43:45 +00003077 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Rafael Espindola7996d042012-08-21 16:06:48 +00003078 for (;;) {
3079 MacroParameter Parameter;
Rafael Espindola65366442011-06-05 02:43:45 +00003080 if (getParser().ParseIdentifier(Parameter))
Rafael Espindolad7ae0f12012-08-21 17:12:05 +00003081 return TokError("expected identifier in '.macro' directive");
Rafael Espindola65366442011-06-05 02:43:45 +00003082 Parameters.push_back(Parameter);
3083
3084 if (getLexer().isNot(AsmToken::Comma))
3085 break;
3086 Lex();
3087 }
3088 }
3089
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003090 if (getLexer().isNot(AsmToken::EndOfStatement))
3091 return TokError("unexpected token in '.macro' directive");
3092
3093 // Eat the end of statement.
3094 Lex();
3095
3096 AsmToken EndToken, StartToken = getTok();
3097
3098 // Lex the macro definition.
3099 for (;;) {
3100 // Check whether we have reached the end of the file.
3101 if (getLexer().is(AsmToken::Eof))
3102 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3103
3104 // Otherwise, check whether we have reach the .endmacro.
3105 if (getLexer().is(AsmToken::Identifier) &&
3106 (getTok().getIdentifier() == ".endm" ||
3107 getTok().getIdentifier() == ".endmacro")) {
3108 EndToken = getTok();
3109 Lex();
3110 if (getLexer().isNot(AsmToken::EndOfStatement))
3111 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3112 "' directive");
3113 break;
3114 }
3115
3116 // Otherwise, scan til the end of the statement.
3117 getParser().EatToEndOfStatement();
3118 }
3119
3120 if (getParser().MacroMap.lookup(Name)) {
3121 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3122 }
3123
3124 const char *BodyStart = StartToken.getLoc().getPointer();
3125 const char *BodyEnd = EndToken.getLoc().getPointer();
3126 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003127 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003128 return false;
3129}
3130
3131/// ParseDirectiveEndMacro
3132/// ::= .endm
3133/// ::= .endmacro
3134bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003135 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003136 if (getLexer().isNot(AsmToken::EndOfStatement))
3137 return TokError("unexpected token in '" + Directive + "' directive");
3138
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003139 // If we are inside a macro instantiation, terminate the current
3140 // instantiation.
3141 if (!getParser().ActiveMacros.empty()) {
3142 getParser().HandleMacroExit();
3143 return false;
3144 }
3145
3146 // Otherwise, this .endmacro is a stray entry in the file; well formed
3147 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003148 return TokError("unexpected '" + Directive + "' in file, "
3149 "no current macro definition");
3150}
3151
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003152/// ParseDirectivePurgeMacro
3153/// ::= .purgem
3154bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3155 SMLoc DirectiveLoc) {
3156 StringRef Name;
3157 if (getParser().ParseIdentifier(Name))
3158 return TokError("expected identifier in '.purgem' directive");
3159
3160 if (getLexer().isNot(AsmToken::EndOfStatement))
3161 return TokError("unexpected token in '.purgem' directive");
3162
3163 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3164 if (I == getParser().MacroMap.end())
3165 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3166
3167 // Undefine the macro.
3168 delete I->getValue();
3169 getParser().MacroMap.erase(I);
3170 return false;
3171}
3172
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003173bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003174 getParser().CheckForValidSection();
3175
3176 const MCExpr *Value;
3177
3178 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003179 return true;
3180
3181 if (getLexer().isNot(AsmToken::EndOfStatement))
3182 return TokError("unexpected token in directive");
3183
3184 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003185 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003186 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003187 getStreamer().EmitULEB128Value(Value);
3188
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003189 return false;
3190}
3191
Rafael Espindola761cb062012-06-03 23:57:14 +00003192Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003193 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003194
Rafael Espindola761cb062012-06-03 23:57:14 +00003195 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003196 for (;;) {
3197 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003198 if (getLexer().is(AsmToken::Eof)) {
3199 Error(DirectiveLoc, "no matching '.endr' in definition");
3200 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003201 }
3202
Rafael Espindola761cb062012-06-03 23:57:14 +00003203 if (Lexer.is(AsmToken::Identifier) &&
3204 (getTok().getIdentifier() == ".rept")) {
3205 ++NestLevel;
3206 }
3207
3208 // Otherwise, check whether we have reached the .endr.
3209 if (Lexer.is(AsmToken::Identifier) &&
3210 getTok().getIdentifier() == ".endr") {
3211 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003212 EndToken = getTok();
3213 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003214 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3215 TokError("unexpected token in '.endr' directive");
3216 return 0;
3217 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003218 break;
3219 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003220 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003221 }
3222
Rafael Espindola761cb062012-06-03 23:57:14 +00003223 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003224 EatToEndOfStatement();
3225 }
3226
3227 const char *BodyStart = StartToken.getLoc().getPointer();
3228 const char *BodyEnd = EndToken.getLoc().getPointer();
3229 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3230
Rafael Espindola761cb062012-06-03 23:57:14 +00003231 // We Are Anonymous.
3232 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003233 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003234 return new Macro(Name, Body, Parameters);
3235}
3236
3237void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3238 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003239 OS << ".endr\n";
3240
3241 MemoryBuffer *Instantiation =
3242 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3243
Rafael Espindola761cb062012-06-03 23:57:14 +00003244 // Create the macro instantiation object and add to the current macro
3245 // instantiation stack.
3246 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3247 getTok().getLoc(),
3248 Instantiation);
3249 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003250
Rafael Espindola761cb062012-06-03 23:57:14 +00003251 // Jump to the macro instantiation and prime the lexer.
3252 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3253 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3254 Lex();
3255}
3256
3257bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3258 int64_t Count;
3259 if (ParseAbsoluteExpression(Count))
3260 return TokError("unexpected token in '.rept' directive");
3261
3262 if (Count < 0)
3263 return TokError("Count is negative");
3264
3265 if (Lexer.isNot(AsmToken::EndOfStatement))
3266 return TokError("unexpected token in '.rept' directive");
3267
3268 // Eat the end of statement.
3269 Lex();
3270
3271 // Lex the rept definition.
3272 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3273 if (!M)
3274 return true;
3275
3276 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3277 // to hold the macro body with substitutions.
3278 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003279 MacroParameters Parameters;
3280 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003281 raw_svector_ostream OS(Buf);
3282 while (Count--) {
3283 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3284 return true;
3285 }
3286 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003287
3288 return false;
3289}
3290
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003291/// ParseDirectiveIrp
3292/// ::= .irp symbol,values
3293bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003294 MacroParameters Parameters;
3295 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003296
3297 if (ParseIdentifier(Parameter))
3298 return TokError("expected identifier in '.irp' directive");
3299
3300 Parameters.push_back(Parameter);
3301
3302 if (Lexer.isNot(AsmToken::Comma))
3303 return TokError("expected comma in '.irp' directive");
3304
3305 Lex();
3306
Rafael Espindola8a403d32012-08-08 14:51:03 +00003307 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003308 if (ParseMacroArguments(0, A))
3309 return true;
3310
3311 // Eat the end of statement.
3312 Lex();
3313
3314 // Lex the irp definition.
3315 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3316 if (!M)
3317 return true;
3318
3319 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3320 // to hold the macro body with substitutions.
3321 SmallString<256> Buf;
3322 raw_svector_ostream OS(Buf);
3323
Rafael Espindola7996d042012-08-21 16:06:48 +00003324 for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) {
3325 MacroArguments Args;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003326 Args.push_back(*i);
3327
3328 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3329 return true;
3330 }
3331
3332 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3333
3334 return false;
3335}
3336
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003337/// ParseDirectiveIrpc
3338/// ::= .irpc symbol,values
3339bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003340 MacroParameters Parameters;
3341 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003342
3343 if (ParseIdentifier(Parameter))
3344 return TokError("expected identifier in '.irpc' directive");
3345
3346 Parameters.push_back(Parameter);
3347
3348 if (Lexer.isNot(AsmToken::Comma))
3349 return TokError("expected comma in '.irpc' directive");
3350
3351 Lex();
3352
Rafael Espindola8a403d32012-08-08 14:51:03 +00003353 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003354 if (ParseMacroArguments(0, A))
3355 return true;
3356
3357 if (A.size() != 1 || A.front().size() != 1)
3358 return TokError("unexpected token in '.irpc' directive");
3359
3360 // Eat the end of statement.
3361 Lex();
3362
3363 // Lex the irpc definition.
3364 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3365 if (!M)
3366 return true;
3367
3368 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3369 // to hold the macro body with substitutions.
3370 SmallString<256> Buf;
3371 raw_svector_ostream OS(Buf);
3372
3373 StringRef Values = A.front().front().getString();
3374 std::size_t I, End = Values.size();
3375 for (I = 0; I < End; ++I) {
3376 MacroArgument Arg;
3377 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3378
Rafael Espindola8a403d32012-08-08 14:51:03 +00003379 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003380 Args.push_back(Arg);
3381
3382 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3383 return true;
3384 }
3385
3386 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3387
3388 return false;
3389}
3390
Rafael Espindola761cb062012-06-03 23:57:14 +00003391bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3392 if (ActiveMacros.empty())
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003393 return TokError("unexpected '.endr' directive, no current .rept");
3394
3395 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003396 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003397 assert(getLexer().is(AsmToken::EndOfStatement));
3398
Rafael Espindola761cb062012-06-03 23:57:14 +00003399 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003400 return false;
3401}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003402
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003403/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003404MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003405 MCContext &C, MCStreamer &Out,
3406 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003407 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003408}