blob: 25419916c71ee20ce6f6d7373071b85219069d8c [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.
48struct Macro {
49 StringRef Name;
50 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000051 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000052
53public:
Rafael Espindola65366442011-06-05 02:43:45 +000054 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
55 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000056};
57
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000058/// \brief Helper class for storing information about an active macro
59/// instantiation.
60struct MacroInstantiation {
61 /// The macro being instantiated.
62 const Macro *TheMacro;
63
64 /// The macro instantiation with substitutions.
65 MemoryBuffer *Instantiation;
66
67 /// The location of the instantiation.
68 SMLoc InstantiationLoc;
69
70 /// The location where parsing should resume upon instantiation completion.
71 SMLoc ExitLoc;
72
73public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000074 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000075 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000076};
77
Daniel Dunbaraef87e32010-07-18 18:31:38 +000078/// \brief The concrete assembly parser instance.
79class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000080 friend class GenericAsmParser;
81
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
83 void operator=(const AsmParser &); // DO NOT IMPLEMENT
84private:
85 AsmLexer Lexer;
86 MCContext &Ctx;
87 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000088 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000089 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000090 SourceMgr::DiagHandlerTy SavedDiagHandler;
91 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000092 MCAsmParserExtension *GenericParser;
93 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000094
Daniel Dunbaraef87e32010-07-18 18:31:38 +000095 /// This is the current buffer index we're lexing from as managed by the
96 /// SourceMgr object.
97 int CurBuffer;
98
99 AsmCond TheCondState;
100 std::vector<AsmCond> TheCondStack;
101
102 /// DirectiveMap - This is a table handlers for directives. Each handler is
103 /// invoked after the directive identifier is read and is responsible for
104 /// parsing and validating the rest of the directive. The handler is passed
105 /// in the directive name and the location of the directive keyword.
106 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000107
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000108 /// MacroMap - Map of currently defined macros.
109 StringMap<Macro*> MacroMap;
110
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000111 /// ActiveMacros - Stack of active macro instantiations.
112 std::vector<MacroInstantiation*> ActiveMacros;
113
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000114 /// Boolean tracking whether macro substitution is enabled.
115 unsigned MacrosEnabled : 1;
116
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000117 /// Flag tracking whether any errors have been encountered.
118 unsigned HadError : 1;
119
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000120 /// The values from the last parsed cpp hash file line comment if any.
121 StringRef CppHashFilename;
122 int64_t CppHashLineNumber;
123 SMLoc CppHashLoc;
124
Devang Patel0db58bf2012-01-31 18:14:05 +0000125 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
126 unsigned AssemblerDialect;
127
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000128public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000129 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000130 const MCAsmInfo &MAI);
131 ~AsmParser();
132
133 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
134
135 void AddDirectiveHandler(MCAsmParserExtension *Object,
136 StringRef Directive,
137 DirectiveHandler Handler) {
138 DirectiveMap[Directive] = std::make_pair(Object, Handler);
139 }
140
141public:
142 /// @name MCAsmParser Interface
143 /// {
144
145 virtual SourceMgr &getSourceManager() { return SrcMgr; }
146 virtual MCAsmLexer &getLexer() { return Lexer; }
147 virtual MCContext &getContext() { return Ctx; }
148 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000149 virtual unsigned getAssemblerDialect() {
150 if (AssemblerDialect == ~0U)
151 return MAI.getAssemblerDialect();
152 else
153 return AssemblerDialect;
154 }
155 virtual void setAssemblerDialect(unsigned i) {
156 AssemblerDialect = i;
157 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000158
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000159 virtual bool Warning(SMLoc L, const Twine &Msg,
160 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
161 virtual bool Error(SMLoc L, const Twine &Msg,
162 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163
164 const AsmToken &Lex();
165
166 bool ParseExpression(const MCExpr *&Res);
167 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
168 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
169 virtual bool ParseAbsoluteExpression(int64_t &Res);
170
171 /// }
172
173private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000174 void CheckForValidSection();
175
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000176 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000177 void EatToEndOfLine();
178 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000180 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000181 bool expandMacro(SmallString<256> &Buf, StringRef Body,
182 const std::vector<StringRef> &Parameters,
183 const std::vector<std::vector<AsmToken> > &A,
184 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000185 void HandleMacroExit();
186
187 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000188 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000189 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
190 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000191 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000192 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000193
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000194 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
195 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000196 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
197 /// This returns true on failure.
198 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000199
200 /// \brief Reset the current lexer position to that given by \arg Loc. The
201 /// current token is not set; clients should ensure Lex() is called
202 /// subsequently.
203 void JumpToLoc(SMLoc Loc);
204
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000205 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000206
207 /// \brief Parse up to the end of statement and a return the contents from the
208 /// current token until the end of the statement; the current token on exit
209 /// will be either the EndOfStatement or EOF.
210 StringRef ParseStringToEndOfStatement();
211
Nico Weber4c4c7322011-01-28 03:04:41 +0000212 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213
214 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
215 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
216 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000217 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000218
219 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
220 /// and set \arg Res to the identifier contents.
221 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000222
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000223 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000224
225 // ".ascii", ".asciiz", ".string"
226 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000227 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000228 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000229 bool ParseDirectiveFill(); // ".fill"
230 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000231 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000232 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000233 bool ParseDirectiveOrg(); // ".org"
234 // ".align{,32}", ".p2align{,w,l}"
235 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
236
237 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
238 /// accepts a single symbol (which should be a label or an external).
239 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000240
241 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
242
243 bool ParseDirectiveAbort(); // ".abort"
244 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000245 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000246
247 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000248 // ".ifdef" or ".ifndef", depending on expect_defined
249 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000250 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
251 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
252 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
253
254 /// ParseEscapedString - Parse the current token as a string which may include
255 /// escaped characters and return the string contents.
256 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000257
258 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
259 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000260};
261
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000262/// \brief Generic implementations of directive handling, etc. which is shared
263/// (or the default, at least) for all assembler parser.
264class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000265 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
266 void AddDirectiveHandler(StringRef Directive) {
267 getParser().AddDirectiveHandler(this, Directive,
268 HandleDirective<GenericAsmParser, Handler>);
269 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000270public:
271 GenericAsmParser() {}
272
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000273 AsmParser &getParser() {
274 return (AsmParser&) this->MCAsmParserExtension::getParser();
275 }
276
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000277 virtual void Initialize(MCAsmParser &Parser) {
278 // Call the base implementation.
279 this->MCAsmParserExtension::Initialize(Parser);
280
281 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000282 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
283 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000285 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000286
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000287 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000288 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
289 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000290 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
291 ".cfi_startproc");
292 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
293 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000294 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
295 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000296 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
297 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000298 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
299 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000300 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
301 ".cfi_def_cfa_register");
302 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
303 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000304 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
305 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000306 AddDirectiveHandler<
307 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
308 AddDirectiveHandler<
309 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000310 AddDirectiveHandler<
311 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
312 AddDirectiveHandler<
313 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000314 AddDirectiveHandler<
315 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000316 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000317 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
318 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000319 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000320 AddDirectiveHandler<
321 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000322
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000323 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000324 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
325 ".macros_on");
326 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
327 ".macros_off");
328 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
329 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
330 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000331
332 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
333 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000334 }
335
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000336 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
337
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000338 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
339 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
340 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000341 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000342 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000343 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
344 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000345 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000346 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000347 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000348 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
349 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000350 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000351 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000352 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
353 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000354 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000355 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000356 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000357 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000358
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000359 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000360 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
361 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000362
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000363 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000364};
365
366}
367
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000368namespace llvm {
369
370extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000371extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000372extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000373
374}
375
Chris Lattneraaec2052010-01-19 19:46:13 +0000376enum { DEFAULT_ADDRSPACE = 0 };
377
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000378AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000379 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000380 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000381 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000382 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
383 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000384 // Save the old handler.
385 SavedDiagHandler = SrcMgr.getDiagHandler();
386 SavedDiagContext = SrcMgr.getDiagContext();
387 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000388 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000389 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000390
391 // Initialize the generic parser.
392 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000393
394 // Initialize the platform / file format parser.
395 //
396 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
397 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000398 if (_MAI.hasMicrosoftFastStdCallMangling()) {
399 PlatformParser = createCOFFAsmParser();
400 PlatformParser->Initialize(*this);
401 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000402 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000403 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000404 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000405 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000406 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000407 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000408}
409
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000410AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000411 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
412
413 // Destroy any macros.
414 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
415 ie = MacroMap.end(); it != ie; ++it)
416 delete it->getValue();
417
Daniel Dunbare4749702010-07-12 18:12:02 +0000418 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000419 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000420}
421
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000422void AsmParser::PrintMacroInstantiations() {
423 // Print the active macro instantiation stack.
424 for (std::vector<MacroInstantiation*>::const_reverse_iterator
425 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000426 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
427 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000428}
429
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000430bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000431 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000432 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000433 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000434 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000435 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000436}
437
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000438bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000439 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000440 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000441 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000442 return true;
443}
444
Sean Callananfd0b0282010-01-21 00:19:58 +0000445bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000446 std::string IncludedFile;
447 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000448 if (NewBuf == -1)
449 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000450
Sean Callananfd0b0282010-01-21 00:19:58 +0000451 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000452
Sean Callananfd0b0282010-01-21 00:19:58 +0000453 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000454
Sean Callananfd0b0282010-01-21 00:19:58 +0000455 return false;
456}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000457
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000458/// Process the specified .incbin file by seaching for it in the include paths
459/// then just emiting the byte contents of the file to the streamer. This
460/// returns true on failure.
461bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
462 std::string IncludedFile;
463 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
464 if (NewBuf == -1)
465 return true;
466
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000467 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000468 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
469 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000470 return false;
471}
472
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000473void AsmParser::JumpToLoc(SMLoc Loc) {
474 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
475 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
476}
477
Sean Callananfd0b0282010-01-21 00:19:58 +0000478const AsmToken &AsmParser::Lex() {
479 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000480
Sean Callananfd0b0282010-01-21 00:19:58 +0000481 if (tok->is(AsmToken::Eof)) {
482 // If this is the end of an included file, pop the parent file off the
483 // include stack.
484 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
485 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000486 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000487 tok = &Lexer.Lex();
488 }
489 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000490
Sean Callananfd0b0282010-01-21 00:19:58 +0000491 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000492 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000493
Sean Callananfd0b0282010-01-21 00:19:58 +0000494 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000495}
496
Chris Lattner79180e22010-04-05 23:15:42 +0000497bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000498 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000499 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000500 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000501
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000502 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000503 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000504
505 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000506 AsmCond StartingCondState = TheCondState;
507
Kevin Enderby613b7572011-11-01 22:27:22 +0000508 // If we are generating dwarf for assembly source files save the initial text
509 // section and generate a .file directive.
510 if (getContext().getGenDwarfForAssembly()) {
511 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000512 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
513 getStreamer().EmitLabel(SectionStartSym);
514 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000515 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
516 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
517 }
518
Chris Lattnerb717fb02009-07-02 21:53:43 +0000519 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000520 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000521 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000522
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000523 // We had an error, validate that one was emitted and recover by skipping to
524 // the next line.
525 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000526 EatToEndOfStatement();
527 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000528
529 if (TheCondState.TheCond != StartingCondState.TheCond ||
530 TheCondState.Ignore != StartingCondState.Ignore)
531 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000532
533 // Check to see there are no empty DwarfFile slots.
534 const std::vector<MCDwarfFile *> &MCDwarfFiles =
535 getContext().getMCDwarfFiles();
536 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000537 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000538 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000539 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000540
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000541 // Check to see that all assembler local symbols were actually defined.
542 // Targets that don't do subsections via symbols may not want this, though,
543 // so conservatively exclude them. Only do this if we're finalizing, though,
544 // as otherwise we won't necessarilly have seen everything yet.
545 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
546 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
547 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
548 e = Symbols.end();
549 i != e; ++i) {
550 MCSymbol *Sym = i->getValue();
551 // Variable symbols may not be marked as defined, so check those
552 // explicitly. If we know it's a variable, we have a definition for
553 // the purposes of this check.
554 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
555 // FIXME: We would really like to refer back to where the symbol was
556 // first referenced for a source location. We need to add something
557 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000558 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
559 "assembler local symbol '" + Sym->getName() +
560 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000561 }
562 }
563
564
Chris Lattner79180e22010-04-05 23:15:42 +0000565 // Finalize the output stream if there are no errors and if the client wants
566 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000567 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 Out.Finish();
569
Chris Lattnerb717fb02009-07-02 21:53:43 +0000570 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000571}
572
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000573void AsmParser::CheckForValidSection() {
574 if (!getStreamer().getCurrentSection()) {
575 TokError("expected section directive before assembly directive");
576 Out.SwitchSection(Ctx.getMachOSection(
577 "__TEXT", "__text",
578 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
579 0, SectionKind::getText()));
580 }
581}
582
Chris Lattner2cf5f142009-06-22 01:29:09 +0000583/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
584void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000585 while (Lexer.isNot(AsmToken::EndOfStatement) &&
586 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000587 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000588
Chris Lattner2cf5f142009-06-22 01:29:09 +0000589 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000590 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000591 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000592}
593
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000594StringRef AsmParser::ParseStringToEndOfStatement() {
595 const char *Start = getTok().getLoc().getPointer();
596
597 while (Lexer.isNot(AsmToken::EndOfStatement) &&
598 Lexer.isNot(AsmToken::Eof))
599 Lex();
600
601 const char *End = getTok().getLoc().getPointer();
602 return StringRef(Start, End - Start);
603}
Chris Lattnerc4193832009-06-22 05:51:26 +0000604
Chris Lattner74ec1a32009-06-22 06:32:03 +0000605/// ParseParenExpr - Parse a paren expression and return it.
606/// NOTE: This assumes the leading '(' has already been consumed.
607///
608/// parenexpr ::= expr)
609///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000610bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000611 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000612 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000613 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000614 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000615 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000616 return false;
617}
Chris Lattnerc4193832009-06-22 05:51:26 +0000618
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000619/// ParseBracketExpr - Parse a bracket expression and return it.
620/// NOTE: This assumes the leading '[' has already been consumed.
621///
622/// bracketexpr ::= expr]
623///
624bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
625 if (ParseExpression(Res)) return true;
626 if (Lexer.isNot(AsmToken::RBrac))
627 return TokError("expected ']' in brackets expression");
628 EndLoc = Lexer.getLoc();
629 Lex();
630 return false;
631}
632
Chris Lattner74ec1a32009-06-22 06:32:03 +0000633/// ParsePrimaryExpr - Parse a primary expression and return it.
634/// primaryexpr ::= (parenexpr
635/// primaryexpr ::= symbol
636/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000637/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000638/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000639bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000640 switch (Lexer.getKind()) {
641 default:
642 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000643 // If we have an error assume that we've already handled it.
644 case AsmToken::Error:
645 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000646 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000647 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000648 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000649 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000650 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000651 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000652 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000653 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000654 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000655 EndLoc = Lexer.getLoc();
656
657 StringRef Identifier;
658 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000659 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000660
Daniel Dunbarfffff912009-10-16 01:34:54 +0000661 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000662 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000663 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000664
665 // Lookup the symbol variant if used.
666 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000667 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000668 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000669 if (Variant == MCSymbolRefExpr::VK_Invalid) {
670 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000671 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000672 }
673 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000674
Daniel Dunbarfffff912009-10-16 01:34:54 +0000675 // If this is an absolute variable reference, substitute it now to preserve
676 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000677 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000678 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000679 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000680
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000681 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000682 return false;
683 }
684
685 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000686 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000687 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000688 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000689 case AsmToken::Integer: {
690 SMLoc Loc = getTok().getLoc();
691 int64_t IntVal = getTok().getIntVal();
692 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000693 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000694 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000695 // Look for 'b' or 'f' following an Integer as a directional label
696 if (Lexer.getKind() == AsmToken::Identifier) {
697 StringRef IDVal = getTok().getString();
698 if (IDVal == "f" || IDVal == "b"){
699 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
700 IDVal == "f" ? 1 : 0);
701 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
702 getContext());
703 if(IDVal == "b" && Sym->isUndefined())
704 return Error(Loc, "invalid reference to undefined symbol");
705 EndLoc = Lexer.getLoc();
706 Lex(); // Eat identifier.
707 }
708 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000709 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000710 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000711 case AsmToken::Real: {
712 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000713 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000714 Res = MCConstantExpr::Create(IntVal, getContext());
715 Lex(); // Eat token.
716 return false;
717 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000718 case AsmToken::Dot: {
719 // This is a '.' reference, which references the current PC. Emit a
720 // temporary label to the streamer and refer to it.
721 MCSymbol *Sym = Ctx.CreateTempSymbol();
722 Out.EmitLabel(Sym);
723 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
724 EndLoc = Lexer.getLoc();
725 Lex(); // Eat identifier.
726 return false;
727 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000728 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000729 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000730 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000731 case AsmToken::LBrac:
732 if (!PlatformParser->HasBracketExpressions())
733 return TokError("brackets expression not supported on this target");
734 Lex(); // Eat the '['.
735 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000736 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000737 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000738 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000739 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000740 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000741 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000742 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000743 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000744 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000745 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000746 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000747 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000748 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000749 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000750 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000751 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000752 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000753 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000754 }
755}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000756
Chris Lattnerb4307b32010-01-15 19:28:38 +0000757bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000758 SMLoc EndLoc;
759 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000760}
761
Daniel Dunbarcceba832010-09-17 02:47:07 +0000762const MCExpr *
763AsmParser::ApplyModifierToExpr(const MCExpr *E,
764 MCSymbolRefExpr::VariantKind Variant) {
765 // Recurse over the given expression, rebuilding it to apply the given variant
766 // if there is exactly one symbol.
767 switch (E->getKind()) {
768 case MCExpr::Target:
769 case MCExpr::Constant:
770 return 0;
771
772 case MCExpr::SymbolRef: {
773 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
774
775 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
776 TokError("invalid variant on expression '" +
777 getTok().getIdentifier() + "' (already modified)");
778 return E;
779 }
780
781 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
782 }
783
784 case MCExpr::Unary: {
785 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
786 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
787 if (!Sub)
788 return 0;
789 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
790 }
791
792 case MCExpr::Binary: {
793 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
794 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
795 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
796
797 if (!LHS && !RHS)
798 return 0;
799
800 if (!LHS) LHS = BE->getLHS();
801 if (!RHS) RHS = BE->getRHS();
802
803 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
804 }
805 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000806
Craig Topper85814382012-02-07 05:05:23 +0000807 llvm_unreachable("Invalid expression kind!");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000808}
809
Chris Lattner74ec1a32009-06-22 06:32:03 +0000810/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000811///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000812/// expr ::= expr &&,|| expr -> lowest.
813/// expr ::= expr |,^,&,! expr
814/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
815/// expr ::= expr <<,>> expr
816/// expr ::= expr +,- expr
817/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000818/// expr ::= primaryexpr
819///
Chris Lattner54482b42010-01-15 19:39:23 +0000820bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000821 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000822 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000823 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
824 return true;
825
Daniel Dunbarcceba832010-09-17 02:47:07 +0000826 // As a special case, we support 'a op b @ modifier' by rewriting the
827 // expression to include the modifier. This is inefficient, but in general we
828 // expect users to use 'a@modifier op b'.
829 if (Lexer.getKind() == AsmToken::At) {
830 Lex();
831
832 if (Lexer.isNot(AsmToken::Identifier))
833 return TokError("unexpected symbol modifier following '@'");
834
835 MCSymbolRefExpr::VariantKind Variant =
836 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
837 if (Variant == MCSymbolRefExpr::VK_Invalid)
838 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
839
840 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
841 if (!ModifiedRes) {
842 return TokError("invalid modifier '" + getTok().getIdentifier() +
843 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000844 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000845
Daniel Dunbarcceba832010-09-17 02:47:07 +0000846 Res = ModifiedRes;
847 Lex();
848 }
849
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000850 // Try to constant fold it up front, if possible.
851 int64_t Value;
852 if (Res->EvaluateAsAbsolute(Value))
853 Res = MCConstantExpr::Create(Value, getContext());
854
855 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000856}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000857
Chris Lattnerb4307b32010-01-15 19:28:38 +0000858bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000859 Res = 0;
860 return ParseParenExpr(Res, EndLoc) ||
861 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000862}
863
Daniel Dunbar475839e2009-06-29 20:37:27 +0000864bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000865 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000866
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000867 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000868 if (ParseExpression(Expr))
869 return true;
870
Daniel Dunbare00b0112009-10-16 01:57:52 +0000871 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000872 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000873
874 return false;
875}
876
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000877static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000878 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000879 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000880 default:
881 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000882
Jim Grosbachfbe16812011-08-20 16:24:13 +0000883 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000884 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000885 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000886 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000887 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000888 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000889 return 1;
890
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000891
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000892 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000893 //
894 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000895 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000896 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000897 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000898 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000899 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000900 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000901 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000902 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000903 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000904
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000905 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000906 case AsmToken::EqualEqual:
907 Kind = MCBinaryExpr::EQ;
908 return 3;
909 case AsmToken::ExclaimEqual:
910 case AsmToken::LessGreater:
911 Kind = MCBinaryExpr::NE;
912 return 3;
913 case AsmToken::Less:
914 Kind = MCBinaryExpr::LT;
915 return 3;
916 case AsmToken::LessEqual:
917 Kind = MCBinaryExpr::LTE;
918 return 3;
919 case AsmToken::Greater:
920 Kind = MCBinaryExpr::GT;
921 return 3;
922 case AsmToken::GreaterEqual:
923 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000924 return 3;
925
Jim Grosbachfbe16812011-08-20 16:24:13 +0000926 // Intermediate Precedence: <<, >>
927 case AsmToken::LessLess:
928 Kind = MCBinaryExpr::Shl;
929 return 4;
930 case AsmToken::GreaterGreater:
931 Kind = MCBinaryExpr::Shr;
932 return 4;
933
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000934 // High Intermediate Precedence: +, -
935 case AsmToken::Plus:
936 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000937 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000938 case AsmToken::Minus:
939 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000940 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000941
Jim Grosbachfbe16812011-08-20 16:24:13 +0000942 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000943 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000944 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000945 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000946 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000947 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000948 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000949 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000950 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000951 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000952 }
953}
954
955
956/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
957/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000958bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
959 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000960 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000961 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000962 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000963
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000964 // If the next token is lower precedence than we are allowed to eat, return
965 // successfully with what we ate already.
966 if (TokPrec < Precedence)
967 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000968
Sean Callanan79ed1a82010-01-19 20:22:31 +0000969 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000970
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000971 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000972 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000973 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000974
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000975 // If BinOp binds less tightly with RHS than the operator after RHS, let
976 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000977 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000978 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000979 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000980 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000981 }
982
Daniel Dunbar475839e2009-06-29 20:37:27 +0000983 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000984 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000985 }
986}
987
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000988
989
990
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000991/// ParseStatement:
992/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000993/// ::= Label* Directive ...Operands... EndOfStatement
994/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000995bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000996 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000997 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000998 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000999 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001000 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001001
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001002 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001003 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001004 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001005 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001006 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001007 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001008 if (Lexer.is(AsmToken::Hash))
1009 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001010
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001011 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001012 if (Lexer.is(AsmToken::Integer)) {
1013 LocalLabelVal = getTok().getIntVal();
1014 if (LocalLabelVal < 0) {
1015 if (!TheCondState.Ignore)
1016 return TokError("unexpected token at start of statement");
1017 IDVal = "";
1018 }
1019 else {
1020 IDVal = getTok().getString();
1021 Lex(); // Consume the integer token to be used as an identifier token.
1022 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001023 if (!TheCondState.Ignore)
1024 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001025 }
1026 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001027
1028 } else if (Lexer.is(AsmToken::Dot)) {
1029 // Treat '.' as a valid identifier in this context.
1030 Lex();
1031 IDVal = ".";
1032
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001033 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001034 if (!TheCondState.Ignore)
1035 return TokError("unexpected token at start of statement");
1036 IDVal = "";
1037 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001038
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001039
Chris Lattner7834fac2010-04-17 18:14:27 +00001040 // Handle conditional assembly here before checking for skipping. We
1041 // have to do this so that .endif isn't skipped in a ".if 0" block for
1042 // example.
1043 if (IDVal == ".if")
1044 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001045 if (IDVal == ".ifdef")
1046 return ParseDirectiveIfdef(IDLoc, true);
1047 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1048 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001049 if (IDVal == ".elseif")
1050 return ParseDirectiveElseIf(IDLoc);
1051 if (IDVal == ".else")
1052 return ParseDirectiveElse(IDLoc);
1053 if (IDVal == ".endif")
1054 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001055
Chris Lattner7834fac2010-04-17 18:14:27 +00001056 // If we are in a ".if 0" block, ignore this statement.
1057 if (TheCondState.Ignore) {
1058 EatToEndOfStatement();
1059 return false;
1060 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001061
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001062 // FIXME: Recurse on local labels?
1063
1064 // See what kind of statement we have.
1065 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001066 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001067 CheckForValidSection();
1068
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001069 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001070 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001071
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001072 // Diagnose attempt to use '.' as a label.
1073 if (IDVal == ".")
1074 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1075
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001076 // Diagnose attempt to use a variable as a label.
1077 //
1078 // FIXME: Diagnostics. Note the location of the definition as a label.
1079 // FIXME: This doesn't diagnose assignment to a symbol which has been
1080 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001081 MCSymbol *Sym;
1082 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001083 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001084 else
1085 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001086 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001087 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001088
Daniel Dunbar959fd882009-08-26 22:13:22 +00001089 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001090 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001091
Kevin Enderby94c2e852011-12-09 18:09:40 +00001092 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001093 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001094 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001095 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1096 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001097
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001098 // Consume any end of statement token, if present, to avoid spurious
1099 // AddBlankLine calls().
1100 if (Lexer.is(AsmToken::EndOfStatement)) {
1101 Lex();
1102 if (Lexer.is(AsmToken::Eof))
1103 return false;
1104 }
1105
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001106 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001107 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001108
Daniel Dunbar3f872332009-07-28 16:08:33 +00001109 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001110 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001111 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001112
Nico Weber4c4c7322011-01-28 03:04:41 +00001113 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001114
1115 default: // Normal instruction or directive.
1116 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001117 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001118
1119 // If macros are enabled, check to see if this is a macro instantiation.
1120 if (MacrosEnabled)
1121 if (const Macro *M = MacroMap.lookup(IDVal))
1122 return HandleMacroEntry(IDVal, IDLoc, M);
1123
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001124 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001125 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001126 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001127 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001128 return ParseDirectiveSet(IDVal, true);
1129 if (IDVal == ".equiv")
1130 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001131
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132 // Data directives
1133
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001134 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001135 return ParseDirectiveAscii(IDVal, false);
1136 if (IDVal == ".asciz" || IDVal == ".string")
1137 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001138
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001139 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001140 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001141 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001142 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001143 if (IDVal == ".value")
1144 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001145 if (IDVal == ".2byte")
1146 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001147 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001149 if (IDVal == ".int")
1150 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001151 if (IDVal == ".4byte")
1152 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001153 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001155 if (IDVal == ".8byte")
1156 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001157 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001158 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1159 if (IDVal == ".double")
1160 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001161
Eli Friedman5d68ec22010-07-19 04:17:25 +00001162 if (IDVal == ".align") {
1163 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1164 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1165 }
1166 if (IDVal == ".align32") {
1167 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1168 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1169 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001170 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001171 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001172 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001173 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001174 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001175 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001176 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001178 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001180 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1182
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001183 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001184 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001185
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001186 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001187 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001188 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001189 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001190 if (IDVal == ".zero")
1191 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001192
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001193 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001194
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001195 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001196 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001197 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001198 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001199 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001200 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001201 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001202 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001203 if (IDVal == ".symbol_resolver")
1204 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001205 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001206 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001207 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001208 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001209 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001210 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001211 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001212 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001213 if (IDVal == ".weak_def_can_be_hidden")
1214 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001215
Hans Wennborg5cc64912011-06-18 13:51:54 +00001216 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001217 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001218 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001219 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001220
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001221 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001222 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001223 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001224 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001225 if (IDVal == ".incbin")
1226 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001227
Evan Chengbd27f5a2011-07-27 00:38:12 +00001228 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001229 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001230
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001231 // Look up the handler in the handler table.
1232 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1233 DirectiveMap.lookup(IDVal);
1234 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001235 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001236
Kevin Enderby9c656452009-09-10 20:51:44 +00001237 // Target hook for parsing target specific directives.
1238 if (!getTargetParser().ParseDirective(ID))
1239 return false;
1240
Jim Grosbach686c0182012-05-01 18:38:27 +00001241 return Error(IDLoc, "unknown directive");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001242 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001243
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001244 CheckForValidSection();
1245
Chris Lattnera7f13542010-05-19 23:34:33 +00001246 // Canonicalize the opcode to lower case.
1247 SmallString<128> Opcode;
1248 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1249 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001250
Chris Lattner98986712010-01-14 22:21:20 +00001251 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001252 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001253 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001254
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001255 // Dump the parsed representation, if requested.
1256 if (getShowParsedOperands()) {
1257 SmallString<256> Str;
1258 raw_svector_ostream OS(Str);
1259 OS << "parsed instruction: [";
1260 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1261 if (i != 0)
1262 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001263 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001264 }
1265 OS << "]";
1266
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001267 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001268 }
1269
Kevin Enderby613b7572011-11-01 22:27:22 +00001270 // If we are generating dwarf for assembly source files and the current
1271 // section is the initial text section then generate a .loc directive for
1272 // the instruction.
1273 if (!HadError && getContext().getGenDwarfForAssembly() &&
1274 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1275 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1276 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1277 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001278 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001279 StringRef());
1280 }
1281
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001282 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001283 if (!HadError)
1284 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1285 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001286
Chris Lattner98986712010-01-14 22:21:20 +00001287 // Free any parsed operands.
1288 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1289 delete ParsedOperands[i];
1290
Chris Lattnercbf8a982010-09-11 16:18:25 +00001291 // Don't skip the rest of the line, the instruction parser is responsible for
1292 // that.
1293 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001294}
Chris Lattner9a023f72009-06-24 04:43:34 +00001295
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001296/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1297/// since they may not be able to be tokenized to get to the end of line token.
1298void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001299 if (!Lexer.is(AsmToken::EndOfStatement))
1300 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001301 // Eat EOL.
1302 Lex();
1303}
1304
1305/// ParseCppHashLineFilenameComment as this:
1306/// ::= # number "filename"
1307/// or just as a full line comment if it doesn't have a number and a string.
1308bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1309 Lex(); // Eat the hash token.
1310
1311 if (getLexer().isNot(AsmToken::Integer)) {
1312 // Consume the line since in cases it is not a well-formed line directive,
1313 // as if were simply a full line comment.
1314 EatToEndOfLine();
1315 return false;
1316 }
1317
1318 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001319 Lex();
1320
1321 if (getLexer().isNot(AsmToken::String)) {
1322 EatToEndOfLine();
1323 return false;
1324 }
1325
1326 StringRef Filename = getTok().getString();
1327 // Get rid of the enclosing quotes.
1328 Filename = Filename.substr(1, Filename.size()-2);
1329
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001330 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1331 CppHashLoc = L;
1332 CppHashFilename = Filename;
1333 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001334
1335 // Ignore any trailing characters, they're just comment.
1336 EatToEndOfLine();
1337 return false;
1338}
1339
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001340/// DiagHandler - will use the the last parsed cpp hash line filename comment
1341/// for the Filename and LineNo if any in the diagnostic.
1342void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1343 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1344 raw_ostream &OS = errs();
1345
1346 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1347 const SMLoc &DiagLoc = Diag.getLoc();
1348 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1349 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1350
1351 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1352 // before printing the message.
1353 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001354 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001355 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1356 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1357 }
1358
1359 // If we have not parsed a cpp hash line filename comment or the source
1360 // manager changed or buffer changed (like in a nested include) then just
1361 // print the normal diagnostic using its Filename and LineNo.
1362 if (!Parser->CppHashLineNumber ||
1363 &DiagSrcMgr != &Parser->SrcMgr ||
1364 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001365 if (Parser->SavedDiagHandler)
1366 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1367 else
1368 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001369 return;
1370 }
1371
1372 // Use the CppHashFilename and calculate a line number based on the
1373 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1374 // the diagnostic.
1375 const std::string Filename = Parser->CppHashFilename;
1376
1377 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1378 int CppHashLocLineNo =
1379 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1380 int LineNo = Parser->CppHashLineNumber - 1 +
1381 (DiagLocLineNo - CppHashLocLineNo);
1382
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001383 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1384 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001385 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001386 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001387
Benjamin Kramer04a04262011-10-16 10:48:29 +00001388 if (Parser->SavedDiagHandler)
1389 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1390 else
1391 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001392}
1393
Rafael Espindola65366442011-06-05 02:43:45 +00001394bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1395 const std::vector<StringRef> &Parameters,
1396 const std::vector<std::vector<AsmToken> > &A,
1397 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001398 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001399 unsigned NParameters = Parameters.size();
1400 if (NParameters != 0 && NParameters != A.size())
1401 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001402
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001403 while (!Body.empty()) {
1404 // Scan for the next substitution.
1405 std::size_t End = Body.size(), Pos = 0;
1406 for (; Pos != End; ++Pos) {
1407 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001408 if (!NParameters) {
1409 // This macro has no parameters, look for $0, $1, etc.
1410 if (Body[Pos] != '$' || Pos + 1 == End)
1411 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001412
Rafael Espindola65366442011-06-05 02:43:45 +00001413 char Next = Body[Pos + 1];
1414 if (Next == '$' || Next == 'n' || isdigit(Next))
1415 break;
1416 } else {
1417 // This macro has parameters, look for \foo, \bar, etc.
1418 if (Body[Pos] == '\\' && Pos + 1 != End)
1419 break;
1420 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001421 }
1422
1423 // Add the prefix.
1424 OS << Body.slice(0, Pos);
1425
1426 // Check if we reached the end.
1427 if (Pos == End)
1428 break;
1429
Rafael Espindola65366442011-06-05 02:43:45 +00001430 if (!NParameters) {
1431 switch (Body[Pos+1]) {
1432 // $$ => $
1433 case '$':
1434 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001435 break;
1436
Rafael Espindola65366442011-06-05 02:43:45 +00001437 // $n => number of arguments
1438 case 'n':
1439 OS << A.size();
1440 break;
1441
1442 // $[0-9] => argument
1443 default: {
1444 // Missing arguments are ignored.
1445 unsigned Index = Body[Pos+1] - '0';
1446 if (Index >= A.size())
1447 break;
1448
1449 // Otherwise substitute with the token values, with spaces eliminated.
1450 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1451 ie = A[Index].end(); it != ie; ++it)
1452 OS << it->getString();
1453 break;
1454 }
1455 }
1456 Pos += 2;
1457 } else {
1458 unsigned I = Pos + 1;
1459 while (isalnum(Body[I]) && I + 1 != End)
1460 ++I;
1461
1462 const char *Begin = Body.data() + Pos +1;
1463 StringRef Argument(Begin, I - (Pos +1));
1464 unsigned Index = 0;
1465 for (; Index < NParameters; ++Index)
1466 if (Parameters[Index] == Argument)
1467 break;
1468
1469 // FIXME: We should error at the macro definition.
1470 if (Index == NParameters)
1471 return Error(L, "Parameter not found");
1472
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001473 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1474 ie = A[Index].end(); it != ie; ++it)
1475 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001476
Rafael Espindola65366442011-06-05 02:43:45 +00001477 Pos += 1 + Argument.size();
1478 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001479 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001480 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001481 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001482
1483 // We include the .endmacro in the buffer as our queue to exit the macro
1484 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001485 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001486 return false;
1487}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001488
Rafael Espindola65366442011-06-05 02:43:45 +00001489MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1490 MemoryBuffer *I)
1491 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1492{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001493}
1494
1495bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1496 const Macro *M) {
1497 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1498 // this, although we should protect against infinite loops.
1499 if (ActiveMacros.size() == 20)
1500 return TokError("macros cannot be nested more than 20 levels deep");
1501
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001502 // Parse the macro instantiation arguments.
1503 std::vector<std::vector<AsmToken> > MacroArguments;
1504 MacroArguments.push_back(std::vector<AsmToken>());
1505 unsigned ParenLevel = 0;
1506 for (;;) {
1507 if (Lexer.is(AsmToken::Eof))
1508 return TokError("unexpected token in macro instantiation");
1509 if (Lexer.is(AsmToken::EndOfStatement))
1510 break;
1511
1512 // If we aren't inside parentheses and this is a comma, start a new token
1513 // list.
1514 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1515 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001516 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001517 // Adjust the current parentheses level.
1518 if (Lexer.is(AsmToken::LParen))
1519 ++ParenLevel;
1520 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1521 --ParenLevel;
1522
1523 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001524 MacroArguments.back().push_back(getTok());
1525 }
1526 Lex();
1527 }
Jim Grosbach68f89a62012-04-16 21:18:49 +00001528 // If the last argument didn't end up with any tokens, it's not a real
1529 // argument and we should remove it from the list. This happens with either
1530 // a tailing comma or an empty argument list.
1531 if (MacroArguments.back().empty())
1532 MacroArguments.pop_back();
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001533
Rafael Espindola65366442011-06-05 02:43:45 +00001534 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1535 // to hold the macro body with substitutions.
1536 SmallString<256> Buf;
1537 StringRef Body = M->Body;
1538
1539 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1540 return true;
1541
1542 MemoryBuffer *Instantiation =
1543 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1544
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001545 // Create the macro instantiation object and add to the current macro
1546 // instantiation stack.
1547 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001548 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001549 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001550 ActiveMacros.push_back(MI);
1551
1552 // Jump to the macro instantiation and prime the lexer.
1553 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1554 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1555 Lex();
1556
1557 return false;
1558}
1559
1560void AsmParser::HandleMacroExit() {
1561 // Jump to the EndOfStatement we should return to, and consume it.
1562 JumpToLoc(ActiveMacros.back()->ExitLoc);
1563 Lex();
1564
1565 // Pop the instantiation entry.
1566 delete ActiveMacros.back();
1567 ActiveMacros.pop_back();
1568}
1569
Rafael Espindolae71cc862012-01-28 05:57:00 +00001570static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001571 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001572 case MCExpr::Binary: {
1573 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1574 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001575 break;
1576 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001577 case MCExpr::Target:
1578 case MCExpr::Constant:
1579 return false;
1580 case MCExpr::SymbolRef: {
1581 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001582 if (S.isVariable())
1583 return IsUsedIn(Sym, S.getVariableValue());
1584 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001585 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001586 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001587 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001588 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001589
1590 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001591}
1592
Nico Weber4c4c7322011-01-28 03:04:41 +00001593bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001594 // FIXME: Use better location, we should use proper tokens.
1595 SMLoc EqualLoc = Lexer.getLoc();
1596
Daniel Dunbar821e3332009-08-31 08:09:28 +00001597 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001598 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001599 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001600
Rafael Espindolae71cc862012-01-28 05:57:00 +00001601 // Note: we don't count b as used in "a = b". This is to allow
1602 // a = b
1603 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001604
Daniel Dunbar3f872332009-07-28 16:08:33 +00001605 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001606 return TokError("unexpected token in assignment");
1607
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001608 // Error on assignment to '.'.
1609 if (Name == ".") {
1610 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1611 "(use '.space' or '.org').)"));
1612 }
1613
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001614 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001615 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001616
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001617 // Validate that the LHS is allowed to be a variable (either it has not been
1618 // used as a symbol, or it is an absolute symbol).
1619 MCSymbol *Sym = getContext().LookupSymbol(Name);
1620 if (Sym) {
1621 // Diagnose assignment to a label.
1622 //
1623 // FIXME: Diagnostics. Note the location of the definition as a label.
1624 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001625 if (IsUsedIn(Sym, Value))
1626 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1627 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001628 ; // Allow redefinitions of undefined symbols only used in directives.
Jim Grosbach48c95332012-03-20 21:33:21 +00001629 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef)
1630 ; // Allow redefinitions of variables that haven't yet been used.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001631 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001632 return Error(EqualLoc, "redefinition of '" + Name + "'");
1633 else if (!Sym->isVariable())
1634 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001635 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001636 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1637 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001638
1639 // Don't count these checks as uses.
1640 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001641 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001642 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001643
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001644 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001645
1646 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001647 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001648
1649 return false;
1650}
1651
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001652/// ParseIdentifier:
1653/// ::= identifier
1654/// ::= string
1655bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001656 // The assembler has relaxed rules for accepting identifiers, in particular we
1657 // allow things like '.globl $foo', which would normally be separate
1658 // tokens. At this level, we have already lexed so we cannot (currently)
1659 // handle this as a context dependent token, instead we detect adjacent tokens
1660 // and return the combined identifier.
1661 if (Lexer.is(AsmToken::Dollar)) {
1662 SMLoc DollarLoc = getLexer().getLoc();
1663
1664 // Consume the dollar sign, and check for a following identifier.
1665 Lex();
1666 if (Lexer.isNot(AsmToken::Identifier))
1667 return true;
1668
1669 // We have a '$' followed by an identifier, make sure they are adjacent.
1670 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1671 return true;
1672
1673 // Construct the joined identifier and consume the token.
1674 Res = StringRef(DollarLoc.getPointer(),
1675 getTok().getIdentifier().size() + 1);
1676 Lex();
1677 return false;
1678 }
1679
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001680 if (Lexer.isNot(AsmToken::Identifier) &&
1681 Lexer.isNot(AsmToken::String))
1682 return true;
1683
Sean Callanan18b83232010-01-19 21:44:56 +00001684 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001685
Sean Callanan79ed1a82010-01-19 20:22:31 +00001686 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001687
1688 return false;
1689}
1690
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001691/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001692/// ::= .equ identifier ',' expression
1693/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001694/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001695bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001696 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001697
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001698 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001699 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001700
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001702 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001703 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001704
Nico Weber4c4c7322011-01-28 03:04:41 +00001705 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001706}
1707
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001708bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001709 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001710
1711 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001712 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001713 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1714 if (Str[i] != '\\') {
1715 Data += Str[i];
1716 continue;
1717 }
1718
1719 // Recognize escaped characters. Note that this escape semantics currently
1720 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1721 ++i;
1722 if (i == e)
1723 return TokError("unexpected backslash at end of string");
1724
1725 // Recognize octal sequences.
1726 if ((unsigned) (Str[i] - '0') <= 7) {
1727 // Consume up to three octal characters.
1728 unsigned Value = Str[i] - '0';
1729
1730 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1731 ++i;
1732 Value = Value * 8 + (Str[i] - '0');
1733
1734 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1735 ++i;
1736 Value = Value * 8 + (Str[i] - '0');
1737 }
1738 }
1739
1740 if (Value > 255)
1741 return TokError("invalid octal escape sequence (out of range)");
1742
1743 Data += (unsigned char) Value;
1744 continue;
1745 }
1746
1747 // Otherwise recognize individual escapes.
1748 switch (Str[i]) {
1749 default:
1750 // Just reject invalid escape sequences for now.
1751 return TokError("invalid escape sequence (unrecognized character)");
1752
1753 case 'b': Data += '\b'; break;
1754 case 'f': Data += '\f'; break;
1755 case 'n': Data += '\n'; break;
1756 case 'r': Data += '\r'; break;
1757 case 't': Data += '\t'; break;
1758 case '"': Data += '"'; break;
1759 case '\\': Data += '\\'; break;
1760 }
1761 }
1762
1763 return false;
1764}
1765
Daniel Dunbara0d14262009-06-24 23:30:00 +00001766/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001767/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1768bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001769 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001770 CheckForValidSection();
1771
Daniel Dunbara0d14262009-06-24 23:30:00 +00001772 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001773 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001774 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001775
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001776 std::string Data;
1777 if (ParseEscapedString(Data))
1778 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001779
1780 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001781 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001782 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1783
Sean Callanan79ed1a82010-01-19 20:22:31 +00001784 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001785
1786 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001787 break;
1788
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001789 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001790 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001791 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001792 }
1793 }
1794
Sean Callanan79ed1a82010-01-19 20:22:31 +00001795 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001796 return false;
1797}
1798
1799/// ParseDirectiveValue
1800/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1801bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001802 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001803 CheckForValidSection();
1804
Daniel Dunbara0d14262009-06-24 23:30:00 +00001805 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001806 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001807 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001808 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001809 return true;
1810
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001811 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001812 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1813 assert(Size <= 8 && "Invalid size");
1814 uint64_t IntValue = MCE->getValue();
1815 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1816 return Error(ExprLoc, "literal value out of range for directive");
1817 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1818 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001819 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001820
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001821 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001822 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001823
Daniel Dunbara0d14262009-06-24 23:30:00 +00001824 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001825 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001826 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001827 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001828 }
1829 }
1830
Sean Callanan79ed1a82010-01-19 20:22:31 +00001831 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001832 return false;
1833}
1834
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001835/// ParseDirectiveRealValue
1836/// ::= (.single | .double) [ expression (, expression)* ]
1837bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1838 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1839 CheckForValidSection();
1840
1841 for (;;) {
1842 // We don't truly support arithmetic on floating point expressions, so we
1843 // have to manually parse unary prefixes.
1844 bool IsNeg = false;
1845 if (getLexer().is(AsmToken::Minus)) {
1846 Lex();
1847 IsNeg = true;
1848 } else if (getLexer().is(AsmToken::Plus))
1849 Lex();
1850
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001851 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001852 getLexer().isNot(AsmToken::Real) &&
1853 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001854 return TokError("unexpected token in directive");
1855
1856 // Convert to an APFloat.
1857 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001858 StringRef IDVal = getTok().getString();
1859 if (getLexer().is(AsmToken::Identifier)) {
1860 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1861 Value = APFloat::getInf(Semantics);
1862 else if (!IDVal.compare_lower("nan"))
1863 Value = APFloat::getNaN(Semantics, false, ~0);
1864 else
1865 return TokError("invalid floating point literal");
1866 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001867 APFloat::opInvalidOp)
1868 return TokError("invalid floating point literal");
1869 if (IsNeg)
1870 Value.changeSign();
1871
1872 // Consume the numeric token.
1873 Lex();
1874
1875 // Emit the value as an integer.
1876 APInt AsInt = Value.bitcastToAPInt();
1877 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1878 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1879
1880 if (getLexer().is(AsmToken::EndOfStatement))
1881 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001882
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001883 if (getLexer().isNot(AsmToken::Comma))
1884 return TokError("unexpected token in directive");
1885 Lex();
1886 }
1887 }
1888
1889 Lex();
1890 return false;
1891}
1892
Daniel Dunbara0d14262009-06-24 23:30:00 +00001893/// ParseDirectiveSpace
1894/// ::= .space expression [ , expression ]
1895bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001896 CheckForValidSection();
1897
Daniel Dunbara0d14262009-06-24 23:30:00 +00001898 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001899 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001900 return true;
1901
1902 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001903 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1904 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001905 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001906 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001907
Daniel Dunbar475839e2009-06-29 20:37:27 +00001908 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001909 return true;
1910
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001911 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001912 return TokError("unexpected token in '.space' directive");
1913 }
1914
Sean Callanan79ed1a82010-01-19 20:22:31 +00001915 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001916
1917 if (NumBytes <= 0)
1918 return TokError("invalid number of bytes in '.space' directive");
1919
1920 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001921 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001922
1923 return false;
1924}
1925
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001926/// ParseDirectiveZero
1927/// ::= .zero expression
1928bool AsmParser::ParseDirectiveZero() {
1929 CheckForValidSection();
1930
1931 int64_t NumBytes;
1932 if (ParseAbsoluteExpression(NumBytes))
1933 return true;
1934
Rafael Espindolae452b172010-10-05 19:42:57 +00001935 int64_t Val = 0;
1936 if (getLexer().is(AsmToken::Comma)) {
1937 Lex();
1938 if (ParseAbsoluteExpression(Val))
1939 return true;
1940 }
1941
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001942 if (getLexer().isNot(AsmToken::EndOfStatement))
1943 return TokError("unexpected token in '.zero' directive");
1944
1945 Lex();
1946
Rafael Espindolae452b172010-10-05 19:42:57 +00001947 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001948
1949 return false;
1950}
1951
Daniel Dunbara0d14262009-06-24 23:30:00 +00001952/// ParseDirectiveFill
1953/// ::= .fill expression , expression , expression
1954bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001955 CheckForValidSection();
1956
Daniel Dunbara0d14262009-06-24 23:30:00 +00001957 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001958 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001959 return true;
1960
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001961 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001962 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001963 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001964
Daniel Dunbara0d14262009-06-24 23:30:00 +00001965 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001966 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001967 return true;
1968
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001969 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001970 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001971 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001972
Daniel Dunbara0d14262009-06-24 23:30:00 +00001973 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001974 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001975 return true;
1976
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001977 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001978 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001979
Sean Callanan79ed1a82010-01-19 20:22:31 +00001980 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001981
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001982 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1983 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001984
1985 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001986 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001987
1988 return false;
1989}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001990
1991/// ParseDirectiveOrg
1992/// ::= .org expression [ , expression ]
1993bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001994 CheckForValidSection();
1995
Daniel Dunbar821e3332009-08-31 08:09:28 +00001996 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00001997 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001998 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001999 return true;
2000
2001 // Parse optional fill expression.
2002 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002003 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2004 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002005 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002006 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002007
Daniel Dunbar475839e2009-06-29 20:37:27 +00002008 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002009 return true;
2010
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002011 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002012 return TokError("unexpected token in '.org' directive");
2013 }
2014
Sean Callanan79ed1a82010-01-19 20:22:31 +00002015 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002016
Jim Grosbachebd4c052012-01-27 00:37:08 +00002017 // Only limited forms of relocatable expressions are accepted here, it
2018 // has to be relative to the current section. The streamer will return
2019 // 'true' if the expression wasn't evaluatable.
2020 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2021 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002022
2023 return false;
2024}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002025
2026/// ParseDirectiveAlign
2027/// ::= {.align, ...} expression [ , expression [ , expression ]]
2028bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002029 CheckForValidSection();
2030
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002031 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002032 int64_t Alignment;
2033 if (ParseAbsoluteExpression(Alignment))
2034 return true;
2035
2036 SMLoc MaxBytesLoc;
2037 bool HasFillExpr = false;
2038 int64_t FillExpr = 0;
2039 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002040 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2041 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002042 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002043 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002044
2045 // The fill expression can be omitted while specifying a maximum number of
2046 // alignment bytes, e.g:
2047 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002048 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002049 HasFillExpr = true;
2050 if (ParseAbsoluteExpression(FillExpr))
2051 return true;
2052 }
2053
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002054 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2055 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002056 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002057 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002058
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002059 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002060 if (ParseAbsoluteExpression(MaxBytesToFill))
2061 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002062
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002063 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002064 return TokError("unexpected token in directive");
2065 }
2066 }
2067
Sean Callanan79ed1a82010-01-19 20:22:31 +00002068 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002069
Daniel Dunbar648ac512010-05-17 21:54:30 +00002070 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002071 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002072
2073 // Compute alignment in bytes.
2074 if (IsPow2) {
2075 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002076 if (Alignment >= 32) {
2077 Error(AlignmentLoc, "invalid alignment value");
2078 Alignment = 31;
2079 }
2080
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002081 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002082 }
2083
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002084 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002085 if (MaxBytesLoc.isValid()) {
2086 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002087 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2088 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002089 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002090 }
2091
2092 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002093 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2094 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002095 MaxBytesToFill = 0;
2096 }
2097 }
2098
Daniel Dunbar648ac512010-05-17 21:54:30 +00002099 // Check whether we should use optimal code alignment for this .align
2100 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002101 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002102 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2103 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002104 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002105 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002106 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002107 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2108 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002109 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002110
2111 return false;
2112}
2113
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002114/// ParseDirectiveSymbolAttribute
2115/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002116bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002117 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002118 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002119 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002120 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002121
2122 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002123 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002124
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002125 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002126
Jim Grosbach10ec6502011-09-15 17:56:49 +00002127 // Assembler local symbols don't make any sense here. Complain loudly.
2128 if (Sym->isTemporary())
2129 return Error(Loc, "non-local symbol required in directive");
2130
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002131 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002132
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002133 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002134 break;
2135
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002136 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002137 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002138 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002139 }
2140 }
2141
Sean Callanan79ed1a82010-01-19 20:22:31 +00002142 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002143 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002144}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002145
2146/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002147/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2148bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002149 CheckForValidSection();
2150
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002151 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002152 StringRef Name;
2153 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002154 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002155
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002156 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002157 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002158
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002159 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002160 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002161 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002162
2163 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002164 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002165 if (ParseAbsoluteExpression(Size))
2166 return true;
2167
2168 int64_t Pow2Alignment = 0;
2169 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002170 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002171 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002172 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002173 if (ParseAbsoluteExpression(Pow2Alignment))
2174 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002175
Chris Lattner258281d2010-01-19 06:22:22 +00002176 // If this target takes alignments in bytes (not log) validate and convert.
2177 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2178 if (!isPowerOf2_64(Pow2Alignment))
2179 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2180 Pow2Alignment = Log2_64(Pow2Alignment);
2181 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002182 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002183
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002184 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002185 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002186
Sean Callanan79ed1a82010-01-19 20:22:31 +00002187 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002188
Chris Lattner1fc3d752009-07-09 17:25:12 +00002189 // NOTE: a size of zero for a .comm should create a undefined symbol
2190 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002191 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002192 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2193 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002194
Eric Christopherc260a3e2010-05-14 01:38:54 +00002195 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002196 // may internally end up wanting an alignment in bytes.
2197 // FIXME: Diagnose overflow.
2198 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002199 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2200 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002201
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002202 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002203 return Error(IDLoc, "invalid symbol redefinition");
2204
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002205 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002206 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002207 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002208 getStreamer().EmitZerofill(Ctx.getMachOSection(
2209 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2210 0, SectionKind::getBSS()),
2211 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002212 return false;
2213 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002214
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002215 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002216 return false;
2217}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002218
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002219/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002220/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002221bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002222 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002223 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002224
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002225 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002226 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002227 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002228
Sean Callanan79ed1a82010-01-19 20:22:31 +00002229 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002230
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002231 if (Str.empty())
2232 Error(Loc, ".abort detected. Assembly stopping.");
2233 else
2234 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002235 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002236
2237 return false;
2238}
Kevin Enderby71148242009-07-14 21:35:03 +00002239
Kevin Enderby1f049b22009-07-14 23:21:55 +00002240/// ParseDirectiveInclude
2241/// ::= .include "filename"
2242bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002243 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002244 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002245
Sean Callanan18b83232010-01-19 21:44:56 +00002246 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002247 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002248 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002249
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002250 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002251 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002252
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002253 // Strip the quotes.
2254 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002255
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002256 // Attempt to switch the lexer to the included file before consuming the end
2257 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002258 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002259 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002260 return true;
2261 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002262
2263 return false;
2264}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002265
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002266/// ParseDirectiveIncbin
2267/// ::= .incbin "filename"
2268bool AsmParser::ParseDirectiveIncbin() {
2269 if (getLexer().isNot(AsmToken::String))
2270 return TokError("expected string in '.incbin' directive");
2271
2272 std::string Filename = getTok().getString();
2273 SMLoc IncbinLoc = getLexer().getLoc();
2274 Lex();
2275
2276 if (getLexer().isNot(AsmToken::EndOfStatement))
2277 return TokError("unexpected token in '.incbin' directive");
2278
2279 // Strip the quotes.
2280 Filename = Filename.substr(1, Filename.size()-2);
2281
2282 // Attempt to process the included file.
2283 if (ProcessIncbinFile(Filename)) {
2284 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2285 return true;
2286 }
2287
2288 return false;
2289}
2290
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002291/// ParseDirectiveIf
2292/// ::= .if expression
2293bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002294 TheCondStack.push_back(TheCondState);
2295 TheCondState.TheCond = AsmCond::IfCond;
2296 if(TheCondState.Ignore) {
2297 EatToEndOfStatement();
2298 }
2299 else {
2300 int64_t ExprValue;
2301 if (ParseAbsoluteExpression(ExprValue))
2302 return true;
2303
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002304 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002305 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002306
Sean Callanan79ed1a82010-01-19 20:22:31 +00002307 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002308
2309 TheCondState.CondMet = ExprValue;
2310 TheCondState.Ignore = !TheCondState.CondMet;
2311 }
2312
2313 return false;
2314}
2315
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002316bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2317 StringRef Name;
2318 TheCondStack.push_back(TheCondState);
2319 TheCondState.TheCond = AsmCond::IfCond;
2320
2321 if (TheCondState.Ignore) {
2322 EatToEndOfStatement();
2323 } else {
2324 if (ParseIdentifier(Name))
2325 return TokError("expected identifier after '.ifdef'");
2326
2327 Lex();
2328
2329 MCSymbol *Sym = getContext().LookupSymbol(Name);
2330
2331 if (expect_defined)
2332 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2333 else
2334 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2335 TheCondState.Ignore = !TheCondState.CondMet;
2336 }
2337
2338 return false;
2339}
2340
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002341/// ParseDirectiveElseIf
2342/// ::= .elseif expression
2343bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2344 if (TheCondState.TheCond != AsmCond::IfCond &&
2345 TheCondState.TheCond != AsmCond::ElseIfCond)
2346 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2347 " an .elseif");
2348 TheCondState.TheCond = AsmCond::ElseIfCond;
2349
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002350 bool LastIgnoreState = false;
2351 if (!TheCondStack.empty())
2352 LastIgnoreState = TheCondStack.back().Ignore;
2353 if (LastIgnoreState || TheCondState.CondMet) {
2354 TheCondState.Ignore = true;
2355 EatToEndOfStatement();
2356 }
2357 else {
2358 int64_t ExprValue;
2359 if (ParseAbsoluteExpression(ExprValue))
2360 return true;
2361
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002362 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002363 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002364
Sean Callanan79ed1a82010-01-19 20:22:31 +00002365 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002366 TheCondState.CondMet = ExprValue;
2367 TheCondState.Ignore = !TheCondState.CondMet;
2368 }
2369
2370 return false;
2371}
2372
2373/// ParseDirectiveElse
2374/// ::= .else
2375bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002376 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002377 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002378
Sean Callanan79ed1a82010-01-19 20:22:31 +00002379 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002380
2381 if (TheCondState.TheCond != AsmCond::IfCond &&
2382 TheCondState.TheCond != AsmCond::ElseIfCond)
2383 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2384 ".elseif");
2385 TheCondState.TheCond = AsmCond::ElseCond;
2386 bool LastIgnoreState = false;
2387 if (!TheCondStack.empty())
2388 LastIgnoreState = TheCondStack.back().Ignore;
2389 if (LastIgnoreState || TheCondState.CondMet)
2390 TheCondState.Ignore = true;
2391 else
2392 TheCondState.Ignore = false;
2393
2394 return false;
2395}
2396
2397/// ParseDirectiveEndIf
2398/// ::= .endif
2399bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002400 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002401 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002402
Sean Callanan79ed1a82010-01-19 20:22:31 +00002403 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002404
2405 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2406 TheCondStack.empty())
2407 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2408 ".else");
2409 if (!TheCondStack.empty()) {
2410 TheCondState = TheCondStack.back();
2411 TheCondStack.pop_back();
2412 }
2413
2414 return false;
2415}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002416
2417/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002418/// ::= .file [number] filename
2419/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002420bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002421 // FIXME: I'm not sure what this is.
2422 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002423 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002424 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002425 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002426 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002427
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002428 if (FileNumber < 1)
2429 return TokError("file number less than one");
2430 }
2431
Daniel Dunbareceec052010-07-12 17:45:27 +00002432 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002433 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002434
Nick Lewycky44d798d2011-10-17 23:05:28 +00002435 // Usually the directory and filename together, otherwise just the directory.
2436 StringRef Path = getTok().getString();
2437 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002438 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002439
Nick Lewycky44d798d2011-10-17 23:05:28 +00002440 StringRef Directory;
2441 StringRef Filename;
2442 if (getLexer().is(AsmToken::String)) {
2443 if (FileNumber == -1)
2444 return TokError("explicit path specified, but no file number");
2445 Filename = getTok().getString();
2446 Filename = Filename.substr(1, Filename.size()-2);
2447 Directory = Path;
2448 Lex();
2449 } else {
2450 Filename = Path;
2451 }
2452
Daniel Dunbareceec052010-07-12 17:45:27 +00002453 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002454 return TokError("unexpected token in '.file' directive");
2455
Chris Lattnerd32e8032010-01-25 19:02:58 +00002456 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002457 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002458 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002459 if (getContext().getGenDwarfForAssembly() == true)
2460 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2461 "used to generate dwarf debug info for assembly code");
2462
Nick Lewycky44d798d2011-10-17 23:05:28 +00002463 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002464 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002465 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002466
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002467 return false;
2468}
2469
2470/// ParseDirectiveLine
2471/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002472bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002473 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2474 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002475 return TokError("unexpected token in '.line' directive");
2476
Sean Callanan18b83232010-01-19 21:44:56 +00002477 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002478 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002479 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002480
2481 // FIXME: Do something with the .line.
2482 }
2483
Daniel Dunbareceec052010-07-12 17:45:27 +00002484 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002485 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002486
2487 return false;
2488}
2489
2490
2491/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002492/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002493/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2494/// The first number is a file number, must have been previously assigned with
2495/// a .file directive, the second number is the line number and optionally the
2496/// third number is a column position (zero if not specified). The remaining
2497/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002498bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002499
Daniel Dunbareceec052010-07-12 17:45:27 +00002500 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002501 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002502 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002503 if (FileNumber < 1)
2504 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002505 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002506 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002507 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002508
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002509 int64_t LineNumber = 0;
2510 if (getLexer().is(AsmToken::Integer)) {
2511 LineNumber = getTok().getIntVal();
2512 if (LineNumber < 1)
2513 return TokError("line number less than one in '.loc' directive");
2514 Lex();
2515 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002516
2517 int64_t ColumnPos = 0;
2518 if (getLexer().is(AsmToken::Integer)) {
2519 ColumnPos = getTok().getIntVal();
2520 if (ColumnPos < 0)
2521 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002522 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002523 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002524
Kevin Enderbyc0957932010-09-30 16:52:03 +00002525 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002526 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002527 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002528 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2529 for (;;) {
2530 if (getLexer().is(AsmToken::EndOfStatement))
2531 break;
2532
2533 StringRef Name;
2534 SMLoc Loc = getTok().getLoc();
2535 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002536 return TokError("unexpected token in '.loc' directive");
2537
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002538 if (Name == "basic_block")
2539 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2540 else if (Name == "prologue_end")
2541 Flags |= DWARF2_FLAG_PROLOGUE_END;
2542 else if (Name == "epilogue_begin")
2543 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2544 else if (Name == "is_stmt") {
2545 SMLoc Loc = getTok().getLoc();
2546 const MCExpr *Value;
2547 if (getParser().ParseExpression(Value))
2548 return true;
2549 // The expression must be the constant 0 or 1.
2550 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2551 int Value = MCE->getValue();
2552 if (Value == 0)
2553 Flags &= ~DWARF2_FLAG_IS_STMT;
2554 else if (Value == 1)
2555 Flags |= DWARF2_FLAG_IS_STMT;
2556 else
2557 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002558 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002559 else {
2560 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2561 }
2562 }
2563 else if (Name == "isa") {
2564 SMLoc Loc = getTok().getLoc();
2565 const MCExpr *Value;
2566 if (getParser().ParseExpression(Value))
2567 return true;
2568 // The expression must be a constant greater or equal to 0.
2569 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2570 int Value = MCE->getValue();
2571 if (Value < 0)
2572 return Error(Loc, "isa number less than zero");
2573 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002574 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002575 else {
2576 return Error(Loc, "isa number not a constant value");
2577 }
2578 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002579 else if (Name == "discriminator") {
2580 if (getParser().ParseAbsoluteExpression(Discriminator))
2581 return true;
2582 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002583 else {
2584 return Error(Loc, "unknown sub-directive in '.loc' directive");
2585 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002586
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002587 if (getLexer().is(AsmToken::EndOfStatement))
2588 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002589 }
2590 }
2591
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002592 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002593 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002594
2595 return false;
2596}
2597
Daniel Dunbar138abae2010-10-16 04:56:42 +00002598/// ParseDirectiveStabs
2599/// ::= .stabs string, number, number, number
2600bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2601 SMLoc DirectiveLoc) {
2602 return TokError("unsupported directive '" + Directive + "'");
2603}
2604
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002605/// ParseDirectiveCFISections
2606/// ::= .cfi_sections section [, section]
2607bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2608 SMLoc DirectiveLoc) {
2609 StringRef Name;
2610 bool EH = false;
2611 bool Debug = false;
2612
2613 if (getParser().ParseIdentifier(Name))
2614 return TokError("Expected an identifier");
2615
2616 if (Name == ".eh_frame")
2617 EH = true;
2618 else if (Name == ".debug_frame")
2619 Debug = true;
2620
2621 if (getLexer().is(AsmToken::Comma)) {
2622 Lex();
2623
2624 if (getParser().ParseIdentifier(Name))
2625 return TokError("Expected an identifier");
2626
2627 if (Name == ".eh_frame")
2628 EH = true;
2629 else if (Name == ".debug_frame")
2630 Debug = true;
2631 }
2632
2633 getStreamer().EmitCFISections(EH, Debug);
2634
2635 return false;
2636}
2637
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002638/// ParseDirectiveCFIStartProc
2639/// ::= .cfi_startproc
2640bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2641 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002642 getStreamer().EmitCFIStartProc();
2643 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002644}
2645
2646/// ParseDirectiveCFIEndProc
2647/// ::= .cfi_endproc
2648bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002649 getStreamer().EmitCFIEndProc();
2650 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002651}
2652
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002653/// ParseRegisterOrRegisterNumber - parse register name or number.
2654bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2655 SMLoc DirectiveLoc) {
2656 unsigned RegNo;
2657
Jim Grosbach6f888a82011-06-02 17:14:04 +00002658 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002659 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2660 DirectiveLoc))
2661 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002662 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002663 } else
2664 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002665
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002666 return false;
2667}
2668
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002669/// ParseDirectiveCFIDefCfa
2670/// ::= .cfi_def_cfa register, offset
2671bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2672 SMLoc DirectiveLoc) {
2673 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002674 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002675 return true;
2676
2677 if (getLexer().isNot(AsmToken::Comma))
2678 return TokError("unexpected token in directive");
2679 Lex();
2680
2681 int64_t Offset = 0;
2682 if (getParser().ParseAbsoluteExpression(Offset))
2683 return true;
2684
Rafael Espindola066c2f42011-04-12 23:59:07 +00002685 getStreamer().EmitCFIDefCfa(Register, Offset);
2686 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002687}
2688
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002689/// ParseDirectiveCFIDefCfaOffset
2690/// ::= .cfi_def_cfa_offset offset
2691bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2692 SMLoc DirectiveLoc) {
2693 int64_t Offset = 0;
2694 if (getParser().ParseAbsoluteExpression(Offset))
2695 return true;
2696
Rafael Espindola066c2f42011-04-12 23:59:07 +00002697 getStreamer().EmitCFIDefCfaOffset(Offset);
2698 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002699}
2700
2701/// ParseDirectiveCFIAdjustCfaOffset
2702/// ::= .cfi_adjust_cfa_offset adjustment
2703bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2704 SMLoc DirectiveLoc) {
2705 int64_t Adjustment = 0;
2706 if (getParser().ParseAbsoluteExpression(Adjustment))
2707 return true;
2708
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002709 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2710 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002711}
2712
2713/// ParseDirectiveCFIDefCfaRegister
2714/// ::= .cfi_def_cfa_register register
2715bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2716 SMLoc DirectiveLoc) {
2717 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002718 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002719 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002720
Rafael Espindola066c2f42011-04-12 23:59:07 +00002721 getStreamer().EmitCFIDefCfaRegister(Register);
2722 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002723}
2724
2725/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002726/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002727bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2728 int64_t Register = 0;
2729 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002730
2731 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002732 return true;
2733
2734 if (getLexer().isNot(AsmToken::Comma))
2735 return TokError("unexpected token in directive");
2736 Lex();
2737
2738 if (getParser().ParseAbsoluteExpression(Offset))
2739 return true;
2740
Rafael Espindola066c2f42011-04-12 23:59:07 +00002741 getStreamer().EmitCFIOffset(Register, Offset);
2742 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002743}
2744
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002745/// ParseDirectiveCFIRelOffset
2746/// ::= .cfi_rel_offset register, offset
2747bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2748 SMLoc DirectiveLoc) {
2749 int64_t Register = 0;
2750
2751 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2752 return true;
2753
2754 if (getLexer().isNot(AsmToken::Comma))
2755 return TokError("unexpected token in directive");
2756 Lex();
2757
2758 int64_t Offset = 0;
2759 if (getParser().ParseAbsoluteExpression(Offset))
2760 return true;
2761
Rafael Espindola25f492e2011-04-12 16:12:03 +00002762 getStreamer().EmitCFIRelOffset(Register, Offset);
2763 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002764}
2765
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002766static bool isValidEncoding(int64_t Encoding) {
2767 if (Encoding & ~0xff)
2768 return false;
2769
2770 if (Encoding == dwarf::DW_EH_PE_omit)
2771 return true;
2772
2773 const unsigned Format = Encoding & 0xf;
2774 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2775 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2776 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2777 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2778 return false;
2779
Rafael Espindolacaf11582010-12-29 04:31:26 +00002780 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002781 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002782 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002783 return false;
2784
2785 return true;
2786}
2787
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002788/// ParseDirectiveCFIPersonalityOrLsda
2789/// ::= .cfi_personality encoding, [symbol_name]
2790/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002791bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002792 SMLoc DirectiveLoc) {
2793 int64_t Encoding = 0;
2794 if (getParser().ParseAbsoluteExpression(Encoding))
2795 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002796 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002797 return false;
2798
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002799 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002800 return TokError("unsupported encoding.");
2801
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002802 if (getLexer().isNot(AsmToken::Comma))
2803 return TokError("unexpected token in directive");
2804 Lex();
2805
2806 StringRef Name;
2807 if (getParser().ParseIdentifier(Name))
2808 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002809
2810 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2811
2812 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002813 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002814 else {
2815 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002816 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002817 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002818 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002819}
2820
Rafael Espindolafe024d02010-12-28 18:36:23 +00002821/// ParseDirectiveCFIRememberState
2822/// ::= .cfi_remember_state
2823bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2824 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002825 getStreamer().EmitCFIRememberState();
2826 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002827}
2828
2829/// ParseDirectiveCFIRestoreState
2830/// ::= .cfi_remember_state
2831bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2832 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002833 getStreamer().EmitCFIRestoreState();
2834 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002835}
2836
Rafael Espindolac5754392011-04-12 15:31:05 +00002837/// ParseDirectiveCFISameValue
2838/// ::= .cfi_same_value register
2839bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2840 SMLoc DirectiveLoc) {
2841 int64_t Register = 0;
2842
2843 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2844 return true;
2845
2846 getStreamer().EmitCFISameValue(Register);
2847
2848 return false;
2849}
2850
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002851/// ParseDirectiveCFIRestore
2852/// ::= .cfi_restore register
2853bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2854 SMLoc DirectiveLoc) {
2855 int64_t Register = 0;
2856 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2857 return true;
2858
2859 getStreamer().EmitCFIRestore(Register);
2860
2861 return false;
2862}
2863
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002864/// ParseDirectiveCFIEscape
2865/// ::= .cfi_escape expression[,...]
2866bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2867 SMLoc DirectiveLoc) {
2868 std::string Values;
2869 int64_t CurrValue;
2870 if (getParser().ParseAbsoluteExpression(CurrValue))
2871 return true;
2872
2873 Values.push_back((uint8_t)CurrValue);
2874
2875 while (getLexer().is(AsmToken::Comma)) {
2876 Lex();
2877
2878 if (getParser().ParseAbsoluteExpression(CurrValue))
2879 return true;
2880
2881 Values.push_back((uint8_t)CurrValue);
2882 }
2883
2884 getStreamer().EmitCFIEscape(Values);
2885 return false;
2886}
2887
Rafael Espindola16d7d432012-01-23 21:51:52 +00002888/// ParseDirectiveCFISignalFrame
2889/// ::= .cfi_signal_frame
2890bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2891 SMLoc DirectiveLoc) {
2892 if (getLexer().isNot(AsmToken::EndOfStatement))
2893 return Error(getLexer().getLoc(),
2894 "unexpected token in '" + Directive + "' directive");
2895
2896 getStreamer().EmitCFISignalFrame();
2897
2898 return false;
2899}
2900
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002901/// ParseDirectiveMacrosOnOff
2902/// ::= .macros_on
2903/// ::= .macros_off
2904bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2905 SMLoc DirectiveLoc) {
2906 if (getLexer().isNot(AsmToken::EndOfStatement))
2907 return Error(getLexer().getLoc(),
2908 "unexpected token in '" + Directive + "' directive");
2909
2910 getParser().MacrosEnabled = Directive == ".macros_on";
2911
2912 return false;
2913}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002914
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002915/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002916/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002917bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2918 SMLoc DirectiveLoc) {
2919 StringRef Name;
2920 if (getParser().ParseIdentifier(Name))
2921 return TokError("expected identifier in directive");
2922
Rafael Espindola65366442011-06-05 02:43:45 +00002923 std::vector<StringRef> Parameters;
2924 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2925 for(;;) {
2926 StringRef Parameter;
2927 if (getParser().ParseIdentifier(Parameter))
2928 return TokError("expected identifier in directive");
2929 Parameters.push_back(Parameter);
2930
2931 if (getLexer().isNot(AsmToken::Comma))
2932 break;
2933 Lex();
2934 }
2935 }
2936
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002937 if (getLexer().isNot(AsmToken::EndOfStatement))
2938 return TokError("unexpected token in '.macro' directive");
2939
2940 // Eat the end of statement.
2941 Lex();
2942
2943 AsmToken EndToken, StartToken = getTok();
2944
2945 // Lex the macro definition.
2946 for (;;) {
2947 // Check whether we have reached the end of the file.
2948 if (getLexer().is(AsmToken::Eof))
2949 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2950
2951 // Otherwise, check whether we have reach the .endmacro.
2952 if (getLexer().is(AsmToken::Identifier) &&
2953 (getTok().getIdentifier() == ".endm" ||
2954 getTok().getIdentifier() == ".endmacro")) {
2955 EndToken = getTok();
2956 Lex();
2957 if (getLexer().isNot(AsmToken::EndOfStatement))
2958 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2959 "' directive");
2960 break;
2961 }
2962
2963 // Otherwise, scan til the end of the statement.
2964 getParser().EatToEndOfStatement();
2965 }
2966
2967 if (getParser().MacroMap.lookup(Name)) {
2968 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2969 }
2970
2971 const char *BodyStart = StartToken.getLoc().getPointer();
2972 const char *BodyEnd = EndToken.getLoc().getPointer();
2973 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002974 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002975 return false;
2976}
2977
2978/// ParseDirectiveEndMacro
2979/// ::= .endm
2980/// ::= .endmacro
2981bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2982 SMLoc DirectiveLoc) {
2983 if (getLexer().isNot(AsmToken::EndOfStatement))
2984 return TokError("unexpected token in '" + Directive + "' directive");
2985
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002986 // If we are inside a macro instantiation, terminate the current
2987 // instantiation.
2988 if (!getParser().ActiveMacros.empty()) {
2989 getParser().HandleMacroExit();
2990 return false;
2991 }
2992
2993 // Otherwise, this .endmacro is a stray entry in the file; well formed
2994 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002995 return TokError("unexpected '" + Directive + "' in file, "
2996 "no current macro definition");
2997}
2998
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002999bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00003000 getParser().CheckForValidSection();
3001
3002 const MCExpr *Value;
3003
3004 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003005 return true;
3006
3007 if (getLexer().isNot(AsmToken::EndOfStatement))
3008 return TokError("unexpected token in directive");
3009
3010 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003011 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003012 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003013 getStreamer().EmitULEB128Value(Value);
3014
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003015 return false;
3016}
3017
3018
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003019/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003020MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003021 MCContext &C, MCStreamer &Out,
3022 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003023 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003024}