blob: 80bab43a22a53da4a9a2923852b7a24934eafaff [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"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000021#include "llvm/MC/MCExpr.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000022#include "llvm/MC/MCParser/AsmCond.h"
23#include "llvm/MC/MCParser/AsmLexer.h"
24#include "llvm/MC/MCParser/MCAsmParser.h"
25#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000027#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000028#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000029#include "llvm/MC/MCDwarf.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000030#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000031#include "llvm/Support/SourceMgr.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000032#include "llvm/Support/raw_ostream.h"
Roman Divacky54b0f4f2011-01-27 17:16:37 +000033#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000034#include "llvm/Target/TargetAsmParser.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000035#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000036#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000037using namespace llvm;
38
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000039namespace {
40
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000041/// \brief Helper class for tracking macro definitions.
42struct Macro {
43 StringRef Name;
44 StringRef Body;
45
46public:
47 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
48};
49
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000050/// \brief Helper class for storing information about an active macro
51/// instantiation.
52struct MacroInstantiation {
53 /// The macro being instantiated.
54 const Macro *TheMacro;
55
56 /// The macro instantiation with substitutions.
57 MemoryBuffer *Instantiation;
58
59 /// The location of the instantiation.
60 SMLoc InstantiationLoc;
61
62 /// The location where parsing should resume upon instantiation completion.
63 SMLoc ExitLoc;
64
65public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000066 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
67 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000068};
69
Daniel Dunbaraef87e32010-07-18 18:31:38 +000070/// \brief The concrete assembly parser instance.
71class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000072 friend class GenericAsmParser;
73
Daniel Dunbaraef87e32010-07-18 18:31:38 +000074 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
75 void operator=(const AsmParser &); // DO NOT IMPLEMENT
76private:
77 AsmLexer Lexer;
78 MCContext &Ctx;
79 MCStreamer &Out;
80 SourceMgr &SrcMgr;
81 MCAsmParserExtension *GenericParser;
82 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000083
Daniel Dunbaraef87e32010-07-18 18:31:38 +000084 /// This is the current buffer index we're lexing from as managed by the
85 /// SourceMgr object.
86 int CurBuffer;
87
88 AsmCond TheCondState;
89 std::vector<AsmCond> TheCondStack;
90
91 /// DirectiveMap - This is a table handlers for directives. Each handler is
92 /// invoked after the directive identifier is read and is responsible for
93 /// parsing and validating the rest of the directive. The handler is passed
94 /// in the directive name and the location of the directive keyword.
95 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000096
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000097 /// MacroMap - Map of currently defined macros.
98 StringMap<Macro*> MacroMap;
99
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000100 /// ActiveMacros - Stack of active macro instantiations.
101 std::vector<MacroInstantiation*> ActiveMacros;
102
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000103 /// Boolean tracking whether macro substitution is enabled.
104 unsigned MacrosEnabled : 1;
105
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000106 /// Flag tracking whether any errors have been encountered.
107 unsigned HadError : 1;
108
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000109public:
110 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
111 const MCAsmInfo &MAI);
112 ~AsmParser();
113
114 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
115
116 void AddDirectiveHandler(MCAsmParserExtension *Object,
117 StringRef Directive,
118 DirectiveHandler Handler) {
119 DirectiveMap[Directive] = std::make_pair(Object, Handler);
120 }
121
122public:
123 /// @name MCAsmParser Interface
124 /// {
125
126 virtual SourceMgr &getSourceManager() { return SrcMgr; }
127 virtual MCAsmLexer &getLexer() { return Lexer; }
128 virtual MCContext &getContext() { return Ctx; }
129 virtual MCStreamer &getStreamer() { return Out; }
130
131 virtual void Warning(SMLoc L, const Twine &Meg);
132 virtual bool Error(SMLoc L, const Twine &Msg);
133
134 const AsmToken &Lex();
135
136 bool ParseExpression(const MCExpr *&Res);
137 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
139 virtual bool ParseAbsoluteExpression(int64_t &Res);
140
141 /// }
142
143private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000144 void CheckForValidSection();
145
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000146 bool ParseStatement();
147
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000148 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
149 void HandleMacroExit();
150
151 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000152 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
153 SrcMgr.PrintMessage(Loc, Msg, Type);
154 }
155
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000156 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
157 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000158
159 /// \brief Reset the current lexer position to that given by \arg Loc. The
160 /// current token is not set; clients should ensure Lex() is called
161 /// subsequently.
162 void JumpToLoc(SMLoc Loc);
163
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000164 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000165
166 /// \brief Parse up to the end of statement and a return the contents from the
167 /// current token until the end of the statement; the current token on exit
168 /// will be either the EndOfStatement or EOF.
169 StringRef ParseStringToEndOfStatement();
170
Nico Weber4c4c7322011-01-28 03:04:41 +0000171 bool ParseAssignment(StringRef Name, bool allow_redef);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000172
173 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
175 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
176
177 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
178 /// and set \arg Res to the identifier contents.
179 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000180
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000181 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000182
183 // ".ascii", ".asciiz", ".string"
184 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000186 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000187 bool ParseDirectiveFill(); // ".fill"
188 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000189 bool ParseDirectiveZero(); // ".zero"
Nico Weber4c4c7322011-01-28 03:04:41 +0000190 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); // ".set", ".equ", ".equiv"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000191 bool ParseDirectiveOrg(); // ".org"
192 // ".align{,32}", ".p2align{,w,l}"
193 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
194
195 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
196 /// accepts a single symbol (which should be a label or an external).
197 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000198
199 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
200
201 bool ParseDirectiveAbort(); // ".abort"
202 bool ParseDirectiveInclude(); // ".include"
203
204 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000205 // ".ifdef" or ".ifndef", depending on expect_defined
206 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000207 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
208 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
209 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
210
211 /// ParseEscapedString - Parse the current token as a string which may include
212 /// escaped characters and return the string contents.
213 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000214
215 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
216 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000217};
218
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000219/// \brief Generic implementations of directive handling, etc. which is shared
220/// (or the default, at least) for all assembler parser.
221class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000222 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
223 void AddDirectiveHandler(StringRef Directive) {
224 getParser().AddDirectiveHandler(this, Directive,
225 HandleDirective<GenericAsmParser, Handler>);
226 }
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000227public:
228 GenericAsmParser() {}
229
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000230 AsmParser &getParser() {
231 return (AsmParser&) this->MCAsmParserExtension::getParser();
232 }
233
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000234 virtual void Initialize(MCAsmParser &Parser) {
235 // Call the base implementation.
236 this->MCAsmParserExtension::Initialize(Parser);
237
238 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
241 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000243
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000244 // CFI directives.
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
246 ".cfi_startproc");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
248 ".cfi_endproc");
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfa>(
250 ".cfi_def_cfa");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
252 ".cfi_def_cfa_offset");
253 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
254 ".cfi_def_cfa_register");
255 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
256 ".cfi_offset");
257 AddDirectiveHandler<
258 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
259 AddDirectiveHandler<
260 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
Rafael Espindolafe024d02010-12-28 18:36:23 +0000261 AddDirectiveHandler<
262 &GenericAsmParser::ParseDirectiveCFIRememberState>(".cfi_remember_state");
263 AddDirectiveHandler<
264 &GenericAsmParser::ParseDirectiveCFIRestoreState>(".cfi_restore_state");
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000265
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000266 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
268 ".macros_on");
269 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
270 ".macros_off");
271 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
272 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
273 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000274
275 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
276 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000277 }
278
Roman Divacky54b0f4f2011-01-27 17:16:37 +0000279 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
280
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000281 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
282 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
283 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000284 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000285 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
286 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab40a71f2010-12-29 01:42:56 +0000287 bool ParseDirectiveCFIDefCfa(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000288 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
289 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
290 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
291 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Rafael Espindolafe024d02010-12-28 18:36:23 +0000292 bool ParseDirectiveCFIRememberState(StringRef, SMLoc DirectiveLoc);
293 bool ParseDirectiveCFIRestoreState(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000294
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000295 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000296 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
297 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000298
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000299 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000300};
301
302}
303
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000304namespace llvm {
305
306extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000307extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000308extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000309
310}
311
Chris Lattneraaec2052010-01-19 19:46:13 +0000312enum { DEFAULT_ADDRSPACE = 0 };
313
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000314AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
315 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000316 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000317 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000318 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000319 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000320
321 // Initialize the generic parser.
322 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000323
324 // Initialize the platform / file format parser.
325 //
326 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
327 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000328 if (_MAI.hasMicrosoftFastStdCallMangling()) {
329 PlatformParser = createCOFFAsmParser();
330 PlatformParser->Initialize(*this);
331 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000332 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000333 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000334 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000335 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000336 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000337 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000338}
339
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000340AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000341 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
342
343 // Destroy any macros.
344 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
345 ie = MacroMap.end(); it != ie; ++it)
346 delete it->getValue();
347
Daniel Dunbare4749702010-07-12 18:12:02 +0000348 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000349 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000350}
351
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000352void AsmParser::PrintMacroInstantiations() {
353 // Print the active macro instantiation stack.
354 for (std::vector<MacroInstantiation*>::const_reverse_iterator
355 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
356 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
357 "note");
358}
359
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000360void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000361 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000362 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000363}
364
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000365bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000366 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000367 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000368 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000369 return true;
370}
371
Sean Callananfd0b0282010-01-21 00:19:58 +0000372bool AsmParser::EnterIncludeFile(const std::string &Filename) {
373 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
374 if (NewBuf == -1)
375 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000376
Sean Callananfd0b0282010-01-21 00:19:58 +0000377 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000378
Sean Callananfd0b0282010-01-21 00:19:58 +0000379 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000380
Sean Callananfd0b0282010-01-21 00:19:58 +0000381 return false;
382}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000383
384void AsmParser::JumpToLoc(SMLoc Loc) {
385 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
386 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
387}
388
Sean Callananfd0b0282010-01-21 00:19:58 +0000389const AsmToken &AsmParser::Lex() {
390 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000391
Sean Callananfd0b0282010-01-21 00:19:58 +0000392 if (tok->is(AsmToken::Eof)) {
393 // If this is the end of an included file, pop the parent file off the
394 // include stack.
395 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
396 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000397 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000398 tok = &Lexer.Lex();
399 }
400 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000401
Sean Callananfd0b0282010-01-21 00:19:58 +0000402 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000403 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000404
Sean Callananfd0b0282010-01-21 00:19:58 +0000405 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000406}
407
Chris Lattner79180e22010-04-05 23:15:42 +0000408bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000409 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000410 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000411 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000412
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000413 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000414 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000415
416 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000417 AsmCond StartingCondState = TheCondState;
418
Chris Lattnerb717fb02009-07-02 21:53:43 +0000419 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000420 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000421 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000422
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000423 // We had an error, validate that one was emitted and recover by skipping to
424 // the next line.
425 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000426 EatToEndOfStatement();
427 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000428
429 if (TheCondState.TheCond != StartingCondState.TheCond ||
430 TheCondState.Ignore != StartingCondState.Ignore)
431 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000432
433 // Check to see there are no empty DwarfFile slots.
434 const std::vector<MCDwarfFile *> &MCDwarfFiles =
435 getContext().getMCDwarfFiles();
436 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000437 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000438 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000439 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000440
Chris Lattner79180e22010-04-05 23:15:42 +0000441 // Finalize the output stream if there are no errors and if the client wants
442 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000443 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000444 Out.Finish();
445
Chris Lattnerb717fb02009-07-02 21:53:43 +0000446 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000447}
448
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000449void AsmParser::CheckForValidSection() {
450 if (!getStreamer().getCurrentSection()) {
451 TokError("expected section directive before assembly directive");
452 Out.SwitchSection(Ctx.getMachOSection(
453 "__TEXT", "__text",
454 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
455 0, SectionKind::getText()));
456 }
457}
458
Chris Lattner2cf5f142009-06-22 01:29:09 +0000459/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
460void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000461 while (Lexer.isNot(AsmToken::EndOfStatement) &&
462 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000463 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000464
Chris Lattner2cf5f142009-06-22 01:29:09 +0000465 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000466 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000467 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000468}
469
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000470StringRef AsmParser::ParseStringToEndOfStatement() {
471 const char *Start = getTok().getLoc().getPointer();
472
473 while (Lexer.isNot(AsmToken::EndOfStatement) &&
474 Lexer.isNot(AsmToken::Eof))
475 Lex();
476
477 const char *End = getTok().getLoc().getPointer();
478 return StringRef(Start, End - Start);
479}
Chris Lattnerc4193832009-06-22 05:51:26 +0000480
Chris Lattner74ec1a32009-06-22 06:32:03 +0000481/// ParseParenExpr - Parse a paren expression and return it.
482/// NOTE: This assumes the leading '(' has already been consumed.
483///
484/// parenexpr ::= expr)
485///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000486bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000487 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000488 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000489 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000490 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000491 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000492 return false;
493}
Chris Lattnerc4193832009-06-22 05:51:26 +0000494
Chris Lattner74ec1a32009-06-22 06:32:03 +0000495/// ParsePrimaryExpr - Parse a primary expression and return it.
496/// primaryexpr ::= (parenexpr
497/// primaryexpr ::= symbol
498/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000499/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000500/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000501bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000502 switch (Lexer.getKind()) {
503 default:
504 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000505 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000506 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000507 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000508 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000509 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000510 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000511 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000512 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000513 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000514 EndLoc = Lexer.getLoc();
515
516 StringRef Identifier;
517 if (ParseIdentifier(Identifier))
518 return false;
519
Daniel Dunbarfffff912009-10-16 01:34:54 +0000520 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000521 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000522 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000523
524 // Lookup the symbol variant if used.
525 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000526 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000527 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000528 if (Variant == MCSymbolRefExpr::VK_Invalid) {
529 Variant = MCSymbolRefExpr::VK_None;
Jim Grosbach4121e8a2011-01-19 23:06:07 +0000530 return TokError("invalid variant '" + Split.second + "'");
Daniel Dunbarcceba832010-09-17 02:47:07 +0000531 }
532 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000533
Daniel Dunbarfffff912009-10-16 01:34:54 +0000534 // If this is an absolute variable reference, substitute it now to preserve
535 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000536 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000537 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000538 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000539
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000540 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000541 return false;
542 }
543
544 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000545 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000546 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000547 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000548 case AsmToken::Integer: {
549 SMLoc Loc = getTok().getLoc();
550 int64_t IntVal = getTok().getIntVal();
551 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000552 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000553 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000554 // Look for 'b' or 'f' following an Integer as a directional label
555 if (Lexer.getKind() == AsmToken::Identifier) {
556 StringRef IDVal = getTok().getString();
557 if (IDVal == "f" || IDVal == "b"){
558 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
559 IDVal == "f" ? 1 : 0);
560 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
561 getContext());
562 if(IDVal == "b" && Sym->isUndefined())
563 return Error(Loc, "invalid reference to undefined symbol");
564 EndLoc = Lexer.getLoc();
565 Lex(); // Eat identifier.
566 }
567 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000568 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000569 }
Bill Wendling69c4ef32011-01-25 21:26:41 +0000570 case AsmToken::Real: {
571 APFloat RealVal(APFloat::IEEEdouble, getTok().getString());
Bob Wilson720b9182011-02-03 23:17:47 +0000572 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
Bill Wendling69c4ef32011-01-25 21:26:41 +0000573 Res = MCConstantExpr::Create(IntVal, getContext());
574 Lex(); // Eat token.
575 return false;
576 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000577 case AsmToken::Dot: {
578 // This is a '.' reference, which references the current PC. Emit a
579 // temporary label to the streamer and refer to it.
580 MCSymbol *Sym = Ctx.CreateTempSymbol();
581 Out.EmitLabel(Sym);
582 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
583 EndLoc = Lexer.getLoc();
584 Lex(); // Eat identifier.
585 return false;
586 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000587 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000588 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000589 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000590 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000591 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000592 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000593 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000594 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000595 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000596 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000597 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000598 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000599 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000600 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000601 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000602 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000603 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000604 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000605 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000606 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000607 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000608 }
609}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000610
Chris Lattnerb4307b32010-01-15 19:28:38 +0000611bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000612 SMLoc EndLoc;
613 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000614}
615
Daniel Dunbarcceba832010-09-17 02:47:07 +0000616const MCExpr *
617AsmParser::ApplyModifierToExpr(const MCExpr *E,
618 MCSymbolRefExpr::VariantKind Variant) {
619 // Recurse over the given expression, rebuilding it to apply the given variant
620 // if there is exactly one symbol.
621 switch (E->getKind()) {
622 case MCExpr::Target:
623 case MCExpr::Constant:
624 return 0;
625
626 case MCExpr::SymbolRef: {
627 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
628
629 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
630 TokError("invalid variant on expression '" +
631 getTok().getIdentifier() + "' (already modified)");
632 return E;
633 }
634
635 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
636 }
637
638 case MCExpr::Unary: {
639 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
640 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
641 if (!Sub)
642 return 0;
643 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
644 }
645
646 case MCExpr::Binary: {
647 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
648 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
649 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
650
651 if (!LHS && !RHS)
652 return 0;
653
654 if (!LHS) LHS = BE->getLHS();
655 if (!RHS) RHS = BE->getRHS();
656
657 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
658 }
659 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000660
661 assert(0 && "Invalid expression kind!");
662 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000663}
664
Chris Lattner74ec1a32009-06-22 06:32:03 +0000665/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000666///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000667/// expr ::= expr +,- expr -> lowest.
668/// expr ::= expr |,^,&,! expr -> middle.
669/// expr ::= expr *,/,%,<<,>> expr -> highest.
670/// expr ::= primaryexpr
671///
Chris Lattner54482b42010-01-15 19:39:23 +0000672bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000673 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000674 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000675 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
676 return true;
677
Daniel Dunbarcceba832010-09-17 02:47:07 +0000678 // As a special case, we support 'a op b @ modifier' by rewriting the
679 // expression to include the modifier. This is inefficient, but in general we
680 // expect users to use 'a@modifier op b'.
681 if (Lexer.getKind() == AsmToken::At) {
682 Lex();
683
684 if (Lexer.isNot(AsmToken::Identifier))
685 return TokError("unexpected symbol modifier following '@'");
686
687 MCSymbolRefExpr::VariantKind Variant =
688 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
689 if (Variant == MCSymbolRefExpr::VK_Invalid)
690 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
691
692 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
693 if (!ModifiedRes) {
694 return TokError("invalid modifier '" + getTok().getIdentifier() +
695 "' (no symbols present)");
696 return true;
697 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000698
Daniel Dunbarcceba832010-09-17 02:47:07 +0000699 Res = ModifiedRes;
700 Lex();
701 }
702
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000703 // Try to constant fold it up front, if possible.
704 int64_t Value;
705 if (Res->EvaluateAsAbsolute(Value))
706 Res = MCConstantExpr::Create(Value, getContext());
707
708 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000709}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000710
Chris Lattnerb4307b32010-01-15 19:28:38 +0000711bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000712 Res = 0;
713 return ParseParenExpr(Res, EndLoc) ||
714 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000715}
716
Daniel Dunbar475839e2009-06-29 20:37:27 +0000717bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000718 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000719
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000720 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000721 if (ParseExpression(Expr))
722 return true;
723
Daniel Dunbare00b0112009-10-16 01:57:52 +0000724 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000725 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000726
727 return false;
728}
729
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000730static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000731 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000732 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000733 default:
734 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000735
Daniel Dunbarcceba832010-09-17 02:47:07 +0000736 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000737 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000738 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000739 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000740 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000741 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000742 return 1;
743
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000744
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000745 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000746 //
747 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000748 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000749 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000750 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000751 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000752 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000753 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000754 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000755 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000756 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000757
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000758 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000759 case AsmToken::EqualEqual:
760 Kind = MCBinaryExpr::EQ;
761 return 3;
762 case AsmToken::ExclaimEqual:
763 case AsmToken::LessGreater:
764 Kind = MCBinaryExpr::NE;
765 return 3;
766 case AsmToken::Less:
767 Kind = MCBinaryExpr::LT;
768 return 3;
769 case AsmToken::LessEqual:
770 Kind = MCBinaryExpr::LTE;
771 return 3;
772 case AsmToken::Greater:
773 Kind = MCBinaryExpr::GT;
774 return 3;
775 case AsmToken::GreaterEqual:
776 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000777 return 3;
778
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000779 // High Intermediate Precedence: +, -
780 case AsmToken::Plus:
781 Kind = MCBinaryExpr::Add;
782 return 4;
783 case AsmToken::Minus:
784 Kind = MCBinaryExpr::Sub;
785 return 4;
786
Daniel Dunbar475839e2009-06-29 20:37:27 +0000787 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000788 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000789 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000790 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000791 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000792 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000793 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000794 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000795 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000796 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000797 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000798 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000799 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000800 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000801 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000802 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000803 }
804}
805
806
807/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
808/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000809bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
810 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000811 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000812 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000813 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000814
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000815 // If the next token is lower precedence than we are allowed to eat, return
816 // successfully with what we ate already.
817 if (TokPrec < Precedence)
818 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000819
Sean Callanan79ed1a82010-01-19 20:22:31 +0000820 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000821
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000822 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000823 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000824 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000825
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000826 // If BinOp binds less tightly with RHS than the operator after RHS, let
827 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000828 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000829 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000830 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000831 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000832 }
833
Daniel Dunbar475839e2009-06-29 20:37:27 +0000834 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000835 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000836 }
837}
838
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000839
840
841
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000842/// ParseStatement:
843/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000844/// ::= Label* Directive ...Operands... EndOfStatement
845/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000846bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000847 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000848 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000849 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000850 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000851 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000852
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000853 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000854 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000855 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000856 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000857 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000858 // A full line comment is a '#' as the first token.
859 if (Lexer.is(AsmToken::Hash)) {
860 EatToEndOfStatement();
861 return false;
862 }
863 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000864 if (Lexer.is(AsmToken::Integer)) {
865 LocalLabelVal = getTok().getIntVal();
866 if (LocalLabelVal < 0) {
867 if (!TheCondState.Ignore)
868 return TokError("unexpected token at start of statement");
869 IDVal = "";
870 }
871 else {
872 IDVal = getTok().getString();
873 Lex(); // Consume the integer token to be used as an identifier token.
874 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000875 if (!TheCondState.Ignore)
876 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000877 }
878 }
879 }
880 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000881 if (!TheCondState.Ignore)
882 return TokError("unexpected token at start of statement");
883 IDVal = "";
884 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000885
Chris Lattner7834fac2010-04-17 18:14:27 +0000886 // Handle conditional assembly here before checking for skipping. We
887 // have to do this so that .endif isn't skipped in a ".if 0" block for
888 // example.
889 if (IDVal == ".if")
890 return ParseDirectiveIf(IDLoc);
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +0000891 if (IDVal == ".ifdef")
892 return ParseDirectiveIfdef(IDLoc, true);
893 if (IDVal == ".ifndef" || IDVal == ".ifnotdef")
894 return ParseDirectiveIfdef(IDLoc, false);
Chris Lattner7834fac2010-04-17 18:14:27 +0000895 if (IDVal == ".elseif")
896 return ParseDirectiveElseIf(IDLoc);
897 if (IDVal == ".else")
898 return ParseDirectiveElse(IDLoc);
899 if (IDVal == ".endif")
900 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000901
Chris Lattner7834fac2010-04-17 18:14:27 +0000902 // If we are in a ".if 0" block, ignore this statement.
903 if (TheCondState.Ignore) {
904 EatToEndOfStatement();
905 return false;
906 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000907
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000908 // FIXME: Recurse on local labels?
909
910 // See what kind of statement we have.
911 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000912 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000913 CheckForValidSection();
914
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000915 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000916 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000917
918 // Diagnose attempt to use a variable as a label.
919 //
920 // FIXME: Diagnostics. Note the location of the definition as a label.
921 // FIXME: This doesn't diagnose assignment to a symbol which has been
922 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000923 MCSymbol *Sym;
924 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000925 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000926 else
927 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000928 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000929 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000930
Daniel Dunbar959fd882009-08-26 22:13:22 +0000931 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000932 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000933
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000934 // Consume any end of statement token, if present, to avoid spurious
935 // AddBlankLine calls().
936 if (Lexer.is(AsmToken::EndOfStatement)) {
937 Lex();
938 if (Lexer.is(AsmToken::Eof))
939 return false;
940 }
941
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000942 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000943 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000944
Daniel Dunbar3f872332009-07-28 16:08:33 +0000945 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000946 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000947 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000948
Nico Weber4c4c7322011-01-28 03:04:41 +0000949 return ParseAssignment(IDVal, true);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000950
951 default: // Normal instruction or directive.
952 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000953 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000954
955 // If macros are enabled, check to see if this is a macro instantiation.
956 if (MacrosEnabled)
957 if (const Macro *M = MacroMap.lookup(IDVal))
958 return HandleMacroEntry(IDVal, IDLoc, M);
959
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000960 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000961 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000962 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000963 if (IDVal == ".set" || IDVal == ".equ")
Nico Weber4c4c7322011-01-28 03:04:41 +0000964 return ParseDirectiveSet(IDVal, true);
965 if (IDVal == ".equiv")
966 return ParseDirectiveSet(IDVal, false);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000967
Daniel Dunbara0d14262009-06-24 23:30:00 +0000968 // Data directives
969
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000970 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000971 return ParseDirectiveAscii(IDVal, false);
972 if (IDVal == ".asciz" || IDVal == ".string")
973 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000974
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000975 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000977 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000978 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000979 if (IDVal == ".value")
980 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000981 if (IDVal == ".2byte")
982 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000984 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000985 if (IDVal == ".int")
986 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000987 if (IDVal == ".4byte")
988 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000990 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000991 if (IDVal == ".8byte")
992 return ParseDirectiveValue(8);
Roman Divacky14e66552011-01-28 14:20:32 +0000993 if (IDVal == ".single" || IDVal == ".float")
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000994 return ParseDirectiveRealValue(APFloat::IEEEsingle);
995 if (IDVal == ".double")
996 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000997
Eli Friedman5d68ec22010-07-19 04:17:25 +0000998 if (IDVal == ".align") {
999 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1000 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
1001 }
1002 if (IDVal == ".align32") {
1003 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
1004 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
1005 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001006 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001007 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001008 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001009 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001010 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001011 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001012 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001013 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001014 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001015 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001016 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001017 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
1018
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001019 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +00001020 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001021
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001022 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001023 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001024 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001025 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001026 if (IDVal == ".zero")
1027 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001029 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001030
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001032 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001033 // ELF only? Should it be here?
1034 if (IDVal == ".local")
1035 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001036 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001037 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001038 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001039 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001040 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001041 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001042 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001043 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001044 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001045 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001046 if (IDVal == ".symbol_resolver")
1047 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001048 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001049 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001050 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001051 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001052 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001053 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001054 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001055 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001056 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001057 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001058 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001059 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001060 if (IDVal == ".weak_def_can_be_hidden")
1061 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001062
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001063 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001064 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001065 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001066 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001067
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001068 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001069 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001070 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001071 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001072
Roman Divackybb6d14f2011-01-31 21:19:43 +00001073 if (IDVal == ".code16" || IDVal == ".code32" || IDVal == ".code64")
Roman Divackyf6fbd842011-01-31 20:56:49 +00001074 return TokError(Twine(IDVal) + " not supported yet");
Roman Divackycb727802011-01-28 19:29:48 +00001075
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001076 // Look up the handler in the handler table.
1077 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1078 DirectiveMap.lookup(IDVal);
1079 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001080 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001081
Kevin Enderby9c656452009-09-10 20:51:44 +00001082 // Target hook for parsing target specific directives.
1083 if (!getTargetParser().ParseDirective(ID))
1084 return false;
1085
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001086 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001087 EatToEndOfStatement();
1088 return false;
1089 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001090
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001091 CheckForValidSection();
1092
Chris Lattnera7f13542010-05-19 23:34:33 +00001093 // Canonicalize the opcode to lower case.
1094 SmallString<128> Opcode;
1095 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1096 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001097
Chris Lattner98986712010-01-14 22:21:20 +00001098 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001099 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001100 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001101
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001102 // Dump the parsed representation, if requested.
1103 if (getShowParsedOperands()) {
1104 SmallString<256> Str;
1105 raw_svector_ostream OS(Str);
1106 OS << "parsed instruction: [";
1107 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1108 if (i != 0)
1109 OS << ", ";
1110 ParsedOperands[i]->dump(OS);
1111 }
1112 OS << "]";
1113
1114 PrintMessage(IDLoc, OS.str(), "note");
1115 }
1116
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001117 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001118 if (!HadError)
1119 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1120 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001121
Chris Lattner98986712010-01-14 22:21:20 +00001122 // Free any parsed operands.
1123 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1124 delete ParsedOperands[i];
1125
Chris Lattnercbf8a982010-09-11 16:18:25 +00001126 // Don't skip the rest of the line, the instruction parser is responsible for
1127 // that.
1128 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001129}
Chris Lattner9a023f72009-06-24 04:43:34 +00001130
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001131MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1132 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001133 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1134{
1135 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1136 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001137 SmallString<256> Buf;
1138 raw_svector_ostream OS(Buf);
1139
1140 StringRef Body = M->Body;
1141 while (!Body.empty()) {
1142 // Scan for the next substitution.
1143 std::size_t End = Body.size(), Pos = 0;
1144 for (; Pos != End; ++Pos) {
1145 // Check for a substitution or escape.
1146 if (Body[Pos] != '$' || Pos + 1 == End)
1147 continue;
1148
1149 char Next = Body[Pos + 1];
1150 if (Next == '$' || Next == 'n' || isdigit(Next))
1151 break;
1152 }
1153
1154 // Add the prefix.
1155 OS << Body.slice(0, Pos);
1156
1157 // Check if we reached the end.
1158 if (Pos == End)
1159 break;
1160
1161 switch (Body[Pos+1]) {
1162 // $$ => $
1163 case '$':
1164 OS << '$';
1165 break;
1166
1167 // $n => number of arguments
1168 case 'n':
1169 OS << A.size();
1170 break;
1171
1172 // $[0-9] => argument
1173 default: {
1174 // Missing arguments are ignored.
1175 unsigned Index = Body[Pos+1] - '0';
1176 if (Index >= A.size())
1177 break;
1178
1179 // Otherwise substitute with the token values, with spaces eliminated.
1180 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1181 ie = A[Index].end(); it != ie; ++it)
1182 OS << it->getString();
1183 break;
1184 }
1185 }
1186
1187 // Update the scan point.
1188 Body = Body.substr(Pos + 2);
1189 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001190
1191 // We include the .endmacro in the buffer as our queue to exit the macro
1192 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001193 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001194
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001195 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001196}
1197
1198bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1199 const Macro *M) {
1200 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1201 // this, although we should protect against infinite loops.
1202 if (ActiveMacros.size() == 20)
1203 return TokError("macros cannot be nested more than 20 levels deep");
1204
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001205 // Parse the macro instantiation arguments.
1206 std::vector<std::vector<AsmToken> > MacroArguments;
1207 MacroArguments.push_back(std::vector<AsmToken>());
1208 unsigned ParenLevel = 0;
1209 for (;;) {
1210 if (Lexer.is(AsmToken::Eof))
1211 return TokError("unexpected token in macro instantiation");
1212 if (Lexer.is(AsmToken::EndOfStatement))
1213 break;
1214
1215 // If we aren't inside parentheses and this is a comma, start a new token
1216 // list.
1217 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1218 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001219 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001220 // Adjust the current parentheses level.
1221 if (Lexer.is(AsmToken::LParen))
1222 ++ParenLevel;
1223 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1224 --ParenLevel;
1225
1226 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001227 MacroArguments.back().push_back(getTok());
1228 }
1229 Lex();
1230 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001231
1232 // Create the macro instantiation object and add to the current macro
1233 // instantiation stack.
1234 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001235 getTok().getLoc(),
1236 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001237 ActiveMacros.push_back(MI);
1238
1239 // Jump to the macro instantiation and prime the lexer.
1240 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1241 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1242 Lex();
1243
1244 return false;
1245}
1246
1247void AsmParser::HandleMacroExit() {
1248 // Jump to the EndOfStatement we should return to, and consume it.
1249 JumpToLoc(ActiveMacros.back()->ExitLoc);
1250 Lex();
1251
1252 // Pop the instantiation entry.
1253 delete ActiveMacros.back();
1254 ActiveMacros.pop_back();
1255}
1256
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001257static void MarkUsed(const MCExpr *Value) {
1258 switch (Value->getKind()) {
1259 case MCExpr::Binary:
1260 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1261 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1262 break;
1263 case MCExpr::Target:
1264 case MCExpr::Constant:
1265 break;
1266 case MCExpr::SymbolRef: {
1267 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1268 break;
1269 }
1270 case MCExpr::Unary:
1271 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1272 break;
1273 }
1274}
1275
Nico Weber4c4c7322011-01-28 03:04:41 +00001276bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001277 // FIXME: Use better location, we should use proper tokens.
1278 SMLoc EqualLoc = Lexer.getLoc();
1279
Daniel Dunbar821e3332009-08-31 08:09:28 +00001280 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001281 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001282 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001283
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001284 MarkUsed(Value);
1285
Daniel Dunbar3f872332009-07-28 16:08:33 +00001286 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001287 return TokError("unexpected token in assignment");
1288
1289 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001290 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001291
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001292 // Validate that the LHS is allowed to be a variable (either it has not been
1293 // used as a symbol, or it is an absolute symbol).
1294 MCSymbol *Sym = getContext().LookupSymbol(Name);
1295 if (Sym) {
1296 // Diagnose assignment to a label.
1297 //
1298 // FIXME: Diagnostics. Note the location of the definition as a label.
1299 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001300 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001301 ; // Allow redefinitions of undefined symbols only used in directives.
Nico Weber4c4c7322011-01-28 03:04:41 +00001302 else if (!Sym->isUndefined() && (!Sym->isAbsolute() || !allow_redef))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001303 return Error(EqualLoc, "redefinition of '" + Name + "'");
1304 else if (!Sym->isVariable())
1305 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001306 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001307 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1308 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001309
1310 // Don't count these checks as uses.
1311 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001312 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001313 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001314
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001315 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001316
1317 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001318 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001319
1320 return false;
1321}
1322
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001323/// ParseIdentifier:
1324/// ::= identifier
1325/// ::= string
1326bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001327 // The assembler has relaxed rules for accepting identifiers, in particular we
1328 // allow things like '.globl $foo', which would normally be separate
1329 // tokens. At this level, we have already lexed so we cannot (currently)
1330 // handle this as a context dependent token, instead we detect adjacent tokens
1331 // and return the combined identifier.
1332 if (Lexer.is(AsmToken::Dollar)) {
1333 SMLoc DollarLoc = getLexer().getLoc();
1334
1335 // Consume the dollar sign, and check for a following identifier.
1336 Lex();
1337 if (Lexer.isNot(AsmToken::Identifier))
1338 return true;
1339
1340 // We have a '$' followed by an identifier, make sure they are adjacent.
1341 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1342 return true;
1343
1344 // Construct the joined identifier and consume the token.
1345 Res = StringRef(DollarLoc.getPointer(),
1346 getTok().getIdentifier().size() + 1);
1347 Lex();
1348 return false;
1349 }
1350
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001351 if (Lexer.isNot(AsmToken::Identifier) &&
1352 Lexer.isNot(AsmToken::String))
1353 return true;
1354
Sean Callanan18b83232010-01-19 21:44:56 +00001355 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001356
Sean Callanan79ed1a82010-01-19 20:22:31 +00001357 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001358
1359 return false;
1360}
1361
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001362/// ParseDirectiveSet:
Nico Weber4c4c7322011-01-28 03:04:41 +00001363/// ::= .equ identifier ',' expression
1364/// ::= .equiv identifier ',' expression
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001365/// ::= .set identifier ',' expression
Nico Weber4c4c7322011-01-28 03:04:41 +00001366bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001367 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001368
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001369 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001370 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001371
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001373 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001374 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001375
Nico Weber4c4c7322011-01-28 03:04:41 +00001376 return ParseAssignment(Name, allow_redef);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001377}
1378
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001379bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001380 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001381
1382 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001383 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001384 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1385 if (Str[i] != '\\') {
1386 Data += Str[i];
1387 continue;
1388 }
1389
1390 // Recognize escaped characters. Note that this escape semantics currently
1391 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1392 ++i;
1393 if (i == e)
1394 return TokError("unexpected backslash at end of string");
1395
1396 // Recognize octal sequences.
1397 if ((unsigned) (Str[i] - '0') <= 7) {
1398 // Consume up to three octal characters.
1399 unsigned Value = Str[i] - '0';
1400
1401 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1402 ++i;
1403 Value = Value * 8 + (Str[i] - '0');
1404
1405 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1406 ++i;
1407 Value = Value * 8 + (Str[i] - '0');
1408 }
1409 }
1410
1411 if (Value > 255)
1412 return TokError("invalid octal escape sequence (out of range)");
1413
1414 Data += (unsigned char) Value;
1415 continue;
1416 }
1417
1418 // Otherwise recognize individual escapes.
1419 switch (Str[i]) {
1420 default:
1421 // Just reject invalid escape sequences for now.
1422 return TokError("invalid escape sequence (unrecognized character)");
1423
1424 case 'b': Data += '\b'; break;
1425 case 'f': Data += '\f'; break;
1426 case 'n': Data += '\n'; break;
1427 case 'r': Data += '\r'; break;
1428 case 't': Data += '\t'; break;
1429 case '"': Data += '"'; break;
1430 case '\\': Data += '\\'; break;
1431 }
1432 }
1433
1434 return false;
1435}
1436
Daniel Dunbara0d14262009-06-24 23:30:00 +00001437/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001438/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1439bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001440 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001441 CheckForValidSection();
1442
Daniel Dunbara0d14262009-06-24 23:30:00 +00001443 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001444 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001445 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001446
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001447 std::string Data;
1448 if (ParseEscapedString(Data))
1449 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001450
1451 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001452 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001453 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1454
Sean Callanan79ed1a82010-01-19 20:22:31 +00001455 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001456
1457 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001458 break;
1459
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001460 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001461 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001462 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001463 }
1464 }
1465
Sean Callanan79ed1a82010-01-19 20:22:31 +00001466 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001467 return false;
1468}
1469
1470/// ParseDirectiveValue
1471/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1472bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001473 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001474 CheckForValidSection();
1475
Daniel Dunbara0d14262009-06-24 23:30:00 +00001476 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001477 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001478 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001479 return true;
1480
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001481 // Special case constant expressions to match code generator.
1482 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001483 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001484 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001485 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001486
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001487 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001488 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001489
Daniel Dunbara0d14262009-06-24 23:30:00 +00001490 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001491 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001492 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001493 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001494 }
1495 }
1496
Sean Callanan79ed1a82010-01-19 20:22:31 +00001497 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001498 return false;
1499}
1500
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001501/// ParseDirectiveRealValue
1502/// ::= (.single | .double) [ expression (, expression)* ]
1503bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1504 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1505 CheckForValidSection();
1506
1507 for (;;) {
1508 // We don't truly support arithmetic on floating point expressions, so we
1509 // have to manually parse unary prefixes.
1510 bool IsNeg = false;
1511 if (getLexer().is(AsmToken::Minus)) {
1512 Lex();
1513 IsNeg = true;
1514 } else if (getLexer().is(AsmToken::Plus))
1515 Lex();
1516
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001517 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001518 getLexer().isNot(AsmToken::Real))
1519 return TokError("unexpected token in directive");
1520
1521 // Convert to an APFloat.
1522 APFloat Value(Semantics);
1523 if (Value.convertFromString(getTok().getString(),
1524 APFloat::rmNearestTiesToEven) ==
1525 APFloat::opInvalidOp)
1526 return TokError("invalid floating point literal");
1527 if (IsNeg)
1528 Value.changeSign();
1529
1530 // Consume the numeric token.
1531 Lex();
1532
1533 // Emit the value as an integer.
1534 APInt AsInt = Value.bitcastToAPInt();
1535 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1536 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1537
1538 if (getLexer().is(AsmToken::EndOfStatement))
1539 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001540
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001541 if (getLexer().isNot(AsmToken::Comma))
1542 return TokError("unexpected token in directive");
1543 Lex();
1544 }
1545 }
1546
1547 Lex();
1548 return false;
1549}
1550
Daniel Dunbara0d14262009-06-24 23:30:00 +00001551/// ParseDirectiveSpace
1552/// ::= .space expression [ , expression ]
1553bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001554 CheckForValidSection();
1555
Daniel Dunbara0d14262009-06-24 23:30:00 +00001556 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001557 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001558 return true;
1559
1560 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1562 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001563 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001564 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001565
Daniel Dunbar475839e2009-06-29 20:37:27 +00001566 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001567 return true;
1568
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001569 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001570 return TokError("unexpected token in '.space' directive");
1571 }
1572
Sean Callanan79ed1a82010-01-19 20:22:31 +00001573 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001574
1575 if (NumBytes <= 0)
1576 return TokError("invalid number of bytes in '.space' directive");
1577
1578 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001579 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001580
1581 return false;
1582}
1583
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001584/// ParseDirectiveZero
1585/// ::= .zero expression
1586bool AsmParser::ParseDirectiveZero() {
1587 CheckForValidSection();
1588
1589 int64_t NumBytes;
1590 if (ParseAbsoluteExpression(NumBytes))
1591 return true;
1592
Rafael Espindolae452b172010-10-05 19:42:57 +00001593 int64_t Val = 0;
1594 if (getLexer().is(AsmToken::Comma)) {
1595 Lex();
1596 if (ParseAbsoluteExpression(Val))
1597 return true;
1598 }
1599
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001600 if (getLexer().isNot(AsmToken::EndOfStatement))
1601 return TokError("unexpected token in '.zero' directive");
1602
1603 Lex();
1604
Rafael Espindolae452b172010-10-05 19:42:57 +00001605 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001606
1607 return false;
1608}
1609
Daniel Dunbara0d14262009-06-24 23:30:00 +00001610/// ParseDirectiveFill
1611/// ::= .fill expression , expression , expression
1612bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001613 CheckForValidSection();
1614
Daniel Dunbara0d14262009-06-24 23:30:00 +00001615 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001616 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001617 return true;
1618
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001619 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001620 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001621 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001622
Daniel Dunbara0d14262009-06-24 23:30:00 +00001623 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001624 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001625 return true;
1626
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001627 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001628 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001629 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001630
Daniel Dunbara0d14262009-06-24 23:30:00 +00001631 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001632 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001633 return true;
1634
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001635 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001636 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001637
Sean Callanan79ed1a82010-01-19 20:22:31 +00001638 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001639
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001640 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1641 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001642
1643 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001644 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001645
1646 return false;
1647}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001648
1649/// ParseDirectiveOrg
1650/// ::= .org expression [ , expression ]
1651bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001652 CheckForValidSection();
1653
Daniel Dunbar821e3332009-08-31 08:09:28 +00001654 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001655 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001656 return true;
1657
1658 // Parse optional fill expression.
1659 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001660 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1661 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001662 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001663 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001664
Daniel Dunbar475839e2009-06-29 20:37:27 +00001665 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001666 return true;
1667
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001668 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001669 return TokError("unexpected token in '.org' directive");
1670 }
1671
Sean Callanan79ed1a82010-01-19 20:22:31 +00001672 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001673
1674 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1675 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001676 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001677
1678 return false;
1679}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001680
1681/// ParseDirectiveAlign
1682/// ::= {.align, ...} expression [ , expression [ , expression ]]
1683bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001684 CheckForValidSection();
1685
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001686 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001687 int64_t Alignment;
1688 if (ParseAbsoluteExpression(Alignment))
1689 return true;
1690
1691 SMLoc MaxBytesLoc;
1692 bool HasFillExpr = false;
1693 int64_t FillExpr = 0;
1694 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1696 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001697 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001698 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001699
1700 // The fill expression can be omitted while specifying a maximum number of
1701 // alignment bytes, e.g:
1702 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001703 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001704 HasFillExpr = true;
1705 if (ParseAbsoluteExpression(FillExpr))
1706 return true;
1707 }
1708
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001709 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1710 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001711 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001712 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001713
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001714 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001715 if (ParseAbsoluteExpression(MaxBytesToFill))
1716 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001717
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001718 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001719 return TokError("unexpected token in directive");
1720 }
1721 }
1722
Sean Callanan79ed1a82010-01-19 20:22:31 +00001723 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001724
Daniel Dunbar648ac512010-05-17 21:54:30 +00001725 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001726 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001727
1728 // Compute alignment in bytes.
1729 if (IsPow2) {
1730 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001731 if (Alignment >= 32) {
1732 Error(AlignmentLoc, "invalid alignment value");
1733 Alignment = 31;
1734 }
1735
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001736 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001737 }
1738
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001739 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001740 if (MaxBytesLoc.isValid()) {
1741 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001742 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1743 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001744 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001745 }
1746
1747 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001748 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1749 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001750 MaxBytesToFill = 0;
1751 }
1752 }
1753
Daniel Dunbar648ac512010-05-17 21:54:30 +00001754 // Check whether we should use optimal code alignment for this .align
1755 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001756 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001757 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1758 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001760 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001761 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001762 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1763 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001764 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001765
1766 return false;
1767}
1768
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001769/// ParseDirectiveSymbolAttribute
1770/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001771bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001772 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001773 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001774 StringRef Name;
1775
1776 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001777 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001778
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001779 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001780
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001782
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001783 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001784 break;
1785
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001786 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001787 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001788 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001789 }
1790 }
1791
Sean Callanan79ed1a82010-01-19 20:22:31 +00001792 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001793 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001794}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001795
1796/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001797/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1798bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001799 CheckForValidSection();
1800
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001802 StringRef Name;
1803 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001804 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001805
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001806 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001807 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001808
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001809 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001810 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001811 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001812
1813 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001814 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001815 if (ParseAbsoluteExpression(Size))
1816 return true;
1817
1818 int64_t Pow2Alignment = 0;
1819 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001820 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001821 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001822 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001823 if (ParseAbsoluteExpression(Pow2Alignment))
1824 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001825
Chris Lattner258281d2010-01-19 06:22:22 +00001826 // If this target takes alignments in bytes (not log) validate and convert.
1827 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1828 if (!isPowerOf2_64(Pow2Alignment))
1829 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1830 Pow2Alignment = Log2_64(Pow2Alignment);
1831 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001832 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001833
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001834 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001835 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001836
Sean Callanan79ed1a82010-01-19 20:22:31 +00001837 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001838
Chris Lattner1fc3d752009-07-09 17:25:12 +00001839 // NOTE: a size of zero for a .comm should create a undefined symbol
1840 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001841 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001842 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1843 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001844
Eric Christopherc260a3e2010-05-14 01:38:54 +00001845 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001846 // may internally end up wanting an alignment in bytes.
1847 // FIXME: Diagnose overflow.
1848 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001849 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1850 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001851
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001852 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001853 return Error(IDLoc, "invalid symbol redefinition");
1854
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001855 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001856 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001857 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001858 getStreamer().EmitZerofill(Ctx.getMachOSection(
1859 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1860 0, SectionKind::getBSS()),
1861 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001862 return false;
1863 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001864
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001865 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001866 return false;
1867}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001868
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001869/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001870/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001871bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001872 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001873 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001874
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001875 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001876 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001877 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001878
Sean Callanan79ed1a82010-01-19 20:22:31 +00001879 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001880
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001881 if (Str.empty())
1882 Error(Loc, ".abort detected. Assembly stopping.");
1883 else
1884 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001885 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001886
1887 return false;
1888}
Kevin Enderby71148242009-07-14 21:35:03 +00001889
Kevin Enderby1f049b22009-07-14 23:21:55 +00001890/// ParseDirectiveInclude
1891/// ::= .include "filename"
1892bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001893 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001894 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001895
Sean Callanan18b83232010-01-19 21:44:56 +00001896 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001897 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001898 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001899
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001900 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001901 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001902
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001903 // Strip the quotes.
1904 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001905
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001906 // Attempt to switch the lexer to the included file before consuming the end
1907 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001908 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001909 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001910 return true;
1911 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001912
1913 return false;
1914}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001915
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001916/// ParseDirectiveIf
1917/// ::= .if expression
1918bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001919 TheCondStack.push_back(TheCondState);
1920 TheCondState.TheCond = AsmCond::IfCond;
1921 if(TheCondState.Ignore) {
1922 EatToEndOfStatement();
1923 }
1924 else {
1925 int64_t ExprValue;
1926 if (ParseAbsoluteExpression(ExprValue))
1927 return true;
1928
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001929 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001930 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001931
Sean Callanan79ed1a82010-01-19 20:22:31 +00001932 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001933
1934 TheCondState.CondMet = ExprValue;
1935 TheCondState.Ignore = !TheCondState.CondMet;
1936 }
1937
1938 return false;
1939}
1940
Benjamin Kramer0fd90bc2011-02-08 22:29:56 +00001941bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
1942 StringRef Name;
1943 TheCondStack.push_back(TheCondState);
1944 TheCondState.TheCond = AsmCond::IfCond;
1945
1946 if (TheCondState.Ignore) {
1947 EatToEndOfStatement();
1948 } else {
1949 if (ParseIdentifier(Name))
1950 return TokError("expected identifier after '.ifdef'");
1951
1952 Lex();
1953
1954 MCSymbol *Sym = getContext().LookupSymbol(Name);
1955
1956 if (expect_defined)
1957 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined());
1958 else
1959 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined());
1960 TheCondState.Ignore = !TheCondState.CondMet;
1961 }
1962
1963 return false;
1964}
1965
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001966/// ParseDirectiveElseIf
1967/// ::= .elseif expression
1968bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1969 if (TheCondState.TheCond != AsmCond::IfCond &&
1970 TheCondState.TheCond != AsmCond::ElseIfCond)
1971 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1972 " an .elseif");
1973 TheCondState.TheCond = AsmCond::ElseIfCond;
1974
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001975 bool LastIgnoreState = false;
1976 if (!TheCondStack.empty())
1977 LastIgnoreState = TheCondStack.back().Ignore;
1978 if (LastIgnoreState || TheCondState.CondMet) {
1979 TheCondState.Ignore = true;
1980 EatToEndOfStatement();
1981 }
1982 else {
1983 int64_t ExprValue;
1984 if (ParseAbsoluteExpression(ExprValue))
1985 return true;
1986
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001987 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001988 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001989
Sean Callanan79ed1a82010-01-19 20:22:31 +00001990 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001991 TheCondState.CondMet = ExprValue;
1992 TheCondState.Ignore = !TheCondState.CondMet;
1993 }
1994
1995 return false;
1996}
1997
1998/// ParseDirectiveElse
1999/// ::= .else
2000bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002001 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002002 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002003
Sean Callanan79ed1a82010-01-19 20:22:31 +00002004 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002005
2006 if (TheCondState.TheCond != AsmCond::IfCond &&
2007 TheCondState.TheCond != AsmCond::ElseIfCond)
2008 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2009 ".elseif");
2010 TheCondState.TheCond = AsmCond::ElseCond;
2011 bool LastIgnoreState = false;
2012 if (!TheCondStack.empty())
2013 LastIgnoreState = TheCondStack.back().Ignore;
2014 if (LastIgnoreState || TheCondState.CondMet)
2015 TheCondState.Ignore = true;
2016 else
2017 TheCondState.Ignore = false;
2018
2019 return false;
2020}
2021
2022/// ParseDirectiveEndIf
2023/// ::= .endif
2024bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002025 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002026 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002027
Sean Callanan79ed1a82010-01-19 20:22:31 +00002028 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002029
2030 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2031 TheCondStack.empty())
2032 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2033 ".else");
2034 if (!TheCondStack.empty()) {
2035 TheCondState = TheCondStack.back();
2036 TheCondStack.pop_back();
2037 }
2038
2039 return false;
2040}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002041
2042/// ParseDirectiveFile
2043/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002044bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002045 // FIXME: I'm not sure what this is.
2046 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002047 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00002048 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002049 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002050 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002051
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002052 if (FileNumber < 1)
2053 return TokError("file number less than one");
2054 }
2055
Daniel Dunbareceec052010-07-12 17:45:27 +00002056 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002057 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002058
Chris Lattnerd32e8032010-01-25 19:02:58 +00002059 StringRef Filename = getTok().getString();
2060 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002061 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002062
Daniel Dunbareceec052010-07-12 17:45:27 +00002063 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002064 return TokError("unexpected token in '.file' directive");
2065
Chris Lattnerd32e8032010-01-25 19:02:58 +00002066 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002067 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002068 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002069 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002070 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002071 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002072
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002073 return false;
2074}
2075
2076/// ParseDirectiveLine
2077/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002078bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002079 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2080 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002081 return TokError("unexpected token in '.line' directive");
2082
Sean Callanan18b83232010-01-19 21:44:56 +00002083 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002084 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002085 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002086
2087 // FIXME: Do something with the .line.
2088 }
2089
Daniel Dunbareceec052010-07-12 17:45:27 +00002090 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002091 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002092
2093 return false;
2094}
2095
2096
2097/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002098/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002099/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2100/// The first number is a file number, must have been previously assigned with
2101/// a .file directive, the second number is the line number and optionally the
2102/// third number is a column position (zero if not specified). The remaining
2103/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002104bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002105
Daniel Dunbareceec052010-07-12 17:45:27 +00002106 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002107 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002108 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002109 if (FileNumber < 1)
2110 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002111 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002112 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002113 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002114
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002115 int64_t LineNumber = 0;
2116 if (getLexer().is(AsmToken::Integer)) {
2117 LineNumber = getTok().getIntVal();
2118 if (LineNumber < 1)
2119 return TokError("line number less than one in '.loc' directive");
2120 Lex();
2121 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002122
2123 int64_t ColumnPos = 0;
2124 if (getLexer().is(AsmToken::Integer)) {
2125 ColumnPos = getTok().getIntVal();
2126 if (ColumnPos < 0)
2127 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002128 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002129 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002130
Kevin Enderbyc0957932010-09-30 16:52:03 +00002131 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002132 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002133 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002134 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2135 for (;;) {
2136 if (getLexer().is(AsmToken::EndOfStatement))
2137 break;
2138
2139 StringRef Name;
2140 SMLoc Loc = getTok().getLoc();
2141 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002142 return TokError("unexpected token in '.loc' directive");
2143
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002144 if (Name == "basic_block")
2145 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2146 else if (Name == "prologue_end")
2147 Flags |= DWARF2_FLAG_PROLOGUE_END;
2148 else if (Name == "epilogue_begin")
2149 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2150 else if (Name == "is_stmt") {
2151 SMLoc Loc = getTok().getLoc();
2152 const MCExpr *Value;
2153 if (getParser().ParseExpression(Value))
2154 return true;
2155 // The expression must be the constant 0 or 1.
2156 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2157 int Value = MCE->getValue();
2158 if (Value == 0)
2159 Flags &= ~DWARF2_FLAG_IS_STMT;
2160 else if (Value == 1)
2161 Flags |= DWARF2_FLAG_IS_STMT;
2162 else
2163 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002164 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002165 else {
2166 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2167 }
2168 }
2169 else if (Name == "isa") {
2170 SMLoc Loc = getTok().getLoc();
2171 const MCExpr *Value;
2172 if (getParser().ParseExpression(Value))
2173 return true;
2174 // The expression must be a constant greater or equal to 0.
2175 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2176 int Value = MCE->getValue();
2177 if (Value < 0)
2178 return Error(Loc, "isa number less than zero");
2179 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002180 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002181 else {
2182 return Error(Loc, "isa number not a constant value");
2183 }
2184 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002185 else if (Name == "discriminator") {
2186 if (getParser().ParseAbsoluteExpression(Discriminator))
2187 return true;
2188 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002189 else {
2190 return Error(Loc, "unknown sub-directive in '.loc' directive");
2191 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002192
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002193 if (getLexer().is(AsmToken::EndOfStatement))
2194 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002195 }
2196 }
2197
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002198 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2199 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002200
2201 return false;
2202}
2203
Daniel Dunbar138abae2010-10-16 04:56:42 +00002204/// ParseDirectiveStabs
2205/// ::= .stabs string, number, number, number
2206bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2207 SMLoc DirectiveLoc) {
2208 return TokError("unsupported directive '" + Directive + "'");
2209}
2210
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002211/// ParseDirectiveCFIStartProc
2212/// ::= .cfi_startproc
2213bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2214 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002215 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002216}
2217
2218/// ParseDirectiveCFIEndProc
2219/// ::= .cfi_endproc
2220bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002221 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002222}
2223
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002224/// ParseRegisterOrRegisterNumber - parse register name or number.
2225bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register,
2226 SMLoc DirectiveLoc) {
2227 unsigned RegNo;
2228
2229 if (getLexer().is(AsmToken::Percent)) {
2230 if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc,
2231 DirectiveLoc))
2232 return true;
2233 Register = getContext().getTargetAsmInfo().getDwarfRegNum(RegNo, true);
2234 } else
2235 return getParser().ParseAbsoluteExpression(Register);
2236
2237 return false;
2238}
2239
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002240/// ParseDirectiveCFIDefCfa
2241/// ::= .cfi_def_cfa register, offset
2242bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef,
2243 SMLoc DirectiveLoc) {
2244 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002245 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindolab40a71f2010-12-29 01:42:56 +00002246 return true;
2247
2248 if (getLexer().isNot(AsmToken::Comma))
2249 return TokError("unexpected token in directive");
2250 Lex();
2251
2252 int64_t Offset = 0;
2253 if (getParser().ParseAbsoluteExpression(Offset))
2254 return true;
2255
2256 return getStreamer().EmitCFIDefCfa(Register, Offset);
2257}
2258
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002259/// ParseDirectiveCFIDefCfaOffset
2260/// ::= .cfi_def_cfa_offset offset
2261bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2262 SMLoc DirectiveLoc) {
2263 int64_t Offset = 0;
2264 if (getParser().ParseAbsoluteExpression(Offset))
2265 return true;
2266
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002267 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002268}
2269
2270/// ParseDirectiveCFIDefCfaRegister
2271/// ::= .cfi_def_cfa_register register
2272bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2273 SMLoc DirectiveLoc) {
2274 int64_t Register = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002275 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002276 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002277
2278 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002279}
2280
2281/// ParseDirectiveCFIOffset
2282/// ::= .cfi_off register, offset
2283bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2284 int64_t Register = 0;
2285 int64_t Offset = 0;
Roman Divacky54b0f4f2011-01-27 17:16:37 +00002286
2287 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc))
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002288 return true;
2289
2290 if (getLexer().isNot(AsmToken::Comma))
2291 return TokError("unexpected token in directive");
2292 Lex();
2293
2294 if (getParser().ParseAbsoluteExpression(Offset))
2295 return true;
2296
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002297 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002298}
2299
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002300static bool isValidEncoding(int64_t Encoding) {
2301 if (Encoding & ~0xff)
2302 return false;
2303
2304 if (Encoding == dwarf::DW_EH_PE_omit)
2305 return true;
2306
2307 const unsigned Format = Encoding & 0xf;
2308 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2309 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2310 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2311 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2312 return false;
2313
Rafael Espindolacaf11582010-12-29 04:31:26 +00002314 const unsigned Application = Encoding & 0x70;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002315 if (Application != dwarf::DW_EH_PE_absptr &&
Rafael Espindolacaf11582010-12-29 04:31:26 +00002316 Application != dwarf::DW_EH_PE_pcrel)
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002317 return false;
2318
2319 return true;
2320}
2321
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002322/// ParseDirectiveCFIPersonalityOrLsda
2323/// ::= .cfi_personality encoding, [symbol_name]
2324/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002325bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002326 SMLoc DirectiveLoc) {
2327 int64_t Encoding = 0;
2328 if (getParser().ParseAbsoluteExpression(Encoding))
2329 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002330 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002331 return false;
2332
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002333 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002334 return TokError("unsupported encoding.");
2335
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002336 if (getLexer().isNot(AsmToken::Comma))
2337 return TokError("unexpected token in directive");
2338 Lex();
2339
2340 StringRef Name;
2341 if (getParser().ParseIdentifier(Name))
2342 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002343
2344 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2345
2346 if (IDVal == ".cfi_personality")
Rafael Espindola3a83c402010-12-27 00:36:05 +00002347 return getStreamer().EmitCFIPersonality(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002348 else {
2349 assert(IDVal == ".cfi_lsda");
Rafael Espindolabdc31672010-12-27 15:56:22 +00002350 return getStreamer().EmitCFILsda(Sym, Encoding);
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002351 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002352}
2353
Rafael Espindolafe024d02010-12-28 18:36:23 +00002354/// ParseDirectiveCFIRememberState
2355/// ::= .cfi_remember_state
2356bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal,
2357 SMLoc DirectiveLoc) {
2358 return getStreamer().EmitCFIRememberState();
2359}
2360
2361/// ParseDirectiveCFIRestoreState
2362/// ::= .cfi_remember_state
2363bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal,
2364 SMLoc DirectiveLoc) {
2365 return getStreamer().EmitCFIRestoreState();
2366}
2367
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002368/// ParseDirectiveMacrosOnOff
2369/// ::= .macros_on
2370/// ::= .macros_off
2371bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2372 SMLoc DirectiveLoc) {
2373 if (getLexer().isNot(AsmToken::EndOfStatement))
2374 return Error(getLexer().getLoc(),
2375 "unexpected token in '" + Directive + "' directive");
2376
2377 getParser().MacrosEnabled = Directive == ".macros_on";
2378
2379 return false;
2380}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002381
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002382/// ParseDirectiveMacro
2383/// ::= .macro name
2384bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2385 SMLoc DirectiveLoc) {
2386 StringRef Name;
2387 if (getParser().ParseIdentifier(Name))
2388 return TokError("expected identifier in directive");
2389
2390 if (getLexer().isNot(AsmToken::EndOfStatement))
2391 return TokError("unexpected token in '.macro' directive");
2392
2393 // Eat the end of statement.
2394 Lex();
2395
2396 AsmToken EndToken, StartToken = getTok();
2397
2398 // Lex the macro definition.
2399 for (;;) {
2400 // Check whether we have reached the end of the file.
2401 if (getLexer().is(AsmToken::Eof))
2402 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2403
2404 // Otherwise, check whether we have reach the .endmacro.
2405 if (getLexer().is(AsmToken::Identifier) &&
2406 (getTok().getIdentifier() == ".endm" ||
2407 getTok().getIdentifier() == ".endmacro")) {
2408 EndToken = getTok();
2409 Lex();
2410 if (getLexer().isNot(AsmToken::EndOfStatement))
2411 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2412 "' directive");
2413 break;
2414 }
2415
2416 // Otherwise, scan til the end of the statement.
2417 getParser().EatToEndOfStatement();
2418 }
2419
2420 if (getParser().MacroMap.lookup(Name)) {
2421 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2422 }
2423
2424 const char *BodyStart = StartToken.getLoc().getPointer();
2425 const char *BodyEnd = EndToken.getLoc().getPointer();
2426 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2427 getParser().MacroMap[Name] = new Macro(Name, Body);
2428 return false;
2429}
2430
2431/// ParseDirectiveEndMacro
2432/// ::= .endm
2433/// ::= .endmacro
2434bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2435 SMLoc DirectiveLoc) {
2436 if (getLexer().isNot(AsmToken::EndOfStatement))
2437 return TokError("unexpected token in '" + Directive + "' directive");
2438
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002439 // If we are inside a macro instantiation, terminate the current
2440 // instantiation.
2441 if (!getParser().ActiveMacros.empty()) {
2442 getParser().HandleMacroExit();
2443 return false;
2444 }
2445
2446 // Otherwise, this .endmacro is a stray entry in the file; well formed
2447 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002448 return TokError("unexpected '" + Directive + "' in file, "
2449 "no current macro definition");
2450}
2451
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002452bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002453 getParser().CheckForValidSection();
2454
2455 const MCExpr *Value;
2456
2457 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002458 return true;
2459
2460 if (getLexer().isNot(AsmToken::EndOfStatement))
2461 return TokError("unexpected token in directive");
2462
2463 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002464 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002465 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002466 getStreamer().EmitULEB128Value(Value);
2467
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002468 return false;
2469}
2470
2471
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002472/// \brief Create an MCAsmParser instance.
2473MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2474 MCContext &C, MCStreamer &Out,
2475 const MCAsmInfo &MAI) {
2476 return new AsmParser(T, SM, C, Out, MAI);
2477}