blob: b67c7691138e0bb6162c096eb7f1004f65dda71c [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 (;;) {
1561 SMLoc LastTokenLoc;
1562
1563 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
1564 return TokError("unexpected token in macro instantiation");
1565
1566 // HandleMacroEntry relies on not advancing the lexer here
1567 // to be able to fill in the remaining default parameter values
1568 if (Lexer.is(AsmToken::EndOfStatement))
1569 break;
1570 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
1571 break;
1572
1573 // Adjust the current parentheses level.
1574 if (Lexer.is(AsmToken::LParen))
1575 ++ParenLevel;
1576 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1577 --ParenLevel;
1578
1579 // Append the token to the current argument list.
1580 MA.push_back(getTok());
1581 Lex();
1582 }
1583 if (ParenLevel != 0)
1584 return TokError("unbalanced parenthesises in macro argument");
1585 return false;
1586}
1587
1588// Parse the macro instantiation arguments.
Rafael Espindola8a403d32012-08-08 14:51:03 +00001589bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) {
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001590 const unsigned NParameters = M ? M->Parameters.size() : 0;
1591
1592 // Parse two kinds of macro invocations:
1593 // - macros defined without any parameters accept an arbitrary number of them
1594 // - macros defined with parameters accept at most that many of them
1595 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
1596 ++Parameter) {
1597 MacroArgument MA;
1598
1599 if (ParseMacroArgument(MA))
1600 return true;
1601
Jim Grosbach97146442012-07-30 22:44:17 +00001602 A.push_back(MA);
1603
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001604 if (Lexer.is(AsmToken::EndOfStatement))
1605 return false;
1606
1607 if (Lexer.is(AsmToken::Comma))
1608 Lex();
1609 }
1610 return TokError("Too many arguments");
1611}
1612
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001613bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1614 const Macro *M) {
1615 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1616 // this, although we should protect against infinite loops.
1617 if (ActiveMacros.size() == 20)
1618 return TokError("macros cannot be nested more than 20 levels deep");
1619
Rafael Espindola8a403d32012-08-08 14:51:03 +00001620 MacroArguments A;
1621 if (ParseMacroArguments(M, A))
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00001622 return true;
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001623
Jim Grosbach97146442012-07-30 22:44:17 +00001624 // Remove any trailing empty arguments. Do this after-the-fact as we have
1625 // to keep empty arguments in the middle of the list or positionality
1626 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2,"
Rafael Espindola8a403d32012-08-08 14:51:03 +00001627 while (!A.empty() && A.back().empty())
1628 A.pop_back();
Jim Grosbach97146442012-07-30 22:44:17 +00001629
Rafael Espindola65366442011-06-05 02:43:45 +00001630 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1631 // to hold the macro body with substitutions.
1632 SmallString<256> Buf;
1633 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001634 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001635
Rafael Espindola8a403d32012-08-08 14:51:03 +00001636 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001637 return true;
1638
Rafael Espindola761cb062012-06-03 23:57:14 +00001639 // We include the .endmacro in the buffer as our queue to exit the macro
1640 // instantiation.
1641 OS << ".endmacro\n";
1642
Rafael Espindola65366442011-06-05 02:43:45 +00001643 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001644 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001645
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001646 // Create the macro instantiation object and add to the current macro
1647 // instantiation stack.
1648 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001649 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001650 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001651 ActiveMacros.push_back(MI);
1652
1653 // Jump to the macro instantiation and prime the lexer.
1654 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1655 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1656 Lex();
1657
1658 return false;
1659}
1660
1661void AsmParser::HandleMacroExit() {
1662 // Jump to the EndOfStatement we should return to, and consume it.
1663 JumpToLoc(ActiveMacros.back()->ExitLoc);
1664 Lex();
1665
1666 // Pop the instantiation entry.
1667 delete ActiveMacros.back();
1668 ActiveMacros.pop_back();
1669}
1670
Rafael Espindolae71cc862012-01-28 05:57:00 +00001671static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001672 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001673 case MCExpr::Binary: {
1674 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1675 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001676 break;
1677 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001678 case MCExpr::Target:
1679 case MCExpr::Constant:
1680 return false;
1681 case MCExpr::SymbolRef: {
1682 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001683 if (S.isVariable())
1684 return IsUsedIn(Sym, S.getVariableValue());
1685 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001686 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001687 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001688 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001689 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001690
1691 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001692}
1693
Nico Weber4c4c7322011-01-28 03:04:41 +00001694bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001695 // FIXME: Use better location, we should use proper tokens.
1696 SMLoc EqualLoc = Lexer.getLoc();
1697
Daniel Dunbar821e3332009-08-31 08:09:28 +00001698 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001699 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001700 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001701
Rafael Espindolae71cc862012-01-28 05:57:00 +00001702 // Note: we don't count b as used in "a = b". This is to allow
1703 // a = b
1704 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001705
Daniel Dunbar3f872332009-07-28 16:08:33 +00001706 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001707 return TokError("unexpected token in assignment");
1708
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001709 // Error on assignment to '.'.
1710 if (Name == ".") {
1711 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1712 "(use '.space' or '.org').)"));
1713 }
1714
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001715 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001716 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001717
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001718 // Validate that the LHS is allowed to be a variable (either it has not been
1719 // used as a symbol, or it is an absolute symbol).
1720 MCSymbol *Sym = getContext().LookupSymbol(Name);
1721 if (Sym) {
1722 // Diagnose assignment to a label.
1723 //
1724 // FIXME: Diagnostics. Note the location of the definition as a label.
1725 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001726 if (IsUsedIn(Sym, Value))
1727 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1728 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001729 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001730 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1731 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001732 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001733 return Error(EqualLoc, "redefinition of '" + Name + "'");
1734 else if (!Sym->isVariable())
1735 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001736 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001737 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1738 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001739
1740 // Don't count these checks as uses.
1741 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001742 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001743 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001744
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001745 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001746
1747 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001748 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001749
1750 return false;
1751}
1752
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001753/// ParseIdentifier:
1754/// ::= identifier
1755/// ::= string
1756bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001757 // The assembler has relaxed rules for accepting identifiers, in particular we
1758 // allow things like '.globl $foo', which would normally be separate
1759 // tokens. At this level, we have already lexed so we cannot (currently)
1760 // handle this as a context dependent token, instead we detect adjacent tokens
1761 // and return the combined identifier.
1762 if (Lexer.is(AsmToken::Dollar)) {
1763 SMLoc DollarLoc = getLexer().getLoc();
1764
1765 // Consume the dollar sign, and check for a following identifier.
1766 Lex();
1767 if (Lexer.isNot(AsmToken::Identifier))
1768 return true;
1769
1770 // We have a '$' followed by an identifier, make sure they are adjacent.
1771 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1772 return true;
1773
1774 // Construct the joined identifier and consume the token.
1775 Res = StringRef(DollarLoc.getPointer(),
1776 getTok().getIdentifier().size() + 1);
1777 Lex();
1778 return false;
1779 }
1780
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001781 if (Lexer.isNot(AsmToken::Identifier) &&
1782 Lexer.isNot(AsmToken::String))
1783 return true;
1784
Sean Callanan18b83232010-01-19 21:44:56 +00001785 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001786
Sean Callanan79ed1a82010-01-19 20:22:31 +00001787 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001788
1789 return false;
1790}
1791
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001792/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001793/// ::= .equ identifier ',' expression
1794/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001795/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001796bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001797 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001798
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001799 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001800 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001801
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001802 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001803 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001804 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001805
Nico Weber4c4c7322011-01-28 03:04:41 +00001806 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001807}
1808
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001809bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001810 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001811
1812 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001813 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001814 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1815 if (Str[i] != '\\') {
1816 Data += Str[i];
1817 continue;
1818 }
1819
1820 // Recognize escaped characters. Note that this escape semantics currently
1821 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1822 ++i;
1823 if (i == e)
1824 return TokError("unexpected backslash at end of string");
1825
1826 // Recognize octal sequences.
1827 if ((unsigned) (Str[i] - '0') <= 7) {
1828 // Consume up to three octal characters.
1829 unsigned Value = Str[i] - '0';
1830
1831 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1832 ++i;
1833 Value = Value * 8 + (Str[i] - '0');
1834
1835 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1836 ++i;
1837 Value = Value * 8 + (Str[i] - '0');
1838 }
1839 }
1840
1841 if (Value > 255)
1842 return TokError("invalid octal escape sequence (out of range)");
1843
1844 Data += (unsigned char) Value;
1845 continue;
1846 }
1847
1848 // Otherwise recognize individual escapes.
1849 switch (Str[i]) {
1850 default:
1851 // Just reject invalid escape sequences for now.
1852 return TokError("invalid escape sequence (unrecognized character)");
1853
1854 case 'b': Data += '\b'; break;
1855 case 'f': Data += '\f'; break;
1856 case 'n': Data += '\n'; break;
1857 case 'r': Data += '\r'; break;
1858 case 't': Data += '\t'; break;
1859 case '"': Data += '"'; break;
1860 case '\\': Data += '\\'; break;
1861 }
1862 }
1863
1864 return false;
1865}
1866
Daniel Dunbara0d14262009-06-24 23:30:00 +00001867/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001868/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1869bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001870 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001871 CheckForValidSection();
1872
Daniel Dunbara0d14262009-06-24 23:30:00 +00001873 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001875 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001876
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001877 std::string Data;
1878 if (ParseEscapedString(Data))
1879 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001880
1881 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001882 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001883 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1884
Sean Callanan79ed1a82010-01-19 20:22:31 +00001885 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001886
1887 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001888 break;
1889
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001890 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001891 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001892 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001893 }
1894 }
1895
Sean Callanan79ed1a82010-01-19 20:22:31 +00001896 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001897 return false;
1898}
1899
1900/// ParseDirectiveValue
1901/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1902bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001903 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001904 CheckForValidSection();
1905
Daniel Dunbara0d14262009-06-24 23:30:00 +00001906 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001907 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001908 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001909 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001910 return true;
1911
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001912 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001913 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1914 assert(Size <= 8 && "Invalid size");
1915 uint64_t IntValue = MCE->getValue();
1916 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1917 return Error(ExprLoc, "literal value out of range for directive");
1918 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1919 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001920 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001921
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001922 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001923 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001924
Daniel Dunbara0d14262009-06-24 23:30:00 +00001925 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001926 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001927 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001928 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001929 }
1930 }
1931
Sean Callanan79ed1a82010-01-19 20:22:31 +00001932 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001933 return false;
1934}
1935
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001936/// ParseDirectiveRealValue
1937/// ::= (.single | .double) [ expression (, expression)* ]
1938bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1939 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1940 CheckForValidSection();
1941
1942 for (;;) {
1943 // We don't truly support arithmetic on floating point expressions, so we
1944 // have to manually parse unary prefixes.
1945 bool IsNeg = false;
1946 if (getLexer().is(AsmToken::Minus)) {
1947 Lex();
1948 IsNeg = true;
1949 } else if (getLexer().is(AsmToken::Plus))
1950 Lex();
1951
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001952 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001953 getLexer().isNot(AsmToken::Real) &&
1954 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001955 return TokError("unexpected token in directive");
1956
1957 // Convert to an APFloat.
1958 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001959 StringRef IDVal = getTok().getString();
1960 if (getLexer().is(AsmToken::Identifier)) {
1961 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1962 Value = APFloat::getInf(Semantics);
1963 else if (!IDVal.compare_lower("nan"))
1964 Value = APFloat::getNaN(Semantics, false, ~0);
1965 else
1966 return TokError("invalid floating point literal");
1967 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001968 APFloat::opInvalidOp)
1969 return TokError("invalid floating point literal");
1970 if (IsNeg)
1971 Value.changeSign();
1972
1973 // Consume the numeric token.
1974 Lex();
1975
1976 // Emit the value as an integer.
1977 APInt AsInt = Value.bitcastToAPInt();
1978 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1979 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1980
1981 if (getLexer().is(AsmToken::EndOfStatement))
1982 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001983
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001984 if (getLexer().isNot(AsmToken::Comma))
1985 return TokError("unexpected token in directive");
1986 Lex();
1987 }
1988 }
1989
1990 Lex();
1991 return false;
1992}
1993
Daniel Dunbara0d14262009-06-24 23:30:00 +00001994/// ParseDirectiveSpace
1995/// ::= .space expression [ , expression ]
1996bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001997 CheckForValidSection();
1998
Daniel Dunbara0d14262009-06-24 23:30:00 +00001999 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002000 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002001 return true;
2002
2003 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002004 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2005 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002006 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002007 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002008
Daniel Dunbar475839e2009-06-29 20:37:27 +00002009 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002010 return true;
2011
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002012 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002013 return TokError("unexpected token in '.space' directive");
2014 }
2015
Sean Callanan79ed1a82010-01-19 20:22:31 +00002016 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002017
2018 if (NumBytes <= 0)
2019 return TokError("invalid number of bytes in '.space' directive");
2020
2021 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002022 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002023
2024 return false;
2025}
2026
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002027/// ParseDirectiveZero
2028/// ::= .zero expression
2029bool AsmParser::ParseDirectiveZero() {
2030 CheckForValidSection();
2031
2032 int64_t NumBytes;
2033 if (ParseAbsoluteExpression(NumBytes))
2034 return true;
2035
Rafael Espindolae452b172010-10-05 19:42:57 +00002036 int64_t Val = 0;
2037 if (getLexer().is(AsmToken::Comma)) {
2038 Lex();
2039 if (ParseAbsoluteExpression(Val))
2040 return true;
2041 }
2042
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002043 if (getLexer().isNot(AsmToken::EndOfStatement))
2044 return TokError("unexpected token in '.zero' directive");
2045
2046 Lex();
2047
Rafael Espindolae452b172010-10-05 19:42:57 +00002048 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00002049
2050 return false;
2051}
2052
Daniel Dunbara0d14262009-06-24 23:30:00 +00002053/// ParseDirectiveFill
2054/// ::= .fill expression , expression , expression
2055bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002056 CheckForValidSection();
2057
Daniel Dunbara0d14262009-06-24 23:30:00 +00002058 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002059 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002060 return true;
2061
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002062 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002063 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002064 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002065
Daniel Dunbara0d14262009-06-24 23:30:00 +00002066 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002067 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002068 return true;
2069
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002070 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002071 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002072 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002073
Daniel Dunbara0d14262009-06-24 23:30:00 +00002074 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002075 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002076 return true;
2077
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002078 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002079 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002080
Sean Callanan79ed1a82010-01-19 20:22:31 +00002081 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002082
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002083 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2084 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002085
2086 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002087 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002088
2089 return false;
2090}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002091
2092/// ParseDirectiveOrg
2093/// ::= .org expression [ , expression ]
2094bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002095 CheckForValidSection();
2096
Daniel Dunbar821e3332009-08-31 08:09:28 +00002097 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002098 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002099 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002100 return true;
2101
2102 // Parse optional fill expression.
2103 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002104 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2105 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002106 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002107 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002108
Daniel Dunbar475839e2009-06-29 20:37:27 +00002109 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002110 return true;
2111
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002112 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002113 return TokError("unexpected token in '.org' directive");
2114 }
2115
Sean Callanan79ed1a82010-01-19 20:22:31 +00002116 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002117
Jim Grosbachebd4c052012-01-27 00:37:08 +00002118 // Only limited forms of relocatable expressions are accepted here, it
2119 // has to be relative to the current section. The streamer will return
2120 // 'true' if the expression wasn't evaluatable.
2121 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2122 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002123
2124 return false;
2125}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002126
2127/// ParseDirectiveAlign
2128/// ::= {.align, ...} expression [ , expression [ , expression ]]
2129bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002130 CheckForValidSection();
2131
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002132 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002133 int64_t Alignment;
2134 if (ParseAbsoluteExpression(Alignment))
2135 return true;
2136
2137 SMLoc MaxBytesLoc;
2138 bool HasFillExpr = false;
2139 int64_t FillExpr = 0;
2140 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002141 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2142 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002143 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002144 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002145
2146 // The fill expression can be omitted while specifying a maximum number of
2147 // alignment bytes, e.g:
2148 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002149 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002150 HasFillExpr = true;
2151 if (ParseAbsoluteExpression(FillExpr))
2152 return true;
2153 }
2154
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002155 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2156 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002157 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002158 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002159
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002160 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002161 if (ParseAbsoluteExpression(MaxBytesToFill))
2162 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002163
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002164 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002165 return TokError("unexpected token in directive");
2166 }
2167 }
2168
Sean Callanan79ed1a82010-01-19 20:22:31 +00002169 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002170
Daniel Dunbar648ac512010-05-17 21:54:30 +00002171 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002172 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002173
2174 // Compute alignment in bytes.
2175 if (IsPow2) {
2176 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002177 if (Alignment >= 32) {
2178 Error(AlignmentLoc, "invalid alignment value");
2179 Alignment = 31;
2180 }
2181
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002182 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002183 }
2184
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002185 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002186 if (MaxBytesLoc.isValid()) {
2187 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002188 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2189 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002190 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002191 }
2192
2193 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002194 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2195 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002196 MaxBytesToFill = 0;
2197 }
2198 }
2199
Daniel Dunbar648ac512010-05-17 21:54:30 +00002200 // Check whether we should use optimal code alignment for this .align
2201 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002202 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002203 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2204 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002205 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002206 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002207 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002208 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2209 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002210 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002211
2212 return false;
2213}
2214
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002215/// ParseDirectiveSymbolAttribute
2216/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002217bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002218 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002219 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002220 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002221 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002222
2223 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002224 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002225
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002226 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002227
Jim Grosbach10ec6502011-09-15 17:56:49 +00002228 // Assembler local symbols don't make any sense here. Complain loudly.
2229 if (Sym->isTemporary())
2230 return Error(Loc, "non-local symbol required in directive");
2231
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002232 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002233
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002234 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002235 break;
2236
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002237 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002238 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002239 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002240 }
2241 }
2242
Sean Callanan79ed1a82010-01-19 20:22:31 +00002243 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002244 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002245}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002246
2247/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002248/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2249bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002250 CheckForValidSection();
2251
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002252 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002253 StringRef Name;
2254 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002255 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002256
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002257 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002258 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002259
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002260 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002261 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002262 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002263
2264 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002265 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002266 if (ParseAbsoluteExpression(Size))
2267 return true;
2268
2269 int64_t Pow2Alignment = 0;
2270 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002271 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002272 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002273 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002274 if (ParseAbsoluteExpression(Pow2Alignment))
2275 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002276
Chris Lattner258281d2010-01-19 06:22:22 +00002277 // If this target takes alignments in bytes (not log) validate and convert.
2278 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2279 if (!isPowerOf2_64(Pow2Alignment))
2280 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2281 Pow2Alignment = Log2_64(Pow2Alignment);
2282 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002283 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002284
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002285 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002286 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002287
Sean Callanan79ed1a82010-01-19 20:22:31 +00002288 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002289
Chris Lattner1fc3d752009-07-09 17:25:12 +00002290 // NOTE: a size of zero for a .comm should create a undefined symbol
2291 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002292 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002293 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2294 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002295
Eric Christopherc260a3e2010-05-14 01:38:54 +00002296 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002297 // may internally end up wanting an alignment in bytes.
2298 // FIXME: Diagnose overflow.
2299 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002300 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2301 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002302
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002303 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002304 return Error(IDLoc, "invalid symbol redefinition");
2305
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002306 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002307 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002308 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002309 getStreamer().EmitZerofill(Ctx.getMachOSection(
2310 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2311 0, SectionKind::getBSS()),
2312 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002313 return false;
2314 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002315
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002316 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002317 return false;
2318}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002319
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002320/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002321/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002322bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002323 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002324 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002325
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002326 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002327 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002328 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002329
Sean Callanan79ed1a82010-01-19 20:22:31 +00002330 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002331
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002332 if (Str.empty())
2333 Error(Loc, ".abort detected. Assembly stopping.");
2334 else
2335 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002336 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002337
2338 return false;
2339}
Kevin Enderby71148242009-07-14 21:35:03 +00002340
Kevin Enderby1f049b22009-07-14 23:21:55 +00002341/// ParseDirectiveInclude
2342/// ::= .include "filename"
2343bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002344 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002345 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002346
Sean Callanan18b83232010-01-19 21:44:56 +00002347 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002348 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002349 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002350
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002351 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002352 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002353
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002354 // Strip the quotes.
2355 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002356
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002357 // Attempt to switch the lexer to the included file before consuming the end
2358 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002359 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002360 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002361 return true;
2362 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002363
2364 return false;
2365}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002366
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002367/// ParseDirectiveIncbin
2368/// ::= .incbin "filename"
2369bool AsmParser::ParseDirectiveIncbin() {
2370 if (getLexer().isNot(AsmToken::String))
2371 return TokError("expected string in '.incbin' directive");
2372
2373 std::string Filename = getTok().getString();
2374 SMLoc IncbinLoc = getLexer().getLoc();
2375 Lex();
2376
2377 if (getLexer().isNot(AsmToken::EndOfStatement))
2378 return TokError("unexpected token in '.incbin' directive");
2379
2380 // Strip the quotes.
2381 Filename = Filename.substr(1, Filename.size()-2);
2382
2383 // Attempt to process the included file.
2384 if (ProcessIncbinFile(Filename)) {
2385 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2386 return true;
2387 }
2388
2389 return false;
2390}
2391
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002392/// ParseDirectiveIf
2393/// ::= .if expression
2394bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002395 TheCondStack.push_back(TheCondState);
2396 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002397 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002398 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002399 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002400 int64_t ExprValue;
2401 if (ParseAbsoluteExpression(ExprValue))
2402 return true;
2403
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002404 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002405 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002406
Sean Callanan79ed1a82010-01-19 20:22:31 +00002407 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002408
2409 TheCondState.CondMet = ExprValue;
2410 TheCondState.Ignore = !TheCondState.CondMet;
2411 }
2412
2413 return false;
2414}
2415
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002416/// ParseDirectiveIfb
2417/// ::= .ifb string
2418bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2419 TheCondStack.push_back(TheCondState);
2420 TheCondState.TheCond = AsmCond::IfCond;
2421
Benjamin Kramer29739e72012-05-12 16:52:21 +00002422 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002423 EatToEndOfStatement();
2424 } else {
2425 StringRef Str = ParseStringToEndOfStatement();
2426
2427 if (getLexer().isNot(AsmToken::EndOfStatement))
2428 return TokError("unexpected token in '.ifb' directive");
2429
2430 Lex();
2431
2432 TheCondState.CondMet = ExpectBlank == Str.empty();
2433 TheCondState.Ignore = !TheCondState.CondMet;
2434 }
2435
2436 return false;
2437}
2438
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002439/// ParseDirectiveIfc
2440/// ::= .ifc string1, string2
2441bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2442 TheCondStack.push_back(TheCondState);
2443 TheCondState.TheCond = AsmCond::IfCond;
2444
Benjamin Kramer29739e72012-05-12 16:52:21 +00002445 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002446 EatToEndOfStatement();
2447 } else {
2448 StringRef Str1 = ParseStringToComma();
2449
2450 if (getLexer().isNot(AsmToken::Comma))
2451 return TokError("unexpected token in '.ifc' directive");
2452
2453 Lex();
2454
2455 StringRef Str2 = ParseStringToEndOfStatement();
2456
2457 if (getLexer().isNot(AsmToken::EndOfStatement))
2458 return TokError("unexpected token in '.ifc' directive");
2459
2460 Lex();
2461
2462 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2463 TheCondState.Ignore = !TheCondState.CondMet;
2464 }
2465
2466 return false;
2467}
2468
2469/// ParseDirectiveIfdef
2470/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002471bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2472 StringRef Name;
2473 TheCondStack.push_back(TheCondState);
2474 TheCondState.TheCond = AsmCond::IfCond;
2475
2476 if (TheCondState.Ignore) {
2477 EatToEndOfStatement();
2478 } else {
2479 if (ParseIdentifier(Name))
2480 return TokError("expected identifier after '.ifdef'");
2481
2482 Lex();
2483
2484 MCSymbol *Sym = getContext().LookupSymbol(Name);
2485
2486 if (expect_defined)
2487 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2488 else
2489 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2490 TheCondState.Ignore = !TheCondState.CondMet;
2491 }
2492
2493 return false;
2494}
2495
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002496/// ParseDirectiveElseIf
2497/// ::= .elseif expression
2498bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2499 if (TheCondState.TheCond != AsmCond::IfCond &&
2500 TheCondState.TheCond != AsmCond::ElseIfCond)
2501 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2502 " an .elseif");
2503 TheCondState.TheCond = AsmCond::ElseIfCond;
2504
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002505 bool LastIgnoreState = false;
2506 if (!TheCondStack.empty())
2507 LastIgnoreState = TheCondStack.back().Ignore;
2508 if (LastIgnoreState || TheCondState.CondMet) {
2509 TheCondState.Ignore = true;
2510 EatToEndOfStatement();
2511 }
2512 else {
2513 int64_t ExprValue;
2514 if (ParseAbsoluteExpression(ExprValue))
2515 return true;
2516
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002517 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002518 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002519
Sean Callanan79ed1a82010-01-19 20:22:31 +00002520 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002521 TheCondState.CondMet = ExprValue;
2522 TheCondState.Ignore = !TheCondState.CondMet;
2523 }
2524
2525 return false;
2526}
2527
2528/// ParseDirectiveElse
2529/// ::= .else
2530bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002531 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002532 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002533
Sean Callanan79ed1a82010-01-19 20:22:31 +00002534 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002535
2536 if (TheCondState.TheCond != AsmCond::IfCond &&
2537 TheCondState.TheCond != AsmCond::ElseIfCond)
2538 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2539 ".elseif");
2540 TheCondState.TheCond = AsmCond::ElseCond;
2541 bool LastIgnoreState = false;
2542 if (!TheCondStack.empty())
2543 LastIgnoreState = TheCondStack.back().Ignore;
2544 if (LastIgnoreState || TheCondState.CondMet)
2545 TheCondState.Ignore = true;
2546 else
2547 TheCondState.Ignore = false;
2548
2549 return false;
2550}
2551
2552/// ParseDirectiveEndIf
2553/// ::= .endif
2554bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002555 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002556 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002557
Sean Callanan79ed1a82010-01-19 20:22:31 +00002558 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002559
2560 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2561 TheCondStack.empty())
2562 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2563 ".else");
2564 if (!TheCondStack.empty()) {
2565 TheCondState = TheCondStack.back();
2566 TheCondStack.pop_back();
2567 }
2568
2569 return false;
2570}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002571
2572/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002573/// ::= .file [number] filename
2574/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002575bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002576 // FIXME: I'm not sure what this is.
2577 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002578 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002579 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002580 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002581 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002582
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002583 if (FileNumber < 1)
2584 return TokError("file number less than one");
2585 }
2586
Daniel Dunbareceec052010-07-12 17:45:27 +00002587 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002588 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002589
Nick Lewycky44d798d2011-10-17 23:05:28 +00002590 // Usually the directory and filename together, otherwise just the directory.
2591 StringRef Path = getTok().getString();
2592 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002593 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002594
Nick Lewycky44d798d2011-10-17 23:05:28 +00002595 StringRef Directory;
2596 StringRef Filename;
2597 if (getLexer().is(AsmToken::String)) {
2598 if (FileNumber == -1)
2599 return TokError("explicit path specified, but no file number");
2600 Filename = getTok().getString();
2601 Filename = Filename.substr(1, Filename.size()-2);
2602 Directory = Path;
2603 Lex();
2604 } else {
2605 Filename = Path;
2606 }
2607
Daniel Dunbareceec052010-07-12 17:45:27 +00002608 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002609 return TokError("unexpected token in '.file' directive");
2610
Chris Lattnerd32e8032010-01-25 19:02:58 +00002611 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002612 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002613 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002614 if (getContext().getGenDwarfForAssembly() == true)
2615 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2616 "used to generate dwarf debug info for assembly code");
2617
Nick Lewycky44d798d2011-10-17 23:05:28 +00002618 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002619 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002620 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002621
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002622 return false;
2623}
2624
2625/// ParseDirectiveLine
2626/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002627bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002628 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2629 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002630 return TokError("unexpected token in '.line' directive");
2631
Sean Callanan18b83232010-01-19 21:44:56 +00002632 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002633 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002634 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002635
2636 // FIXME: Do something with the .line.
2637 }
2638
Daniel Dunbareceec052010-07-12 17:45:27 +00002639 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002640 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002641
2642 return false;
2643}
2644
2645
2646/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002647/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002648/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2649/// The first number is a file number, must have been previously assigned with
2650/// a .file directive, the second number is the line number and optionally the
2651/// third number is a column position (zero if not specified). The remaining
2652/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002653bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002654
Daniel Dunbareceec052010-07-12 17:45:27 +00002655 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002656 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002657 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002658 if (FileNumber < 1)
2659 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002660 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002661 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002662 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002663
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002664 int64_t LineNumber = 0;
2665 if (getLexer().is(AsmToken::Integer)) {
2666 LineNumber = getTok().getIntVal();
2667 if (LineNumber < 1)
2668 return TokError("line number less than one in '.loc' directive");
2669 Lex();
2670 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002671
2672 int64_t ColumnPos = 0;
2673 if (getLexer().is(AsmToken::Integer)) {
2674 ColumnPos = getTok().getIntVal();
2675 if (ColumnPos < 0)
2676 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002677 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002678 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002679
Kevin Enderbyc0957932010-09-30 16:52:03 +00002680 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002681 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002682 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002683 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2684 for (;;) {
2685 if (getLexer().is(AsmToken::EndOfStatement))
2686 break;
2687
2688 StringRef Name;
2689 SMLoc Loc = getTok().getLoc();
2690 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002691 return TokError("unexpected token in '.loc' directive");
2692
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002693 if (Name == "basic_block")
2694 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2695 else if (Name == "prologue_end")
2696 Flags |= DWARF2_FLAG_PROLOGUE_END;
2697 else if (Name == "epilogue_begin")
2698 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2699 else if (Name == "is_stmt") {
2700 SMLoc Loc = getTok().getLoc();
2701 const MCExpr *Value;
2702 if (getParser().ParseExpression(Value))
2703 return true;
2704 // The expression must be the constant 0 or 1.
2705 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2706 int Value = MCE->getValue();
2707 if (Value == 0)
2708 Flags &= ~DWARF2_FLAG_IS_STMT;
2709 else if (Value == 1)
2710 Flags |= DWARF2_FLAG_IS_STMT;
2711 else
2712 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002713 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002714 else {
2715 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2716 }
2717 }
2718 else if (Name == "isa") {
2719 SMLoc Loc = getTok().getLoc();
2720 const MCExpr *Value;
2721 if (getParser().ParseExpression(Value))
2722 return true;
2723 // The expression must be a constant greater or equal to 0.
2724 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2725 int Value = MCE->getValue();
2726 if (Value < 0)
2727 return Error(Loc, "isa number less than zero");
2728 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002729 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002730 else {
2731 return Error(Loc, "isa number not a constant value");
2732 }
2733 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002734 else if (Name == "discriminator") {
2735 if (getParser().ParseAbsoluteExpression(Discriminator))
2736 return true;
2737 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002738 else {
2739 return Error(Loc, "unknown sub-directive in '.loc' directive");
2740 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002741
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002742 if (getLexer().is(AsmToken::EndOfStatement))
2743 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002744 }
2745 }
2746
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002747 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002748 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002749
2750 return false;
2751}
2752
Daniel Dunbar138abae2010-10-16 04:56:42 +00002753/// ParseDirectiveStabs
2754/// ::= .stabs string, number, number, number
2755bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2756 SMLoc DirectiveLoc) {
2757 return TokError("unsupported directive '" + Directive + "'");
2758}
2759
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002760/// ParseDirectiveCFISections
2761/// ::= .cfi_sections section [, section]
2762bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2763 SMLoc DirectiveLoc) {
2764 StringRef Name;
2765 bool EH = false;
2766 bool Debug = false;
2767
2768 if (getParser().ParseIdentifier(Name))
2769 return TokError("Expected an identifier");
2770
2771 if (Name == ".eh_frame")
2772 EH = true;
2773 else if (Name == ".debug_frame")
2774 Debug = true;
2775
2776 if (getLexer().is(AsmToken::Comma)) {
2777 Lex();
2778
2779 if (getParser().ParseIdentifier(Name))
2780 return TokError("Expected an identifier");
2781
2782 if (Name == ".eh_frame")
2783 EH = true;
2784 else if (Name == ".debug_frame")
2785 Debug = true;
2786 }
2787
2788 getStreamer().EmitCFISections(EH, Debug);
2789
2790 return false;
2791}
2792
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002793/// ParseDirectiveCFIStartProc
2794/// ::= .cfi_startproc
2795bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2796 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002797 getStreamer().EmitCFIStartProc();
2798 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002799}
2800
2801/// ParseDirectiveCFIEndProc
2802/// ::= .cfi_endproc
2803bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002804 getStreamer().EmitCFIEndProc();
2805 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002806}
2807
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002808/// ParseRegisterOrRegisterNumber - parse register name or number.
2809bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2810 SMLoc DirectiveLoc) {
2811 unsigned RegNo;
2812
Jim Grosbach6f888a82011-06-02 17:14:04 +00002813 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002814 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2815 DirectiveLoc))
2816 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002817 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002818 } else
2819 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002820
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002821 return false;
2822}
2823
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002824/// ParseDirectiveCFIDefCfa
2825/// ::= .cfi_def_cfa register, offset
2826bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2827 SMLoc DirectiveLoc) {
2828 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002829 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002830 return true;
2831
2832 if (getLexer().isNot(AsmToken::Comma))
2833 return TokError("unexpected token in directive");
2834 Lex();
2835
2836 int64_t Offset = 0;
2837 if (getParser().ParseAbsoluteExpression(Offset))
2838 return true;
2839
Rafael Espindola066c2f42011-04-12 23:59:07 +00002840 getStreamer().EmitCFIDefCfa(Register, Offset);
2841 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002842}
2843
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002844/// ParseDirectiveCFIDefCfaOffset
2845/// ::= .cfi_def_cfa_offset offset
2846bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2847 SMLoc DirectiveLoc) {
2848 int64_t Offset = 0;
2849 if (getParser().ParseAbsoluteExpression(Offset))
2850 return true;
2851
Rafael Espindola066c2f42011-04-12 23:59:07 +00002852 getStreamer().EmitCFIDefCfaOffset(Offset);
2853 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002854}
2855
2856/// ParseDirectiveCFIAdjustCfaOffset
2857/// ::= .cfi_adjust_cfa_offset adjustment
2858bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2859 SMLoc DirectiveLoc) {
2860 int64_t Adjustment = 0;
2861 if (getParser().ParseAbsoluteExpression(Adjustment))
2862 return true;
2863
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002864 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2865 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002866}
2867
2868/// ParseDirectiveCFIDefCfaRegister
2869/// ::= .cfi_def_cfa_register register
2870bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2871 SMLoc DirectiveLoc) {
2872 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002873 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002874 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002875
Rafael Espindola066c2f42011-04-12 23:59:07 +00002876 getStreamer().EmitCFIDefCfaRegister(Register);
2877 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002878}
2879
2880/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002881/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002882bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2883 int64_t Register = 0;
2884 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002885
2886 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002887 return true;
2888
2889 if (getLexer().isNot(AsmToken::Comma))
2890 return TokError("unexpected token in directive");
2891 Lex();
2892
2893 if (getParser().ParseAbsoluteExpression(Offset))
2894 return true;
2895
Rafael Espindola066c2f42011-04-12 23:59:07 +00002896 getStreamer().EmitCFIOffset(Register, Offset);
2897 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002898}
2899
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002900/// ParseDirectiveCFIRelOffset
2901/// ::= .cfi_rel_offset register, offset
2902bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2903 SMLoc DirectiveLoc) {
2904 int64_t Register = 0;
2905
2906 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2907 return true;
2908
2909 if (getLexer().isNot(AsmToken::Comma))
2910 return TokError("unexpected token in directive");
2911 Lex();
2912
2913 int64_t Offset = 0;
2914 if (getParser().ParseAbsoluteExpression(Offset))
2915 return true;
2916
Rafael Espindola25f492e2011-04-12 16:12:03 +00002917 getStreamer().EmitCFIRelOffset(Register, Offset);
2918 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002919}
2920
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002921static bool isValidEncoding(int64_t Encoding) {
2922 if (Encoding & ~0xff)
2923 return false;
2924
2925 if (Encoding == dwarf::DW_EH_PE_omit)
2926 return true;
2927
2928 const unsigned Format = Encoding & 0xf;
2929 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2930 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2931 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2932 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2933 return false;
2934
Rafael Espindolacaf11582010-12-29 04:31:26 +00002935 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002936 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002937 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002938 return false;
2939
2940 return true;
2941}
2942
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002943/// ParseDirectiveCFIPersonalityOrLsda
2944/// ::= .cfi_personality encoding, [symbol_name]
2945/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002946bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002947 SMLoc DirectiveLoc) {
2948 int64_t Encoding = 0;
2949 if (getParser().ParseAbsoluteExpression(Encoding))
2950 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002951 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002952 return false;
2953
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002954 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002955 return TokError("unsupported encoding.");
2956
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002957 if (getLexer().isNot(AsmToken::Comma))
2958 return TokError("unexpected token in directive");
2959 Lex();
2960
2961 StringRef Name;
2962 if (getParser().ParseIdentifier(Name))
2963 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002964
2965 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2966
2967 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002968 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002969 else {
2970 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002971 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002972 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002973 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002974}
2975
Rafael Espindolafe024d02010-12-28 18:36:23 +00002976/// ParseDirectiveCFIRememberState
2977/// ::= .cfi_remember_state
2978bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2979 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002980 getStreamer().EmitCFIRememberState();
2981 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002982}
2983
2984/// ParseDirectiveCFIRestoreState
2985/// ::= .cfi_remember_state
2986bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2987 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002988 getStreamer().EmitCFIRestoreState();
2989 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002990}
2991
Rafael Espindolac5754392011-04-12 15:31:05 +00002992/// ParseDirectiveCFISameValue
2993/// ::= .cfi_same_value register
2994bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2995 SMLoc DirectiveLoc) {
2996 int64_t Register = 0;
2997
2998 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2999 return true;
3000
3001 getStreamer().EmitCFISameValue(Register);
3002
3003 return false;
3004}
3005
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003006/// ParseDirectiveCFIRestore
3007/// ::= .cfi_restore register
3008bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003009 SMLoc DirectiveLoc) {
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00003010 int64_t Register = 0;
3011 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
3012 return true;
3013
3014 getStreamer().EmitCFIRestore(Register);
3015
3016 return false;
3017}
3018
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003019/// ParseDirectiveCFIEscape
3020/// ::= .cfi_escape expression[,...]
3021bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
Bill Wendling96cb1122012-07-19 00:04:14 +00003022 SMLoc DirectiveLoc) {
Rafael Espindola6f0b1812011-12-29 20:24:47 +00003023 std::string Values;
3024 int64_t CurrValue;
3025 if (getParser().ParseAbsoluteExpression(CurrValue))
3026 return true;
3027
3028 Values.push_back((uint8_t)CurrValue);
3029
3030 while (getLexer().is(AsmToken::Comma)) {
3031 Lex();
3032
3033 if (getParser().ParseAbsoluteExpression(CurrValue))
3034 return true;
3035
3036 Values.push_back((uint8_t)CurrValue);
3037 }
3038
3039 getStreamer().EmitCFIEscape(Values);
3040 return false;
3041}
3042
Rafael Espindola16d7d432012-01-23 21:51:52 +00003043/// ParseDirectiveCFISignalFrame
3044/// ::= .cfi_signal_frame
3045bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
3046 SMLoc DirectiveLoc) {
3047 if (getLexer().isNot(AsmToken::EndOfStatement))
3048 return Error(getLexer().getLoc(),
3049 "unexpected token in '" + Directive + "' directive");
3050
3051 getStreamer().EmitCFISignalFrame();
3052
3053 return false;
3054}
3055
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003056/// ParseDirectiveMacrosOnOff
3057/// ::= .macros_on
3058/// ::= .macros_off
3059bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3060 SMLoc DirectiveLoc) {
3061 if (getLexer().isNot(AsmToken::EndOfStatement))
3062 return Error(getLexer().getLoc(),
3063 "unexpected token in '" + Directive + "' directive");
3064
3065 getParser().MacrosEnabled = Directive == ".macros_on";
3066
3067 return false;
3068}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003069
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003070/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003071/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003072bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3073 SMLoc DirectiveLoc) {
3074 StringRef Name;
3075 if (getParser().ParseIdentifier(Name))
3076 return TokError("expected identifier in directive");
3077
Rafael Espindola8a403d32012-08-08 14:51:03 +00003078 MacroParameters Parameters;
Rafael Espindola65366442011-06-05 02:43:45 +00003079 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3080 for(;;) {
3081 StringRef Parameter;
3082 if (getParser().ParseIdentifier(Parameter))
3083 return TokError("expected identifier in directive");
3084 Parameters.push_back(Parameter);
3085
3086 if (getLexer().isNot(AsmToken::Comma))
3087 break;
3088 Lex();
3089 }
3090 }
3091
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003092 if (getLexer().isNot(AsmToken::EndOfStatement))
3093 return TokError("unexpected token in '.macro' directive");
3094
3095 // Eat the end of statement.
3096 Lex();
3097
3098 AsmToken EndToken, StartToken = getTok();
3099
3100 // Lex the macro definition.
3101 for (;;) {
3102 // Check whether we have reached the end of the file.
3103 if (getLexer().is(AsmToken::Eof))
3104 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3105
3106 // Otherwise, check whether we have reach the .endmacro.
3107 if (getLexer().is(AsmToken::Identifier) &&
3108 (getTok().getIdentifier() == ".endm" ||
3109 getTok().getIdentifier() == ".endmacro")) {
3110 EndToken = getTok();
3111 Lex();
3112 if (getLexer().isNot(AsmToken::EndOfStatement))
3113 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3114 "' directive");
3115 break;
3116 }
3117
3118 // Otherwise, scan til the end of the statement.
3119 getParser().EatToEndOfStatement();
3120 }
3121
3122 if (getParser().MacroMap.lookup(Name)) {
3123 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3124 }
3125
3126 const char *BodyStart = StartToken.getLoc().getPointer();
3127 const char *BodyEnd = EndToken.getLoc().getPointer();
3128 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003129 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003130 return false;
3131}
3132
3133/// ParseDirectiveEndMacro
3134/// ::= .endm
3135/// ::= .endmacro
3136bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
Rafael Espindola8a403d32012-08-08 14:51:03 +00003137 SMLoc DirectiveLoc) {
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003138 if (getLexer().isNot(AsmToken::EndOfStatement))
3139 return TokError("unexpected token in '" + Directive + "' directive");
3140
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003141 // If we are inside a macro instantiation, terminate the current
3142 // instantiation.
3143 if (!getParser().ActiveMacros.empty()) {
3144 getParser().HandleMacroExit();
3145 return false;
3146 }
3147
3148 // Otherwise, this .endmacro is a stray entry in the file; well formed
3149 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003150 return TokError("unexpected '" + Directive + "' in file, "
3151 "no current macro definition");
3152}
3153
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003154/// ParseDirectivePurgeMacro
3155/// ::= .purgem
3156bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3157 SMLoc DirectiveLoc) {
3158 StringRef Name;
3159 if (getParser().ParseIdentifier(Name))
3160 return TokError("expected identifier in '.purgem' directive");
3161
3162 if (getLexer().isNot(AsmToken::EndOfStatement))
3163 return TokError("unexpected token in '.purgem' directive");
3164
3165 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3166 if (I == getParser().MacroMap.end())
3167 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3168
3169 // Undefine the macro.
3170 delete I->getValue();
3171 getParser().MacroMap.erase(I);
3172 return false;
3173}
3174
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003175bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003176 getParser().CheckForValidSection();
3177
3178 const MCExpr *Value;
3179
3180 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003181 return true;
3182
3183 if (getLexer().isNot(AsmToken::EndOfStatement))
3184 return TokError("unexpected token in directive");
3185
3186 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003187 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003188 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003189 getStreamer().EmitULEB128Value(Value);
3190
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003191 return false;
3192}
3193
Rafael Espindola761cb062012-06-03 23:57:14 +00003194Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003195 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003196
Rafael Espindola761cb062012-06-03 23:57:14 +00003197 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003198 for (;;) {
3199 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003200 if (getLexer().is(AsmToken::Eof)) {
3201 Error(DirectiveLoc, "no matching '.endr' in definition");
3202 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003203 }
3204
Rafael Espindola761cb062012-06-03 23:57:14 +00003205 if (Lexer.is(AsmToken::Identifier) &&
3206 (getTok().getIdentifier() == ".rept")) {
3207 ++NestLevel;
3208 }
3209
3210 // Otherwise, check whether we have reached the .endr.
3211 if (Lexer.is(AsmToken::Identifier) &&
3212 getTok().getIdentifier() == ".endr") {
3213 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003214 EndToken = getTok();
3215 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003216 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3217 TokError("unexpected token in '.endr' directive");
3218 return 0;
3219 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003220 break;
3221 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003222 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003223 }
3224
Rafael Espindola761cb062012-06-03 23:57:14 +00003225 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003226 EatToEndOfStatement();
3227 }
3228
3229 const char *BodyStart = StartToken.getLoc().getPointer();
3230 const char *BodyEnd = EndToken.getLoc().getPointer();
3231 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3232
Rafael Espindola761cb062012-06-03 23:57:14 +00003233 // We Are Anonymous.
3234 StringRef Name;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003235 MacroParameters Parameters;
Rafael Espindola761cb062012-06-03 23:57:14 +00003236 return new Macro(Name, Body, Parameters);
3237}
3238
3239void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3240 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003241 OS << ".endr\n";
3242
3243 MemoryBuffer *Instantiation =
3244 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3245
Rafael Espindola761cb062012-06-03 23:57:14 +00003246 // Create the macro instantiation object and add to the current macro
3247 // instantiation stack.
3248 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3249 getTok().getLoc(),
3250 Instantiation);
3251 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003252
Rafael Espindola761cb062012-06-03 23:57:14 +00003253 // Jump to the macro instantiation and prime the lexer.
3254 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3255 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3256 Lex();
3257}
3258
3259bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3260 int64_t Count;
3261 if (ParseAbsoluteExpression(Count))
3262 return TokError("unexpected token in '.rept' directive");
3263
3264 if (Count < 0)
3265 return TokError("Count is negative");
3266
3267 if (Lexer.isNot(AsmToken::EndOfStatement))
3268 return TokError("unexpected token in '.rept' directive");
3269
3270 // Eat the end of statement.
3271 Lex();
3272
3273 // Lex the rept definition.
3274 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3275 if (!M)
3276 return true;
3277
3278 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3279 // to hold the macro body with substitutions.
3280 SmallString<256> Buf;
Rafael Espindola8a403d32012-08-08 14:51:03 +00003281 MacroParameters Parameters;
3282 MacroArguments A;
Rafael Espindola761cb062012-06-03 23:57:14 +00003283 raw_svector_ostream OS(Buf);
3284 while (Count--) {
3285 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3286 return true;
3287 }
3288 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003289
3290 return false;
3291}
3292
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003293/// ParseDirectiveIrp
3294/// ::= .irp symbol,values
3295bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003296 MacroParameters Parameters;
3297 MacroParameter Parameter;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003298
3299 if (ParseIdentifier(Parameter))
3300 return TokError("expected identifier in '.irp' directive");
3301
3302 Parameters.push_back(Parameter);
3303
3304 if (Lexer.isNot(AsmToken::Comma))
3305 return TokError("expected comma in '.irp' directive");
3306
3307 Lex();
3308
Rafael Espindola8a403d32012-08-08 14:51:03 +00003309 MacroArguments A;
Rafael Espindolaaa7a2f22012-06-15 14:02:34 +00003310 if (ParseMacroArguments(0, A))
3311 return true;
3312
3313 // Eat the end of statement.
3314 Lex();
3315
3316 // Lex the irp definition.
3317 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3318 if (!M)
3319 return true;
3320
3321 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3322 // to hold the macro body with substitutions.
3323 SmallString<256> Buf;
3324 raw_svector_ostream OS(Buf);
3325
3326 for (std::vector<MacroArgument>::iterator i = A.begin(), e = A.end(); i != e;
3327 ++i) {
3328 std::vector<MacroArgument> Args;
3329 Args.push_back(*i);
3330
3331 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3332 return true;
3333 }
3334
3335 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3336
3337 return false;
3338}
3339
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003340/// ParseDirectiveIrpc
3341/// ::= .irpc symbol,values
3342bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
Rafael Espindola8a403d32012-08-08 14:51:03 +00003343 MacroParameters Parameters;
3344 MacroParameter Parameter;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003345
3346 if (ParseIdentifier(Parameter))
3347 return TokError("expected identifier in '.irpc' directive");
3348
3349 Parameters.push_back(Parameter);
3350
3351 if (Lexer.isNot(AsmToken::Comma))
3352 return TokError("expected comma in '.irpc' directive");
3353
3354 Lex();
3355
Rafael Espindola8a403d32012-08-08 14:51:03 +00003356 MacroArguments A;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003357 if (ParseMacroArguments(0, A))
3358 return true;
3359
3360 if (A.size() != 1 || A.front().size() != 1)
3361 return TokError("unexpected token in '.irpc' directive");
3362
3363 // Eat the end of statement.
3364 Lex();
3365
3366 // Lex the irpc definition.
3367 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3368 if (!M)
3369 return true;
3370
3371 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3372 // to hold the macro body with substitutions.
3373 SmallString<256> Buf;
3374 raw_svector_ostream OS(Buf);
3375
3376 StringRef Values = A.front().front().getString();
3377 std::size_t I, End = Values.size();
3378 for (I = 0; I < End; ++I) {
3379 MacroArgument Arg;
3380 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
3381
Rafael Espindola8a403d32012-08-08 14:51:03 +00003382 MacroArguments Args;
Rafael Espindolafc9216e2012-06-16 18:03:25 +00003383 Args.push_back(Arg);
3384
3385 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
3386 return true;
3387 }
3388
3389 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
3390
3391 return false;
3392}
3393
Rafael Espindola761cb062012-06-03 23:57:14 +00003394bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3395 if (ActiveMacros.empty())
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003396 return TokError("unexpected '.endr' directive, no current .rept");
3397
3398 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003399 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003400 assert(getLexer().is(AsmToken::EndOfStatement));
3401
Rafael Espindola761cb062012-06-03 23:57:14 +00003402 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003403 return false;
3404}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003405
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003406/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003407MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003408 MCContext &C, MCStreamer &Out,
3409 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003410 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003411}