blob: dad72d2050aee965766fff5a075d88c286f6d030 [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"
Matt Fleming924c5e52010-05-21 11:36:59 +000017#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000018#include "llvm/ADT/Twine.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000019#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020#include "llvm/MC/MCContext.h"
Evan Cheng94b95502011-07-26 00:24:13 +000021#include "llvm/MC/MCDwarf.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000022#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000023#include "llvm/MC/MCParser/AsmCond.h"
24#include "llvm/MC/MCParser/AsmLexer.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
26#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000028#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000029#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000030#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/MC/MCTargetAsmParser.h"
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000032#include "llvm/Support/CommandLine.h"
Benjamin Kramer518ff562012-01-28 15:28:41 +000033#include "llvm/Support/ErrorHandling.h"
Jim Grosbach254cf032011-06-29 16:05:14 +000034#include "llvm/Support/MathExtras.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000035#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000036#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000037#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000038#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000039#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000040using namespace llvm;
41
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +000042static cl::opt<bool>
43FatalAssemblerWarnings("fatal-assembler-warnings",
44 cl::desc("Consider warnings as error"));
45
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000046namespace {
47
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000048/// \brief Helper class for tracking macro definitions.
49struct Macro {
50 StringRef Name;
51 StringRef Body;
Rafael Espindola65366442011-06-05 02:43:45 +000052 std::vector<StringRef> Parameters;
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000053
54public:
Rafael Espindola65366442011-06-05 02:43:45 +000055 Macro(StringRef N, StringRef B, const std::vector<StringRef> &P) :
56 Name(N), Body(B), Parameters(P) {}
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000057};
58
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000059/// \brief Helper class for storing information about an active macro
60/// instantiation.
61struct MacroInstantiation {
62 /// The macro being instantiated.
63 const Macro *TheMacro;
64
65 /// The macro instantiation with substitutions.
66 MemoryBuffer *Instantiation;
67
68 /// The location of the instantiation.
69 SMLoc InstantiationLoc;
70
71 /// The location where parsing should resume upon instantiation completion.
72 SMLoc ExitLoc;
73
74public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000075 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
Rafael Espindola65366442011-06-05 02:43:45 +000076 MemoryBuffer *I);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000077};
78
Daniel Dunbaraef87e32010-07-18 18:31:38 +000079/// \brief The concrete assembly parser instance.
80class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000081 friend class GenericAsmParser;
82
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
84 void operator=(const AsmParser &); // DO NOT IMPLEMENT
85private:
86 AsmLexer Lexer;
87 MCContext &Ctx;
88 MCStreamer &Out;
Jim Grosbache82b8ee2011-06-15 18:33:28 +000089 const MCAsmInfo &MAI;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000090 SourceMgr &SrcMgr;
Benjamin Kramer04a04262011-10-16 10:48:29 +000091 SourceMgr::DiagHandlerTy SavedDiagHandler;
92 void *SavedDiagContext;
Daniel Dunbaraef87e32010-07-18 18:31:38 +000093 MCAsmParserExtension *GenericParser;
94 MCAsmParserExtension *PlatformParser;
Rafael Espindolaa61842b2011-04-11 21:49:50 +000095
Daniel Dunbaraef87e32010-07-18 18:31:38 +000096 /// This is the current buffer index we're lexing from as managed by the
97 /// SourceMgr object.
98 int CurBuffer;
99
100 AsmCond TheCondState;
101 std::vector<AsmCond> TheCondStack;
102
103 /// DirectiveMap - This is a table handlers for directives. Each handler is
104 /// invoked after the directive identifier is read and is responsible for
105 /// parsing and validating the rest of the directive. The handler is passed
106 /// in the directive name and the location of the directive keyword.
107 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000108
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000109 /// MacroMap - Map of currently defined macros.
110 StringMap<Macro*> MacroMap;
111
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000112 /// ActiveMacros - Stack of active macro instantiations.
113 std::vector<MacroInstantiation*> ActiveMacros;
114
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000115 /// Boolean tracking whether macro substitution is enabled.
116 unsigned MacrosEnabled : 1;
117
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000118 /// Flag tracking whether any errors have been encountered.
119 unsigned HadError : 1;
120
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000121 /// The values from the last parsed cpp hash file line comment if any.
122 StringRef CppHashFilename;
123 int64_t CppHashLineNumber;
124 SMLoc CppHashLoc;
125
Devang Patel0db58bf2012-01-31 18:14:05 +0000126 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
127 unsigned AssemblerDialect;
128
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000129public:
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000130 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000131 const MCAsmInfo &MAI);
132 ~AsmParser();
133
134 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
135
136 void AddDirectiveHandler(MCAsmParserExtension *Object,
137 StringRef Directive,
138 DirectiveHandler Handler) {
139 DirectiveMap[Directive] = std::make_pair(Object, Handler);
140 }
141
142public:
143 /// @name MCAsmParser Interface
144 /// {
145
146 virtual SourceMgr &getSourceManager() { return SrcMgr; }
147 virtual MCAsmLexer &getLexer() { return Lexer; }
148 virtual MCContext &getContext() { return Ctx; }
149 virtual MCStreamer &getStreamer() { return Out; }
Devang Patel0db58bf2012-01-31 18:14:05 +0000150 virtual unsigned getAssemblerDialect() {
151 if (AssemblerDialect == ~0U)
152 return MAI.getAssemblerDialect();
153 else
154 return AssemblerDialect;
155 }
156 virtual void setAssemblerDialect(unsigned i) {
157 AssemblerDialect = i;
158 }
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000159
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000160 virtual bool Warning(SMLoc L, const Twine &Msg,
161 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
162 virtual bool Error(SMLoc L, const Twine &Msg,
163 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>());
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164
165 const AsmToken &Lex();
166
167 bool ParseExpression(const MCExpr *&Res);
168 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
169 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
170 virtual bool ParseAbsoluteExpression(int64_t &Res);
171
172 /// }
173
174private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000175 void CheckForValidSection();
176
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000177 bool ParseStatement();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000178 void EatToEndOfLine();
179 bool ParseCppHashLineFilenameComment(const SMLoc &L);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000181 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
Rafael Espindola65366442011-06-05 02:43:45 +0000182 bool expandMacro(SmallString<256> &Buf, StringRef Body,
183 const std::vector<StringRef> &Parameters,
184 const std::vector<std::vector<AsmToken> > &A,
185 const SMLoc &L);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000186 void HandleMacroExit();
187
188 void PrintMacroInstantiations();
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000189 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
Chris Lattner462b43c2011-10-16 05:47:55 +0000190 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const {
191 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000192 }
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000193 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000194
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000195 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
196 bool EnterIncludeFile(const std::string &Filename);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000197 /// ProcessIncbinFile - Process the specified file for the .incbin directive.
198 /// This returns true on failure.
199 bool ProcessIncbinFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000200
201 /// \brief Reset the current lexer position to that given by \arg Loc. The
202 /// current token is not set; clients should ensure Lex() is called
203 /// subsequently.
204 void JumpToLoc(SMLoc Loc);
205
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000206 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000207
208 /// \brief Parse up to the end of statement and a return the contents from the
209 /// current token until the end of the statement; the current token on exit
210 /// will be either the EndOfStatement or EOF.
211 StringRef ParseStringToEndOfStatement();
212
Nico Weber4c4c7322011-01-28 03:04:41 +0000213 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214
215 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
216 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
217 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000218 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000219
220 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
221 /// and set \arg Res to the identifier contents.
222 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000223
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000224 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000225
226 // ".ascii", ".asciiz", ".string"
227 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000228 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000229 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000230 bool ParseDirectiveFill(); // ".fill"
231 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000232 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000233 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000234 bool ParseDirectiveOrg(); // ".org"
235 // ".align{,32}", ".p2align{,w,l}"
236 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
237
238 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
239 /// accepts a single symbol (which should be a label or an external).
240 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000241
242 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
243
244 bool ParseDirectiveAbort(); // ".abort"
245 bool ParseDirectiveInclude(); // ".include"
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000246 bool ParseDirectiveIncbin(); // ".incbin"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000247
248 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000249 // ".ifdef" or ".ifndef", depending on expect_defined
250 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000251 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
252 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
253 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
254
255 /// ParseEscapedString - Parse the current token as a string which may include
256 /// escaped characters and return the string contents.
257 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000258
259 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
260 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000261};
262
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000263/// \brief Generic implementations of directive handling, etc. which is shared
264/// (or the default, at least) for all assembler parser.
265class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000266 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
267 void AddDirectiveHandler(StringRef Directive) {
268 getParser().AddDirectiveHandler(this, Directive,
269 HandleDirective<GenericAsmParser, Handler>);
270 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000271public:
272 GenericAsmParser() {}
273
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000274 AsmParser &getParser() {
275 return (AsmParser&) this->MCAsmParserExtension::getParser();
276 }
277
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000278 virtual void Initialize(MCAsmParser &Parser) {
279 // Call the base implementation.
280 this->MCAsmParserExtension::Initialize(Parser);
281
282 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000283 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
284 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
285 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000286 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000287
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000288 // CFI directives.
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000289 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFISections>(
290 ".cfi_sections");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000291 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
292 ".cfi_startproc");
293 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
294 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000295 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
296 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000297 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
298 ".cfi_def_cfa_offset");
Rafael Espindola53abbe52011-04-11 20:29:16 +0000299 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset>(
300 ".cfi_adjust_cfa_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000301 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
302 ".cfi_def_cfa_register");
303 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
304 ".cfi_offset");
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000305 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIRelOffset>(
306 ".cfi_rel_offset");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000307 AddDirectiveHandler<
308 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
309 AddDirectiveHandler<
310 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000311 AddDirectiveHandler<
312 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
313 AddDirectiveHandler<
314 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindolac5754392011-04-12 15:31:05 +0000315 AddDirectiveHandler<
316 &GenericAsmParser::ParseDirectiveCFISameValue>(".cfi_same_value");
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000317 AddDirectiveHandler<
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000318 &GenericAsmParser::ParseDirectiveCFIRestore>(".cfi_restore");
319 AddDirectiveHandler<
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000320 &GenericAsmParser::ParseDirectiveCFIEscape>(".cfi_escape");
Rafael Espindola16d7d432012-01-23 21:51:52 +0000321 AddDirectiveHandler<
322 &GenericAsmParser::ParseDirectiveCFISignalFrame>(".cfi_signal_frame");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000323
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000324 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000325 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
326 ".macros_on");
327 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
328 ".macros_off");
329 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
330 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
331 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000332
333 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
334 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000335 }
336
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000337 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
338
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000339 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
340 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
341 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000342 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaf9efd832011-05-10 01:10:18 +0000343 bool ParseDirectiveCFISections(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000344 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
345 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000346 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000347 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola53abbe52011-04-11 20:29:16 +0000348 bool ParseDirectiveCFIAdjustCfaOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000349 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
350 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaa61842b2011-04-11 21:49:50 +0000351 bool ParseDirectiveCFIRelOffset(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000352 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000353 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
354 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Rafael Espindolac5754392011-04-12 15:31:05 +0000355 bool ParseDirectiveCFISameValue(StringRef, SMLoc DirectiveLoc);
Rafael Espindolaed23bdb2011-12-29 21:43:03 +0000356 bool ParseDirectiveCFIRestore(StringRef, SMLoc DirectiveLoc);
Rafael Espindola6f0b1812011-12-29 20:24:47 +0000357 bool ParseDirectiveCFIEscape(StringRef, SMLoc DirectiveLoc);
Rafael Espindola16d7d432012-01-23 21:51:52 +0000358 bool ParseDirectiveCFISignalFrame(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000359
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000360 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000361 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
362 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000363
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000364 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000365};
366
367}
368
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000369namespace llvm {
370
371extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000372extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000373extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000374
375}
376
Chris Lattneraaec2052010-01-19 19:46:13 +0000377enum { DEFAULT_ADDRSPACE = 0 };
378
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000379AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx,
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000380 MCStreamer &_Out, const MCAsmInfo &_MAI)
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000381 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM),
Rafael Espindola5d7dcd32011-04-12 18:53:30 +0000382 GenericParser(new GenericAsmParser), PlatformParser(0),
Devang Patel0db58bf2012-01-31 18:14:05 +0000383 CurBuffer(0), MacrosEnabled(true), CppHashLineNumber(0),
384 AssemblerDialect(~0U) {
Benjamin Kramer04a04262011-10-16 10:48:29 +0000385 // Save the old handler.
386 SavedDiagHandler = SrcMgr.getDiagHandler();
387 SavedDiagContext = SrcMgr.getDiagContext();
388 // Set our own handler which calls the saved handler.
Kevin Enderbyacbaecd2011-10-12 21:38:39 +0000389 SrcMgr.setDiagHandler(DiagHandler, this);
Sean Callananfd0b0282010-01-21 00:19:58 +0000390 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000391
392 // Initialize the generic parser.
393 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000394
395 // Initialize the platform / file format parser.
396 //
397 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
398 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000399 if (_MAI.hasMicrosoftFastStdCallMangling()) {
400 PlatformParser = createCOFFAsmParser();
401 PlatformParser->Initialize(*this);
402 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000403 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000404 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000405 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000406 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000407 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000408 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000409}
410
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000411AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000412 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
413
414 // Destroy any macros.
415 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
416 ie = MacroMap.end(); it != ie; ++it)
417 delete it->getValue();
418
Daniel Dunbare4749702010-07-12 18:12:02 +0000419 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000420 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000421}
422
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000423void AsmParser::PrintMacroInstantiations() {
424 // Print the active macro instantiation stack.
425 for (std::vector<MacroInstantiation*>::const_reverse_iterator
426 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000427 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
428 "while in macro instantiation");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000429}
430
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000431bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000432 if (FatalAssemblerWarnings)
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000433 return Error(L, Msg, Ranges);
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000434 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000435 PrintMacroInstantiations();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +0000436 return false;
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000437}
438
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000439bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000440 HadError = true;
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000441 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000442 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000443 return true;
444}
445
Sean Callananfd0b0282010-01-21 00:19:58 +0000446bool AsmParser::EnterIncludeFile(const std::string &Filename) {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000447 std::string IncludedFile;
448 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
Sean Callananfd0b0282010-01-21 00:19:58 +0000449 if (NewBuf == -1)
450 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000451
Sean Callananfd0b0282010-01-21 00:19:58 +0000452 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000453
Sean Callananfd0b0282010-01-21 00:19:58 +0000454 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000455
Sean Callananfd0b0282010-01-21 00:19:58 +0000456 return false;
457}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000458
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000459/// Process the specified .incbin file by seaching for it in the include paths
460/// then just emiting the byte contents of the file to the streamer. This
461/// returns true on failure.
462bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
463 std::string IncludedFile;
464 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
465 if (NewBuf == -1)
466 return true;
467
Kevin Enderbyc3fc3132011-12-14 22:34:45 +0000468 // Pick up the bytes from the file and emit them.
Kevin Enderbydac29532011-12-15 00:00:27 +0000469 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(),
470 DEFAULT_ADDRSPACE);
Kevin Enderbyc55acca2011-12-14 21:47:48 +0000471 return false;
472}
473
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000474void AsmParser::JumpToLoc(SMLoc Loc) {
475 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
476 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
477}
478
Sean Callananfd0b0282010-01-21 00:19:58 +0000479const AsmToken &AsmParser::Lex() {
480 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000481
Sean Callananfd0b0282010-01-21 00:19:58 +0000482 if (tok->is(AsmToken::Eof)) {
483 // If this is the end of an included file, pop the parent file off the
484 // include stack.
485 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
486 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000487 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000488 tok = &Lexer.Lex();
489 }
490 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000491
Sean Callananfd0b0282010-01-21 00:19:58 +0000492 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000493 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000494
Sean Callananfd0b0282010-01-21 00:19:58 +0000495 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000496}
497
Chris Lattner79180e22010-04-05 23:15:42 +0000498bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000499 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000500 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000501 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000502
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000503 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000504 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000505
506 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000507 AsmCond StartingCondState = TheCondState;
508
Kevin Enderby613b7572011-11-01 22:27:22 +0000509 // If we are generating dwarf for assembly source files save the initial text
510 // section and generate a .file directive.
511 if (getContext().getGenDwarfForAssembly()) {
512 getContext().setGenDwarfSection(getStreamer().getCurrentSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000513 MCSymbol *SectionStartSym = getContext().CreateTempSymbol();
514 getStreamer().EmitLabel(SectionStartSym);
515 getContext().setGenDwarfSectionStartSym(SectionStartSym);
Kevin Enderby613b7572011-11-01 22:27:22 +0000516 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(),
517 StringRef(), SrcMgr.getMemoryBuffer(CurBuffer)->getBufferIdentifier());
518 }
519
Chris Lattnerb717fb02009-07-02 21:53:43 +0000520 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000521 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000522 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000523
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000524 // We had an error, validate that one was emitted and recover by skipping to
525 // the next line.
526 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000527 EatToEndOfStatement();
528 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000529
530 if (TheCondState.TheCond != StartingCondState.TheCond ||
531 TheCondState.Ignore != StartingCondState.Ignore)
532 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000533
534 // Check to see there are no empty DwarfFile slots.
535 const std::vector<MCDwarfFile *> &MCDwarfFiles =
536 getContext().getMCDwarfFiles();
537 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000538 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000539 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000540 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000541
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000542 // Check to see that all assembler local symbols were actually defined.
543 // Targets that don't do subsections via symbols may not want this, though,
544 // so conservatively exclude them. Only do this if we're finalizing, though,
545 // as otherwise we won't necessarilly have seen everything yet.
546 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
547 const MCContext::SymbolTable &Symbols = getContext().getSymbols();
548 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(),
549 e = Symbols.end();
550 i != e; ++i) {
551 MCSymbol *Sym = i->getValue();
552 // Variable symbols may not be marked as defined, so check those
553 // explicitly. If we know it's a variable, we have a definition for
554 // the purposes of this check.
555 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
556 // FIXME: We would really like to refer back to where the symbol was
557 // first referenced for a source location. We need to add something
558 // to track that. Currently, we just point to the end of the file.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000559 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error,
560 "assembler local symbol '" + Sym->getName() +
561 "' not defined");
Jim Grosbache82b8ee2011-06-15 18:33:28 +0000562 }
563 }
564
565
Chris Lattner79180e22010-04-05 23:15:42 +0000566 // Finalize the output stream if there are no errors and if the client wants
567 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000568 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000569 Out.Finish();
570
Chris Lattnerb717fb02009-07-02 21:53:43 +0000571 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000572}
573
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000574void AsmParser::CheckForValidSection() {
575 if (!getStreamer().getCurrentSection()) {
576 TokError("expected section directive before assembly directive");
577 Out.SwitchSection(Ctx.getMachOSection(
578 "__TEXT", "__text",
579 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
580 0, SectionKind::getText()));
581 }
582}
583
Chris Lattner2cf5f142009-06-22 01:29:09 +0000584/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
585void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000586 while (Lexer.isNot(AsmToken::EndOfStatement) &&
587 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000588 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000589
Chris Lattner2cf5f142009-06-22 01:29:09 +0000590 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000591 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000592 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000593}
594
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000595StringRef AsmParser::ParseStringToEndOfStatement() {
596 const char *Start = getTok().getLoc().getPointer();
597
598 while (Lexer.isNot(AsmToken::EndOfStatement) &&
599 Lexer.isNot(AsmToken::Eof))
600 Lex();
601
602 const char *End = getTok().getLoc().getPointer();
603 return StringRef(Start, End - Start);
604}
Chris Lattnerc4193832009-06-22 05:51:26 +0000605
Chris Lattner74ec1a32009-06-22 06:32:03 +0000606/// ParseParenExpr - Parse a paren expression and return it.
607/// NOTE: This assumes the leading '(' has already been consumed.
608///
609/// parenexpr ::= expr)
610///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000611bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000612 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000613 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000614 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000615 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000616 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000617 return false;
618}
Chris Lattnerc4193832009-06-22 05:51:26 +0000619
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000620/// ParseBracketExpr - Parse a bracket expression and return it.
621/// NOTE: This assumes the leading '[' has already been consumed.
622///
623/// bracketexpr ::= expr]
624///
625bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
626 if (ParseExpression(Res)) return true;
627 if (Lexer.isNot(AsmToken::RBrac))
628 return TokError("expected ']' in brackets expression");
629 EndLoc = Lexer.getLoc();
630 Lex();
631 return false;
632}
633
Chris Lattner74ec1a32009-06-22 06:32:03 +0000634/// ParsePrimaryExpr - Parse a primary expression and return it.
635/// primaryexpr ::= (parenexpr
636/// primaryexpr ::= symbol
637/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000638/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000639/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000640bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000641 switch (Lexer.getKind()) {
642 default:
643 return TokError("unknown token in expression");
Eric Christopherf3755b22011-04-12 00:03:13 +0000644 // If we have an error assume that we've already handled it.
645 case AsmToken::Error:
646 return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000647 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000648 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000649 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000650 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000651 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000652 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000653 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000654 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000655 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000656 EndLoc = Lexer.getLoc();
657
658 StringRef Identifier;
659 if (ParseIdentifier(Identifier))
Jim Grosbach95ae09a2011-05-23 20:36:04 +0000660 return true;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000661
Daniel Dunbarfffff912009-10-16 01:34:54 +0000662 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000663 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000664 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000665
666 // Lookup the symbol variant if used.
667 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000668 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000669 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000670 if (Variant == MCSymbolRefExpr::VK_Invalid) {
671 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000672 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000673 }
674 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000675
Daniel Dunbarfffff912009-10-16 01:34:54 +0000676 // If this is an absolute variable reference, substitute it now to preserve
677 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000678 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000679 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000680 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000681
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000682 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000683 return false;
684 }
685
686 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000687 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000688 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000689 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000690 case AsmToken::Integer: {
691 SMLoc Loc = getTok().getLoc();
692 int64_t IntVal = getTok().getIntVal();
693 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000694 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000695 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000696 // Look for 'b' or 'f' following an Integer as a directional label
697 if (Lexer.getKind() == AsmToken::Identifier) {
698 StringRef IDVal = getTok().getString();
699 if (IDVal == "f" || IDVal == "b"){
700 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
701 IDVal == "f" ? 1 : 0);
702 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
703 getContext());
704 if(IDVal == "b" && Sym->isUndefined())
705 return Error(Loc, "invalid reference to undefined symbol");
706 EndLoc = Lexer.getLoc();
707 Lex(); // Eat identifier.
708 }
709 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000710 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000711 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000712 case AsmToken::Real: {
713 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000714 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000715 Res = MCConstantExpr::Create(IntVal, getContext());
716 Lex(); // Eat token.
717 return false;
718 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000719 case AsmToken::Dot: {
720 // This is a '.' reference, which references the current PC. Emit a
721 // temporary label to the streamer and refer to it.
722 MCSymbol *Sym = Ctx.CreateTempSymbol();
723 Out.EmitLabel(Sym);
724 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
725 EndLoc = Lexer.getLoc();
726 Lex(); // Eat identifier.
727 return false;
728 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000729 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000730 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000731 return ParseParenExpr(Res, EndLoc);
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +0000732 case AsmToken::LBrac:
733 if (!PlatformParser->HasBracketExpressions())
734 return TokError("brackets expression not supported on this target");
735 Lex(); // Eat the '['.
736 return ParseBracketExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000737 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000738 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000739 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000740 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000741 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000742 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000743 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000744 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000745 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000746 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000747 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000748 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000749 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000750 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000751 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000752 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000753 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000754 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000755 }
756}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000757
Chris Lattnerb4307b32010-01-15 19:28:38 +0000758bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000759 SMLoc EndLoc;
760 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000761}
762
Daniel Dunbarcceba832010-09-17 02:47:07 +0000763const MCExpr *
764AsmParser::ApplyModifierToExpr(const MCExpr *E,
765 MCSymbolRefExpr::VariantKind Variant) {
766 // Recurse over the given expression, rebuilding it to apply the given variant
767 // if there is exactly one symbol.
768 switch (E->getKind()) {
769 case MCExpr::Target:
770 case MCExpr::Constant:
771 return 0;
772
773 case MCExpr::SymbolRef: {
774 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
775
776 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
777 TokError("invalid variant on expression '" +
778 getTok().getIdentifier() + "' (already modified)");
779 return E;
780 }
781
782 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
783 }
784
785 case MCExpr::Unary: {
786 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
787 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
788 if (!Sub)
789 return 0;
790 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
791 }
792
793 case MCExpr::Binary: {
794 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
795 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
796 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
797
798 if (!LHS && !RHS)
799 return 0;
800
801 if (!LHS) LHS = BE->getLHS();
802 if (!RHS) RHS = BE->getRHS();
803
804 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
805 }
806 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000807
808 assert(0 && "Invalid expression kind!");
809 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000810}
811
Chris Lattner74ec1a32009-06-22 06:32:03 +0000812/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000813///
Jim Grosbachfbe16812011-08-20 16:24:13 +0000814/// expr ::= expr &&,|| expr -> lowest.
815/// expr ::= expr |,^,&,! expr
816/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
817/// expr ::= expr <<,>> expr
818/// expr ::= expr +,- expr
819/// expr ::= expr *,/,% expr -> highest.
Chris Lattner74ec1a32009-06-22 06:32:03 +0000820/// expr ::= primaryexpr
821///
Chris Lattner54482b42010-01-15 19:39:23 +0000822bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000823 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000824 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000825 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
826 return true;
827
Daniel Dunbarcceba832010-09-17 02:47:07 +0000828 // As a special case, we support 'a op b @ modifier' by rewriting the
829 // expression to include the modifier. This is inefficient, but in general we
830 // expect users to use 'a@modifier op b'.
831 if (Lexer.getKind() == AsmToken::At) {
832 Lex();
833
834 if (Lexer.isNot(AsmToken::Identifier))
835 return TokError("unexpected symbol modifier following '@'");
836
837 MCSymbolRefExpr::VariantKind Variant =
838 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
839 if (Variant == MCSymbolRefExpr::VK_Invalid)
840 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
841
842 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
843 if (!ModifiedRes) {
844 return TokError("invalid modifier '" + getTok().getIdentifier() +
845 "' (no symbols present)");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000846 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000847
Daniel Dunbarcceba832010-09-17 02:47:07 +0000848 Res = ModifiedRes;
849 Lex();
850 }
851
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000852 // Try to constant fold it up front, if possible.
853 int64_t Value;
854 if (Res->EvaluateAsAbsolute(Value))
855 Res = MCConstantExpr::Create(Value, getContext());
856
857 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000858}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000859
Chris Lattnerb4307b32010-01-15 19:28:38 +0000860bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000861 Res = 0;
862 return ParseParenExpr(Res, EndLoc) ||
863 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000864}
865
Daniel Dunbar475839e2009-06-29 20:37:27 +0000866bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000867 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000868
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000869 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000870 if (ParseExpression(Expr))
871 return true;
872
Daniel Dunbare00b0112009-10-16 01:57:52 +0000873 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000874 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000875
876 return false;
877}
878
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000879static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000880 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000881 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000882 default:
883 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000884
Jim Grosbachfbe16812011-08-20 16:24:13 +0000885 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000886 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000887 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000888 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000889 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000890 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000891 return 1;
892
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000893
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000894 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000895 //
896 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000897 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000898 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000899 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000900 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000901 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000902 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000903 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000904 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000905 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000906
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000907 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000908 case AsmToken::EqualEqual:
909 Kind = MCBinaryExpr::EQ;
910 return 3;
911 case AsmToken::ExclaimEqual:
912 case AsmToken::LessGreater:
913 Kind = MCBinaryExpr::NE;
914 return 3;
915 case AsmToken::Less:
916 Kind = MCBinaryExpr::LT;
917 return 3;
918 case AsmToken::LessEqual:
919 Kind = MCBinaryExpr::LTE;
920 return 3;
921 case AsmToken::Greater:
922 Kind = MCBinaryExpr::GT;
923 return 3;
924 case AsmToken::GreaterEqual:
925 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000926 return 3;
927
Jim Grosbachfbe16812011-08-20 16:24:13 +0000928 // Intermediate Precedence: <<, >>
929 case AsmToken::LessLess:
930 Kind = MCBinaryExpr::Shl;
931 return 4;
932 case AsmToken::GreaterGreater:
933 Kind = MCBinaryExpr::Shr;
934 return 4;
935
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000936 // High Intermediate Precedence: +, -
937 case AsmToken::Plus:
938 Kind = MCBinaryExpr::Add;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000939 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000940 case AsmToken::Minus:
941 Kind = MCBinaryExpr::Sub;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000942 return 5;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000943
Jim Grosbachfbe16812011-08-20 16:24:13 +0000944 // Highest Precedence: *, /, %
Daniel Dunbar3f872332009-07-28 16:08:33 +0000945 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000946 Kind = MCBinaryExpr::Mul;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000947 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000948 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000949 Kind = MCBinaryExpr::Div;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000950 return 6;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000951 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000952 Kind = MCBinaryExpr::Mod;
Jim Grosbachfbe16812011-08-20 16:24:13 +0000953 return 6;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000954 }
955}
956
957
958/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
959/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000960bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
961 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000962 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000963 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000964 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000965
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000966 // If the next token is lower precedence than we are allowed to eat, return
967 // successfully with what we ate already.
968 if (TokPrec < Precedence)
969 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000970
Sean Callanan79ed1a82010-01-19 20:22:31 +0000971 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000972
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000973 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000974 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000975 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000976
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000977 // If BinOp binds less tightly with RHS than the operator after RHS, let
978 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000979 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000980 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000981 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000982 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000983 }
984
Daniel Dunbar475839e2009-06-29 20:37:27 +0000985 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000986 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000987 }
988}
989
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000990
991
992
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000993/// ParseStatement:
994/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000995/// ::= Label* Directive ...Operands... EndOfStatement
996/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000997bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000998 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000999 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001000 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001001 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001002 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001003
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001004 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +00001005 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +00001006 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001007 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001008 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001009 // A full line comment is a '#' as the first token.
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001010 if (Lexer.is(AsmToken::Hash))
1011 return ParseCppHashLineFilenameComment(IDLoc);
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001012
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +00001013 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001014 if (Lexer.is(AsmToken::Integer)) {
1015 LocalLabelVal = getTok().getIntVal();
1016 if (LocalLabelVal < 0) {
1017 if (!TheCondState.Ignore)
1018 return TokError("unexpected token at start of statement");
1019 IDVal = "";
1020 }
1021 else {
1022 IDVal = getTok().getString();
1023 Lex(); // Consume the integer token to be used as an identifier token.
1024 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +00001025 if (!TheCondState.Ignore)
1026 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001027 }
1028 }
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001029
1030 } else if (Lexer.is(AsmToken::Dot)) {
1031 // Treat '.' as a valid identifier in this context.
1032 Lex();
1033 IDVal = ".";
1034
Daniel Dunbar0143ac12011-03-25 17:47:14 +00001035 } else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +00001036 if (!TheCondState.Ignore)
1037 return TokError("unexpected token at start of statement");
1038 IDVal = "";
1039 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001040
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001041
Chris Lattner7834fac2010-04-17 18:14:27 +00001042 // Handle conditional assembly here before checking for skipping. We
1043 // have to do this so that .endif isn't skipped in a ".if 0" block for
1044 // example.
1045 if (IDVal == ".if")
1046 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001047 if (IDVal == ".ifdef")
1048 return ParseDirectiveIfdef(IDLoc, true);
1049 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
1050 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +00001051 if (IDVal == ".elseif")
1052 return ParseDirectiveElseIf(IDLoc);
1053 if (IDVal == ".else")
1054 return ParseDirectiveElse(IDLoc);
1055 if (IDVal == ".endif")
1056 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001057
Chris Lattner7834fac2010-04-17 18:14:27 +00001058 // If we are in a ".if 0" block, ignore this statement.
1059 if (TheCondState.Ignore) {
1060 EatToEndOfStatement();
1061 return false;
1062 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001063
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001064 // FIXME: Recurse on local labels?
1065
1066 // See what kind of statement we have.
1067 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001068 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001069 CheckForValidSection();
1070
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001071 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001072 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001073
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001074 // Diagnose attempt to use '.' as a label.
1075 if (IDVal == ".")
1076 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1077
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001078 // Diagnose attempt to use a variable as a label.
1079 //
1080 // FIXME: Diagnostics. Note the location of the definition as a label.
1081 // FIXME: This doesn't diagnose assignment to a symbol which has been
1082 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001083 MCSymbol *Sym;
1084 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001085 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +00001086 else
1087 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +00001088 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001089 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001090
Daniel Dunbar959fd882009-08-26 22:13:22 +00001091 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001092 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001093
Kevin Enderby94c2e852011-12-09 18:09:40 +00001094 // If we are generating dwarf for assembly source files then gather the
Kevin Enderby11c2def2012-01-10 21:12:34 +00001095 // info to make a dwarf label entry for this label if needed.
Kevin Enderby94c2e852011-12-09 18:09:40 +00001096 if (getContext().getGenDwarfForAssembly())
Kevin Enderby11c2def2012-01-10 21:12:34 +00001097 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
1098 IDLoc);
Kevin Enderby94c2e852011-12-09 18:09:40 +00001099
Daniel Dunbar01777ff2010-05-23 18:36:34 +00001100 // Consume any end of statement token, if present, to avoid spurious
1101 // AddBlankLine calls().
1102 if (Lexer.is(AsmToken::EndOfStatement)) {
1103 Lex();
1104 if (Lexer.is(AsmToken::Eof))
1105 return false;
1106 }
1107
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001108 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001109 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001110
Daniel Dunbar3f872332009-07-28 16:08:33 +00001111 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001112 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +00001113 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001114
Nico Weber4c4c7322011-01-28 03:04:41 +00001115 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001116
1117 default: // Normal instruction or directive.
1118 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001119 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001120
1121 // If macros are enabled, check to see if this is a macro instantiation.
1122 if (MacrosEnabled)
1123 if (const Macro *M = MacroMap.lookup(IDVal))
1124 return HandleMacroEntry(IDVal, IDLoc, M);
1125
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001126 // Otherwise, we have a normal instruction or directive.
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001127 if (IDVal[0] == '.' && IDVal != ".") {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001128 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +00001129 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +00001130 return ParseDirectiveSet(IDVal, true);
1131 if (IDVal == ".equiv")
1132 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001133
Daniel Dunbara0d14262009-06-24 23:30:00 +00001134 // Data directives
1135
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001136 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +00001137 return ParseDirectiveAscii(IDVal, false);
1138 if (IDVal == ".asciz" || IDVal == ".string")
1139 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001140
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001141 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001142 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +00001143 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001144 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +00001145 if (IDVal == ".value")
1146 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001147 if (IDVal == ".2byte")
1148 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001149 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001150 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +00001151 if (IDVal == ".int")
1152 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001153 if (IDVal == ".4byte")
1154 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001155 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001156 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +00001157 if (IDVal == ".8byte")
1158 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +00001159 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001160 return ParseDirectiveRealValue(APFloat::IEEEsingle);
1161 if (IDVal == ".double")
1162 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001163
Eli Friedman5d68ec22010-07-19 04:17:25 +00001164 if (IDVal == ".align") {
1165 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1166 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1167 }
1168 if (IDVal == ".align32") {
1169 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1170 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1171 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001172 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001173 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001174 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001175 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001176 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001177 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001178 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001180 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001181 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001182 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001183 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1184
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001185 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001186 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001187
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001188 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001189 return ParseDirectiveFill();
Rafael Espindolace8463f2011-04-07 20:26:23 +00001190 if (IDVal == ".space" || IDVal == ".skip")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001191 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001192 if (IDVal == ".zero")
1193 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001194
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001195 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001196
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001197 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001198 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001199 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001200 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001201 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001202 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001203 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001204 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001205 if (IDVal == ".symbol_resolver")
1206 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001207 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001208 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001209 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001210 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001211 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001212 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001213 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001214 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001215 if (IDVal == ".weak_def_can_be_hidden")
1216 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001217
Hans Wennborg5cc64912011-06-18 13:51:54 +00001218 if (IDVal == ".comm" || IDVal == ".common")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001219 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001220 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001221 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001222
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001223 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001224 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001225 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001226 return ParseDirectiveInclude();
Kevin Enderbyc55acca2011-12-14 21:47:48 +00001227 if (IDVal == ".incbin")
1228 return ParseDirectiveIncbin();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001229
Evan Chengbd27f5a2011-07-27 00:38:12 +00001230 if (IDVal == ".code16")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001231 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001232
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001233 // Look up the handler in the handler table.
1234 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1235 DirectiveMap.lookup(IDVal);
1236 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001237 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001238
Kevin Enderby9c656452009-09-10 20:51:44 +00001239 // Target hook for parsing target specific directives.
1240 if (!getTargetParser().ParseDirective(ID))
1241 return false;
1242
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001243 bool retval = Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001244 EatToEndOfStatement();
Joerg Sonnenbergerf8cd7082011-05-19 18:00:13 +00001245 return retval;
Chris Lattner2cf5f142009-06-22 01:29:09 +00001246 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001247
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001248 CheckForValidSection();
1249
Chris Lattnera7f13542010-05-19 23:34:33 +00001250 // Canonicalize the opcode to lower case.
1251 SmallString<128> Opcode;
1252 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1253 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001254
Chris Lattner98986712010-01-14 22:21:20 +00001255 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001256 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001257 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001258
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001259 // Dump the parsed representation, if requested.
1260 if (getShowParsedOperands()) {
1261 SmallString<256> Str;
1262 raw_svector_ostream OS(Str);
1263 OS << "parsed instruction: [";
1264 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1265 if (i != 0)
1266 OS << ", ";
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001267 ParsedOperands[i]->print(OS);
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001268 }
1269 OS << "]";
1270
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001271 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str());
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001272 }
1273
Kevin Enderby613b7572011-11-01 22:27:22 +00001274 // If we are generating dwarf for assembly source files and the current
1275 // section is the initial text section then generate a .loc directive for
1276 // the instruction.
1277 if (!HadError && getContext().getGenDwarfForAssembly() &&
1278 getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) {
1279 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(),
1280 SrcMgr.FindLineNumber(IDLoc, CurBuffer),
1281 0, DWARF2_LINE_DEFAULT_IS_STMT ?
Kevin Enderbydba9a172011-11-02 17:56:38 +00001282 DWARF2_FLAG_IS_STMT : 0, 0, 0,
Kevin Enderby613b7572011-11-01 22:27:22 +00001283 StringRef());
1284 }
1285
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001286 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001287 if (!HadError)
1288 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1289 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001290
Chris Lattner98986712010-01-14 22:21:20 +00001291 // Free any parsed operands.
1292 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1293 delete ParsedOperands[i];
1294
Chris Lattnercbf8a982010-09-11 16:18:25 +00001295 // Don't skip the rest of the line, the instruction parser is responsible for
1296 // that.
1297 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001298}
Chris Lattner9a023f72009-06-24 04:43:34 +00001299
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001300/// EatToEndOfLine uses the Lexer to eat the characters to the end of the line
1301/// since they may not be able to be tokenized to get to the end of line token.
1302void AsmParser::EatToEndOfLine() {
Rafael Espindola12ae5272011-10-19 18:48:52 +00001303 if (!Lexer.is(AsmToken::EndOfStatement))
1304 Lexer.LexUntilEndOfLine();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001305 // Eat EOL.
1306 Lex();
1307}
1308
1309/// ParseCppHashLineFilenameComment as this:
1310/// ::= # number "filename"
1311/// or just as a full line comment if it doesn't have a number and a string.
1312bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) {
1313 Lex(); // Eat the hash token.
1314
1315 if (getLexer().isNot(AsmToken::Integer)) {
1316 // Consume the line since in cases it is not a well-formed line directive,
1317 // as if were simply a full line comment.
1318 EatToEndOfLine();
1319 return false;
1320 }
1321
1322 int64_t LineNumber = getTok().getIntVal();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001323 Lex();
1324
1325 if (getLexer().isNot(AsmToken::String)) {
1326 EatToEndOfLine();
1327 return false;
1328 }
1329
1330 StringRef Filename = getTok().getString();
1331 // Get rid of the enclosing quotes.
1332 Filename = Filename.substr(1, Filename.size()-2);
1333
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001334 // Save the SMLoc, Filename and LineNumber for later use by diagnostics.
1335 CppHashLoc = L;
1336 CppHashFilename = Filename;
1337 CppHashLineNumber = LineNumber;
Kevin Enderbyf1c21a82011-09-13 23:45:18 +00001338
1339 // Ignore any trailing characters, they're just comment.
1340 EatToEndOfLine();
1341 return false;
1342}
1343
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001344/// DiagHandler - will use the the last parsed cpp hash line filename comment
1345/// for the Filename and LineNo if any in the diagnostic.
1346void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
1347 const AsmParser *Parser = static_cast<const AsmParser*>(Context);
1348 raw_ostream &OS = errs();
1349
1350 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
1351 const SMLoc &DiagLoc = Diag.getLoc();
1352 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
1353 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc);
1354
1355 // Like SourceMgr::PrintMessage() we need to print the include stack if any
1356 // before printing the message.
1357 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
Benjamin Kramer04a04262011-10-16 10:48:29 +00001358 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) {
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001359 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
1360 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
1361 }
1362
1363 // If we have not parsed a cpp hash line filename comment or the source
1364 // manager changed or buffer changed (like in a nested include) then just
1365 // print the normal diagnostic using its Filename and LineNo.
1366 if (!Parser->CppHashLineNumber ||
1367 &DiagSrcMgr != &Parser->SrcMgr ||
1368 DiagBuf != CppHashBuf) {
Benjamin Kramer04a04262011-10-16 10:48:29 +00001369 if (Parser->SavedDiagHandler)
1370 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
1371 else
1372 Diag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001373 return;
1374 }
1375
1376 // Use the CppHashFilename and calculate a line number based on the
1377 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for
1378 // the diagnostic.
1379 const std::string Filename = Parser->CppHashFilename;
1380
1381 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
1382 int CppHashLocLineNo =
1383 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf);
1384 int LineNo = Parser->CppHashLineNumber - 1 +
1385 (DiagLocLineNo - CppHashLocLineNo);
1386
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001387 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(),
1388 Filename, LineNo, Diag.getColumnNo(),
Chris Lattner3f2d5f62011-10-16 05:43:57 +00001389 Diag.getKind(), Diag.getMessage(),
Chris Lattner462b43c2011-10-16 05:47:55 +00001390 Diag.getLineContents(), Diag.getRanges());
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001391
Benjamin Kramer04a04262011-10-16 10:48:29 +00001392 if (Parser->SavedDiagHandler)
1393 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
1394 else
1395 NewDiag.print(0, OS);
Kevin Enderbyacbaecd2011-10-12 21:38:39 +00001396}
1397
Rafael Espindola65366442011-06-05 02:43:45 +00001398bool AsmParser::expandMacro(SmallString<256> &Buf, StringRef Body,
1399 const std::vector<StringRef> &Parameters,
1400 const std::vector<std::vector<AsmToken> > &A,
1401 const SMLoc &L) {
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001402 raw_svector_ostream OS(Buf);
Rafael Espindola65366442011-06-05 02:43:45 +00001403 unsigned NParameters = Parameters.size();
1404 if (NParameters != 0 && NParameters != A.size())
1405 return Error(L, "Wrong number of arguments");
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001406
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001407 while (!Body.empty()) {
1408 // Scan for the next substitution.
1409 std::size_t End = Body.size(), Pos = 0;
1410 for (; Pos != End; ++Pos) {
1411 // Check for a substitution or escape.
Rafael Espindola65366442011-06-05 02:43:45 +00001412 if (!NParameters) {
1413 // This macro has no parameters, look for $0, $1, etc.
1414 if (Body[Pos] != '$' || Pos + 1 == End)
1415 continue;
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001416
Rafael Espindola65366442011-06-05 02:43:45 +00001417 char Next = Body[Pos + 1];
1418 if (Next == '$' || Next == 'n' || isdigit(Next))
1419 break;
1420 } else {
1421 // This macro has parameters, look for \foo, \bar, etc.
1422 if (Body[Pos] == '\\' && Pos + 1 != End)
1423 break;
1424 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001425 }
1426
1427 // Add the prefix.
1428 OS << Body.slice(0, Pos);
1429
1430 // Check if we reached the end.
1431 if (Pos == End)
1432 break;
1433
Rafael Espindola65366442011-06-05 02:43:45 +00001434 if (!NParameters) {
1435 switch (Body[Pos+1]) {
1436 // $$ => $
1437 case '$':
1438 OS << '$';
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001439 break;
1440
Rafael Espindola65366442011-06-05 02:43:45 +00001441 // $n => number of arguments
1442 case 'n':
1443 OS << A.size();
1444 break;
1445
1446 // $[0-9] => argument
1447 default: {
1448 // Missing arguments are ignored.
1449 unsigned Index = Body[Pos+1] - '0';
1450 if (Index >= A.size())
1451 break;
1452
1453 // Otherwise substitute with the token values, with spaces eliminated.
1454 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1455 ie = A[Index].end(); it != ie; ++it)
1456 OS << it->getString();
1457 break;
1458 }
1459 }
1460 Pos += 2;
1461 } else {
1462 unsigned I = Pos + 1;
1463 while (isalnum(Body[I]) && I + 1 != End)
1464 ++I;
1465
1466 const char *Begin = Body.data() + Pos +1;
1467 StringRef Argument(Begin, I - (Pos +1));
1468 unsigned Index = 0;
1469 for (; Index < NParameters; ++Index)
1470 if (Parameters[Index] == Argument)
1471 break;
1472
1473 // FIXME: We should error at the macro definition.
1474 if (Index == NParameters)
1475 return Error(L, "Parameter not found");
1476
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001477 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1478 ie = A[Index].end(); it != ie; ++it)
1479 OS << it->getString();
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001480
Rafael Espindola65366442011-06-05 02:43:45 +00001481 Pos += 1 + Argument.size();
1482 }
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001483 // Update the scan point.
Rafael Espindola65366442011-06-05 02:43:45 +00001484 Body = Body.substr(Pos);
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001485 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001486
1487 // We include the .endmacro in the buffer as our queue to exit the macro
1488 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001489 OS << ".endmacro\n";
Rafael Espindola65366442011-06-05 02:43:45 +00001490 return false;
1491}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001492
Rafael Espindola65366442011-06-05 02:43:45 +00001493MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1494 MemoryBuffer *I)
1495 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL)
1496{
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001497}
1498
1499bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1500 const Macro *M) {
1501 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1502 // this, although we should protect against infinite loops.
1503 if (ActiveMacros.size() == 20)
1504 return TokError("macros cannot be nested more than 20 levels deep");
1505
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001506 // Parse the macro instantiation arguments.
1507 std::vector<std::vector<AsmToken> > MacroArguments;
1508 MacroArguments.push_back(std::vector<AsmToken>());
1509 unsigned ParenLevel = 0;
1510 for (;;) {
1511 if (Lexer.is(AsmToken::Eof))
1512 return TokError("unexpected token in macro instantiation");
1513 if (Lexer.is(AsmToken::EndOfStatement))
1514 break;
1515
1516 // If we aren't inside parentheses and this is a comma, start a new token
1517 // list.
1518 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1519 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001520 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001521 // Adjust the current parentheses level.
1522 if (Lexer.is(AsmToken::LParen))
1523 ++ParenLevel;
1524 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1525 --ParenLevel;
1526
1527 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001528 MacroArguments.back().push_back(getTok());
1529 }
1530 Lex();
1531 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001532
Rafael Espindola65366442011-06-05 02:43:45 +00001533 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1534 // to hold the macro body with substitutions.
1535 SmallString<256> Buf;
1536 StringRef Body = M->Body;
1537
1538 if (expandMacro(Buf, Body, M->Parameters, MacroArguments, getTok().getLoc()))
1539 return true;
1540
1541 MemoryBuffer *Instantiation =
1542 MemoryBuffer::getMemBufferCopy(Buf.str(), "<instantiation>");
1543
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001544 // Create the macro instantiation object and add to the current macro
1545 // instantiation stack.
1546 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001547 getTok().getLoc(),
Rafael Espindola65366442011-06-05 02:43:45 +00001548 Instantiation);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001549 ActiveMacros.push_back(MI);
1550
1551 // Jump to the macro instantiation and prime the lexer.
1552 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1553 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1554 Lex();
1555
1556 return false;
1557}
1558
1559void AsmParser::HandleMacroExit() {
1560 // Jump to the EndOfStatement we should return to, and consume it.
1561 JumpToLoc(ActiveMacros.back()->ExitLoc);
1562 Lex();
1563
1564 // Pop the instantiation entry.
1565 delete ActiveMacros.back();
1566 ActiveMacros.pop_back();
1567}
1568
Rafael Espindolae71cc862012-01-28 05:57:00 +00001569static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001570 switch (Value->getKind()) {
Rafael Espindolae71cc862012-01-28 05:57:00 +00001571 case MCExpr::Binary: {
1572 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value);
1573 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001574 break;
1575 }
Rafael Espindolae71cc862012-01-28 05:57:00 +00001576 case MCExpr::Target:
1577 case MCExpr::Constant:
1578 return false;
1579 case MCExpr::SymbolRef: {
1580 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol();
Rafael Espindola8b01c822012-01-28 06:22:14 +00001581 if (S.isVariable())
1582 return IsUsedIn(Sym, S.getVariableValue());
1583 return &S == Sym;
Rafael Espindolae71cc862012-01-28 05:57:00 +00001584 }
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001585 case MCExpr::Unary:
Rafael Espindolae71cc862012-01-28 05:57:00 +00001586 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001587 }
Benjamin Kramer518ff562012-01-28 15:28:41 +00001588
1589 llvm_unreachable("Unknown expr kind!");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001590}
1591
Nico Weber4c4c7322011-01-28 03:04:41 +00001592bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001593 // FIXME: Use better location, we should use proper tokens.
1594 SMLoc EqualLoc = Lexer.getLoc();
1595
Daniel Dunbar821e3332009-08-31 08:09:28 +00001596 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001597 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001598 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001599
Rafael Espindolae71cc862012-01-28 05:57:00 +00001600 // Note: we don't count b as used in "a = b". This is to allow
1601 // a = b
1602 // b = c
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001603
Daniel Dunbar3f872332009-07-28 16:08:33 +00001604 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001605 return TokError("unexpected token in assignment");
1606
Daniel Dunbar8b2b43c2011-03-25 17:47:17 +00001607 // Error on assignment to '.'.
1608 if (Name == ".") {
1609 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported "
1610 "(use '.space' or '.org').)"));
1611 }
1612
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001613 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001614 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001615
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001616 // Validate that the LHS is allowed to be a variable (either it has not been
1617 // used as a symbol, or it is an absolute symbol).
1618 MCSymbol *Sym = getContext().LookupSymbol(Name);
1619 if (Sym) {
1620 // Diagnose assignment to a label.
1621 //
1622 // FIXME: Diagnostics. Note the location of the definition as a label.
1623 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindolae71cc862012-01-28 05:57:00 +00001624 if (IsUsedIn(Sym, Value))
1625 return Error(EqualLoc, "Recursive use of '" + Name + "'");
1626 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001627 ; // Allow redefinitions of undefined symbols only used in directives.
Daniel Dunbar6db7fe82011-04-29 17:53:11 +00001628 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001629 return Error(EqualLoc, "redefinition of '" + Name + "'");
1630 else if (!Sym->isVariable())
1631 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001632 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001633 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1634 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001635
1636 // Don't count these checks as uses.
1637 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001638 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001639 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001640
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001641 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001642
1643 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001644 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001645
1646 return false;
1647}
1648
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001649/// ParseIdentifier:
1650/// ::= identifier
1651/// ::= string
1652bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001653 // The assembler has relaxed rules for accepting identifiers, in particular we
1654 // allow things like '.globl $foo', which would normally be separate
1655 // tokens. At this level, we have already lexed so we cannot (currently)
1656 // handle this as a context dependent token, instead we detect adjacent tokens
1657 // and return the combined identifier.
1658 if (Lexer.is(AsmToken::Dollar)) {
1659 SMLoc DollarLoc = getLexer().getLoc();
1660
1661 // Consume the dollar sign, and check for a following identifier.
1662 Lex();
1663 if (Lexer.isNot(AsmToken::Identifier))
1664 return true;
1665
1666 // We have a '$' followed by an identifier, make sure they are adjacent.
1667 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1668 return true;
1669
1670 // Construct the joined identifier and consume the token.
1671 Res = StringRef(DollarLoc.getPointer(),
1672 getTok().getIdentifier().size() + 1);
1673 Lex();
1674 return false;
1675 }
1676
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001677 if (Lexer.isNot(AsmToken::Identifier) &&
1678 Lexer.isNot(AsmToken::String))
1679 return true;
1680
Sean Callanan18b83232010-01-19 21:44:56 +00001681 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001682
Sean Callanan79ed1a82010-01-19 20:22:31 +00001683 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001684
1685 return false;
1686}
1687
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001688/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001689/// ::= .equ identifier ',' expression
1690/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001691/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001692bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001693 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001694
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001695 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001696 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001697
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001698 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001699 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001700 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001701
Nico Weber4c4c7322011-01-28 03:04:41 +00001702 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001703}
1704
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001705bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001707
1708 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001709 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001710 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1711 if (Str[i] != '\\') {
1712 Data += Str[i];
1713 continue;
1714 }
1715
1716 // Recognize escaped characters. Note that this escape semantics currently
1717 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1718 ++i;
1719 if (i == e)
1720 return TokError("unexpected backslash at end of string");
1721
1722 // Recognize octal sequences.
1723 if ((unsigned) (Str[i] - '0') <= 7) {
1724 // Consume up to three octal characters.
1725 unsigned Value = Str[i] - '0';
1726
1727 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1728 ++i;
1729 Value = Value * 8 + (Str[i] - '0');
1730
1731 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1732 ++i;
1733 Value = Value * 8 + (Str[i] - '0');
1734 }
1735 }
1736
1737 if (Value > 255)
1738 return TokError("invalid octal escape sequence (out of range)");
1739
1740 Data += (unsigned char) Value;
1741 continue;
1742 }
1743
1744 // Otherwise recognize individual escapes.
1745 switch (Str[i]) {
1746 default:
1747 // Just reject invalid escape sequences for now.
1748 return TokError("invalid escape sequence (unrecognized character)");
1749
1750 case 'b': Data += '\b'; break;
1751 case 'f': Data += '\f'; break;
1752 case 'n': Data += '\n'; break;
1753 case 'r': Data += '\r'; break;
1754 case 't': Data += '\t'; break;
1755 case '"': Data += '"'; break;
1756 case '\\': Data += '\\'; break;
1757 }
1758 }
1759
1760 return false;
1761}
1762
Daniel Dunbara0d14262009-06-24 23:30:00 +00001763/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001764/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1765bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001766 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001767 CheckForValidSection();
1768
Daniel Dunbara0d14262009-06-24 23:30:00 +00001769 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001770 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001771 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001772
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001773 std::string Data;
1774 if (ParseEscapedString(Data))
1775 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001776
1777 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001778 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001779 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1780
Sean Callanan79ed1a82010-01-19 20:22:31 +00001781 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001782
1783 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001784 break;
1785
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001786 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001787 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001788 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001789 }
1790 }
1791
Sean Callanan79ed1a82010-01-19 20:22:31 +00001792 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001793 return false;
1794}
1795
1796/// ParseDirectiveValue
1797/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1798bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001799 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001800 CheckForValidSection();
1801
Daniel Dunbara0d14262009-06-24 23:30:00 +00001802 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001803 const MCExpr *Value;
Jim Grosbach254cf032011-06-29 16:05:14 +00001804 SMLoc ExprLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001805 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001806 return true;
1807
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001808 // Special case constant expressions to match code generator.
Jim Grosbach254cf032011-06-29 16:05:14 +00001809 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
1810 assert(Size <= 8 && "Invalid size");
1811 uint64_t IntValue = MCE->getValue();
1812 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
1813 return Error(ExprLoc, "literal value out of range for directive");
1814 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE);
1815 } else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001816 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001817
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001818 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001819 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001820
Daniel Dunbara0d14262009-06-24 23:30:00 +00001821 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001822 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001823 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001824 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001825 }
1826 }
1827
Sean Callanan79ed1a82010-01-19 20:22:31 +00001828 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001829 return false;
1830}
1831
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001832/// ParseDirectiveRealValue
1833/// ::= (.single | .double) [ expression (, expression)* ]
1834bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1835 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1836 CheckForValidSection();
1837
1838 for (;;) {
1839 // We don't truly support arithmetic on floating point expressions, so we
1840 // have to manually parse unary prefixes.
1841 bool IsNeg = false;
1842 if (getLexer().is(AsmToken::Minus)) {
1843 Lex();
1844 IsNeg = true;
1845 } else if (getLexer().is(AsmToken::Plus))
1846 Lex();
1847
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001848 if (getLexer().isNot(AsmToken::Integer) &&
Kevin Enderby360d8d72011-03-29 21:11:52 +00001849 getLexer().isNot(AsmToken::Real) &&
1850 getLexer().isNot(AsmToken::Identifier))
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001851 return TokError("unexpected token in directive");
1852
1853 // Convert to an APFloat.
1854 APFloat Value(Semantics);
Kevin Enderby360d8d72011-03-29 21:11:52 +00001855 StringRef IDVal = getTok().getString();
1856 if (getLexer().is(AsmToken::Identifier)) {
1857 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf"))
1858 Value = APFloat::getInf(Semantics);
1859 else if (!IDVal.compare_lower("nan"))
1860 Value = APFloat::getNaN(Semantics, false, ~0);
1861 else
1862 return TokError("invalid floating point literal");
1863 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001864 APFloat::opInvalidOp)
1865 return TokError("invalid floating point literal");
1866 if (IsNeg)
1867 Value.changeSign();
1868
1869 // Consume the numeric token.
1870 Lex();
1871
1872 // Emit the value as an integer.
1873 APInt AsInt = Value.bitcastToAPInt();
1874 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1875 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1876
1877 if (getLexer().is(AsmToken::EndOfStatement))
1878 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001879
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001880 if (getLexer().isNot(AsmToken::Comma))
1881 return TokError("unexpected token in directive");
1882 Lex();
1883 }
1884 }
1885
1886 Lex();
1887 return false;
1888}
1889
Daniel Dunbara0d14262009-06-24 23:30:00 +00001890/// ParseDirectiveSpace
1891/// ::= .space expression [ , expression ]
1892bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001893 CheckForValidSection();
1894
Daniel Dunbara0d14262009-06-24 23:30:00 +00001895 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001896 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001897 return true;
1898
1899 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001900 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1901 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001902 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001903 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001904
Daniel Dunbar475839e2009-06-29 20:37:27 +00001905 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001906 return true;
1907
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001908 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001909 return TokError("unexpected token in '.space' directive");
1910 }
1911
Sean Callanan79ed1a82010-01-19 20:22:31 +00001912 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001913
1914 if (NumBytes <= 0)
1915 return TokError("invalid number of bytes in '.space' directive");
1916
1917 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001918 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001919
1920 return false;
1921}
1922
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001923/// ParseDirectiveZero
1924/// ::= .zero expression
1925bool AsmParser::ParseDirectiveZero() {
1926 CheckForValidSection();
1927
1928 int64_t NumBytes;
1929 if (ParseAbsoluteExpression(NumBytes))
1930 return true;
1931
Rafael Espindolae452b172010-10-05 19:42:57 +00001932 int64_t Val = 0;
1933 if (getLexer().is(AsmToken::Comma)) {
1934 Lex();
1935 if (ParseAbsoluteExpression(Val))
1936 return true;
1937 }
1938
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001939 if (getLexer().isNot(AsmToken::EndOfStatement))
1940 return TokError("unexpected token in '.zero' directive");
1941
1942 Lex();
1943
Rafael Espindolae452b172010-10-05 19:42:57 +00001944 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001945
1946 return false;
1947}
1948
Daniel Dunbara0d14262009-06-24 23:30:00 +00001949/// ParseDirectiveFill
1950/// ::= .fill expression , expression , expression
1951bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001952 CheckForValidSection();
1953
Daniel Dunbara0d14262009-06-24 23:30:00 +00001954 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001955 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001956 return true;
1957
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001958 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001959 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001960 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001961
Daniel Dunbara0d14262009-06-24 23:30:00 +00001962 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001963 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001964 return true;
1965
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001966 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001967 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001968 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001969
Daniel Dunbara0d14262009-06-24 23:30:00 +00001970 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001971 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001972 return true;
1973
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001974 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001975 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001976
Sean Callanan79ed1a82010-01-19 20:22:31 +00001977 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001978
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001979 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1980 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001981
1982 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001983 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001984
1985 return false;
1986}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001987
1988/// ParseDirectiveOrg
1989/// ::= .org expression [ , expression ]
1990bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001991 CheckForValidSection();
1992
Daniel Dunbar821e3332009-08-31 08:09:28 +00001993 const MCExpr *Offset;
Jim Grosbachebd4c052012-01-27 00:37:08 +00001994 SMLoc Loc = getTok().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001995 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001996 return true;
1997
1998 // Parse optional fill expression.
1999 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002000 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2001 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002002 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002003 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002004
Daniel Dunbar475839e2009-06-29 20:37:27 +00002005 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002006 return true;
2007
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002008 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00002009 return TokError("unexpected token in '.org' directive");
2010 }
2011
Sean Callanan79ed1a82010-01-19 20:22:31 +00002012 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00002013
Jim Grosbachebd4c052012-01-27 00:37:08 +00002014 // Only limited forms of relocatable expressions are accepted here, it
2015 // has to be relative to the current section. The streamer will return
2016 // 'true' if the expression wasn't evaluatable.
2017 if (getStreamer().EmitValueToOffset(Offset, FillExpr))
2018 return Error(Loc, "expected assembly-time absolute expression");
Daniel Dunbarc238b582009-06-25 22:44:51 +00002019
2020 return false;
2021}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002022
2023/// ParseDirectiveAlign
2024/// ::= {.align, ...} expression [ , expression [ , expression ]]
2025bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002026 CheckForValidSection();
2027
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002028 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002029 int64_t Alignment;
2030 if (ParseAbsoluteExpression(Alignment))
2031 return true;
2032
2033 SMLoc MaxBytesLoc;
2034 bool HasFillExpr = false;
2035 int64_t FillExpr = 0;
2036 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002037 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2038 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002039 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002040 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002041
2042 // The fill expression can be omitted while specifying a maximum number of
2043 // alignment bytes, e.g:
2044 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002045 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002046 HasFillExpr = true;
2047 if (ParseAbsoluteExpression(FillExpr))
2048 return true;
2049 }
2050
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002051 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2052 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002053 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002054 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002055
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002056 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002057 if (ParseAbsoluteExpression(MaxBytesToFill))
2058 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002059
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002060 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002061 return TokError("unexpected token in directive");
2062 }
2063 }
2064
Sean Callanan79ed1a82010-01-19 20:22:31 +00002065 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002066
Daniel Dunbar648ac512010-05-17 21:54:30 +00002067 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002068 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002069
2070 // Compute alignment in bytes.
2071 if (IsPow2) {
2072 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002073 if (Alignment >= 32) {
2074 Error(AlignmentLoc, "invalid alignment value");
2075 Alignment = 31;
2076 }
2077
Benjamin Kramer12fd7672009-09-06 09:35:10 +00002078 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002079 }
2080
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002081 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002082 if (MaxBytesLoc.isValid()) {
2083 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00002084 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
2085 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00002086 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002087 }
2088
2089 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00002090 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
2091 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002092 MaxBytesToFill = 0;
2093 }
2094 }
2095
Daniel Dunbar648ac512010-05-17 21:54:30 +00002096 // Check whether we should use optimal code alignment for this .align
2097 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00002098 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00002099 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
2100 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002101 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002102 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00002103 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00002104 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
2105 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00002106 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00002107
2108 return false;
2109}
2110
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002111/// ParseDirectiveSymbolAttribute
2112/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00002113bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002114 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002115 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002116 StringRef Name;
Jim Grosbach10ec6502011-09-15 17:56:49 +00002117 SMLoc Loc = getTok().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002118
2119 if (ParseIdentifier(Name))
Jim Grosbach10ec6502011-09-15 17:56:49 +00002120 return Error(Loc, "expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002121
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002122 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002123
Jim Grosbach10ec6502011-09-15 17:56:49 +00002124 // Assembler local symbols don't make any sense here. Complain loudly.
2125 if (Sym->isTemporary())
2126 return Error(Loc, "non-local symbol required in directive");
2127
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002128 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002129
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002130 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002131 break;
2132
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002133 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002134 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002135 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002136 }
2137 }
2138
Sean Callanan79ed1a82010-01-19 20:22:31 +00002139 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00002140 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00002141}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002142
2143/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00002144/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
2145bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00002146 CheckForValidSection();
2147
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002148 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00002149 StringRef Name;
2150 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002151 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002152
Daniel Dunbar76c4d762009-07-31 21:55:09 +00002153 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00002154 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002155
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002156 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002157 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002158 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002159
2160 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002161 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002162 if (ParseAbsoluteExpression(Size))
2163 return true;
2164
2165 int64_t Pow2Alignment = 0;
2166 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002167 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00002168 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002169 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002170 if (ParseAbsoluteExpression(Pow2Alignment))
2171 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002172
Chris Lattner258281d2010-01-19 06:22:22 +00002173 // If this target takes alignments in bytes (not log) validate and convert.
2174 if (Lexer.getMAI().getAlignmentIsInBytes()) {
2175 if (!isPowerOf2_64(Pow2Alignment))
2176 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
2177 Pow2Alignment = Log2_64(Pow2Alignment);
2178 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002179 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002180
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002181 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00002182 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002183
Sean Callanan79ed1a82010-01-19 20:22:31 +00002184 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002185
Chris Lattner1fc3d752009-07-09 17:25:12 +00002186 // NOTE: a size of zero for a .comm should create a undefined symbol
2187 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002188 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002189 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
2190 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002191
Eric Christopherc260a3e2010-05-14 01:38:54 +00002192 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002193 // may internally end up wanting an alignment in bytes.
2194 // FIXME: Diagnose overflow.
2195 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00002196 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
2197 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002198
Daniel Dunbar8906ff12009-08-22 07:22:36 +00002199 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002200 return Error(IDLoc, "invalid symbol redefinition");
2201
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002202 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00002203 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002204 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002205 getStreamer().EmitZerofill(Ctx.getMachOSection(
2206 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
2207 0, SectionKind::getBSS()),
2208 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00002209 return false;
2210 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002211
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002212 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00002213 return false;
2214}
Chris Lattner9be3fee2009-07-10 22:20:30 +00002215
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002216/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002217/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002218bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002219 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002220 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002221
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002222 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002223 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002224 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002225
Sean Callanan79ed1a82010-01-19 20:22:31 +00002226 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002227
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00002228 if (Str.empty())
2229 Error(Loc, ".abort detected. Assembly stopping.");
2230 else
2231 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00002232 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00002233
2234 return false;
2235}
Kevin Enderby71148242009-07-14 21:35:03 +00002236
Kevin Enderby1f049b22009-07-14 23:21:55 +00002237/// ParseDirectiveInclude
2238/// ::= .include "filename"
2239bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002240 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002241 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002242
Sean Callanan18b83232010-01-19 21:44:56 +00002243 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002244 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002245 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00002246
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002247 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00002248 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002249
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002250 // Strip the quotes.
2251 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002252
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002253 // Attempt to switch the lexer to the included file before consuming the end
2254 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00002255 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00002256 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00002257 return true;
2258 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00002259
2260 return false;
2261}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002262
Kevin Enderbyc55acca2011-12-14 21:47:48 +00002263/// ParseDirectiveIncbin
2264/// ::= .incbin "filename"
2265bool AsmParser::ParseDirectiveIncbin() {
2266 if (getLexer().isNot(AsmToken::String))
2267 return TokError("expected string in '.incbin' directive");
2268
2269 std::string Filename = getTok().getString();
2270 SMLoc IncbinLoc = getLexer().getLoc();
2271 Lex();
2272
2273 if (getLexer().isNot(AsmToken::EndOfStatement))
2274 return TokError("unexpected token in '.incbin' directive");
2275
2276 // Strip the quotes.
2277 Filename = Filename.substr(1, Filename.size()-2);
2278
2279 // Attempt to process the included file.
2280 if (ProcessIncbinFile(Filename)) {
2281 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
2282 return true;
2283 }
2284
2285 return false;
2286}
2287
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002288/// ParseDirectiveIf
2289/// ::= .if expression
2290bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002291 TheCondStack.push_back(TheCondState);
2292 TheCondState.TheCond = AsmCond::IfCond;
2293 if(TheCondState.Ignore) {
2294 EatToEndOfStatement();
2295 }
2296 else {
2297 int64_t ExprValue;
2298 if (ParseAbsoluteExpression(ExprValue))
2299 return true;
2300
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002301 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002302 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002303
Sean Callanan79ed1a82010-01-19 20:22:31 +00002304 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002305
2306 TheCondState.CondMet = ExprValue;
2307 TheCondState.Ignore = !TheCondState.CondMet;
2308 }
2309
2310 return false;
2311}
2312
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00002313bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
2314 StringRef Name;
2315 TheCondStack.push_back(TheCondState);
2316 TheCondState.TheCond = AsmCond::IfCond;
2317
2318 if (TheCondState.Ignore) {
2319 EatToEndOfStatement();
2320 } else {
2321 if (ParseIdentifier(Name))
2322 return TokError("expected identifier after '.ifdef'");
2323
2324 Lex();
2325
2326 MCSymbol *Sym = getContext().LookupSymbol(Name);
2327
2328 if (expect_defined)
2329 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
2330 else
2331 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
2332 TheCondState.Ignore = !TheCondState.CondMet;
2333 }
2334
2335 return false;
2336}
2337
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002338/// ParseDirectiveElseIf
2339/// ::= .elseif expression
2340bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2341 if (TheCondState.TheCond != AsmCond::IfCond &&
2342 TheCondState.TheCond != AsmCond::ElseIfCond)
2343 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2344 " an .elseif");
2345 TheCondState.TheCond = AsmCond::ElseIfCond;
2346
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002347 bool LastIgnoreState = false;
2348 if (!TheCondStack.empty())
2349 LastIgnoreState = TheCondStack.back().Ignore;
2350 if (LastIgnoreState || TheCondState.CondMet) {
2351 TheCondState.Ignore = true;
2352 EatToEndOfStatement();
2353 }
2354 else {
2355 int64_t ExprValue;
2356 if (ParseAbsoluteExpression(ExprValue))
2357 return true;
2358
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002359 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002360 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002361
Sean Callanan79ed1a82010-01-19 20:22:31 +00002362 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002363 TheCondState.CondMet = ExprValue;
2364 TheCondState.Ignore = !TheCondState.CondMet;
2365 }
2366
2367 return false;
2368}
2369
2370/// ParseDirectiveElse
2371/// ::= .else
2372bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002373 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002374 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002375
Sean Callanan79ed1a82010-01-19 20:22:31 +00002376 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002377
2378 if (TheCondState.TheCond != AsmCond::IfCond &&
2379 TheCondState.TheCond != AsmCond::ElseIfCond)
2380 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2381 ".elseif");
2382 TheCondState.TheCond = AsmCond::ElseCond;
2383 bool LastIgnoreState = false;
2384 if (!TheCondStack.empty())
2385 LastIgnoreState = TheCondStack.back().Ignore;
2386 if (LastIgnoreState || TheCondState.CondMet)
2387 TheCondState.Ignore = true;
2388 else
2389 TheCondState.Ignore = false;
2390
2391 return false;
2392}
2393
2394/// ParseDirectiveEndIf
2395/// ::= .endif
2396bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002397 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002398 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002399
Sean Callanan79ed1a82010-01-19 20:22:31 +00002400 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002401
2402 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2403 TheCondStack.empty())
2404 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2405 ".else");
2406 if (!TheCondStack.empty()) {
2407 TheCondState = TheCondStack.back();
2408 TheCondStack.pop_back();
2409 }
2410
2411 return false;
2412}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002413
2414/// ParseDirectiveFile
Nick Lewycky44d798d2011-10-17 23:05:28 +00002415/// ::= .file [number] filename
2416/// ::= .file number directory filename
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002417bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002418 // FIXME: I'm not sure what this is.
2419 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002420 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002421 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002422 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002423 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002424
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002425 if (FileNumber < 1)
2426 return TokError("file number less than one");
2427 }
2428
Daniel Dunbareceec052010-07-12 17:45:27 +00002429 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002430 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002431
Nick Lewycky44d798d2011-10-17 23:05:28 +00002432 // Usually the directory and filename together, otherwise just the directory.
2433 StringRef Path = getTok().getString();
2434 Path = Path.substr(1, Path.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002435 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002436
Nick Lewycky44d798d2011-10-17 23:05:28 +00002437 StringRef Directory;
2438 StringRef Filename;
2439 if (getLexer().is(AsmToken::String)) {
2440 if (FileNumber == -1)
2441 return TokError("explicit path specified, but no file number");
2442 Filename = getTok().getString();
2443 Filename = Filename.substr(1, Filename.size()-2);
2444 Directory = Path;
2445 Lex();
2446 } else {
2447 Filename = Path;
2448 }
2449
Daniel Dunbareceec052010-07-12 17:45:27 +00002450 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002451 return TokError("unexpected token in '.file' directive");
2452
Chris Lattnerd32e8032010-01-25 19:02:58 +00002453 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002454 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002455 else {
Kevin Enderby8704b782012-01-11 18:04:47 +00002456 if (getContext().getGenDwarfForAssembly() == true)
2457 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is "
2458 "used to generate dwarf debug info for assembly code");
2459
Nick Lewycky44d798d2011-10-17 23:05:28 +00002460 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002461 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002462 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002463
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002464 return false;
2465}
2466
2467/// ParseDirectiveLine
2468/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002469bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002470 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2471 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002472 return TokError("unexpected token in '.line' directive");
2473
Sean Callanan18b83232010-01-19 21:44:56 +00002474 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002475 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002476 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002477
2478 // FIXME: Do something with the .line.
2479 }
2480
Daniel Dunbareceec052010-07-12 17:45:27 +00002481 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002482 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002483
2484 return false;
2485}
2486
2487
2488/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002489/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002490/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2491/// The first number is a file number, must have been previously assigned with
2492/// a .file directive, the second number is the line number and optionally the
2493/// third number is a column position (zero if not specified). The remaining
2494/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002495bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002496
Daniel Dunbareceec052010-07-12 17:45:27 +00002497 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002498 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002499 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002500 if (FileNumber < 1)
2501 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002502 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002503 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002504 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002505
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002506 int64_t LineNumber = 0;
2507 if (getLexer().is(AsmToken::Integer)) {
2508 LineNumber = getTok().getIntVal();
2509 if (LineNumber < 1)
2510 return TokError("line number less than one in '.loc' directive");
2511 Lex();
2512 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002513
2514 int64_t ColumnPos = 0;
2515 if (getLexer().is(AsmToken::Integer)) {
2516 ColumnPos = getTok().getIntVal();
2517 if (ColumnPos < 0)
2518 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002519 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002520 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002521
Kevin Enderbyc0957932010-09-30 16:52:03 +00002522 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002523 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002524 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002525 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2526 for (;;) {
2527 if (getLexer().is(AsmToken::EndOfStatement))
2528 break;
2529
2530 StringRef Name;
2531 SMLoc Loc = getTok().getLoc();
2532 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002533 return TokError("unexpected token in '.loc' directive");
2534
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002535 if (Name == "basic_block")
2536 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2537 else if (Name == "prologue_end")
2538 Flags |= DWARF2_FLAG_PROLOGUE_END;
2539 else if (Name == "epilogue_begin")
2540 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2541 else if (Name == "is_stmt") {
2542 SMLoc Loc = getTok().getLoc();
2543 const MCExpr *Value;
2544 if (getParser().ParseExpression(Value))
2545 return true;
2546 // The expression must be the constant 0 or 1.
2547 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2548 int Value = MCE->getValue();
2549 if (Value == 0)
2550 Flags &= ~DWARF2_FLAG_IS_STMT;
2551 else if (Value == 1)
2552 Flags |= DWARF2_FLAG_IS_STMT;
2553 else
2554 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002555 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002556 else {
2557 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2558 }
2559 }
2560 else if (Name == "isa") {
2561 SMLoc Loc = getTok().getLoc();
2562 const MCExpr *Value;
2563 if (getParser().ParseExpression(Value))
2564 return true;
2565 // The expression must be a constant greater or equal to 0.
2566 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2567 int Value = MCE->getValue();
2568 if (Value < 0)
2569 return Error(Loc, "isa number less than zero");
2570 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002571 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002572 else {
2573 return Error(Loc, "isa number not a constant value");
2574 }
2575 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002576 else if (Name == "discriminator") {
2577 if (getParser().ParseAbsoluteExpression(Discriminator))
2578 return true;
2579 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002580 else {
2581 return Error(Loc, "unknown sub-directive in '.loc' directive");
2582 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002583
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002584 if (getLexer().is(AsmToken::EndOfStatement))
2585 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002586 }
2587 }
2588
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002589 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
Devang Patel3f3bf932011-04-18 20:26:49 +00002590 Isa, Discriminator, StringRef());
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002591
2592 return false;
2593}
2594
Daniel Dunbar138abae2010-10-16 04:56:42 +00002595/// ParseDirectiveStabs
2596/// ::= .stabs string, number, number, number
2597bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2598 SMLoc DirectiveLoc) {
2599 return TokError("unsupported directive '" + Directive + "'");
2600}
2601
Rafael Espindolaf9efd832011-05-10 01:10:18 +00002602/// ParseDirectiveCFISections
2603/// ::= .cfi_sections section [, section]
2604bool GenericAsmParser::ParseDirectiveCFISections(StringRef,
2605 SMLoc DirectiveLoc) {
2606 StringRef Name;
2607 bool EH = false;
2608 bool Debug = false;
2609
2610 if (getParser().ParseIdentifier(Name))
2611 return TokError("Expected an identifier");
2612
2613 if (Name == ".eh_frame")
2614 EH = true;
2615 else if (Name == ".debug_frame")
2616 Debug = true;
2617
2618 if (getLexer().is(AsmToken::Comma)) {
2619 Lex();
2620
2621 if (getParser().ParseIdentifier(Name))
2622 return TokError("Expected an identifier");
2623
2624 if (Name == ".eh_frame")
2625 EH = true;
2626 else if (Name == ".debug_frame")
2627 Debug = true;
2628 }
2629
2630 getStreamer().EmitCFISections(EH, Debug);
2631
2632 return false;
2633}
2634
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002635/// ParseDirectiveCFIStartProc
2636/// ::= .cfi_startproc
2637bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2638 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002639 getStreamer().EmitCFIStartProc();
2640 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002641}
2642
2643/// ParseDirectiveCFIEndProc
2644/// ::= .cfi_endproc
2645bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002646 getStreamer().EmitCFIEndProc();
2647 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002648}
2649
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002650/// ParseRegisterOrRegisterNumber - parse register name or number.
2651bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2652 SMLoc DirectiveLoc) {
2653 unsigned RegNo;
2654
Jim Grosbach6f888a82011-06-02 17:14:04 +00002655 if (getLexer().isNot(AsmToken::Integer)) {
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002656 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2657 DirectiveLoc))
2658 return true;
Evan Cheng0e6a0522011-07-18 20:57:22 +00002659 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true);
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002660 } else
2661 return getParser().ParseAbsoluteExpression(Register);
Jim Grosbachde2f5f42011-02-11 19:05:56 +00002662
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002663 return false;
2664}
2665
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002666/// ParseDirectiveCFIDefCfa
2667/// ::= .cfi_def_cfa register, offset
2668bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2669 SMLoc DirectiveLoc) {
2670 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002671 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002672 return true;
2673
2674 if (getLexer().isNot(AsmToken::Comma))
2675 return TokError("unexpected token in directive");
2676 Lex();
2677
2678 int64_t Offset = 0;
2679 if (getParser().ParseAbsoluteExpression(Offset))
2680 return true;
2681
Rafael Espindola066c2f42011-04-12 23:59:07 +00002682 getStreamer().EmitCFIDefCfa(Register, Offset);
2683 return false;
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002684}
2685
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002686/// ParseDirectiveCFIDefCfaOffset
2687/// ::= .cfi_def_cfa_offset offset
2688bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2689 SMLoc DirectiveLoc) {
2690 int64_t Offset = 0;
2691 if (getParser().ParseAbsoluteExpression(Offset))
2692 return true;
2693
Rafael Espindola066c2f42011-04-12 23:59:07 +00002694 getStreamer().EmitCFIDefCfaOffset(Offset);
2695 return false;
Rafael Espindola53abbe52011-04-11 20:29:16 +00002696}
2697
2698/// ParseDirectiveCFIAdjustCfaOffset
2699/// ::= .cfi_adjust_cfa_offset adjustment
2700bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef,
2701 SMLoc DirectiveLoc) {
2702 int64_t Adjustment = 0;
2703 if (getParser().ParseAbsoluteExpression(Adjustment))
2704 return true;
2705
Rafael Espindola5d7dcd32011-04-12 18:53:30 +00002706 getStreamer().EmitCFIAdjustCfaOffset(Adjustment);
2707 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002708}
2709
2710/// ParseDirectiveCFIDefCfaRegister
2711/// ::= .cfi_def_cfa_register register
2712bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2713 SMLoc DirectiveLoc) {
2714 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002715 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002716 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002717
Rafael Espindola066c2f42011-04-12 23:59:07 +00002718 getStreamer().EmitCFIDefCfaRegister(Register);
2719 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002720}
2721
2722/// ParseDirectiveCFIOffset
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002723/// ::= .cfi_offset register, offset
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002724bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2725 int64_t Register = 0;
2726 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002727
2728 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002729 return true;
2730
2731 if (getLexer().isNot(AsmToken::Comma))
2732 return TokError("unexpected token in directive");
2733 Lex();
2734
2735 if (getParser().ParseAbsoluteExpression(Offset))
2736 return true;
2737
Rafael Espindola066c2f42011-04-12 23:59:07 +00002738 getStreamer().EmitCFIOffset(Register, Offset);
2739 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002740}
2741
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002742/// ParseDirectiveCFIRelOffset
2743/// ::= .cfi_rel_offset register, offset
2744bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef,
2745 SMLoc DirectiveLoc) {
2746 int64_t Register = 0;
2747
2748 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2749 return true;
2750
2751 if (getLexer().isNot(AsmToken::Comma))
2752 return TokError("unexpected token in directive");
2753 Lex();
2754
2755 int64_t Offset = 0;
2756 if (getParser().ParseAbsoluteExpression(Offset))
2757 return true;
2758
Rafael Espindola25f492e2011-04-12 16:12:03 +00002759 getStreamer().EmitCFIRelOffset(Register, Offset);
2760 return false;
Rafael Espindolaa61842b2011-04-11 21:49:50 +00002761}
2762
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002763static bool isValidEncoding(int64_t Encoding) {
2764 if (Encoding & ~0xff)
2765 return false;
2766
2767 if (Encoding == dwarf::DW_EH_PE_omit)
2768 return true;
2769
2770 const unsigned Format = Encoding & 0xf;
2771 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2772 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2773 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2774 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2775 return false;
2776
Rafael Espindolacaf11582010-12-29 04:31:26 +00002777 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002778 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002779 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002780 return false;
2781
2782 return true;
2783}
2784
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002785/// ParseDirectiveCFIPersonalityOrLsda
2786/// ::= .cfi_personality encoding, [symbol_name]
2787/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002788bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002789 SMLoc DirectiveLoc) {
2790 int64_t Encoding = 0;
2791 if (getParser().ParseAbsoluteExpression(Encoding))
2792 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002793 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002794 return false;
2795
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002796 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002797 return TokError("unsupported encoding.");
2798
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002799 if (getLexer().isNot(AsmToken::Comma))
2800 return TokError("unexpected token in directive");
2801 Lex();
2802
2803 StringRef Name;
2804 if (getParser().ParseIdentifier(Name))
2805 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002806
2807 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2808
2809 if (IDVal == ".cfi_personality")
Rafael Espindola066c2f42011-04-12 23:59:07 +00002810 getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002811 else {
2812 assert(IDVal == ".cfi_lsda");
Rafael Espindola066c2f42011-04-12 23:59:07 +00002813 getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002814 }
Rafael Espindola066c2f42011-04-12 23:59:07 +00002815 return false;
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002816}
2817
Rafael Espindolafe024d02010-12-28 18:36:23 +00002818/// ParseDirectiveCFIRememberState
2819/// ::= .cfi_remember_state
2820bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2821 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002822 getStreamer().EmitCFIRememberState();
2823 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002824}
2825
2826/// ParseDirectiveCFIRestoreState
2827/// ::= .cfi_remember_state
2828bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2829 SMLoc DirectiveLoc) {
Rafael Espindola066c2f42011-04-12 23:59:07 +00002830 getStreamer().EmitCFIRestoreState();
2831 return false;
Rafael Espindolafe024d02010-12-28 18:36:23 +00002832}
2833
Rafael Espindolac5754392011-04-12 15:31:05 +00002834/// ParseDirectiveCFISameValue
2835/// ::= .cfi_same_value register
2836bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal,
2837 SMLoc DirectiveLoc) {
2838 int64_t Register = 0;
2839
2840 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2841 return true;
2842
2843 getStreamer().EmitCFISameValue(Register);
2844
2845 return false;
2846}
2847
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00002848/// ParseDirectiveCFIRestore
2849/// ::= .cfi_restore register
2850bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal,
2851 SMLoc DirectiveLoc) {
2852 int64_t Register = 0;
2853 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
2854 return true;
2855
2856 getStreamer().EmitCFIRestore(Register);
2857
2858 return false;
2859}
2860
Rafael Espindola6f0b1812011-12-29 20:24:47 +00002861/// ParseDirectiveCFIEscape
2862/// ::= .cfi_escape expression[,...]
2863bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal,
2864 SMLoc DirectiveLoc) {
2865 std::string Values;
2866 int64_t CurrValue;
2867 if (getParser().ParseAbsoluteExpression(CurrValue))
2868 return true;
2869
2870 Values.push_back((uint8_t)CurrValue);
2871
2872 while (getLexer().is(AsmToken::Comma)) {
2873 Lex();
2874
2875 if (getParser().ParseAbsoluteExpression(CurrValue))
2876 return true;
2877
2878 Values.push_back((uint8_t)CurrValue);
2879 }
2880
2881 getStreamer().EmitCFIEscape(Values);
2882 return false;
2883}
2884
Rafael Espindola16d7d432012-01-23 21:51:52 +00002885/// ParseDirectiveCFISignalFrame
2886/// ::= .cfi_signal_frame
2887bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive,
2888 SMLoc DirectiveLoc) {
2889 if (getLexer().isNot(AsmToken::EndOfStatement))
2890 return Error(getLexer().getLoc(),
2891 "unexpected token in '" + Directive + "' directive");
2892
2893 getStreamer().EmitCFISignalFrame();
2894
2895 return false;
2896}
2897
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002898/// ParseDirectiveMacrosOnOff
2899/// ::= .macros_on
2900/// ::= .macros_off
2901bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2902 SMLoc DirectiveLoc) {
2903 if (getLexer().isNot(AsmToken::EndOfStatement))
2904 return Error(getLexer().getLoc(),
2905 "unexpected token in '" + Directive + "' directive");
2906
2907 getParser().MacrosEnabled = Directive == ".macros_on";
2908
2909 return false;
2910}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002911
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002912/// ParseDirectiveMacro
Rafael Espindola65366442011-06-05 02:43:45 +00002913/// ::= .macro name [parameters]
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002914bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2915 SMLoc DirectiveLoc) {
2916 StringRef Name;
2917 if (getParser().ParseIdentifier(Name))
2918 return TokError("expected identifier in directive");
2919
Rafael Espindola65366442011-06-05 02:43:45 +00002920 std::vector<StringRef> Parameters;
2921 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2922 for(;;) {
2923 StringRef Parameter;
2924 if (getParser().ParseIdentifier(Parameter))
2925 return TokError("expected identifier in directive");
2926 Parameters.push_back(Parameter);
2927
2928 if (getLexer().isNot(AsmToken::Comma))
2929 break;
2930 Lex();
2931 }
2932 }
2933
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002934 if (getLexer().isNot(AsmToken::EndOfStatement))
2935 return TokError("unexpected token in '.macro' directive");
2936
2937 // Eat the end of statement.
2938 Lex();
2939
2940 AsmToken EndToken, StartToken = getTok();
2941
2942 // Lex the macro definition.
2943 for (;;) {
2944 // Check whether we have reached the end of the file.
2945 if (getLexer().is(AsmToken::Eof))
2946 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2947
2948 // Otherwise, check whether we have reach the .endmacro.
2949 if (getLexer().is(AsmToken::Identifier) &&
2950 (getTok().getIdentifier() == ".endm" ||
2951 getTok().getIdentifier() == ".endmacro")) {
2952 EndToken = getTok();
2953 Lex();
2954 if (getLexer().isNot(AsmToken::EndOfStatement))
2955 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2956 "' directive");
2957 break;
2958 }
2959
2960 // Otherwise, scan til the end of the statement.
2961 getParser().EatToEndOfStatement();
2962 }
2963
2964 if (getParser().MacroMap.lookup(Name)) {
2965 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2966 }
2967
2968 const char *BodyStart = StartToken.getLoc().getPointer();
2969 const char *BodyEnd = EndToken.getLoc().getPointer();
2970 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
Rafael Espindola65366442011-06-05 02:43:45 +00002971 getParser().MacroMap[Name] = new Macro(Name, Body, Parameters);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002972 return false;
2973}
2974
2975/// ParseDirectiveEndMacro
2976/// ::= .endm
2977/// ::= .endmacro
2978bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2979 SMLoc DirectiveLoc) {
2980 if (getLexer().isNot(AsmToken::EndOfStatement))
2981 return TokError("unexpected token in '" + Directive + "' directive");
2982
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002983 // If we are inside a macro instantiation, terminate the current
2984 // instantiation.
2985 if (!getParser().ActiveMacros.empty()) {
2986 getParser().HandleMacroExit();
2987 return false;
2988 }
2989
2990 // Otherwise, this .endmacro is a stray entry in the file; well formed
2991 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002992 return TokError("unexpected '" + Directive + "' in file, "
2993 "no current macro definition");
2994}
2995
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002996bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002997 getParser().CheckForValidSection();
2998
2999 const MCExpr *Value;
3000
3001 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003002 return true;
3003
3004 if (getLexer().isNot(AsmToken::EndOfStatement))
3005 return TokError("unexpected token in directive");
3006
3007 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00003008 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003009 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00003010 getStreamer().EmitULEB128Value(Value);
3011
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00003012 return false;
3013}
3014
3015
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003016/// \brief Create an MCAsmParser instance.
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003017MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM,
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003018 MCContext &C, MCStreamer &Out,
3019 const MCAsmInfo &MAI) {
Jim Grosbach1b84cce2011-08-16 18:33:49 +00003020 return new AsmParser(SM, C, Out, MAI);
Daniel Dunbard1e3b442010-07-17 02:26:10 +00003021}