blob: 04603e994a34b0357029b671e54305bf4b864918 [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;
49
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000050struct Macro {
51 StringRef Name;
52 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000053 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000054
55public:
Rafael Espindola65366442011-06-05 02:43:45 +000056 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
57 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000058};
59
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000060/// \brief Helper class for storing information about an active macro
61/// instantiation.
62struct MacroInstantiation {
63 /// The macro being instantiated.
64 const Macro *TheMacro;
65
66 /// The macro instantiation with substitutions.
67 MemoryBuffer *Instantiation;
68
69 /// The location of the instantiation.
70 SMLoc InstantiationLoc;
71
72 /// The location where parsing should resume upon instantiation completion.
73 SMLoc ExitLoc;
74
75public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000076 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000077 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000078};
79
Daniel Dunbaraef87e32010-07-18 18:31:38 +000080/// \brief The concrete assembly parser instance.
81class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082 friend class GenericAsmParser;
83
Daniel Dunbaraef87e32010-07-18 18:31:38 +000084 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
85 void operator=(const AsmParser &); // DO NOT IMPLEMENT
86private:
87 AsmLexer Lexer;
88 MCContext &Ctx;
89 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000090 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000091 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000092 SourceMgr::DiagHandlerTy SavedDiagHandler;
93 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000094 MCAsmParserExtension *GenericParser;
95 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000096
Daniel Dunbaraef87e32010-07-18 18:31:38 +000097 /// This is the current buffer index we're lexing from as managed by the
98 /// SourceMgr object.
99 int CurBuffer;
100
101 AsmCond TheCondState;
102 std::vector<AsmCond> TheCondStack;
103
104 /// DirectiveMap - This is a table handlers for directives. Each handler is
105 /// invoked after the directive identifier is read and is responsible for
106 /// parsing and validating the rest of the directive. The handler is passed
107 /// in the directive name and the location of the directive keyword.
108 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000109
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000110 /// MacroMap - Map of currently defined macros.
111 StringMap<Macro*> MacroMap;
112
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000113 /// ActiveMacros - Stack of active macro instantiations.
114 std::vector<MacroInstantiation*> ActiveMacros;
115
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000116 /// Boolean tracking whether macro substitution is enabled.
117 unsigned MacrosEnabled : 1;
118
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000119 /// Flag tracking whether any errors have been encountered.
120 unsigned HadError : 1;
121
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000122 /// The values from the last parsed cpp hash file line comment if any.
123 StringRef CppHashFilename;
124 int64_t CppHashLineNumber;
125 SMLoc CppHashLoc;
126
Devang Patel0db58bf2012-01-31 18:14:05 +0000127 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
128 unsigned AssemblerDialect;
129
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000130public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000131 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000132 const MCAsmInfo &MAI);
133 ~AsmParser();
134
135 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
136
137 void AddDirectiveHandler(MCAsmParserExtension *Object,
138 StringRef Directive,
139 DirectiveHandler Handler) {
140 DirectiveMap[Directive] = std::make_pair(Object, Handler);
141 }
142
143public:
144 /// @name MCAsmParser Interface
145 /// {
146
147 virtual SourceMgr &getSourceManager() { return SrcMgr; }
148 virtual MCAsmLexer &getLexer() { return Lexer; }
149 virtual MCContext &getContext() { return Ctx; }
150 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000151 virtual unsigned getAssemblerDialect() {
152 if (AssemblerDialect == ~0U)
153 return MAI.getAssemblerDialect();
154 else
155 return AssemblerDialect;
156 }
157 virtual void setAssemblerDialect(unsigned i) {
158 AssemblerDialect = i;
159 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000160
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000161 virtual bool Warning(SMLoc L, const Twine &Msg,
162 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
163 virtual bool Error(SMLoc L, const Twine &Msg,
164 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000165
166 const AsmToken &Lex();
167
168 bool ParseExpression(const MCExpr *&Res);
169 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
170 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
171 virtual bool ParseAbsoluteExpression(int64_t &Res);
172
173 /// }
174
175private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000176 void CheckForValidSection();
177
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000178 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000179 void EatToEndOfLine();
180 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000181
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000182 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola761cb062012-06-03 23:57:14 +0000183 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola65366442011-06-05 02:43:45 +0000184 const std::vector<StringRef> &Parameters,
Rafael Espindola28c1f6662012-06-03 22:41:23 +0000185 const std::vector<MacroArgument> &A,
Rafael Espindola65366442011-06-05 02:43:45 +0000186 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000187 void HandleMacroExit();
188
189 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000190 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000191 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
192 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000193 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000194 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000195
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000196 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
197 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000198 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
199 /// This returns true on failure.
200 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000201
202 /// \brief Reset the current lexer position to that given by \arg Loc. The
203 /// current token is not set; clients should ensure Lex() is called
204 /// subsequently.
205 void JumpToLoc(SMLoc Loc);
206
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000208
209 /// \brief Parse up to the end of statement and a return the contents from the
210 /// current token until the end of the statement; the current token on exit
211 /// will be either the EndOfStatement or EOF.
212 StringRef ParseStringToEndOfStatement();
213
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000214 /// \brief Parse until the end of a statement or a comma is encountered,
215 /// return the contents from the current token up to the end or comma.
216 StringRef ParseStringToComma();
217
Nico Weber4c4c7322011-01-28 03:04:41 +0000218 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000219
220 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
221 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
222 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000223 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224
225 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
226 /// and set \arg Res to the identifier contents.
227 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000228
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000229 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000230
231 // ".ascii", ".asciiz", ".string"
232 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000233 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000234 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000235 bool ParseDirectiveFill(); // ".fill"
236 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000237 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000238 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000239 bool ParseDirectiveOrg(); // ".org"
240 // ".align{,32}", ".p2align{,w,l}"
241 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
242
243 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
244 /// accepts a single symbol (which should be a label or an external).
245 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000246
247 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
248
249 bool ParseDirectiveAbort(); // ".abort"
250 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000251 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000252
253 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +0000254 // ".ifb" or ".ifnb", depending on ExpectBlank.
255 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000256 // ".ifc" or ".ifnc", depending on ExpectEqual.
257 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000258 // ".ifdef" or ".ifndef", depending on expect_defined
259 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000260 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
261 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
262 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
263
264 /// ParseEscapedString - Parse the current token as a string which may include
265 /// escaped characters and return the string contents.
266 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000267
268 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
269 MCSymbolRefExpr::VariantKind Variant);
Rafael Espindola2ec304c2012-05-12 16:31:10 +0000270
Rafael Espindola761cb062012-06-03 23:57:14 +0000271 // Macro-like directives
272 Macro *ParseMacroLikeBody(SMLoc DirectiveLoc);
273 void InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
274 raw_svector_ostream &OS);
275 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
276 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000277};
278
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000279/// \brief Generic implementations of directive handling, etc. which is shared
280/// (or the default, at least) for all assembler parser.
281class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000282 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
283 void AddDirectiveHandler(StringRef Directive) {
284 getParser().AddDirectiveHandler(this, Directive,
285 HandleDirective<GenericAsmParser, Handler>);
286 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000287public:
288 GenericAsmParser() {}
289
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000290 AsmParser &getParser() {
291 return (AsmParser&) this->MCAsmParserExtension::getParser();
292 }
293
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000294 virtual void Initialize(MCAsmParser &Parser) {
295 // Call the base implementation.
296 this->MCAsmParserExtension::Initialize(Parser);
297
298 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000299 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
301 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000303
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000304 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000305 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
306 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000307 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
308 ".cfi_startproc");
309 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
310 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000311 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
312 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000313 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
314 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000315 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
316 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000317 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
318 ".cfi_def_cfa_register");
319 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
320 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000321 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
322 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000323 AddDirectiveHandler<
324 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
325 AddDirectiveHandler<
326 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000327 AddDirectiveHandler<
328 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
329 AddDirectiveHandler<
330 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000331 AddDirectiveHandler<
332 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000333 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000334 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
335 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000336 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000337 AddDirectiveHandler<
338 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000339
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000340 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000341 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
342 ".macros_on");
343 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
344 ".macros_off");
345 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
346 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
347 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000348 AddDirectiveHandler<&GenericAsmParser::ParseDirectivePurgeMacro>(".purgem");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000349
350 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
351 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000352 }
353
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000354 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
355
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000356 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
357 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
358 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000359 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000360 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000361 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
362 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000363 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000364 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000365 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000366 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
367 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000368 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000369 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000370 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
371 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000372 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000373 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000374 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000375 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000376
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000377 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000378 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
379 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +0000380 bool ParseDirectivePurgeMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000381
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000382 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000383};
384
385}
386
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000387namespace llvm {
388
389extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000390extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000391extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000392
393}
394
Chris Lattneraaec2052010-01-19 19:46:13 +0000395enum { DEFAULT_ADDRSPACE = 0 };
396
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000397AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000398 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000399 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000400 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000401 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
402 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000403 // Save the old handler.
404 SavedDiagHandler = SrcMgr.getDiagHandler();
405 SavedDiagContext = SrcMgr.getDiagContext();
406 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000407 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000408 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000409
410 // Initialize the generic parser.
411 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000412
413 // Initialize the platform / file format parser.
414 //
415 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
416 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000417 if (_MAI.hasMicrosoftFastStdCallMangling()) {
418 PlatformParser = createCOFFAsmParser();
419 PlatformParser->Initialize(*this);
420 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000421 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000422 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000423 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000424 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000425 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000426 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000427}
428
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000429AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000430 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
431
432 // Destroy any macros.
433 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
434 ie = MacroMap.end(); it != ie; ++it)
435 delete it->getValue();
436
Daniel Dunbare4749702010-07-12 18:12:02 +0000437 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000438 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000439}
440
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000441void AsmParser::PrintMacroInstantiations() {
442 // Print the active macro instantiation stack.
443 for (std::vector<MacroInstantiation*>::const_reverse_iterator
444 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000445 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
446 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000447}
448
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000449bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000450 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000451 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000452 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000453 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000454 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000455}
456
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000457bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000458 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000459 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000460 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000461 return true;
462}
463
Sean Callananfd0b0282010-01-21 00:19:58 +0000464bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000465 std::string IncludedFile;
466 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000467 if (NewBuf == -1)
468 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000469
Sean Callananfd0b0282010-01-21 00:19:58 +0000470 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000471
Sean Callananfd0b0282010-01-21 00:19:58 +0000472 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000473
Sean Callananfd0b0282010-01-21 00:19:58 +0000474 return false;
475}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000476
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000477/// Process the specified .incbin file by seaching for it in the include paths
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000478/// then just emitting the byte contents of the file to the streamer. This
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000479/// returns true on failure.
480bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
481 std::string IncludedFile;
482 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
483 if (NewBuf == -1)
484 return true;
485
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000486 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000487 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
488 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000489 return false;
490}
491
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000492void AsmParser::JumpToLoc(SMLoc Loc) {
493 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
494 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
495}
496
Sean Callananfd0b0282010-01-21 00:19:58 +0000497const AsmToken &AsmParser::Lex() {
498 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000499
Sean Callananfd0b0282010-01-21 00:19:58 +0000500 if (tok->is(AsmToken::Eof)) {
501 // If this is the end of an included file, pop the parent file off the
502 // include stack.
503 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
504 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000505 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000506 tok = &Lexer.Lex();
507 }
508 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000509
Sean Callananfd0b0282010-01-21 00:19:58 +0000510 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000511 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000512
Sean Callananfd0b0282010-01-21 00:19:58 +0000513 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000514}
515
Chris Lattner79180e22010-04-05 23:15:42 +0000516bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000517 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000518 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000519 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000520
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000521 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000522 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000523
524 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000525 AsmCond StartingCondState = TheCondState;
526
Kevin Enderby613b7572011-11-01 22:27:22 +0000527 // If we are generating dwarf for assembly source files save the initial text
528 // section and generate a .file directive.
529 if (getContext().getGenDwarfForAssembly()) {
530 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000531 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
532 getStreamer().EmitLabel(SectionStartSym);
533 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000534 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
535 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
536 }
537
Chris Lattnerb717fb02009-07-02 21:53:43 +0000538 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000539 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000540 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000541
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000542 // We had an error, validate that one was emitted and recover by skipping to
543 // the next line.
544 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000545 EatToEndOfStatement();
546 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000547
548 if (TheCondState.TheCond != StartingCondState.TheCond ||
549 TheCondState.Ignore != StartingCondState.Ignore)
550 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000551
552 // Check to see there are no empty DwarfFile slots.
553 const std::vector<MCDwarfFile *> &MCDwarfFiles =
554 getContext().getMCDwarfFiles();
555 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000556 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000557 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000558 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000559
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000560 // Check to see that all assembler local symbols were actually defined.
561 // Targets that don't do subsections via symbols may not want this, though,
562 // so conservatively exclude them. Only do this if we're finalizing, though,
563 // as otherwise we won't necessarilly have seen everything yet.
564 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
565 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
566 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
567 e = Symbols.end();
568 i != e; ++i) {
569 MCSymbol *Sym = i->getValue();
570 // Variable symbols may not be marked as defined, so check those
571 // explicitly. If we know it's a variable, we have a definition for
572 // the purposes of this check.
573 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
574 // FIXME: We would really like to refer back to where the symbol was
575 // first referenced for a source location. We need to add something
576 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000577 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
578 "assembler local symbol '" + Sym->getName() +
579 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000580 }
581 }
582
583
Chris Lattner79180e22010-04-05 23:15:42 +0000584 // Finalize the output stream if there are no errors and if the client wants
585 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000586 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000587 Out.Finish();
588
Chris Lattnerb717fb02009-07-02 21:53:43 +0000589 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000590}
591
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000592void AsmParser::CheckForValidSection() {
593 if (!getStreamer().getCurrentSection()) {
594 TokError("expected section directive before assembly directive");
595 Out.SwitchSection(Ctx.getMachOSection(
596 "__TEXT", "__text",
597 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
598 0, SectionKind::getText()));
599 }
600}
601
Chris Lattner2cf5f142009-06-22 01:29:09 +0000602/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
603void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000604 while (Lexer.isNot(AsmToken::EndOfStatement) &&
605 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000606 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000607
Chris Lattner2cf5f142009-06-22 01:29:09 +0000608 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000609 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000610 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000611}
612
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000613StringRef AsmParser::ParseStringToEndOfStatement() {
614 const char *Start = getTok().getLoc().getPointer();
615
616 while (Lexer.isNot(AsmToken::EndOfStatement) &&
617 Lexer.isNot(AsmToken::Eof))
618 Lex();
619
620 const char *End = getTok().getLoc().getPointer();
621 return StringRef(Start, End - Start);
622}
Chris Lattnerc4193832009-06-22 05:51:26 +0000623
Benjamin Kramerdec06ef2012-05-12 11:18:51 +0000624StringRef AsmParser::ParseStringToComma() {
625 const char *Start = getTok().getLoc().getPointer();
626
627 while (Lexer.isNot(AsmToken::EndOfStatement) &&
628 Lexer.isNot(AsmToken::Comma) &&
629 Lexer.isNot(AsmToken::Eof))
630 Lex();
631
632 const char *End = getTok().getLoc().getPointer();
633 return StringRef(Start, End - Start);
634}
635
Chris Lattner74ec1a32009-06-22 06:32:03 +0000636/// ParseParenExpr - Parse a paren expression and return it.
637/// NOTE: This assumes the leading '(' has already been consumed.
638///
639/// parenexpr ::= expr)
640///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000641bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000642 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000643 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000644 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000645 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000646 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000647 return false;
648}
Chris Lattnerc4193832009-06-22 05:51:26 +0000649
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000650/// ParseBracketExpr - Parse a bracket expression and return it.
651/// NOTE: This assumes the leading '[' has already been consumed.
652///
653/// bracketexpr ::= expr]
654///
655bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
656 if (ParseExpression(Res)) return true;
657 if (Lexer.isNot(AsmToken::RBrac))
658 return TokError("expected ']' in brackets expression");
659 EndLoc = Lexer.getLoc();
660 Lex();
661 return false;
662}
663
Chris Lattner74ec1a32009-06-22 06:32:03 +0000664/// ParsePrimaryExpr - Parse a primary expression and return it.
665/// primaryexpr ::= (parenexpr
666/// primaryexpr ::= symbol
667/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000668/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000669/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000670bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000671 switch (Lexer.getKind()) {
672 default:
673 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000674 // If we have an error assume that we've already handled it.
675 case AsmToken::Error:
676 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000677 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000678 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000679 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000680 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000681 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000682 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000683 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000684 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000685 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000686 EndLoc = Lexer.getLoc();
687
688 StringRef Identifier;
689 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000690 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000691
Daniel Dunbarfffff912009-10-16 01:34:54 +0000692 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000693 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000694 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000695
696 // Lookup the symbol variant if used.
697 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000698 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000699 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000700 if (Variant == MCSymbolRefExpr::VK_Invalid) {
701 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000702 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000703 }
704 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000705
Daniel Dunbarfffff912009-10-16 01:34:54 +0000706 // If this is an absolute variable reference, substitute it now to preserve
707 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000708 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000709 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000710 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000711
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000712 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000713 return false;
714 }
715
716 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000717 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000718 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000719 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000720 case AsmToken::Integer: {
721 SMLoc Loc = getTok().getLoc();
722 int64_t IntVal = getTok().getIntVal();
723 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000724 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000725 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000726 // Look for 'b' or 'f' following an Integer as a directional label
727 if (Lexer.getKind() == AsmToken::Identifier) {
728 StringRef IDVal = getTok().getString();
729 if (IDVal == "f" || IDVal == "b"){
730 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
731 IDVal == "f" ? 1 : 0);
732 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
733 getContext());
Benjamin Kramer29739e72012-05-12 16:52:21 +0000734 if (IDVal == "b" && Sym->isUndefined())
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000735 return Error(Loc, "invalid reference to undefined symbol");
736 EndLoc = Lexer.getLoc();
737 Lex(); // Eat identifier.
738 }
739 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000740 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000741 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000742 case AsmToken::Real: {
743 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000744 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000745 Res = MCConstantExpr::Create(IntVal, getContext());
746 Lex(); // Eat token.
747 return false;
748 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000749 case AsmToken::Dot: {
750 // This is a '.' reference, which references the current PC. Emit a
751 // temporary label to the streamer and refer to it.
752 MCSymbol *Sym = Ctx.CreateTempSymbol();
753 Out.EmitLabel(Sym);
754 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
755 EndLoc = Lexer.getLoc();
756 Lex(); // Eat identifier.
757 return false;
758 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000759 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000760 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000761 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000762 case AsmToken::LBrac:
763 if (!PlatformParser->HasBracketExpressions())
764 return TokError("brackets expression not supported on this target");
765 Lex(); // Eat the '['.
766 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000767 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000768 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000769 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000770 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000771 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000772 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000773 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000774 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000775 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000776 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000777 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000778 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000779 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000780 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000781 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000782 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000783 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000784 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000785 }
786}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000787
Chris Lattnerb4307b32010-01-15 19:28:38 +0000788bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000789 SMLoc EndLoc;
790 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000791}
792
Daniel Dunbarcceba832010-09-17 02:47:07 +0000793const MCExpr *
794AsmParser::ApplyModifierToExpr(const MCExpr *E,
795 MCSymbolRefExpr::VariantKind Variant) {
796 // Recurse over the given expression, rebuilding it to apply the given variant
797 // if there is exactly one symbol.
798 switch (E->getKind()) {
799 case MCExpr::Target:
800 case MCExpr::Constant:
801 return 0;
802
803 case MCExpr::SymbolRef: {
804 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
805
806 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
807 TokError("invalid variant on expression '" +
808 getTok().getIdentifier() + "' (already modified)");
809 return E;
810 }
811
812 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
813 }
814
815 case MCExpr::Unary: {
816 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
817 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
818 if (!Sub)
819 return 0;
820 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
821 }
822
823 case MCExpr::Binary: {
824 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
825 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
826 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
827
828 if (!LHS && !RHS)
829 return 0;
830
831 if (!LHS) LHS = BE->getLHS();
832 if (!RHS) RHS = BE->getRHS();
833
834 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
835 }
836 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000837
Craig Topper85814382012-02-07 05:05:23 +0000838 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000839}
840
Chris Lattner74ec1a32009-06-22 06:32:03 +0000841/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000842///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000843/// expr ::= expr &&,|| expr -> lowest.
844/// expr ::= expr |,^,&,! expr
845/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
846/// expr ::= expr <<,>> expr
847/// expr ::= expr +,- expr
848/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000849/// expr ::= primaryexpr
850///
Chris Lattner54482b42010-01-15 19:39:23 +0000851bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000852 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000853 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000854 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
855 return true;
856
Daniel Dunbarcceba832010-09-17 02:47:07 +0000857 // As a special case, we support 'a op b @ modifier' by rewriting the
858 // expression to include the modifier. This is inefficient, but in general we
859 // expect users to use 'a@modifier op b'.
860 if (Lexer.getKind() == AsmToken::At) {
861 Lex();
862
863 if (Lexer.isNot(AsmToken::Identifier))
864 return TokError("unexpected symbol modifier following '@'");
865
866 MCSymbolRefExpr::VariantKind Variant =
867 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
868 if (Variant == MCSymbolRefExpr::VK_Invalid)
869 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
870
871 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
872 if (!ModifiedRes) {
873 return TokError("invalid modifier '" + getTok().getIdentifier() +
874 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000875 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000876
Daniel Dunbarcceba832010-09-17 02:47:07 +0000877 Res = ModifiedRes;
878 Lex();
879 }
880
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000881 // Try to constant fold it up front, if possible.
882 int64_t Value;
883 if (Res->EvaluateAsAbsolute(Value))
884 Res = MCConstantExpr::Create(Value, getContext());
885
886 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000887}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000888
Chris Lattnerb4307b32010-01-15 19:28:38 +0000889bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000890 Res = 0;
891 return ParseParenExpr(Res, EndLoc) ||
892 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000893}
894
Daniel Dunbar475839e2009-06-29 20:37:27 +0000895bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000896 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000897
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000898 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000899 if (ParseExpression(Expr))
900 return true;
901
Daniel Dunbare00b0112009-10-16 01:57:52 +0000902 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000903 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000904
905 return false;
906}
907
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000908static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000909 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000910 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000911 default:
912 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000913
Jim Grosbachfbe16812011-08-20 16:24:13 +0000914 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000915 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000916 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000917 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000918 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000919 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000920 return 1;
921
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000922
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000923 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000924 //
925 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000926 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000927 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000928 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000929 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000930 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000931 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000932 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000933 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000934 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000935
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000936 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000937 case AsmToken::EqualEqual:
938 Kind = MCBinaryExpr::EQ;
939 return 3;
940 case AsmToken::ExclaimEqual:
941 case AsmToken::LessGreater:
942 Kind = MCBinaryExpr::NE;
943 return 3;
944 case AsmToken::Less:
945 Kind = MCBinaryExpr::LT;
946 return 3;
947 case AsmToken::LessEqual:
948 Kind = MCBinaryExpr::LTE;
949 return 3;
950 case AsmToken::Greater:
951 Kind = MCBinaryExpr::GT;
952 return 3;
953 case AsmToken::GreaterEqual:
954 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000955 return 3;
956
Jim Grosbachfbe16812011-08-20 16:24:13 +0000957 // Intermediate Precedence: <<, >>
958 case AsmToken::LessLess:
959 Kind = MCBinaryExpr::Shl;
960 return 4;
961 case AsmToken::GreaterGreater:
962 Kind = MCBinaryExpr::Shr;
963 return 4;
964
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000965 // High Intermediate Precedence: +, -
966 case AsmToken::Plus:
967 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000968 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000969 case AsmToken::Minus:
970 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000971 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000972
Jim Grosbachfbe16812011-08-20 16:24:13 +0000973 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000974 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000975 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000976 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000977 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000978 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000979 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000980 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000981 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000982 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000983 }
984}
985
986
987/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
988/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000989bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
990 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000991 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000992 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000993 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000994
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000995 // If the next token is lower precedence than we are allowed to eat, return
996 // successfully with what we ate already.
997 if (TokPrec < Precedence)
998 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000999
Sean Callanan79ed1a82010-01-19 20:22:31 +00001000 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001001
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001002 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +00001003 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001004 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001005
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001006 // If BinOp binds less tightly with RHS than the operator after RHS, let
1007 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001008 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001009 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001010 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001011 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001012 }
1013
Daniel Dunbar475839e2009-06-29 20:37:27 +00001014 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +00001015 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +00001016 }
1017}
1018
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001019
1020
1021
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001022/// ParseStatement:
1023/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +00001024/// ::= Label* Directive ...Operands... EndOfStatement
1025/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001026bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001027 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001028 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001029 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001030 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001031 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001032
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001033 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001034 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001035 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001036 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001037 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001038 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001039 if (Lexer.is(AsmToken::Hash))
1040 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001041
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001042 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001043 if (Lexer.is(AsmToken::Integer)) {
1044 LocalLabelVal = getTok().getIntVal();
1045 if (LocalLabelVal < 0) {
1046 if (!TheCondState.Ignore)
1047 return TokError("unexpected token at start of statement");
1048 IDVal = "";
1049 }
1050 else {
1051 IDVal = getTok().getString();
1052 Lex(); // Consume the integer token to be used as an identifier token.
1053 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001054 if (!TheCondState.Ignore)
1055 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001056 }
1057 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001058
1059 } else if (Lexer.is(AsmToken::Dot)) {
1060 // Treat '.' as a valid identifier in this context.
1061 Lex();
1062 IDVal = ".";
1063
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001064 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001065 if (!TheCondState.Ignore)
1066 return TokError("unexpected token at start of statement");
1067 IDVal = "";
1068 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001069
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001070
Chris Lattner7834fac2010-04-17 18:14:27 +00001071 // Handle conditional assembly here before checking for skipping. We
1072 // have to do this so that .endif isn't skipped in a ".if 0" block for
1073 // example.
1074 if (IDVal == ".if")
1075 return ParseDirectiveIf(IDLoc);
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00001076 if (IDVal == ".ifb")
1077 return ParseDirectiveIfb(IDLoc, true);
1078 if (IDVal == ".ifnb")
1079 return ParseDirectiveIfb(IDLoc, false);
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00001080 if (IDVal == ".ifc")
1081 return ParseDirectiveIfc(IDLoc, true);
1082 if (IDVal == ".ifnc")
1083 return ParseDirectiveIfc(IDLoc, false);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001084 if (IDVal == ".ifdef")
1085 return ParseDirectiveIfdef(IDLoc, true);
1086 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1087 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001088 if (IDVal == ".elseif")
1089 return ParseDirectiveElseIf(IDLoc);
1090 if (IDVal == ".else")
1091 return ParseDirectiveElse(IDLoc);
1092 if (IDVal == ".endif")
1093 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001094
Chris Lattner7834fac2010-04-17 18:14:27 +00001095 // If we are in a ".if 0" block, ignore this statement.
1096 if (TheCondState.Ignore) {
1097 EatToEndOfStatement();
1098 return false;
1099 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001100
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001101 // FIXME: Recurse on local labels?
1102
1103 // See what kind of statement we have.
1104 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001105 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001106 CheckForValidSection();
1107
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001108 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001109 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001110
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001111 // Diagnose attempt to use '.' as a label.
1112 if (IDVal == ".")
1113 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1114
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001115 // Diagnose attempt to use a variable as a label.
1116 //
1117 // FIXME: Diagnostics. Note the location of the definition as a label.
1118 // FIXME: This doesn't diagnose assignment to a symbol which has been
1119 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001120 MCSymbol *Sym;
1121 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001122 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001123 else
1124 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001125 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001126 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001127
Daniel Dunbar959fd882009-08-26 22:13:22 +00001128 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001129 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001130
Kevin Enderby94c2e852011-12-09 18:09:40 +00001131 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001132 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001133 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001134 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1135 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001136
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001137 // Consume any end of statement token, if present, to avoid spurious
1138 // AddBlankLine calls().
1139 if (Lexer.is(AsmToken::EndOfStatement)) {
1140 Lex();
1141 if (Lexer.is(AsmToken::Eof))
1142 return false;
1143 }
1144
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001145 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001146 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001147
Daniel Dunbar3f872332009-07-28 16:08:33 +00001148 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001149 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001150 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001151
Nico Weber4c4c7322011-01-28 03:04:41 +00001152 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001153
1154 default: // Normal instruction or directive.
1155 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001156 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001157
1158 // If macros are enabled, check to see if this is a macro instantiation.
1159 if (MacrosEnabled)
1160 if (const Macro *M = MacroMap.lookup(IDVal))
1161 return HandleMacroEntry(IDVal, IDLoc, M);
1162
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001163 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001164 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001165 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001166 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001167 return ParseDirectiveSet(IDVal, true);
1168 if (IDVal == ".equiv")
1169 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001170
Daniel Dunbara0d14262009-06-24 23:30:00 +00001171 // Data directives
1172
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001173 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001174 return ParseDirectiveAscii(IDVal, false);
1175 if (IDVal == ".asciz" || IDVal == ".string")
1176 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001177
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001178 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001179 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001180 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001181 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001182 if (IDVal == ".value")
1183 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001184 if (IDVal == ".2byte")
1185 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001186 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001187 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001188 if (IDVal == ".int")
1189 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001190 if (IDVal == ".4byte")
1191 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001192 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001193 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001194 if (IDVal == ".8byte")
1195 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001196 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001197 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1198 if (IDVal == ".double")
1199 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001200
Eli Friedman5d68ec22010-07-19 04:17:25 +00001201 if (IDVal == ".align") {
1202 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1203 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1204 }
1205 if (IDVal == ".align32") {
1206 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1207 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1208 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001209 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001210 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001211 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001212 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001213 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001214 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001215 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001216 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001217 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001218 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001219 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001220 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1221
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001222 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001223 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001224
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001225 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001226 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001227 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001228 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001229 if (IDVal == ".zero")
1230 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001231
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001232 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001233
Benjamin Kramere14a3c52012-05-12 11:18:59 +00001234 if (IDVal == ".extern") {
1235 EatToEndOfStatement(); // .extern is the default, ignore it.
1236 return false;
1237 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001238 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001239 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001240 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001241 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001242 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001243 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001244 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001245 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001246 if (IDVal == ".symbol_resolver")
1247 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001248 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001249 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001250 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001251 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001252 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001253 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001254 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001255 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001256 if (IDVal == ".weak_def_can_be_hidden")
1257 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001258
Hans Wennborg5cc64912011-06-18 13:51:54 +00001259 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001260 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001261 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001262 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001263
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001264 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001265 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001266 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001267 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001268 if (IDVal == ".incbin")
1269 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001270
Benjamin Kramer5cdf0ad2012-05-12 11:19:04 +00001271 if (IDVal == ".code16" || IDVal == ".code16gcc")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001272 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001273
Rafael Espindola761cb062012-06-03 23:57:14 +00001274 // Macro-like directives
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001275 if (IDVal == ".rept")
1276 return ParseDirectiveRept(IDLoc);
1277 if (IDVal == ".endr")
Rafael Espindola761cb062012-06-03 23:57:14 +00001278 return ParseDirectiveEndr(IDLoc);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00001279
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001280 // Look up the handler in the handler table.
1281 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1282 DirectiveMap.lookup(IDVal);
1283 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001284 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001285
Kevin Enderby9c656452009-09-10 20:51:44 +00001286 // Target hook for parsing target specific directives.
1287 if (!getTargetParser().ParseDirective(ID))
1288 return false;
1289
Jim Grosbach686c0182012-05-01 18:38:27 +00001290 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001291 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001292
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001293 CheckForValidSection();
1294
Chris Lattnera7f13542010-05-19 23:34:33 +00001295 // Canonicalize the opcode to lower case.
1296 SmallString<128> Opcode;
1297 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1298 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001299
Chris Lattner98986712010-01-14 22:21:20 +00001300 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001301 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001302 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001303
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001304 // Dump the parsed representation, if requested.
1305 if (getShowParsedOperands()) {
1306 SmallString<256> Str;
1307 raw_svector_ostream OS(Str);
1308 OS << "parsed instruction: [";
1309 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1310 if (i != 0)
1311 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001312 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001313 }
1314 OS << "]";
1315
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001316 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001317 }
1318
Kevin Enderby613b7572011-11-01 22:27:22 +00001319 // If we are generating dwarf for assembly source files and the current
1320 // section is the initial text section then generate a .loc directive for
1321 // the instruction.
1322 if (!HadError && getContext().getGenDwarfForAssembly() &&
1323 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1324 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1325 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1326 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001327 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001328 StringRef());
1329 }
1330
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001331 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001332 if (!HadError)
1333 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1334 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001335
Chris Lattner98986712010-01-14 22:21:20 +00001336 // Free any parsed operands.
1337 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1338 delete ParsedOperands[i];
1339
Chris Lattnercbf8a982010-09-11 16:18:25 +00001340 // Don't skip the rest of the line, the instruction parser is responsible for
1341 // that.
1342 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001343}
Chris Lattner9a023f72009-06-24 04:43:34 +00001344
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001345/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1346/// since they may not be able to be tokenized to get to the end of line token.
1347void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001348 if (!Lexer.is(AsmToken::EndOfStatement))
1349 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001350 // Eat EOL.
1351 Lex();
1352}
1353
1354/// ParseCppHashLineFilenameComment as this:
1355/// ::= # number "filename"
1356/// or just as a full line comment if it doesn't have a number and a string.
1357bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1358 Lex(); // Eat the hash token.
1359
1360 if (getLexer().isNot(AsmToken::Integer)) {
1361 // Consume the line since in cases it is not a well-formed line directive,
1362 // as if were simply a full line comment.
1363 EatToEndOfLine();
1364 return false;
1365 }
1366
1367 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001368 Lex();
1369
1370 if (getLexer().isNot(AsmToken::String)) {
1371 EatToEndOfLine();
1372 return false;
1373 }
1374
1375 StringRef Filename = getTok().getString();
1376 // Get rid of the enclosing quotes.
1377 Filename = Filename.substr(1, Filename.size()-2);
1378
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001379 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1380 CppHashLoc = L;
1381 CppHashFilename = Filename;
1382 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001383
1384 // Ignore any trailing characters, they're just comment.
1385 EatToEndOfLine();
1386 return false;
1387}
1388
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001389/// DiagHandler - will use the the last parsed cpp hash line filename comment
1390/// for the Filename and LineNo if any in the diagnostic.
1391void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1392 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1393 raw_ostream &OS = errs();
1394
1395 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1396 const SMLoc &DiagLoc = Diag.getLoc();
1397 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1398 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1399
1400 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1401 // before printing the message.
1402 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001403 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001404 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1405 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1406 }
1407
1408 // If we have not parsed a cpp hash line filename comment or the source
1409 // manager changed or buffer changed (like in a nested include) then just
1410 // print the normal diagnostic using its Filename and LineNo.
1411 if (!Parser->CppHashLineNumber ||
1412 &DiagSrcMgr != &Parser->SrcMgr ||
1413 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001414 if (Parser->SavedDiagHandler)
1415 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1416 else
1417 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001418 return;
1419 }
1420
1421 // Use the CppHashFilename and calculate a line number based on the
1422 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1423 // the diagnostic.
1424 const std::string Filename = Parser->CppHashFilename;
1425
1426 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1427 int CppHashLocLineNo =
1428 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1429 int LineNo = Parser->CppHashLineNumber - 1 +
1430 (DiagLocLineNo - CppHashLocLineNo);
1431
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001432 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1433 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001434 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001435 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001436
Benjamin Kramer04a04262011-10-16 10:48:29 +00001437 if (Parser->SavedDiagHandler)
1438 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1439 else
1440 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001441}
1442
Rafael Espindola761cb062012-06-03 23:57:14 +00001443bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
Rafael Espindola65366442011-06-05 02:43:45 +00001444 const std::vector<StringRef> &Parameters,
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001445 const std::vector<MacroArgument> &A,
Rafael Espindola65366442011-06-05 02:43:45 +00001446 const SMLoc &L) {
Rafael Espindola65366442011-06-05 02:43:45 +00001447 unsigned NParameters = Parameters.size();
1448 if (NParameters != 0 && NParameters != A.size())
1449 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001450
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001451 while (!Body.empty()) {
1452 // Scan for the next substitution.
1453 std::size_t End = Body.size(), Pos = 0;
1454 for (; Pos != End; ++Pos) {
1455 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001456 if (!NParameters) {
1457 // This macro has no parameters, look for $0, $1, etc.
1458 if (Body[Pos] != '$' || Pos + 1 == End)
1459 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001460
Rafael Espindola65366442011-06-05 02:43:45 +00001461 char Next = Body[Pos + 1];
1462 if (Next == '$' || Next == 'n' || isdigit(Next))
1463 break;
1464 } else {
1465 // This macro has parameters, look for \foo, \bar, etc.
1466 if (Body[Pos] == '\\' && Pos + 1 != End)
1467 break;
1468 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001469 }
1470
1471 // Add the prefix.
1472 OS << Body.slice(0, Pos);
1473
1474 // Check if we reached the end.
1475 if (Pos == End)
1476 break;
1477
Rafael Espindola65366442011-06-05 02:43:45 +00001478 if (!NParameters) {
1479 switch (Body[Pos+1]) {
1480 // $$ => $
1481 case '$':
1482 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001483 break;
1484
Rafael Espindola65366442011-06-05 02:43:45 +00001485 // $n => number of arguments
1486 case 'n':
1487 OS << A.size();
1488 break;
1489
1490 // $[0-9] => argument
1491 default: {
1492 // Missing arguments are ignored.
1493 unsigned Index = Body[Pos+1] - '0';
1494 if (Index >= A.size())
1495 break;
1496
1497 // Otherwise substitute with the token values, with spaces eliminated.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001498 for (MacroArgument::const_iterator it = A[Index].begin(),
Rafael Espindola65366442011-06-05 02:43:45 +00001499 ie = A[Index].end(); it != ie; ++it)
1500 OS << it->getString();
1501 break;
1502 }
1503 }
1504 Pos += 2;
1505 } else {
1506 unsigned I = Pos + 1;
1507 while (isalnum(Body[I]) && I + 1 != End)
1508 ++I;
1509
1510 const char *Begin = Body.data() + Pos +1;
1511 StringRef Argument(Begin, I - (Pos +1));
1512 unsigned Index = 0;
1513 for (; Index < NParameters; ++Index)
1514 if (Parameters[Index] == Argument)
1515 break;
1516
1517 // FIXME: We should error at the macro definition.
1518 if (Index == NParameters)
1519 return Error(L, "Parameter not found");
1520
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001521 for (MacroArgument::const_iterator it = A[Index].begin(),
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001522 ie = A[Index].end(); it != ie; ++it)
1523 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001524
Rafael Espindola65366442011-06-05 02:43:45 +00001525 Pos += 1 + Argument.size();
1526 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001527 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001528 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001529 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001530
Rafael Espindola65366442011-06-05 02:43:45 +00001531 return false;
1532}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001533
Rafael Espindola65366442011-06-05 02:43:45 +00001534MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1535 MemoryBuffer *I)
1536 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1537{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001538}
1539
1540bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1541 const Macro *M) {
1542 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1543 // this, although we should protect against infinite loops.
1544 if (ActiveMacros.size() == 20)
1545 return TokError("macros cannot be nested more than 20 levels deep");
1546
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001547 // Parse the macro instantiation arguments.
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001548 std::vector<MacroArgument> MacroArguments;
1549 MacroArguments.push_back(MacroArgument());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001550 unsigned ParenLevel = 0;
1551 for (;;) {
1552 if (Lexer.is(AsmToken::Eof))
1553 return TokError("unexpected token in macro instantiation");
1554 if (Lexer.is(AsmToken::EndOfStatement))
1555 break;
1556
1557 // If we aren't inside parentheses and this is a comma, start a new token
1558 // list.
1559 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
Rafael Espindola28c1f6662012-06-03 22:41:23 +00001560 MacroArguments.push_back(MacroArgument());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001561 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001562 // Adjust the current parentheses level.
1563 if (Lexer.is(AsmToken::LParen))
1564 ++ParenLevel;
1565 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1566 --ParenLevel;
1567
1568 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001569 MacroArguments.back().push_back(getTok());
1570 }
1571 Lex();
1572 }
Jim Grosbach68f89a62012-04-16 21:18:49 +00001573 // If the last argument didn't end up with any tokens, it's not a real
1574 // argument and we should remove it from the list. This happens with either
1575 // a tailing comma or an empty argument list.
1576 if (MacroArguments.back().empty())
1577 MacroArguments.pop_back();
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001578
Rafael Espindola65366442011-06-05 02:43:45 +00001579 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1580 // to hold the macro body with substitutions.
1581 SmallString<256> Buf;
1582 StringRef Body = M->Body;
Rafael Espindola761cb062012-06-03 23:57:14 +00001583 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001584
Rafael Espindola761cb062012-06-03 23:57:14 +00001585 if (expandMacro(OS, Body, M->Parameters, MacroArguments, getTok().getLoc()))
Rafael Espindola65366442011-06-05 02:43:45 +00001586 return true;
1587
Rafael Espindola761cb062012-06-03 23:57:14 +00001588 // We include the .endmacro in the buffer as our queue to exit the macro
1589 // instantiation.
1590 OS << ".endmacro\n";
1591
Rafael Espindola65366442011-06-05 02:43:45 +00001592 MemoryBuffer *Instantiation =
Rafael Espindola761cb062012-06-03 23:57:14 +00001593 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Rafael Espindola65366442011-06-05 02:43:45 +00001594
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001595 // Create the macro instantiation object and add to the current macro
1596 // instantiation stack.
1597 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001598 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001599 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001600 ActiveMacros.push_back(MI);
1601
1602 // Jump to the macro instantiation and prime the lexer.
1603 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1604 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1605 Lex();
1606
1607 return false;
1608}
1609
1610void AsmParser::HandleMacroExit() {
1611 // Jump to the EndOfStatement we should return to, and consume it.
1612 JumpToLoc(ActiveMacros.back()->ExitLoc);
1613 Lex();
1614
1615 // Pop the instantiation entry.
1616 delete ActiveMacros.back();
1617 ActiveMacros.pop_back();
1618}
1619
Rafael Espindolae71cc862012-01-28 05:57:00 +00001620static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001621 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001622 case MCExpr::Binary: {
1623 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1624 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001625 break;
1626 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001627 case MCExpr::Target:
1628 case MCExpr::Constant:
1629 return false;
1630 case MCExpr::SymbolRef: {
1631 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001632 if (S.isVariable())
1633 return IsUsedIn(Sym, S.getVariableValue());
1634 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001635 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001636 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001637 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001638 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001639
1640 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001641}
1642
Nico Weber4c4c7322011-01-28 03:04:41 +00001643bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001644 // FIXME: Use better location, we should use proper tokens.
1645 SMLoc EqualLoc = Lexer.getLoc();
1646
Daniel Dunbar821e3332009-08-31 08:09:28 +00001647 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001648 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001649 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001650
Rafael Espindolae71cc862012-01-28 05:57:00 +00001651 // Note: we don't count b as used in "a = b". This is to allow
1652 // a = b
1653 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001654
Daniel Dunbar3f872332009-07-28 16:08:33 +00001655 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001656 return TokError("unexpected token in assignment");
1657
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001658 // Error on assignment to '.'.
1659 if (Name == ".") {
1660 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1661 "(use '.space' or '.org').)"));
1662 }
1663
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001664 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001665 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001666
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001667 // Validate that the LHS is allowed to be a variable (either it has not been
1668 // used as a symbol, or it is an absolute symbol).
1669 MCSymbol *Sym = getContext().LookupSymbol(Name);
1670 if (Sym) {
1671 // Diagnose assignment to a label.
1672 //
1673 // FIXME: Diagnostics. Note the location of the definition as a label.
1674 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001675 if (IsUsedIn(Sym, Value))
1676 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1677 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001678 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001679 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1680 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001681 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001682 return Error(EqualLoc, "redefinition of '" + Name + "'");
1683 else if (!Sym->isVariable())
1684 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001685 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001686 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1687 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001688
1689 // Don't count these checks as uses.
1690 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001691 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001692 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001693
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001694 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001695
1696 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001697 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001698
1699 return false;
1700}
1701
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001702/// ParseIdentifier:
1703/// ::= identifier
1704/// ::= string
1705bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001706 // The assembler has relaxed rules for accepting identifiers, in particular we
1707 // allow things like '.globl $foo', which would normally be separate
1708 // tokens. At this level, we have already lexed so we cannot (currently)
1709 // handle this as a context dependent token, instead we detect adjacent tokens
1710 // and return the combined identifier.
1711 if (Lexer.is(AsmToken::Dollar)) {
1712 SMLoc DollarLoc = getLexer().getLoc();
1713
1714 // Consume the dollar sign, and check for a following identifier.
1715 Lex();
1716 if (Lexer.isNot(AsmToken::Identifier))
1717 return true;
1718
1719 // We have a '$' followed by an identifier, make sure they are adjacent.
1720 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1721 return true;
1722
1723 // Construct the joined identifier and consume the token.
1724 Res = StringRef(DollarLoc.getPointer(),
1725 getTok().getIdentifier().size() + 1);
1726 Lex();
1727 return false;
1728 }
1729
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001730 if (Lexer.isNot(AsmToken::Identifier) &&
1731 Lexer.isNot(AsmToken::String))
1732 return true;
1733
Sean Callanan18b83232010-01-19 21:44:56 +00001734 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001735
Sean Callanan79ed1a82010-01-19 20:22:31 +00001736 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001737
1738 return false;
1739}
1740
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001741/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001742/// ::= .equ identifier ',' expression
1743/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001744/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001745bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001746 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001747
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001748 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001749 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001750
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001752 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001753 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001754
Nico Weber4c4c7322011-01-28 03:04:41 +00001755 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001756}
1757
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001758bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001760
1761 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001762 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001763 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1764 if (Str[i] != '\\') {
1765 Data += Str[i];
1766 continue;
1767 }
1768
1769 // Recognize escaped characters. Note that this escape semantics currently
1770 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1771 ++i;
1772 if (i == e)
1773 return TokError("unexpected backslash at end of string");
1774
1775 // Recognize octal sequences.
1776 if ((unsigned) (Str[i] - '0') <= 7) {
1777 // Consume up to three octal characters.
1778 unsigned Value = Str[i] - '0';
1779
1780 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1781 ++i;
1782 Value = Value * 8 + (Str[i] - '0');
1783
1784 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1785 ++i;
1786 Value = Value * 8 + (Str[i] - '0');
1787 }
1788 }
1789
1790 if (Value > 255)
1791 return TokError("invalid octal escape sequence (out of range)");
1792
1793 Data += (unsigned char) Value;
1794 continue;
1795 }
1796
1797 // Otherwise recognize individual escapes.
1798 switch (Str[i]) {
1799 default:
1800 // Just reject invalid escape sequences for now.
1801 return TokError("invalid escape sequence (unrecognized character)");
1802
1803 case 'b': Data += '\b'; break;
1804 case 'f': Data += '\f'; break;
1805 case 'n': Data += '\n'; break;
1806 case 'r': Data += '\r'; break;
1807 case 't': Data += '\t'; break;
1808 case '"': Data += '"'; break;
1809 case '\\': Data += '\\'; break;
1810 }
1811 }
1812
1813 return false;
1814}
1815
Daniel Dunbara0d14262009-06-24 23:30:00 +00001816/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001817/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1818bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001819 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001820 CheckForValidSection();
1821
Daniel Dunbara0d14262009-06-24 23:30:00 +00001822 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001823 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001824 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001825
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001826 std::string Data;
1827 if (ParseEscapedString(Data))
1828 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001829
1830 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001831 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1833
Sean Callanan79ed1a82010-01-19 20:22:31 +00001834 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001835
1836 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001837 break;
1838
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001839 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001840 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001841 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001842 }
1843 }
1844
Sean Callanan79ed1a82010-01-19 20:22:31 +00001845 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001846 return false;
1847}
1848
1849/// ParseDirectiveValue
1850/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1851bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001852 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001853 CheckForValidSection();
1854
Daniel Dunbara0d14262009-06-24 23:30:00 +00001855 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001856 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001857 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001858 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001859 return true;
1860
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001861 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001862 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1863 assert(Size <= 8 && "Invalid size");
1864 uint64_t IntValue = MCE->getValue();
1865 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1866 return Error(ExprLoc, "literal value out of range for directive");
1867 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1868 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001869 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001870
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001871 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001872 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001873
Daniel Dunbara0d14262009-06-24 23:30:00 +00001874 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001875 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001876 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001877 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001878 }
1879 }
1880
Sean Callanan79ed1a82010-01-19 20:22:31 +00001881 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001882 return false;
1883}
1884
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001885/// ParseDirectiveRealValue
1886/// ::= (.single | .double) [ expression (, expression)* ]
1887bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1888 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1889 CheckForValidSection();
1890
1891 for (;;) {
1892 // We don't truly support arithmetic on floating point expressions, so we
1893 // have to manually parse unary prefixes.
1894 bool IsNeg = false;
1895 if (getLexer().is(AsmToken::Minus)) {
1896 Lex();
1897 IsNeg = true;
1898 } else if (getLexer().is(AsmToken::Plus))
1899 Lex();
1900
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001901 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001902 getLexer().isNot(AsmToken::Real) &&
1903 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001904 return TokError("unexpected token in directive");
1905
1906 // Convert to an APFloat.
1907 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001908 StringRef IDVal = getTok().getString();
1909 if (getLexer().is(AsmToken::Identifier)) {
1910 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1911 Value = APFloat::getInf(Semantics);
1912 else if (!IDVal.compare_lower("nan"))
1913 Value = APFloat::getNaN(Semantics, false, ~0);
1914 else
1915 return TokError("invalid floating point literal");
1916 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001917 APFloat::opInvalidOp)
1918 return TokError("invalid floating point literal");
1919 if (IsNeg)
1920 Value.changeSign();
1921
1922 // Consume the numeric token.
1923 Lex();
1924
1925 // Emit the value as an integer.
1926 APInt AsInt = Value.bitcastToAPInt();
1927 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1928 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1929
1930 if (getLexer().is(AsmToken::EndOfStatement))
1931 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001932
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001933 if (getLexer().isNot(AsmToken::Comma))
1934 return TokError("unexpected token in directive");
1935 Lex();
1936 }
1937 }
1938
1939 Lex();
1940 return false;
1941}
1942
Daniel Dunbara0d14262009-06-24 23:30:00 +00001943/// ParseDirectiveSpace
1944/// ::= .space expression [ , expression ]
1945bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001946 CheckForValidSection();
1947
Daniel Dunbara0d14262009-06-24 23:30:00 +00001948 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001949 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001950 return true;
1951
1952 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001953 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1954 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001955 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001956 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001957
Daniel Dunbar475839e2009-06-29 20:37:27 +00001958 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001959 return true;
1960
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001961 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001962 return TokError("unexpected token in '.space' directive");
1963 }
1964
Sean Callanan79ed1a82010-01-19 20:22:31 +00001965 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001966
1967 if (NumBytes <= 0)
1968 return TokError("invalid number of bytes in '.space' directive");
1969
1970 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001971 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001972
1973 return false;
1974}
1975
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001976/// ParseDirectiveZero
1977/// ::= .zero expression
1978bool AsmParser::ParseDirectiveZero() {
1979 CheckForValidSection();
1980
1981 int64_t NumBytes;
1982 if (ParseAbsoluteExpression(NumBytes))
1983 return true;
1984
Rafael Espindolae452b172010-10-05 19:42:57 +00001985 int64_t Val = 0;
1986 if (getLexer().is(AsmToken::Comma)) {
1987 Lex();
1988 if (ParseAbsoluteExpression(Val))
1989 return true;
1990 }
1991
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001992 if (getLexer().isNot(AsmToken::EndOfStatement))
1993 return TokError("unexpected token in '.zero' directive");
1994
1995 Lex();
1996
Rafael Espindolae452b172010-10-05 19:42:57 +00001997 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001998
1999 return false;
2000}
2001
Daniel Dunbara0d14262009-06-24 23:30:00 +00002002/// ParseDirectiveFill
2003/// ::= .fill expression , expression , expression
2004bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002005 CheckForValidSection();
2006
Daniel Dunbara0d14262009-06-24 23:30:00 +00002007 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002008 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002009 return true;
2010
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002011 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002012 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002013 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002014
Daniel Dunbara0d14262009-06-24 23:30:00 +00002015 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002016 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002017 return true;
2018
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002019 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002020 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002021 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002022
Daniel Dunbara0d14262009-06-24 23:30:00 +00002023 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00002024 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002025 return true;
2026
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002027 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00002028 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002029
Sean Callanan79ed1a82010-01-19 20:22:31 +00002030 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00002031
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00002032 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
2033 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00002034
2035 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002036 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00002037
2038 return false;
2039}
Daniel Dunbarc238b582009-06-25 22:44:51 +00002040
2041/// ParseDirectiveOrg
2042/// ::= .org expression [ , expression ]
2043bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002044 CheckForValidSection();
2045
Daniel Dunbar821e3332009-08-31 08:09:28 +00002046 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00002047 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00002048 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002049 return true;
2050
2051 // Parse optional fill expression.
2052 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002053 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2054 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002055 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002056 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002057
Daniel Dunbar475839e2009-06-29 20:37:27 +00002058 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002059 return true;
2060
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002061 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002062 return TokError("unexpected token in '.org' directive");
2063 }
2064
Sean Callanan79ed1a82010-01-19 20:22:31 +00002065 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002066
Jim Grosbachebd4c052012-01-27 00:37:08 +00002067 // Only limited forms of relocatable expressions are accepted here, it
2068 // has to be relative to the current section. The streamer will return
2069 // 'true' if the expression wasn't evaluatable.
2070 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2071 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002072
2073 return false;
2074}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002075
2076/// ParseDirectiveAlign
2077/// ::= {.align, ...} expression [ , expression [ , expression ]]
2078bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002079 CheckForValidSection();
2080
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002081 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002082 int64_t Alignment;
2083 if (ParseAbsoluteExpression(Alignment))
2084 return true;
2085
2086 SMLoc MaxBytesLoc;
2087 bool HasFillExpr = false;
2088 int64_t FillExpr = 0;
2089 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002090 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2091 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002092 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002093 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002094
2095 // The fill expression can be omitted while specifying a maximum number of
2096 // alignment bytes, e.g:
2097 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002098 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002099 HasFillExpr = true;
2100 if (ParseAbsoluteExpression(FillExpr))
2101 return true;
2102 }
2103
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002104 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2105 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002106 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002107 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002108
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002109 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002110 if (ParseAbsoluteExpression(MaxBytesToFill))
2111 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002112
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002113 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002114 return TokError("unexpected token in directive");
2115 }
2116 }
2117
Sean Callanan79ed1a82010-01-19 20:22:31 +00002118 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002119
Daniel Dunbar648ac512010-05-17 21:54:30 +00002120 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002121 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002122
2123 // Compute alignment in bytes.
2124 if (IsPow2) {
2125 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002126 if (Alignment >= 32) {
2127 Error(AlignmentLoc, "invalid alignment value");
2128 Alignment = 31;
2129 }
2130
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002131 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002132 }
2133
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002134 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002135 if (MaxBytesLoc.isValid()) {
2136 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002137 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2138 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002139 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002140 }
2141
2142 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002143 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2144 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002145 MaxBytesToFill = 0;
2146 }
2147 }
2148
Daniel Dunbar648ac512010-05-17 21:54:30 +00002149 // Check whether we should use optimal code alignment for this .align
2150 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002151 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002152 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2153 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002154 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002155 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002156 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002157 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2158 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002159 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002160
2161 return false;
2162}
2163
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002164/// ParseDirectiveSymbolAttribute
2165/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002166bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002167 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002168 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002169 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002170 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002171
2172 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002173 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002174
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002175 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002176
Jim Grosbach10ec6502011-09-15 17:56:49 +00002177 // Assembler local symbols don't make any sense here. Complain loudly.
2178 if (Sym->isTemporary())
2179 return Error(Loc, "non-local symbol required in directive");
2180
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002181 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002182
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002183 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002184 break;
2185
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002186 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002187 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002188 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002189 }
2190 }
2191
Sean Callanan79ed1a82010-01-19 20:22:31 +00002192 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002193 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002194}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002195
2196/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002197/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2198bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002199 CheckForValidSection();
2200
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002201 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002202 StringRef Name;
2203 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002204 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002205
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002206 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002207 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002208
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002209 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002210 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002211 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002212
2213 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002214 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002215 if (ParseAbsoluteExpression(Size))
2216 return true;
2217
2218 int64_t Pow2Alignment = 0;
2219 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002220 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002221 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002222 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002223 if (ParseAbsoluteExpression(Pow2Alignment))
2224 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002225
Chris Lattner258281d2010-01-19 06:22:22 +00002226 // If this target takes alignments in bytes (not log) validate and convert.
2227 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2228 if (!isPowerOf2_64(Pow2Alignment))
2229 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2230 Pow2Alignment = Log2_64(Pow2Alignment);
2231 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002232 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002233
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002234 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002235 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002236
Sean Callanan79ed1a82010-01-19 20:22:31 +00002237 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002238
Chris Lattner1fc3d752009-07-09 17:25:12 +00002239 // NOTE: a size of zero for a .comm should create a undefined symbol
2240 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002241 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002242 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2243 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002244
Eric Christopherc260a3e2010-05-14 01:38:54 +00002245 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002246 // may internally end up wanting an alignment in bytes.
2247 // FIXME: Diagnose overflow.
2248 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002249 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2250 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002251
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002252 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002253 return Error(IDLoc, "invalid symbol redefinition");
2254
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002255 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002256 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002257 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002258 getStreamer().EmitZerofill(Ctx.getMachOSection(
2259 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2260 0, SectionKind::getBSS()),
2261 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002262 return false;
2263 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002264
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002265 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002266 return false;
2267}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002268
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002269/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002270/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002271bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002272 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002273 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002274
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002275 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002276 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002277 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002278
Sean Callanan79ed1a82010-01-19 20:22:31 +00002279 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002280
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002281 if (Str.empty())
2282 Error(Loc, ".abort detected. Assembly stopping.");
2283 else
2284 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002285 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002286
2287 return false;
2288}
Kevin Enderby71148242009-07-14 21:35:03 +00002289
Kevin Enderby1f049b22009-07-14 23:21:55 +00002290/// ParseDirectiveInclude
2291/// ::= .include "filename"
2292bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002293 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002294 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002295
Sean Callanan18b83232010-01-19 21:44:56 +00002296 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002297 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002298 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002299
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002300 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002301 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002302
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002303 // Strip the quotes.
2304 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002305
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002306 // Attempt to switch the lexer to the included file before consuming the end
2307 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002308 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002309 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002310 return true;
2311 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002312
2313 return false;
2314}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002315
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002316/// ParseDirectiveIncbin
2317/// ::= .incbin "filename"
2318bool AsmParser::ParseDirectiveIncbin() {
2319 if (getLexer().isNot(AsmToken::String))
2320 return TokError("expected string in '.incbin' directive");
2321
2322 std::string Filename = getTok().getString();
2323 SMLoc IncbinLoc = getLexer().getLoc();
2324 Lex();
2325
2326 if (getLexer().isNot(AsmToken::EndOfStatement))
2327 return TokError("unexpected token in '.incbin' directive");
2328
2329 // Strip the quotes.
2330 Filename = Filename.substr(1, Filename.size()-2);
2331
2332 // Attempt to process the included file.
2333 if (ProcessIncbinFile(Filename)) {
2334 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2335 return true;
2336 }
2337
2338 return false;
2339}
2340
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002341/// ParseDirectiveIf
2342/// ::= .if expression
2343bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002344 TheCondStack.push_back(TheCondState);
2345 TheCondState.TheCond = AsmCond::IfCond;
Benjamin Kramer29739e72012-05-12 16:52:21 +00002346 if (TheCondState.Ignore) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002347 EatToEndOfStatement();
Benjamin Kramer29739e72012-05-12 16:52:21 +00002348 } else {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002349 int64_t ExprValue;
2350 if (ParseAbsoluteExpression(ExprValue))
2351 return true;
2352
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002353 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002354 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002355
Sean Callanan79ed1a82010-01-19 20:22:31 +00002356 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002357
2358 TheCondState.CondMet = ExprValue;
2359 TheCondState.Ignore = !TheCondState.CondMet;
2360 }
2361
2362 return false;
2363}
2364
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002365/// ParseDirectiveIfb
2366/// ::= .ifb string
2367bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
2368 TheCondStack.push_back(TheCondState);
2369 TheCondState.TheCond = AsmCond::IfCond;
2370
Benjamin Kramer29739e72012-05-12 16:52:21 +00002371 if (TheCondState.Ignore) {
Benjamin Kramera3dd0eb2012-05-12 11:18:42 +00002372 EatToEndOfStatement();
2373 } else {
2374 StringRef Str = ParseStringToEndOfStatement();
2375
2376 if (getLexer().isNot(AsmToken::EndOfStatement))
2377 return TokError("unexpected token in '.ifb' directive");
2378
2379 Lex();
2380
2381 TheCondState.CondMet = ExpectBlank == Str.empty();
2382 TheCondState.Ignore = !TheCondState.CondMet;
2383 }
2384
2385 return false;
2386}
2387
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002388/// ParseDirectiveIfc
2389/// ::= .ifc string1, string2
2390bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) {
2391 TheCondStack.push_back(TheCondState);
2392 TheCondState.TheCond = AsmCond::IfCond;
2393
Benjamin Kramer29739e72012-05-12 16:52:21 +00002394 if (TheCondState.Ignore) {
Benjamin Kramerdec06ef2012-05-12 11:18:51 +00002395 EatToEndOfStatement();
2396 } else {
2397 StringRef Str1 = ParseStringToComma();
2398
2399 if (getLexer().isNot(AsmToken::Comma))
2400 return TokError("unexpected token in '.ifc' directive");
2401
2402 Lex();
2403
2404 StringRef Str2 = ParseStringToEndOfStatement();
2405
2406 if (getLexer().isNot(AsmToken::EndOfStatement))
2407 return TokError("unexpected token in '.ifc' directive");
2408
2409 Lex();
2410
2411 TheCondState.CondMet = ExpectEqual == (Str1 == Str2);
2412 TheCondState.Ignore = !TheCondState.CondMet;
2413 }
2414
2415 return false;
2416}
2417
2418/// ParseDirectiveIfdef
2419/// ::= .ifdef symbol
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002420bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2421 StringRef Name;
2422 TheCondStack.push_back(TheCondState);
2423 TheCondState.TheCond = AsmCond::IfCond;
2424
2425 if (TheCondState.Ignore) {
2426 EatToEndOfStatement();
2427 } else {
2428 if (ParseIdentifier(Name))
2429 return TokError("expected identifier after '.ifdef'");
2430
2431 Lex();
2432
2433 MCSymbol *Sym = getContext().LookupSymbol(Name);
2434
2435 if (expect_defined)
2436 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2437 else
2438 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2439 TheCondState.Ignore = !TheCondState.CondMet;
2440 }
2441
2442 return false;
2443}
2444
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002445/// ParseDirectiveElseIf
2446/// ::= .elseif expression
2447bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2448 if (TheCondState.TheCond != AsmCond::IfCond &&
2449 TheCondState.TheCond != AsmCond::ElseIfCond)
2450 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2451 " an .elseif");
2452 TheCondState.TheCond = AsmCond::ElseIfCond;
2453
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002454 bool LastIgnoreState = false;
2455 if (!TheCondStack.empty())
2456 LastIgnoreState = TheCondStack.back().Ignore;
2457 if (LastIgnoreState || TheCondState.CondMet) {
2458 TheCondState.Ignore = true;
2459 EatToEndOfStatement();
2460 }
2461 else {
2462 int64_t ExprValue;
2463 if (ParseAbsoluteExpression(ExprValue))
2464 return true;
2465
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002466 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002467 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002468
Sean Callanan79ed1a82010-01-19 20:22:31 +00002469 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002470 TheCondState.CondMet = ExprValue;
2471 TheCondState.Ignore = !TheCondState.CondMet;
2472 }
2473
2474 return false;
2475}
2476
2477/// ParseDirectiveElse
2478/// ::= .else
2479bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002480 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002481 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002482
Sean Callanan79ed1a82010-01-19 20:22:31 +00002483 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002484
2485 if (TheCondState.TheCond != AsmCond::IfCond &&
2486 TheCondState.TheCond != AsmCond::ElseIfCond)
2487 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2488 ".elseif");
2489 TheCondState.TheCond = AsmCond::ElseCond;
2490 bool LastIgnoreState = false;
2491 if (!TheCondStack.empty())
2492 LastIgnoreState = TheCondStack.back().Ignore;
2493 if (LastIgnoreState || TheCondState.CondMet)
2494 TheCondState.Ignore = true;
2495 else
2496 TheCondState.Ignore = false;
2497
2498 return false;
2499}
2500
2501/// ParseDirectiveEndIf
2502/// ::= .endif
2503bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002504 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002505 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002506
Sean Callanan79ed1a82010-01-19 20:22:31 +00002507 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002508
2509 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2510 TheCondStack.empty())
2511 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2512 ".else");
2513 if (!TheCondStack.empty()) {
2514 TheCondState = TheCondStack.back();
2515 TheCondStack.pop_back();
2516 }
2517
2518 return false;
2519}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002520
2521/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002522/// ::= .file [number] filename
2523/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002524bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002525 // FIXME: I'm not sure what this is.
2526 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002527 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002528 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002529 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002530 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002531
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002532 if (FileNumber < 1)
2533 return TokError("file number less than one");
2534 }
2535
Daniel Dunbareceec052010-07-12 17:45:27 +00002536 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002537 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002538
Nick Lewycky44d798d2011-10-17 23:05:28 +00002539 // Usually the directory and filename together, otherwise just the directory.
2540 StringRef Path = getTok().getString();
2541 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002542 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002543
Nick Lewycky44d798d2011-10-17 23:05:28 +00002544 StringRef Directory;
2545 StringRef Filename;
2546 if (getLexer().is(AsmToken::String)) {
2547 if (FileNumber == -1)
2548 return TokError("explicit path specified, but no file number");
2549 Filename = getTok().getString();
2550 Filename = Filename.substr(1, Filename.size()-2);
2551 Directory = Path;
2552 Lex();
2553 } else {
2554 Filename = Path;
2555 }
2556
Daniel Dunbareceec052010-07-12 17:45:27 +00002557 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002558 return TokError("unexpected token in '.file' directive");
2559
Chris Lattnerd32e8032010-01-25 19:02:58 +00002560 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002561 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002562 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002563 if (getContext().getGenDwarfForAssembly() == true)
2564 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2565 "used to generate dwarf debug info for assembly code");
2566
Nick Lewycky44d798d2011-10-17 23:05:28 +00002567 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002568 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002569 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002570
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002571 return false;
2572}
2573
2574/// ParseDirectiveLine
2575/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002576bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002577 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2578 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002579 return TokError("unexpected token in '.line' directive");
2580
Sean Callanan18b83232010-01-19 21:44:56 +00002581 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002582 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002583 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002584
2585 // FIXME: Do something with the .line.
2586 }
2587
Daniel Dunbareceec052010-07-12 17:45:27 +00002588 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002589 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002590
2591 return false;
2592}
2593
2594
2595/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002596/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002597/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2598/// The first number is a file number, must have been previously assigned with
2599/// a .file directive, the second number is the line number and optionally the
2600/// third number is a column position (zero if not specified). The remaining
2601/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002602bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002603
Daniel Dunbareceec052010-07-12 17:45:27 +00002604 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002605 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002606 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002607 if (FileNumber < 1)
2608 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002609 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002610 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002611 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002612
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002613 int64_t LineNumber = 0;
2614 if (getLexer().is(AsmToken::Integer)) {
2615 LineNumber = getTok().getIntVal();
2616 if (LineNumber < 1)
2617 return TokError("line number less than one in '.loc' directive");
2618 Lex();
2619 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002620
2621 int64_t ColumnPos = 0;
2622 if (getLexer().is(AsmToken::Integer)) {
2623 ColumnPos = getTok().getIntVal();
2624 if (ColumnPos < 0)
2625 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002626 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002627 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002628
Kevin Enderbyc0957932010-09-30 16:52:03 +00002629 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002630 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002631 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002632 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2633 for (;;) {
2634 if (getLexer().is(AsmToken::EndOfStatement))
2635 break;
2636
2637 StringRef Name;
2638 SMLoc Loc = getTok().getLoc();
2639 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002640 return TokError("unexpected token in '.loc' directive");
2641
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002642 if (Name == "basic_block")
2643 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2644 else if (Name == "prologue_end")
2645 Flags |= DWARF2_FLAG_PROLOGUE_END;
2646 else if (Name == "epilogue_begin")
2647 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2648 else if (Name == "is_stmt") {
2649 SMLoc Loc = getTok().getLoc();
2650 const MCExpr *Value;
2651 if (getParser().ParseExpression(Value))
2652 return true;
2653 // The expression must be the constant 0 or 1.
2654 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2655 int Value = MCE->getValue();
2656 if (Value == 0)
2657 Flags &= ~DWARF2_FLAG_IS_STMT;
2658 else if (Value == 1)
2659 Flags |= DWARF2_FLAG_IS_STMT;
2660 else
2661 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002662 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002663 else {
2664 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2665 }
2666 }
2667 else if (Name == "isa") {
2668 SMLoc Loc = getTok().getLoc();
2669 const MCExpr *Value;
2670 if (getParser().ParseExpression(Value))
2671 return true;
2672 // The expression must be a constant greater or equal to 0.
2673 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2674 int Value = MCE->getValue();
2675 if (Value < 0)
2676 return Error(Loc, "isa number less than zero");
2677 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002678 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002679 else {
2680 return Error(Loc, "isa number not a constant value");
2681 }
2682 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002683 else if (Name == "discriminator") {
2684 if (getParser().ParseAbsoluteExpression(Discriminator))
2685 return true;
2686 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002687 else {
2688 return Error(Loc, "unknown sub-directive in '.loc' directive");
2689 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002690
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002691 if (getLexer().is(AsmToken::EndOfStatement))
2692 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002693 }
2694 }
2695
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002696 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002697 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002698
2699 return false;
2700}
2701
Daniel Dunbar138abae2010-10-16 04:56:42 +00002702/// ParseDirectiveStabs
2703/// ::= .stabs string, number, number, number
2704bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2705 SMLoc DirectiveLoc) {
2706 return TokError("unsupported directive '" + Directive + "'");
2707}
2708
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002709/// ParseDirectiveCFISections
2710/// ::= .cfi_sections section [, section]
2711bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2712 SMLoc DirectiveLoc) {
2713 StringRef Name;
2714 bool EH = false;
2715 bool Debug = false;
2716
2717 if (getParser().ParseIdentifier(Name))
2718 return TokError("Expected an identifier");
2719
2720 if (Name == ".eh_frame")
2721 EH = true;
2722 else if (Name == ".debug_frame")
2723 Debug = true;
2724
2725 if (getLexer().is(AsmToken::Comma)) {
2726 Lex();
2727
2728 if (getParser().ParseIdentifier(Name))
2729 return TokError("Expected an identifier");
2730
2731 if (Name == ".eh_frame")
2732 EH = true;
2733 else if (Name == ".debug_frame")
2734 Debug = true;
2735 }
2736
2737 getStreamer().EmitCFISections(EH, Debug);
2738
2739 return false;
2740}
2741
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002742/// ParseDirectiveCFIStartProc
2743/// ::= .cfi_startproc
2744bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2745 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002746 getStreamer().EmitCFIStartProc();
2747 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002748}
2749
2750/// ParseDirectiveCFIEndProc
2751/// ::= .cfi_endproc
2752bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002753 getStreamer().EmitCFIEndProc();
2754 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002755}
2756
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002757/// ParseRegisterOrRegisterNumber - parse register name or number.
2758bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2759 SMLoc DirectiveLoc) {
2760 unsigned RegNo;
2761
Jim Grosbach6f888a82011-06-02 17:14:04 +00002762 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002763 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2764 DirectiveLoc))
2765 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002766 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002767 } else
2768 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002769
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002770 return false;
2771}
2772
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002773/// ParseDirectiveCFIDefCfa
2774/// ::= .cfi_def_cfa register, offset
2775bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2776 SMLoc DirectiveLoc) {
2777 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002778 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002779 return true;
2780
2781 if (getLexer().isNot(AsmToken::Comma))
2782 return TokError("unexpected token in directive");
2783 Lex();
2784
2785 int64_t Offset = 0;
2786 if (getParser().ParseAbsoluteExpression(Offset))
2787 return true;
2788
Rafael Espindola066c2f42011-04-12 23:59:07 +00002789 getStreamer().EmitCFIDefCfa(Register, Offset);
2790 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002791}
2792
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002793/// ParseDirectiveCFIDefCfaOffset
2794/// ::= .cfi_def_cfa_offset offset
2795bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2796 SMLoc DirectiveLoc) {
2797 int64_t Offset = 0;
2798 if (getParser().ParseAbsoluteExpression(Offset))
2799 return true;
2800
Rafael Espindola066c2f42011-04-12 23:59:07 +00002801 getStreamer().EmitCFIDefCfaOffset(Offset);
2802 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002803}
2804
2805/// ParseDirectiveCFIAdjustCfaOffset
2806/// ::= .cfi_adjust_cfa_offset adjustment
2807bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2808 SMLoc DirectiveLoc) {
2809 int64_t Adjustment = 0;
2810 if (getParser().ParseAbsoluteExpression(Adjustment))
2811 return true;
2812
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002813 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2814 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002815}
2816
2817/// ParseDirectiveCFIDefCfaRegister
2818/// ::= .cfi_def_cfa_register register
2819bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2820 SMLoc DirectiveLoc) {
2821 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002822 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002823 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002824
Rafael Espindola066c2f42011-04-12 23:59:07 +00002825 getStreamer().EmitCFIDefCfaRegister(Register);
2826 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002827}
2828
2829/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002830/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002831bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2832 int64_t Register = 0;
2833 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002834
2835 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002836 return true;
2837
2838 if (getLexer().isNot(AsmToken::Comma))
2839 return TokError("unexpected token in directive");
2840 Lex();
2841
2842 if (getParser().ParseAbsoluteExpression(Offset))
2843 return true;
2844
Rafael Espindola066c2f42011-04-12 23:59:07 +00002845 getStreamer().EmitCFIOffset(Register, Offset);
2846 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002847}
2848
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002849/// ParseDirectiveCFIRelOffset
2850/// ::= .cfi_rel_offset register, offset
2851bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2852 SMLoc DirectiveLoc) {
2853 int64_t Register = 0;
2854
2855 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2856 return true;
2857
2858 if (getLexer().isNot(AsmToken::Comma))
2859 return TokError("unexpected token in directive");
2860 Lex();
2861
2862 int64_t Offset = 0;
2863 if (getParser().ParseAbsoluteExpression(Offset))
2864 return true;
2865
Rafael Espindola25f492e2011-04-12 16:12:03 +00002866 getStreamer().EmitCFIRelOffset(Register, Offset);
2867 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002868}
2869
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002870static bool isValidEncoding(int64_t Encoding) {
2871 if (Encoding & ~0xff)
2872 return false;
2873
2874 if (Encoding == dwarf::DW_EH_PE_omit)
2875 return true;
2876
2877 const unsigned Format = Encoding & 0xf;
2878 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2879 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2880 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2881 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2882 return false;
2883
Rafael Espindolacaf11582010-12-29 04:31:26 +00002884 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002885 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002886 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002887 return false;
2888
2889 return true;
2890}
2891
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002892/// ParseDirectiveCFIPersonalityOrLsda
2893/// ::= .cfi_personality encoding, [symbol_name]
2894/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002895bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002896 SMLoc DirectiveLoc) {
2897 int64_t Encoding = 0;
2898 if (getParser().ParseAbsoluteExpression(Encoding))
2899 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002900 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002901 return false;
2902
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002903 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002904 return TokError("unsupported encoding.");
2905
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002906 if (getLexer().isNot(AsmToken::Comma))
2907 return TokError("unexpected token in directive");
2908 Lex();
2909
2910 StringRef Name;
2911 if (getParser().ParseIdentifier(Name))
2912 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002913
2914 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2915
2916 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002917 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002918 else {
2919 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002920 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002921 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002922 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002923}
2924
Rafael Espindolafe024d02010-12-28 18:36:23 +00002925/// ParseDirectiveCFIRememberState
2926/// ::= .cfi_remember_state
2927bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2928 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002929 getStreamer().EmitCFIRememberState();
2930 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002931}
2932
2933/// ParseDirectiveCFIRestoreState
2934/// ::= .cfi_remember_state
2935bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2936 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002937 getStreamer().EmitCFIRestoreState();
2938 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002939}
2940
Rafael Espindolac5754392011-04-12 15:31:05 +00002941/// ParseDirectiveCFISameValue
2942/// ::= .cfi_same_value register
2943bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2944 SMLoc DirectiveLoc) {
2945 int64_t Register = 0;
2946
2947 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2948 return true;
2949
2950 getStreamer().EmitCFISameValue(Register);
2951
2952 return false;
2953}
2954
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002955/// ParseDirectiveCFIRestore
2956/// ::= .cfi_restore register
2957bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2958 SMLoc DirectiveLoc) {
2959 int64_t Register = 0;
2960 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2961 return true;
2962
2963 getStreamer().EmitCFIRestore(Register);
2964
2965 return false;
2966}
2967
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002968/// ParseDirectiveCFIEscape
2969/// ::= .cfi_escape expression[,...]
2970bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2971 SMLoc DirectiveLoc) {
2972 std::string Values;
2973 int64_t CurrValue;
2974 if (getParser().ParseAbsoluteExpression(CurrValue))
2975 return true;
2976
2977 Values.push_back((uint8_t)CurrValue);
2978
2979 while (getLexer().is(AsmToken::Comma)) {
2980 Lex();
2981
2982 if (getParser().ParseAbsoluteExpression(CurrValue))
2983 return true;
2984
2985 Values.push_back((uint8_t)CurrValue);
2986 }
2987
2988 getStreamer().EmitCFIEscape(Values);
2989 return false;
2990}
2991
Rafael Espindola16d7d432012-01-23 21:51:52 +00002992/// ParseDirectiveCFISignalFrame
2993/// ::= .cfi_signal_frame
2994bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2995 SMLoc DirectiveLoc) {
2996 if (getLexer().isNot(AsmToken::EndOfStatement))
2997 return Error(getLexer().getLoc(),
2998 "unexpected token in '" + Directive + "' directive");
2999
3000 getStreamer().EmitCFISignalFrame();
3001
3002 return false;
3003}
3004
Daniel Dunbar3c802de2010-07-18 18:38:02 +00003005/// ParseDirectiveMacrosOnOff
3006/// ::= .macros_on
3007/// ::= .macros_off
3008bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
3009 SMLoc DirectiveLoc) {
3010 if (getLexer().isNot(AsmToken::EndOfStatement))
3011 return Error(getLexer().getLoc(),
3012 "unexpected token in '" + Directive + "' directive");
3013
3014 getParser().MacrosEnabled = Directive == ".macros_on";
3015
3016 return false;
3017}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003018
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003019/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00003020/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003021bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
3022 SMLoc DirectiveLoc) {
3023 StringRef Name;
3024 if (getParser().ParseIdentifier(Name))
3025 return TokError("expected identifier in directive");
3026
Rafael Espindola65366442011-06-05 02:43:45 +00003027 std::vector<StringRef> Parameters;
3028 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3029 for(;;) {
3030 StringRef Parameter;
3031 if (getParser().ParseIdentifier(Parameter))
3032 return TokError("expected identifier in directive");
3033 Parameters.push_back(Parameter);
3034
3035 if (getLexer().isNot(AsmToken::Comma))
3036 break;
3037 Lex();
3038 }
3039 }
3040
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003041 if (getLexer().isNot(AsmToken::EndOfStatement))
3042 return TokError("unexpected token in '.macro' directive");
3043
3044 // Eat the end of statement.
3045 Lex();
3046
3047 AsmToken EndToken, StartToken = getTok();
3048
3049 // Lex the macro definition.
3050 for (;;) {
3051 // Check whether we have reached the end of the file.
3052 if (getLexer().is(AsmToken::Eof))
3053 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
3054
3055 // Otherwise, check whether we have reach the .endmacro.
3056 if (getLexer().is(AsmToken::Identifier) &&
3057 (getTok().getIdentifier() == ".endm" ||
3058 getTok().getIdentifier() == ".endmacro")) {
3059 EndToken = getTok();
3060 Lex();
3061 if (getLexer().isNot(AsmToken::EndOfStatement))
3062 return TokError("unexpected token in '" + EndToken.getIdentifier() +
3063 "' directive");
3064 break;
3065 }
3066
3067 // Otherwise, scan til the end of the statement.
3068 getParser().EatToEndOfStatement();
3069 }
3070
3071 if (getParser().MacroMap.lookup(Name)) {
3072 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
3073 }
3074
3075 const char *BodyStart = StartToken.getLoc().getPointer();
3076 const char *BodyEnd = EndToken.getLoc().getPointer();
3077 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00003078 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003079 return false;
3080}
3081
3082/// ParseDirectiveEndMacro
3083/// ::= .endm
3084/// ::= .endmacro
3085bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
3086 SMLoc DirectiveLoc) {
3087 if (getLexer().isNot(AsmToken::EndOfStatement))
3088 return TokError("unexpected token in '" + Directive + "' directive");
3089
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00003090 // If we are inside a macro instantiation, terminate the current
3091 // instantiation.
3092 if (!getParser().ActiveMacros.empty()) {
3093 getParser().HandleMacroExit();
3094 return false;
3095 }
3096
3097 // Otherwise, this .endmacro is a stray entry in the file; well formed
3098 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00003099 return TokError("unexpected '" + Directive + "' in file, "
3100 "no current macro definition");
3101}
3102
Benjamin Kramerbc3b27c2012-05-12 11:21:46 +00003103/// ParseDirectivePurgeMacro
3104/// ::= .purgem
3105bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive,
3106 SMLoc DirectiveLoc) {
3107 StringRef Name;
3108 if (getParser().ParseIdentifier(Name))
3109 return TokError("expected identifier in '.purgem' directive");
3110
3111 if (getLexer().isNot(AsmToken::EndOfStatement))
3112 return TokError("unexpected token in '.purgem' directive");
3113
3114 StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name);
3115 if (I == getParser().MacroMap.end())
3116 return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
3117
3118 // Undefine the macro.
3119 delete I->getValue();
3120 getParser().MacroMap.erase(I);
3121 return false;
3122}
3123
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003124bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003125 getParser().CheckForValidSection();
3126
3127 const MCExpr *Value;
3128
3129 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003130 return true;
3131
3132 if (getLexer().isNot(AsmToken::EndOfStatement))
3133 return TokError("unexpected token in directive");
3134
3135 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003136 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003137 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003138 getStreamer().EmitULEB128Value(Value);
3139
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003140 return false;
3141}
3142
Rafael Espindola761cb062012-06-03 23:57:14 +00003143Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003144 AsmToken EndToken, StartToken = getTok();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003145
Rafael Espindola761cb062012-06-03 23:57:14 +00003146 unsigned NestLevel = 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003147 for (;;) {
3148 // Check whether we have reached the end of the file.
Rafael Espindola761cb062012-06-03 23:57:14 +00003149 if (getLexer().is(AsmToken::Eof)) {
3150 Error(DirectiveLoc, "no matching '.endr' in definition");
3151 return 0;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003152 }
3153
Rafael Espindola761cb062012-06-03 23:57:14 +00003154 if (Lexer.is(AsmToken::Identifier) &&
3155 (getTok().getIdentifier() == ".rept")) {
3156 ++NestLevel;
3157 }
3158
3159 // Otherwise, check whether we have reached the .endr.
3160 if (Lexer.is(AsmToken::Identifier) &&
3161 getTok().getIdentifier() == ".endr") {
3162 if (NestLevel == 0) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003163 EndToken = getTok();
3164 Lex();
Rafael Espindola761cb062012-06-03 23:57:14 +00003165 if (Lexer.isNot(AsmToken::EndOfStatement)) {
3166 TokError("unexpected token in '.endr' directive");
3167 return 0;
3168 }
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003169 break;
3170 }
Rafael Espindola761cb062012-06-03 23:57:14 +00003171 --NestLevel;
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003172 }
3173
Rafael Espindola761cb062012-06-03 23:57:14 +00003174 // Otherwise, scan till the end of the statement.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003175 EatToEndOfStatement();
3176 }
3177
3178 const char *BodyStart = StartToken.getLoc().getPointer();
3179 const char *BodyEnd = EndToken.getLoc().getPointer();
3180 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
3181
Rafael Espindola761cb062012-06-03 23:57:14 +00003182 // We Are Anonymous.
3183 StringRef Name;
3184 std::vector<StringRef> Parameters;
3185 return new Macro(Name, Body, Parameters);
3186}
3187
3188void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc,
3189 raw_svector_ostream &OS) {
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003190 OS << ".endr\n";
3191
3192 MemoryBuffer *Instantiation =
3193 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
3194
Rafael Espindola761cb062012-06-03 23:57:14 +00003195 // Create the macro instantiation object and add to the current macro
3196 // instantiation stack.
3197 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc,
3198 getTok().getLoc(),
3199 Instantiation);
3200 ActiveMacros.push_back(MI);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003201
Rafael Espindola761cb062012-06-03 23:57:14 +00003202 // Jump to the macro instantiation and prime the lexer.
3203 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
3204 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
3205 Lex();
3206}
3207
3208bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) {
3209 int64_t Count;
3210 if (ParseAbsoluteExpression(Count))
3211 return TokError("unexpected token in '.rept' directive");
3212
3213 if (Count < 0)
3214 return TokError("Count is negative");
3215
3216 if (Lexer.isNot(AsmToken::EndOfStatement))
3217 return TokError("unexpected token in '.rept' directive");
3218
3219 // Eat the end of statement.
3220 Lex();
3221
3222 // Lex the rept definition.
3223 Macro *M = ParseMacroLikeBody(DirectiveLoc);
3224 if (!M)
3225 return true;
3226
3227 // Macro instantiation is lexical, unfortunately. We construct a new buffer
3228 // to hold the macro body with substitutions.
3229 SmallString<256> Buf;
3230 std::vector<StringRef> Parameters;
3231 const std::vector<MacroArgument> A;
3232 raw_svector_ostream OS(Buf);
3233 while (Count--) {
3234 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc()))
3235 return true;
3236 }
3237 InstantiateMacroLikeBody(M, DirectiveLoc, OS);
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003238
3239 return false;
3240}
3241
Rafael Espindola761cb062012-06-03 23:57:14 +00003242bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
3243 if (ActiveMacros.empty())
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003244 return TokError("unexpected '.endr' directive, no current .rept");
3245
3246 // The only .repl that should get here are the ones created by
Rafael Espindola761cb062012-06-03 23:57:14 +00003247 // InstantiateMacroLikeBody.
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003248 assert(getLexer().is(AsmToken::EndOfStatement));
3249
Rafael Espindola761cb062012-06-03 23:57:14 +00003250 HandleMacroExit();
Rafael Espindola2ec304c2012-05-12 16:31:10 +00003251 return false;
3252}
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003253
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003254/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003255MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003256 MCContext &C, MCStreamer &Out,
3257 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003258 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003259}