blob: 63ddfb9f8257993ea941bd4f23c187b1569bccf9 [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"
Daniel Dunbara3af3702009-07-20 18:55:04 +000033#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbaraef87e32010-07-18 18:31:38 +000034#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000035using namespace llvm;
36
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000037namespace {
38
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000039/// \brief Helper class for tracking macro definitions.
40struct Macro {
41 StringRef Name;
42 StringRef Body;
43
44public:
45 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
46};
47
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000048/// \brief Helper class for storing information about an active macro
49/// instantiation.
50struct MacroInstantiation {
51 /// The macro being instantiated.
52 const Macro *TheMacro;
53
54 /// The macro instantiation with substitutions.
55 MemoryBuffer *Instantiation;
56
57 /// The location of the instantiation.
58 SMLoc InstantiationLoc;
59
60 /// The location where parsing should resume upon instantiation completion.
61 SMLoc ExitLoc;
62
63public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000064 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
65 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000066};
67
Daniel Dunbaraef87e32010-07-18 18:31:38 +000068/// \brief The concrete assembly parser instance.
69class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000070 friend class GenericAsmParser;
71
Daniel Dunbaraef87e32010-07-18 18:31:38 +000072 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
73 void operator=(const AsmParser &); // DO NOT IMPLEMENT
74private:
75 AsmLexer Lexer;
76 MCContext &Ctx;
77 MCStreamer &Out;
78 SourceMgr &SrcMgr;
79 MCAsmParserExtension *GenericParser;
80 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000081
Daniel Dunbaraef87e32010-07-18 18:31:38 +000082 /// This is the current buffer index we're lexing from as managed by the
83 /// SourceMgr object.
84 int CurBuffer;
85
86 AsmCond TheCondState;
87 std::vector<AsmCond> TheCondStack;
88
89 /// DirectiveMap - This is a table handlers for directives. Each handler is
90 /// invoked after the directive identifier is read and is responsible for
91 /// parsing and validating the rest of the directive. The handler is passed
92 /// in the directive name and the location of the directive keyword.
93 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000094
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000095 /// MacroMap - Map of currently defined macros.
96 StringMap<Macro*> MacroMap;
97
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000098 /// ActiveMacros - Stack of active macro instantiations.
99 std::vector<MacroInstantiation*> ActiveMacros;
100
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000101 /// Boolean tracking whether macro substitution is enabled.
102 unsigned MacrosEnabled : 1;
103
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000104 /// Flag tracking whether any errors have been encountered.
105 unsigned HadError : 1;
106
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000107public:
108 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
109 const MCAsmInfo &MAI);
110 ~AsmParser();
111
112 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
113
114 void AddDirectiveHandler(MCAsmParserExtension *Object,
115 StringRef Directive,
116 DirectiveHandler Handler) {
117 DirectiveMap[Directive] = std::make_pair(Object, Handler);
118 }
119
120public:
121 /// @name MCAsmParser Interface
122 /// {
123
124 virtual SourceMgr &getSourceManager() { return SrcMgr; }
125 virtual MCAsmLexer &getLexer() { return Lexer; }
126 virtual MCContext &getContext() { return Ctx; }
127 virtual MCStreamer &getStreamer() { return Out; }
128
129 virtual void Warning(SMLoc L, const Twine &Meg);
130 virtual bool Error(SMLoc L, const Twine &Msg);
131
132 const AsmToken &Lex();
133
134 bool ParseExpression(const MCExpr *&Res);
135 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
136 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseAbsoluteExpression(int64_t &Res);
138
139 /// }
140
141private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000142 void CheckForValidSection();
143
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000144 bool ParseStatement();
145
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000146 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
147 void HandleMacroExit();
148
149 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000150 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
151 SrcMgr.PrintMessage(Loc, Msg, Type);
152 }
153
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000154 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
155 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000156
157 /// \brief Reset the current lexer position to that given by \arg Loc. The
158 /// current token is not set; clients should ensure Lex() is called
159 /// subsequently.
160 void JumpToLoc(SMLoc Loc);
161
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000162 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000163
164 /// \brief Parse up to the end of statement and a return the contents from the
165 /// current token until the end of the statement; the current token on exit
166 /// will be either the EndOfStatement or EOF.
167 StringRef ParseStringToEndOfStatement();
168
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000169 bool ParseAssignment(StringRef Name);
170
171 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
172 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
174
175 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
176 /// and set \arg Res to the identifier contents.
177 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000178
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000179 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000180
181 // ".ascii", ".asciiz", ".string"
182 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000183 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000184 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000185 bool ParseDirectiveFill(); // ".fill"
186 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000187 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000188 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000189 bool ParseDirectiveOrg(); // ".org"
190 // ".align{,32}", ".p2align{,w,l}"
191 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
192
193 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
194 /// accepts a single symbol (which should be a label or an external).
195 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000196
197 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
198
199 bool ParseDirectiveAbort(); // ".abort"
200 bool ParseDirectiveInclude(); // ".include"
201
202 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
203 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
204 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
205 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
206
207 /// ParseEscapedString - Parse the current token as a string which may include
208 /// escaped characters and return the string contents.
209 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000210
211 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
212 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000213};
214
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000215/// \brief Generic implementations of directive handling, etc. which is shared
216/// (or the default, at least) for all assembler parser.
217class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000218 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
219 void AddDirectiveHandler(StringRef Directive) {
220 getParser().AddDirectiveHandler(this, Directive,
221 HandleDirective<GenericAsmParser, Handler>);
222 }
223
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000224public:
225 GenericAsmParser() {}
226
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000227 AsmParser &getParser() {
228 return (AsmParser&) this->MCAsmParserExtension::getParser();
229 }
230
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000231 virtual void Initialize(MCAsmParser &Parser) {
232 // Call the base implementation.
233 this->MCAsmParserExtension::Initialize(Parser);
234
235 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000236 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000240
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000241 // CFI directives.
242 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
243 ".cfi_startproc");
244 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
245 ".cfi_endproc");
246 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
247 ".cfi_def_cfa_offset");
248 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
249 ".cfi_def_cfa_register");
250 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
251 ".cfi_offset");
252 AddDirectiveHandler<
253 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
254 AddDirectiveHandler<
255 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
256
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000257 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000258 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
259 ".macros_on");
260 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
261 ".macros_off");
262 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000265
266 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000268 }
269
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000270 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
271 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
272 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000273 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000274 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
275 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
276 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
277 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
278 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
279 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000280
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000281 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000282 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
283 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000284
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000285 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000286};
287
288}
289
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000290namespace llvm {
291
292extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000293extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000294extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000295
296}
297
Chris Lattneraaec2052010-01-19 19:46:13 +0000298enum { DEFAULT_ADDRSPACE = 0 };
299
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000300AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
301 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000302 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000303 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000304 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000305 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000306
307 // Initialize the generic parser.
308 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000309
310 // Initialize the platform / file format parser.
311 //
312 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
313 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000314 if (_MAI.hasMicrosoftFastStdCallMangling()) {
315 PlatformParser = createCOFFAsmParser();
316 PlatformParser->Initialize(*this);
317 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000318 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000319 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000320 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000321 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000322 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000323 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000324}
325
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000326AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000327 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
328
329 // Destroy any macros.
330 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
331 ie = MacroMap.end(); it != ie; ++it)
332 delete it->getValue();
333
Daniel Dunbare4749702010-07-12 18:12:02 +0000334 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000335 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000336}
337
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000338void AsmParser::PrintMacroInstantiations() {
339 // Print the active macro instantiation stack.
340 for (std::vector<MacroInstantiation*>::const_reverse_iterator
341 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
342 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
343 "note");
344}
345
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000346void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000347 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000348 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000349}
350
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000351bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000352 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000353 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000354 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000355 return true;
356}
357
Sean Callananfd0b0282010-01-21 00:19:58 +0000358bool AsmParser::EnterIncludeFile(const std::string &Filename) {
359 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
360 if (NewBuf == -1)
361 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000362
Sean Callananfd0b0282010-01-21 00:19:58 +0000363 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000364
Sean Callananfd0b0282010-01-21 00:19:58 +0000365 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000366
Sean Callananfd0b0282010-01-21 00:19:58 +0000367 return false;
368}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000369
370void AsmParser::JumpToLoc(SMLoc Loc) {
371 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
372 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
373}
374
Sean Callananfd0b0282010-01-21 00:19:58 +0000375const AsmToken &AsmParser::Lex() {
376 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000377
Sean Callananfd0b0282010-01-21 00:19:58 +0000378 if (tok->is(AsmToken::Eof)) {
379 // If this is the end of an included file, pop the parent file off the
380 // include stack.
381 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
382 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000383 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000384 tok = &Lexer.Lex();
385 }
386 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000387
Sean Callananfd0b0282010-01-21 00:19:58 +0000388 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000389 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000390
Sean Callananfd0b0282010-01-21 00:19:58 +0000391 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000392}
393
Chris Lattner79180e22010-04-05 23:15:42 +0000394bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000395 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000396 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000397 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000398
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000399 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000400 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000401
402 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000403 AsmCond StartingCondState = TheCondState;
404
Chris Lattnerb717fb02009-07-02 21:53:43 +0000405 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000406 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000407 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000408
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000409 // We had an error, validate that one was emitted and recover by skipping to
410 // the next line.
411 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000412 EatToEndOfStatement();
413 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000414
415 if (TheCondState.TheCond != StartingCondState.TheCond ||
416 TheCondState.Ignore != StartingCondState.Ignore)
417 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000418
419 // Check to see there are no empty DwarfFile slots.
420 const std::vector<MCDwarfFile *> &MCDwarfFiles =
421 getContext().getMCDwarfFiles();
422 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000423 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000424 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000425 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000426
Chris Lattner79180e22010-04-05 23:15:42 +0000427 // Finalize the output stream if there are no errors and if the client wants
428 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000429 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000430 Out.Finish();
431
Chris Lattnerb717fb02009-07-02 21:53:43 +0000432 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000433}
434
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000435void AsmParser::CheckForValidSection() {
436 if (!getStreamer().getCurrentSection()) {
437 TokError("expected section directive before assembly directive");
438 Out.SwitchSection(Ctx.getMachOSection(
439 "__TEXT", "__text",
440 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
441 0, SectionKind::getText()));
442 }
443}
444
Chris Lattner2cf5f142009-06-22 01:29:09 +0000445/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
446void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000447 while (Lexer.isNot(AsmToken::EndOfStatement) &&
448 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000449 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000450
Chris Lattner2cf5f142009-06-22 01:29:09 +0000451 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000452 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000453 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000454}
455
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000456StringRef AsmParser::ParseStringToEndOfStatement() {
457 const char *Start = getTok().getLoc().getPointer();
458
459 while (Lexer.isNot(AsmToken::EndOfStatement) &&
460 Lexer.isNot(AsmToken::Eof))
461 Lex();
462
463 const char *End = getTok().getLoc().getPointer();
464 return StringRef(Start, End - Start);
465}
Chris Lattnerc4193832009-06-22 05:51:26 +0000466
Chris Lattner74ec1a32009-06-22 06:32:03 +0000467/// ParseParenExpr - Parse a paren expression and return it.
468/// NOTE: This assumes the leading '(' has already been consumed.
469///
470/// parenexpr ::= expr)
471///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000472bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000473 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000474 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000475 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000476 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000477 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000478 return false;
479}
Chris Lattnerc4193832009-06-22 05:51:26 +0000480
Chris Lattner74ec1a32009-06-22 06:32:03 +0000481/// ParsePrimaryExpr - Parse a primary expression and return it.
482/// primaryexpr ::= (parenexpr
483/// primaryexpr ::= symbol
484/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000485/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000486/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000487bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000488 switch (Lexer.getKind()) {
489 default:
490 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000491 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000492 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000493 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000494 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000495 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000496 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000497 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000498 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000499 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000500 EndLoc = Lexer.getLoc();
501
502 StringRef Identifier;
503 if (ParseIdentifier(Identifier))
504 return false;
505
Daniel Dunbarfffff912009-10-16 01:34:54 +0000506 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000507 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000508 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000509
510 // Lookup the symbol variant if used.
511 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000512 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000513 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000514 if (Variant == MCSymbolRefExpr::VK_Invalid) {
515 Variant = MCSymbolRefExpr::VK_None;
516 TokError("invalid variant '" + Split.second + "'");
517 }
518 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000519
Daniel Dunbarfffff912009-10-16 01:34:54 +0000520 // If this is an absolute variable reference, substitute it now to preserve
521 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000522 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000523 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000524 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000525
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000526 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000527 return false;
528 }
529
530 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000531 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000532 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000533 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000534 case AsmToken::Integer: {
535 SMLoc Loc = getTok().getLoc();
536 int64_t IntVal = getTok().getIntVal();
537 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000538 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000539 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000540 // Look for 'b' or 'f' following an Integer as a directional label
541 if (Lexer.getKind() == AsmToken::Identifier) {
542 StringRef IDVal = getTok().getString();
543 if (IDVal == "f" || IDVal == "b"){
544 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
545 IDVal == "f" ? 1 : 0);
546 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
547 getContext());
548 if(IDVal == "b" && Sym->isUndefined())
549 return Error(Loc, "invalid reference to undefined symbol");
550 EndLoc = Lexer.getLoc();
551 Lex(); // Eat identifier.
552 }
553 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000554 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000555 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000556 case AsmToken::Dot: {
557 // This is a '.' reference, which references the current PC. Emit a
558 // temporary label to the streamer and refer to it.
559 MCSymbol *Sym = Ctx.CreateTempSymbol();
560 Out.EmitLabel(Sym);
561 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
562 EndLoc = Lexer.getLoc();
563 Lex(); // Eat identifier.
564 return false;
565 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000566
Daniel Dunbar3f872332009-07-28 16:08:33 +0000567 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000568 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000569 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000570 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000571 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000572 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000573 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000574 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000575 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000576 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000577 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000578 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000579 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000580 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000581 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000582 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000583 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000584 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000585 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000586 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000587 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000588 }
589}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000590
Chris Lattnerb4307b32010-01-15 19:28:38 +0000591bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000592 SMLoc EndLoc;
593 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000594}
595
Daniel Dunbarcceba832010-09-17 02:47:07 +0000596const MCExpr *
597AsmParser::ApplyModifierToExpr(const MCExpr *E,
598 MCSymbolRefExpr::VariantKind Variant) {
599 // Recurse over the given expression, rebuilding it to apply the given variant
600 // if there is exactly one symbol.
601 switch (E->getKind()) {
602 case MCExpr::Target:
603 case MCExpr::Constant:
604 return 0;
605
606 case MCExpr::SymbolRef: {
607 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
608
609 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
610 TokError("invalid variant on expression '" +
611 getTok().getIdentifier() + "' (already modified)");
612 return E;
613 }
614
615 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
616 }
617
618 case MCExpr::Unary: {
619 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
620 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
621 if (!Sub)
622 return 0;
623 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
624 }
625
626 case MCExpr::Binary: {
627 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
628 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
629 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
630
631 if (!LHS && !RHS)
632 return 0;
633
634 if (!LHS) LHS = BE->getLHS();
635 if (!RHS) RHS = BE->getRHS();
636
637 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
638 }
639 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000640
641 assert(0 && "Invalid expression kind!");
642 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000643}
644
Chris Lattner74ec1a32009-06-22 06:32:03 +0000645/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000646///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000647/// expr ::= expr +,- expr -> lowest.
648/// expr ::= expr |,^,&,! expr -> middle.
649/// expr ::= expr *,/,%,<<,>> expr -> highest.
650/// expr ::= primaryexpr
651///
Chris Lattner54482b42010-01-15 19:39:23 +0000652bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000653 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000654 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000655 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
656 return true;
657
Daniel Dunbarcceba832010-09-17 02:47:07 +0000658 // As a special case, we support 'a op b @ modifier' by rewriting the
659 // expression to include the modifier. This is inefficient, but in general we
660 // expect users to use 'a@modifier op b'.
661 if (Lexer.getKind() == AsmToken::At) {
662 Lex();
663
664 if (Lexer.isNot(AsmToken::Identifier))
665 return TokError("unexpected symbol modifier following '@'");
666
667 MCSymbolRefExpr::VariantKind Variant =
668 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
669 if (Variant == MCSymbolRefExpr::VK_Invalid)
670 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
671
672 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
673 if (!ModifiedRes) {
674 return TokError("invalid modifier '" + getTok().getIdentifier() +
675 "' (no symbols present)");
676 return true;
677 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000678
Daniel Dunbarcceba832010-09-17 02:47:07 +0000679 Res = ModifiedRes;
680 Lex();
681 }
682
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000683 // Try to constant fold it up front, if possible.
684 int64_t Value;
685 if (Res->EvaluateAsAbsolute(Value))
686 Res = MCConstantExpr::Create(Value, getContext());
687
688 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000689}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000690
Chris Lattnerb4307b32010-01-15 19:28:38 +0000691bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000692 Res = 0;
693 return ParseParenExpr(Res, EndLoc) ||
694 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000695}
696
Daniel Dunbar475839e2009-06-29 20:37:27 +0000697bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000698 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000699
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000700 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000701 if (ParseExpression(Expr))
702 return true;
703
Daniel Dunbare00b0112009-10-16 01:57:52 +0000704 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000705 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000706
707 return false;
708}
709
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000710static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000711 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000712 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000713 default:
714 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000715
Daniel Dunbarcceba832010-09-17 02:47:07 +0000716 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000717 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000718 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000719 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000720 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000721 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000722 return 1;
723
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000724
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000725 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000726 //
727 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000728 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000729 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000730 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000731 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000732 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000733 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000734 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000735 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000736 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000737
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000738 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000739 case AsmToken::EqualEqual:
740 Kind = MCBinaryExpr::EQ;
741 return 3;
742 case AsmToken::ExclaimEqual:
743 case AsmToken::LessGreater:
744 Kind = MCBinaryExpr::NE;
745 return 3;
746 case AsmToken::Less:
747 Kind = MCBinaryExpr::LT;
748 return 3;
749 case AsmToken::LessEqual:
750 Kind = MCBinaryExpr::LTE;
751 return 3;
752 case AsmToken::Greater:
753 Kind = MCBinaryExpr::GT;
754 return 3;
755 case AsmToken::GreaterEqual:
756 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000757 return 3;
758
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000759 // High Intermediate Precedence: +, -
760 case AsmToken::Plus:
761 Kind = MCBinaryExpr::Add;
762 return 4;
763 case AsmToken::Minus:
764 Kind = MCBinaryExpr::Sub;
765 return 4;
766
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000768 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000769 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000770 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000771 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000772 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000773 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000774 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000775 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000776 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000777 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000778 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000779 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000780 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000781 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000782 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000783 }
784}
785
786
787/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
788/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000789bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
790 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000791 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000792 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000793 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000794
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000795 // If the next token is lower precedence than we are allowed to eat, return
796 // successfully with what we ate already.
797 if (TokPrec < Precedence)
798 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000799
Sean Callanan79ed1a82010-01-19 20:22:31 +0000800 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000801
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000802 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000803 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000804 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000805
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000806 // If BinOp binds less tightly with RHS than the operator after RHS, let
807 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000808 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000809 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000810 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000811 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000812 }
813
Daniel Dunbar475839e2009-06-29 20:37:27 +0000814 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000815 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000816 }
817}
818
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000819
820
821
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000822/// ParseStatement:
823/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000824/// ::= Label* Directive ...Operands... EndOfStatement
825/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000826bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000827 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000828 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000829 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000830 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000831 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000832
833 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000834 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000835 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000836 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000837 int64_t LocalLabelVal = -1;
838 // GUESS allow an integer followed by a ':' as a directional local label
839 if (Lexer.is(AsmToken::Integer)) {
840 LocalLabelVal = getTok().getIntVal();
841 if (LocalLabelVal < 0) {
842 if (!TheCondState.Ignore)
843 return TokError("unexpected token at start of statement");
844 IDVal = "";
845 }
846 else {
847 IDVal = getTok().getString();
848 Lex(); // Consume the integer token to be used as an identifier token.
849 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000850 if (!TheCondState.Ignore)
851 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000852 }
853 }
854 }
855 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000856 if (!TheCondState.Ignore)
857 return TokError("unexpected token at start of statement");
858 IDVal = "";
859 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000860
Chris Lattner7834fac2010-04-17 18:14:27 +0000861 // Handle conditional assembly here before checking for skipping. We
862 // have to do this so that .endif isn't skipped in a ".if 0" block for
863 // example.
864 if (IDVal == ".if")
865 return ParseDirectiveIf(IDLoc);
866 if (IDVal == ".elseif")
867 return ParseDirectiveElseIf(IDLoc);
868 if (IDVal == ".else")
869 return ParseDirectiveElse(IDLoc);
870 if (IDVal == ".endif")
871 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000872
Chris Lattner7834fac2010-04-17 18:14:27 +0000873 // If we are in a ".if 0" block, ignore this statement.
874 if (TheCondState.Ignore) {
875 EatToEndOfStatement();
876 return false;
877 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000878
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000879 // FIXME: Recurse on local labels?
880
881 // See what kind of statement we have.
882 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000883 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000884 CheckForValidSection();
885
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000886 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000887 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000888
889 // Diagnose attempt to use a variable as a label.
890 //
891 // FIXME: Diagnostics. Note the location of the definition as a label.
892 // FIXME: This doesn't diagnose assignment to a symbol which has been
893 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000894 MCSymbol *Sym;
895 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000896 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000897 else
898 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000899 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000900 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000901
Daniel Dunbar959fd882009-08-26 22:13:22 +0000902 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000903 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000904
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000905 // Consume any end of statement token, if present, to avoid spurious
906 // AddBlankLine calls().
907 if (Lexer.is(AsmToken::EndOfStatement)) {
908 Lex();
909 if (Lexer.is(AsmToken::Eof))
910 return false;
911 }
912
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000913 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000914 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000915
Daniel Dunbar3f872332009-07-28 16:08:33 +0000916 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000917 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000918 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000919
Daniel Dunbare2ace502009-08-31 08:09:09 +0000920 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000921
922 default: // Normal instruction or directive.
923 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000924 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000925
926 // If macros are enabled, check to see if this is a macro instantiation.
927 if (MacrosEnabled)
928 if (const Macro *M = MacroMap.lookup(IDVal))
929 return HandleMacroEntry(IDVal, IDLoc, M);
930
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000931 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000932 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000933 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000934 if (IDVal == ".set" || IDVal == ".equ")
935 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000936
Daniel Dunbara0d14262009-06-24 23:30:00 +0000937 // Data directives
938
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000939 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000940 return ParseDirectiveAscii(IDVal, false);
941 if (IDVal == ".asciz" || IDVal == ".string")
942 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000943
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000944 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000945 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000946 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000947 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000948 if (IDVal == ".value")
949 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000950 if (IDVal == ".2byte")
951 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000952 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000953 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000954 if (IDVal == ".int")
955 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000956 if (IDVal == ".4byte")
957 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000958 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000960 if (IDVal == ".8byte")
961 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000962 if (IDVal == ".single")
963 return ParseDirectiveRealValue(APFloat::IEEEsingle);
964 if (IDVal == ".double")
965 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000966
Eli Friedman5d68ec22010-07-19 04:17:25 +0000967 if (IDVal == ".align") {
968 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
969 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
970 }
971 if (IDVal == ".align32") {
972 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
973 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
974 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000975 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000976 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000977 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000978 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000979 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000980 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000981 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000982 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000984 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000985 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000986 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
987
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000988 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000989 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000990
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000992 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000993 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000994 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000995 if (IDVal == ".zero")
996 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000997
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000998 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000999
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001000 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001001 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001002 // ELF only? Should it be here?
1003 if (IDVal == ".local")
1004 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001005 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001006 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001007 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001008 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001009 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001010 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001011 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001012 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001013 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001014 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001015 if (IDVal == ".symbol_resolver")
1016 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001017 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001018 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001019 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001020 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001021 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001022 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001023 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001024 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001025 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001026 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001027 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001028 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001029 if (IDVal == ".weak_def_can_be_hidden")
1030 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001031
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001032 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001033 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001034 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001035 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001036
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001037 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001038 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001039 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001040 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001041
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001042 // Look up the handler in the handler table.
1043 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1044 DirectiveMap.lookup(IDVal);
1045 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001046 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001047
Kevin Enderby9c656452009-09-10 20:51:44 +00001048 // Target hook for parsing target specific directives.
1049 if (!getTargetParser().ParseDirective(ID))
1050 return false;
1051
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001052 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001053 EatToEndOfStatement();
1054 return false;
1055 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001056
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001057 CheckForValidSection();
1058
Chris Lattnera7f13542010-05-19 23:34:33 +00001059 // Canonicalize the opcode to lower case.
1060 SmallString<128> Opcode;
1061 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1062 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001063
Chris Lattner98986712010-01-14 22:21:20 +00001064 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001065 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001066 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001067
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001068 // Dump the parsed representation, if requested.
1069 if (getShowParsedOperands()) {
1070 SmallString<256> Str;
1071 raw_svector_ostream OS(Str);
1072 OS << "parsed instruction: [";
1073 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1074 if (i != 0)
1075 OS << ", ";
1076 ParsedOperands[i]->dump(OS);
1077 }
1078 OS << "]";
1079
1080 PrintMessage(IDLoc, OS.str(), "note");
1081 }
1082
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001083 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001084 if (!HadError)
1085 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1086 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001087
Chris Lattner98986712010-01-14 22:21:20 +00001088 // Free any parsed operands.
1089 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1090 delete ParsedOperands[i];
1091
Chris Lattnercbf8a982010-09-11 16:18:25 +00001092 // Don't skip the rest of the line, the instruction parser is responsible for
1093 // that.
1094 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001095}
Chris Lattner9a023f72009-06-24 04:43:34 +00001096
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001097MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1098 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001099 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1100{
1101 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1102 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001103 SmallString<256> Buf;
1104 raw_svector_ostream OS(Buf);
1105
1106 StringRef Body = M->Body;
1107 while (!Body.empty()) {
1108 // Scan for the next substitution.
1109 std::size_t End = Body.size(), Pos = 0;
1110 for (; Pos != End; ++Pos) {
1111 // Check for a substitution or escape.
1112 if (Body[Pos] != '$' || Pos + 1 == End)
1113 continue;
1114
1115 char Next = Body[Pos + 1];
1116 if (Next == '$' || Next == 'n' || isdigit(Next))
1117 break;
1118 }
1119
1120 // Add the prefix.
1121 OS << Body.slice(0, Pos);
1122
1123 // Check if we reached the end.
1124 if (Pos == End)
1125 break;
1126
1127 switch (Body[Pos+1]) {
1128 // $$ => $
1129 case '$':
1130 OS << '$';
1131 break;
1132
1133 // $n => number of arguments
1134 case 'n':
1135 OS << A.size();
1136 break;
1137
1138 // $[0-9] => argument
1139 default: {
1140 // Missing arguments are ignored.
1141 unsigned Index = Body[Pos+1] - '0';
1142 if (Index >= A.size())
1143 break;
1144
1145 // Otherwise substitute with the token values, with spaces eliminated.
1146 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1147 ie = A[Index].end(); it != ie; ++it)
1148 OS << it->getString();
1149 break;
1150 }
1151 }
1152
1153 // Update the scan point.
1154 Body = Body.substr(Pos + 2);
1155 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001156
1157 // We include the .endmacro in the buffer as our queue to exit the macro
1158 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001159 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001160
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001161 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001162}
1163
1164bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1165 const Macro *M) {
1166 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1167 // this, although we should protect against infinite loops.
1168 if (ActiveMacros.size() == 20)
1169 return TokError("macros cannot be nested more than 20 levels deep");
1170
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001171 // Parse the macro instantiation arguments.
1172 std::vector<std::vector<AsmToken> > MacroArguments;
1173 MacroArguments.push_back(std::vector<AsmToken>());
1174 unsigned ParenLevel = 0;
1175 for (;;) {
1176 if (Lexer.is(AsmToken::Eof))
1177 return TokError("unexpected token in macro instantiation");
1178 if (Lexer.is(AsmToken::EndOfStatement))
1179 break;
1180
1181 // If we aren't inside parentheses and this is a comma, start a new token
1182 // list.
1183 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1184 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001185 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001186 // Adjust the current parentheses level.
1187 if (Lexer.is(AsmToken::LParen))
1188 ++ParenLevel;
1189 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1190 --ParenLevel;
1191
1192 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001193 MacroArguments.back().push_back(getTok());
1194 }
1195 Lex();
1196 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001197
1198 // Create the macro instantiation object and add to the current macro
1199 // instantiation stack.
1200 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001201 getTok().getLoc(),
1202 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001203 ActiveMacros.push_back(MI);
1204
1205 // Jump to the macro instantiation and prime the lexer.
1206 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1207 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1208 Lex();
1209
1210 return false;
1211}
1212
1213void AsmParser::HandleMacroExit() {
1214 // Jump to the EndOfStatement we should return to, and consume it.
1215 JumpToLoc(ActiveMacros.back()->ExitLoc);
1216 Lex();
1217
1218 // Pop the instantiation entry.
1219 delete ActiveMacros.back();
1220 ActiveMacros.pop_back();
1221}
1222
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001223static void MarkUsed(const MCExpr *Value) {
1224 switch (Value->getKind()) {
1225 case MCExpr::Binary:
1226 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1227 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1228 break;
1229 case MCExpr::Target:
1230 case MCExpr::Constant:
1231 break;
1232 case MCExpr::SymbolRef: {
1233 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1234 break;
1235 }
1236 case MCExpr::Unary:
1237 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1238 break;
1239 }
1240}
1241
Benjamin Kramer38e59892010-07-14 22:38:02 +00001242bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001243 // FIXME: Use better location, we should use proper tokens.
1244 SMLoc EqualLoc = Lexer.getLoc();
1245
Daniel Dunbar821e3332009-08-31 08:09:28 +00001246 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001247 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001248 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001249
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001250 MarkUsed(Value);
1251
Daniel Dunbar3f872332009-07-28 16:08:33 +00001252 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001253 return TokError("unexpected token in assignment");
1254
1255 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001256 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001257
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001258 // Validate that the LHS is allowed to be a variable (either it has not been
1259 // used as a symbol, or it is an absolute symbol).
1260 MCSymbol *Sym = getContext().LookupSymbol(Name);
1261 if (Sym) {
1262 // Diagnose assignment to a label.
1263 //
1264 // FIXME: Diagnostics. Note the location of the definition as a label.
1265 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001266 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001267 ; // Allow redefinitions of undefined symbols only used in directives.
1268 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001269 return Error(EqualLoc, "redefinition of '" + Name + "'");
1270 else if (!Sym->isVariable())
1271 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001272 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001273 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1274 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001275
1276 // Don't count these checks as uses.
1277 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001278 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001279 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001280
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001281 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001282
1283 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001284 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001285
1286 return false;
1287}
1288
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001289/// ParseIdentifier:
1290/// ::= identifier
1291/// ::= string
1292bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001293 // The assembler has relaxed rules for accepting identifiers, in particular we
1294 // allow things like '.globl $foo', which would normally be separate
1295 // tokens. At this level, we have already lexed so we cannot (currently)
1296 // handle this as a context dependent token, instead we detect adjacent tokens
1297 // and return the combined identifier.
1298 if (Lexer.is(AsmToken::Dollar)) {
1299 SMLoc DollarLoc = getLexer().getLoc();
1300
1301 // Consume the dollar sign, and check for a following identifier.
1302 Lex();
1303 if (Lexer.isNot(AsmToken::Identifier))
1304 return true;
1305
1306 // We have a '$' followed by an identifier, make sure they are adjacent.
1307 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1308 return true;
1309
1310 // Construct the joined identifier and consume the token.
1311 Res = StringRef(DollarLoc.getPointer(),
1312 getTok().getIdentifier().size() + 1);
1313 Lex();
1314 return false;
1315 }
1316
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001317 if (Lexer.isNot(AsmToken::Identifier) &&
1318 Lexer.isNot(AsmToken::String))
1319 return true;
1320
Sean Callanan18b83232010-01-19 21:44:56 +00001321 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001322
Sean Callanan79ed1a82010-01-19 20:22:31 +00001323 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001324
1325 return false;
1326}
1327
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001328/// ParseDirectiveSet:
1329/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001330bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001331 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001332
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001333 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001334 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001335
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001336 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001337 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001338 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001339
Daniel Dunbare2ace502009-08-31 08:09:09 +00001340 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001341}
1342
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001343bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001344 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001345
1346 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001347 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001348 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1349 if (Str[i] != '\\') {
1350 Data += Str[i];
1351 continue;
1352 }
1353
1354 // Recognize escaped characters. Note that this escape semantics currently
1355 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1356 ++i;
1357 if (i == e)
1358 return TokError("unexpected backslash at end of string");
1359
1360 // Recognize octal sequences.
1361 if ((unsigned) (Str[i] - '0') <= 7) {
1362 // Consume up to three octal characters.
1363 unsigned Value = Str[i] - '0';
1364
1365 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1366 ++i;
1367 Value = Value * 8 + (Str[i] - '0');
1368
1369 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1370 ++i;
1371 Value = Value * 8 + (Str[i] - '0');
1372 }
1373 }
1374
1375 if (Value > 255)
1376 return TokError("invalid octal escape sequence (out of range)");
1377
1378 Data += (unsigned char) Value;
1379 continue;
1380 }
1381
1382 // Otherwise recognize individual escapes.
1383 switch (Str[i]) {
1384 default:
1385 // Just reject invalid escape sequences for now.
1386 return TokError("invalid escape sequence (unrecognized character)");
1387
1388 case 'b': Data += '\b'; break;
1389 case 'f': Data += '\f'; break;
1390 case 'n': Data += '\n'; break;
1391 case 'r': Data += '\r'; break;
1392 case 't': Data += '\t'; break;
1393 case '"': Data += '"'; break;
1394 case '\\': Data += '\\'; break;
1395 }
1396 }
1397
1398 return false;
1399}
1400
Daniel Dunbara0d14262009-06-24 23:30:00 +00001401/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001402/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1403bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001404 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001405 CheckForValidSection();
1406
Daniel Dunbara0d14262009-06-24 23:30:00 +00001407 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001408 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001409 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001410
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001411 std::string Data;
1412 if (ParseEscapedString(Data))
1413 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001414
1415 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001416 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001417 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1418
Sean Callanan79ed1a82010-01-19 20:22:31 +00001419 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001420
1421 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001422 break;
1423
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001424 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001425 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001426 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001427 }
1428 }
1429
Sean Callanan79ed1a82010-01-19 20:22:31 +00001430 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001431 return false;
1432}
1433
1434/// ParseDirectiveValue
1435/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1436bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001437 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001438 CheckForValidSection();
1439
Daniel Dunbara0d14262009-06-24 23:30:00 +00001440 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001441 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001442 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001443 return true;
1444
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001445 // Special case constant expressions to match code generator.
1446 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001447 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001448 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001449 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001450
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001451 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001452 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001453
Daniel Dunbara0d14262009-06-24 23:30:00 +00001454 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001455 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001456 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001457 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001458 }
1459 }
1460
Sean Callanan79ed1a82010-01-19 20:22:31 +00001461 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001462 return false;
1463}
1464
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001465/// ParseDirectiveRealValue
1466/// ::= (.single | .double) [ expression (, expression)* ]
1467bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1468 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1469 CheckForValidSection();
1470
1471 for (;;) {
1472 // We don't truly support arithmetic on floating point expressions, so we
1473 // have to manually parse unary prefixes.
1474 bool IsNeg = false;
1475 if (getLexer().is(AsmToken::Minus)) {
1476 Lex();
1477 IsNeg = true;
1478 } else if (getLexer().is(AsmToken::Plus))
1479 Lex();
1480
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001481 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001482 getLexer().isNot(AsmToken::Real))
1483 return TokError("unexpected token in directive");
1484
1485 // Convert to an APFloat.
1486 APFloat Value(Semantics);
1487 if (Value.convertFromString(getTok().getString(),
1488 APFloat::rmNearestTiesToEven) ==
1489 APFloat::opInvalidOp)
1490 return TokError("invalid floating point literal");
1491 if (IsNeg)
1492 Value.changeSign();
1493
1494 // Consume the numeric token.
1495 Lex();
1496
1497 // Emit the value as an integer.
1498 APInt AsInt = Value.bitcastToAPInt();
1499 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1500 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1501
1502 if (getLexer().is(AsmToken::EndOfStatement))
1503 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001504
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001505 if (getLexer().isNot(AsmToken::Comma))
1506 return TokError("unexpected token in directive");
1507 Lex();
1508 }
1509 }
1510
1511 Lex();
1512 return false;
1513}
1514
Daniel Dunbara0d14262009-06-24 23:30:00 +00001515/// ParseDirectiveSpace
1516/// ::= .space expression [ , expression ]
1517bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001518 CheckForValidSection();
1519
Daniel Dunbara0d14262009-06-24 23:30:00 +00001520 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001521 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001522 return true;
1523
1524 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001525 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1526 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001527 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001528 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001529
Daniel Dunbar475839e2009-06-29 20:37:27 +00001530 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001531 return true;
1532
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001533 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001534 return TokError("unexpected token in '.space' directive");
1535 }
1536
Sean Callanan79ed1a82010-01-19 20:22:31 +00001537 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001538
1539 if (NumBytes <= 0)
1540 return TokError("invalid number of bytes in '.space' directive");
1541
1542 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001543 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001544
1545 return false;
1546}
1547
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001548/// ParseDirectiveZero
1549/// ::= .zero expression
1550bool AsmParser::ParseDirectiveZero() {
1551 CheckForValidSection();
1552
1553 int64_t NumBytes;
1554 if (ParseAbsoluteExpression(NumBytes))
1555 return true;
1556
Rafael Espindolae452b172010-10-05 19:42:57 +00001557 int64_t Val = 0;
1558 if (getLexer().is(AsmToken::Comma)) {
1559 Lex();
1560 if (ParseAbsoluteExpression(Val))
1561 return true;
1562 }
1563
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001564 if (getLexer().isNot(AsmToken::EndOfStatement))
1565 return TokError("unexpected token in '.zero' directive");
1566
1567 Lex();
1568
Rafael Espindolae452b172010-10-05 19:42:57 +00001569 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001570
1571 return false;
1572}
1573
Daniel Dunbara0d14262009-06-24 23:30:00 +00001574/// ParseDirectiveFill
1575/// ::= .fill expression , expression , expression
1576bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001577 CheckForValidSection();
1578
Daniel Dunbara0d14262009-06-24 23:30:00 +00001579 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001580 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001581 return true;
1582
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001584 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001585 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001586
Daniel Dunbara0d14262009-06-24 23:30:00 +00001587 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001588 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001589 return true;
1590
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001591 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001592 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001593 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001594
Daniel Dunbara0d14262009-06-24 23:30:00 +00001595 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001596 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001597 return true;
1598
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001599 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001600 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001601
Sean Callanan79ed1a82010-01-19 20:22:31 +00001602 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001603
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001604 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1605 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001606
1607 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001608 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001609
1610 return false;
1611}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001612
1613/// ParseDirectiveOrg
1614/// ::= .org expression [ , expression ]
1615bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001616 CheckForValidSection();
1617
Daniel Dunbar821e3332009-08-31 08:09:28 +00001618 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001619 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001620 return true;
1621
1622 // Parse optional fill expression.
1623 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001624 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1625 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001626 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001627 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001628
Daniel Dunbar475839e2009-06-29 20:37:27 +00001629 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001630 return true;
1631
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001632 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001633 return TokError("unexpected token in '.org' directive");
1634 }
1635
Sean Callanan79ed1a82010-01-19 20:22:31 +00001636 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001637
1638 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1639 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001640 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001641
1642 return false;
1643}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001644
1645/// ParseDirectiveAlign
1646/// ::= {.align, ...} expression [ , expression [ , expression ]]
1647bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001648 CheckForValidSection();
1649
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001650 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001651 int64_t Alignment;
1652 if (ParseAbsoluteExpression(Alignment))
1653 return true;
1654
1655 SMLoc MaxBytesLoc;
1656 bool HasFillExpr = false;
1657 int64_t FillExpr = 0;
1658 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001659 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1660 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001661 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001662 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001663
1664 // The fill expression can be omitted while specifying a maximum number of
1665 // alignment bytes, e.g:
1666 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001667 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001668 HasFillExpr = true;
1669 if (ParseAbsoluteExpression(FillExpr))
1670 return true;
1671 }
1672
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001673 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1674 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001675 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001676 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001677
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001678 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001679 if (ParseAbsoluteExpression(MaxBytesToFill))
1680 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001681
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001682 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001683 return TokError("unexpected token in directive");
1684 }
1685 }
1686
Sean Callanan79ed1a82010-01-19 20:22:31 +00001687 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001688
Daniel Dunbar648ac512010-05-17 21:54:30 +00001689 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001690 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001691
1692 // Compute alignment in bytes.
1693 if (IsPow2) {
1694 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001695 if (Alignment >= 32) {
1696 Error(AlignmentLoc, "invalid alignment value");
1697 Alignment = 31;
1698 }
1699
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001700 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001701 }
1702
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001703 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001704 if (MaxBytesLoc.isValid()) {
1705 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001706 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1707 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001708 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001709 }
1710
1711 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001712 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1713 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001714 MaxBytesToFill = 0;
1715 }
1716 }
1717
Daniel Dunbar648ac512010-05-17 21:54:30 +00001718 // Check whether we should use optimal code alignment for this .align
1719 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001720 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001721 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1722 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001723 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001724 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001725 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001726 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1727 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001728 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001729
1730 return false;
1731}
1732
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001733/// ParseDirectiveSymbolAttribute
1734/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001735bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001736 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001737 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001738 StringRef Name;
1739
1740 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001741 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001742
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001743 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001744
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001745 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001746
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001747 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001748 break;
1749
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001750 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001751 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001752 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001753 }
1754 }
1755
Sean Callanan79ed1a82010-01-19 20:22:31 +00001756 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001757 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001758}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001759
1760/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001761/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1762bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001763 CheckForValidSection();
1764
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001765 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001766 StringRef Name;
1767 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001768 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001769
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001770 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001771 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001772
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001773 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001774 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001775 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001776
1777 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001778 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001779 if (ParseAbsoluteExpression(Size))
1780 return true;
1781
1782 int64_t Pow2Alignment = 0;
1783 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001784 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001786 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001787 if (ParseAbsoluteExpression(Pow2Alignment))
1788 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001789
Chris Lattner258281d2010-01-19 06:22:22 +00001790 // If this target takes alignments in bytes (not log) validate and convert.
1791 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1792 if (!isPowerOf2_64(Pow2Alignment))
1793 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1794 Pow2Alignment = Log2_64(Pow2Alignment);
1795 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001796 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001797
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001798 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001799 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001800
Sean Callanan79ed1a82010-01-19 20:22:31 +00001801 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001802
Chris Lattner1fc3d752009-07-09 17:25:12 +00001803 // NOTE: a size of zero for a .comm should create a undefined symbol
1804 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001805 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001806 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1807 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001808
Eric Christopherc260a3e2010-05-14 01:38:54 +00001809 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001810 // may internally end up wanting an alignment in bytes.
1811 // FIXME: Diagnose overflow.
1812 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001813 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1814 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001815
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001816 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001817 return Error(IDLoc, "invalid symbol redefinition");
1818
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001819 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001820 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001821 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001822 getStreamer().EmitZerofill(Ctx.getMachOSection(
1823 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1824 0, SectionKind::getBSS()),
1825 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001826 return false;
1827 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001828
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001829 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001830 return false;
1831}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001832
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001833/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001834/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001835bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001836 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001837 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001838
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001839 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001840 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001841 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001842
Sean Callanan79ed1a82010-01-19 20:22:31 +00001843 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001844
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001845 if (Str.empty())
1846 Error(Loc, ".abort detected. Assembly stopping.");
1847 else
1848 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001849 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001850
1851 return false;
1852}
Kevin Enderby71148242009-07-14 21:35:03 +00001853
Kevin Enderby1f049b22009-07-14 23:21:55 +00001854/// ParseDirectiveInclude
1855/// ::= .include "filename"
1856bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001857 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001858 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001859
Sean Callanan18b83232010-01-19 21:44:56 +00001860 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001861 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001862 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001863
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001864 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001865 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001866
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001867 // Strip the quotes.
1868 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001869
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001870 // Attempt to switch the lexer to the included file before consuming the end
1871 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001872 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001873 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001874 return true;
1875 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001876
1877 return false;
1878}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001879
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001880/// ParseDirectiveIf
1881/// ::= .if expression
1882bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001883 TheCondStack.push_back(TheCondState);
1884 TheCondState.TheCond = AsmCond::IfCond;
1885 if(TheCondState.Ignore) {
1886 EatToEndOfStatement();
1887 }
1888 else {
1889 int64_t ExprValue;
1890 if (ParseAbsoluteExpression(ExprValue))
1891 return true;
1892
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001893 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001894 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001895
Sean Callanan79ed1a82010-01-19 20:22:31 +00001896 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001897
1898 TheCondState.CondMet = ExprValue;
1899 TheCondState.Ignore = !TheCondState.CondMet;
1900 }
1901
1902 return false;
1903}
1904
1905/// ParseDirectiveElseIf
1906/// ::= .elseif expression
1907bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1908 if (TheCondState.TheCond != AsmCond::IfCond &&
1909 TheCondState.TheCond != AsmCond::ElseIfCond)
1910 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1911 " an .elseif");
1912 TheCondState.TheCond = AsmCond::ElseIfCond;
1913
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001914 bool LastIgnoreState = false;
1915 if (!TheCondStack.empty())
1916 LastIgnoreState = TheCondStack.back().Ignore;
1917 if (LastIgnoreState || TheCondState.CondMet) {
1918 TheCondState.Ignore = true;
1919 EatToEndOfStatement();
1920 }
1921 else {
1922 int64_t ExprValue;
1923 if (ParseAbsoluteExpression(ExprValue))
1924 return true;
1925
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001926 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001927 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001928
Sean Callanan79ed1a82010-01-19 20:22:31 +00001929 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001930 TheCondState.CondMet = ExprValue;
1931 TheCondState.Ignore = !TheCondState.CondMet;
1932 }
1933
1934 return false;
1935}
1936
1937/// ParseDirectiveElse
1938/// ::= .else
1939bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001940 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001941 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001942
Sean Callanan79ed1a82010-01-19 20:22:31 +00001943 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001944
1945 if (TheCondState.TheCond != AsmCond::IfCond &&
1946 TheCondState.TheCond != AsmCond::ElseIfCond)
1947 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1948 ".elseif");
1949 TheCondState.TheCond = AsmCond::ElseCond;
1950 bool LastIgnoreState = false;
1951 if (!TheCondStack.empty())
1952 LastIgnoreState = TheCondStack.back().Ignore;
1953 if (LastIgnoreState || TheCondState.CondMet)
1954 TheCondState.Ignore = true;
1955 else
1956 TheCondState.Ignore = false;
1957
1958 return false;
1959}
1960
1961/// ParseDirectiveEndIf
1962/// ::= .endif
1963bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001964 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001965 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001966
Sean Callanan79ed1a82010-01-19 20:22:31 +00001967 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001968
1969 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1970 TheCondStack.empty())
1971 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1972 ".else");
1973 if (!TheCondStack.empty()) {
1974 TheCondState = TheCondStack.back();
1975 TheCondStack.pop_back();
1976 }
1977
1978 return false;
1979}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001980
1981/// ParseDirectiveFile
1982/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001983bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001984 // FIXME: I'm not sure what this is.
1985 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001986 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001987 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001988 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001989 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001990
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001991 if (FileNumber < 1)
1992 return TokError("file number less than one");
1993 }
1994
Daniel Dunbareceec052010-07-12 17:45:27 +00001995 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001996 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001997
Chris Lattnerd32e8032010-01-25 19:02:58 +00001998 StringRef Filename = getTok().getString();
1999 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002000 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002001
Daniel Dunbareceec052010-07-12 17:45:27 +00002002 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002003 return TokError("unexpected token in '.file' directive");
2004
Chris Lattnerd32e8032010-01-25 19:02:58 +00002005 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002006 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002007 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002008 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002009 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002010 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002011
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002012 return false;
2013}
2014
2015/// ParseDirectiveLine
2016/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002017bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002018 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2019 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002020 return TokError("unexpected token in '.line' directive");
2021
Sean Callanan18b83232010-01-19 21:44:56 +00002022 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002023 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002024 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002025
2026 // FIXME: Do something with the .line.
2027 }
2028
Daniel Dunbareceec052010-07-12 17:45:27 +00002029 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002030 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002031
2032 return false;
2033}
2034
2035
2036/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002037/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002038/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2039/// The first number is a file number, must have been previously assigned with
2040/// a .file directive, the second number is the line number and optionally the
2041/// third number is a column position (zero if not specified). The remaining
2042/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002043bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002044
Daniel Dunbareceec052010-07-12 17:45:27 +00002045 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002046 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002047 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002048 if (FileNumber < 1)
2049 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002050 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002051 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002052 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002053
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002054 int64_t LineNumber = 0;
2055 if (getLexer().is(AsmToken::Integer)) {
2056 LineNumber = getTok().getIntVal();
2057 if (LineNumber < 1)
2058 return TokError("line number less than one in '.loc' directive");
2059 Lex();
2060 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002061
2062 int64_t ColumnPos = 0;
2063 if (getLexer().is(AsmToken::Integer)) {
2064 ColumnPos = getTok().getIntVal();
2065 if (ColumnPos < 0)
2066 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002067 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002068 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002069
Kevin Enderbyc0957932010-09-30 16:52:03 +00002070 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002071 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002072 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002073 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2074 for (;;) {
2075 if (getLexer().is(AsmToken::EndOfStatement))
2076 break;
2077
2078 StringRef Name;
2079 SMLoc Loc = getTok().getLoc();
2080 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002081 return TokError("unexpected token in '.loc' directive");
2082
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002083 if (Name == "basic_block")
2084 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2085 else if (Name == "prologue_end")
2086 Flags |= DWARF2_FLAG_PROLOGUE_END;
2087 else if (Name == "epilogue_begin")
2088 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2089 else if (Name == "is_stmt") {
2090 SMLoc Loc = getTok().getLoc();
2091 const MCExpr *Value;
2092 if (getParser().ParseExpression(Value))
2093 return true;
2094 // The expression must be the constant 0 or 1.
2095 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2096 int Value = MCE->getValue();
2097 if (Value == 0)
2098 Flags &= ~DWARF2_FLAG_IS_STMT;
2099 else if (Value == 1)
2100 Flags |= DWARF2_FLAG_IS_STMT;
2101 else
2102 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002103 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002104 else {
2105 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2106 }
2107 }
2108 else if (Name == "isa") {
2109 SMLoc Loc = getTok().getLoc();
2110 const MCExpr *Value;
2111 if (getParser().ParseExpression(Value))
2112 return true;
2113 // The expression must be a constant greater or equal to 0.
2114 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2115 int Value = MCE->getValue();
2116 if (Value < 0)
2117 return Error(Loc, "isa number less than zero");
2118 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002119 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002120 else {
2121 return Error(Loc, "isa number not a constant value");
2122 }
2123 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002124 else if (Name == "discriminator") {
2125 if (getParser().ParseAbsoluteExpression(Discriminator))
2126 return true;
2127 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002128 else {
2129 return Error(Loc, "unknown sub-directive in '.loc' directive");
2130 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002131
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002132 if (getLexer().is(AsmToken::EndOfStatement))
2133 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002134 }
2135 }
2136
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002137 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2138 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002139
2140 return false;
2141}
2142
Daniel Dunbar138abae2010-10-16 04:56:42 +00002143/// ParseDirectiveStabs
2144/// ::= .stabs string, number, number, number
2145bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2146 SMLoc DirectiveLoc) {
2147 return TokError("unsupported directive '" + Directive + "'");
2148}
2149
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002150/// ParseDirectiveCFIStartProc
2151/// ::= .cfi_startproc
2152bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2153 SMLoc DirectiveLoc) {
2154 return false;
2155}
2156
2157/// ParseDirectiveCFIEndProc
2158/// ::= .cfi_endproc
2159bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
2160 return false;
2161}
2162
2163/// ParseDirectiveCFIDefCfaOffset
2164/// ::= .cfi_def_cfa_offset offset
2165bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2166 SMLoc DirectiveLoc) {
2167 int64_t Offset = 0;
2168 if (getParser().ParseAbsoluteExpression(Offset))
2169 return true;
2170
2171 return false;
2172}
2173
2174/// ParseDirectiveCFIDefCfaRegister
2175/// ::= .cfi_def_cfa_register register
2176bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2177 SMLoc DirectiveLoc) {
2178 int64_t Register = 0;
2179 if (getParser().ParseAbsoluteExpression(Register))
2180 return true;
2181 return false;
2182}
2183
2184/// ParseDirectiveCFIOffset
2185/// ::= .cfi_off register, offset
2186bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2187 int64_t Register = 0;
2188 int64_t Offset = 0;
2189 if (getParser().ParseAbsoluteExpression(Register))
2190 return true;
2191
2192 if (getLexer().isNot(AsmToken::Comma))
2193 return TokError("unexpected token in directive");
2194 Lex();
2195
2196 if (getParser().ParseAbsoluteExpression(Offset))
2197 return true;
2198
2199 return false;
2200}
2201
2202/// ParseDirectiveCFIPersonalityOrLsda
2203/// ::= .cfi_personality encoding, [symbol_name]
2204/// ::= .cfi_lsda encoding, [symbol_name]
2205bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef,
2206 SMLoc DirectiveLoc) {
2207 int64_t Encoding = 0;
2208 if (getParser().ParseAbsoluteExpression(Encoding))
2209 return true;
2210 if (Encoding == 255)
2211 return false;
2212
2213 if (getLexer().isNot(AsmToken::Comma))
2214 return TokError("unexpected token in directive");
2215 Lex();
2216
2217 StringRef Name;
2218 if (getParser().ParseIdentifier(Name))
2219 return TokError("expected identifier in directive");
2220 return false;
2221}
2222
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002223/// ParseDirectiveMacrosOnOff
2224/// ::= .macros_on
2225/// ::= .macros_off
2226bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2227 SMLoc DirectiveLoc) {
2228 if (getLexer().isNot(AsmToken::EndOfStatement))
2229 return Error(getLexer().getLoc(),
2230 "unexpected token in '" + Directive + "' directive");
2231
2232 getParser().MacrosEnabled = Directive == ".macros_on";
2233
2234 return false;
2235}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002236
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002237/// ParseDirectiveMacro
2238/// ::= .macro name
2239bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2240 SMLoc DirectiveLoc) {
2241 StringRef Name;
2242 if (getParser().ParseIdentifier(Name))
2243 return TokError("expected identifier in directive");
2244
2245 if (getLexer().isNot(AsmToken::EndOfStatement))
2246 return TokError("unexpected token in '.macro' directive");
2247
2248 // Eat the end of statement.
2249 Lex();
2250
2251 AsmToken EndToken, StartToken = getTok();
2252
2253 // Lex the macro definition.
2254 for (;;) {
2255 // Check whether we have reached the end of the file.
2256 if (getLexer().is(AsmToken::Eof))
2257 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2258
2259 // Otherwise, check whether we have reach the .endmacro.
2260 if (getLexer().is(AsmToken::Identifier) &&
2261 (getTok().getIdentifier() == ".endm" ||
2262 getTok().getIdentifier() == ".endmacro")) {
2263 EndToken = getTok();
2264 Lex();
2265 if (getLexer().isNot(AsmToken::EndOfStatement))
2266 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2267 "' directive");
2268 break;
2269 }
2270
2271 // Otherwise, scan til the end of the statement.
2272 getParser().EatToEndOfStatement();
2273 }
2274
2275 if (getParser().MacroMap.lookup(Name)) {
2276 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2277 }
2278
2279 const char *BodyStart = StartToken.getLoc().getPointer();
2280 const char *BodyEnd = EndToken.getLoc().getPointer();
2281 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2282 getParser().MacroMap[Name] = new Macro(Name, Body);
2283 return false;
2284}
2285
2286/// ParseDirectiveEndMacro
2287/// ::= .endm
2288/// ::= .endmacro
2289bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2290 SMLoc DirectiveLoc) {
2291 if (getLexer().isNot(AsmToken::EndOfStatement))
2292 return TokError("unexpected token in '" + Directive + "' directive");
2293
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002294 // If we are inside a macro instantiation, terminate the current
2295 // instantiation.
2296 if (!getParser().ActiveMacros.empty()) {
2297 getParser().HandleMacroExit();
2298 return false;
2299 }
2300
2301 // Otherwise, this .endmacro is a stray entry in the file; well formed
2302 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002303 return TokError("unexpected '" + Directive + "' in file, "
2304 "no current macro definition");
2305}
2306
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002307bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002308 getParser().CheckForValidSection();
2309
2310 const MCExpr *Value;
2311
2312 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002313 return true;
2314
2315 if (getLexer().isNot(AsmToken::EndOfStatement))
2316 return TokError("unexpected token in directive");
2317
2318 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002319 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002320 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002321 getStreamer().EmitULEB128Value(Value);
2322
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002323 return false;
2324}
2325
2326
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002327/// \brief Create an MCAsmParser instance.
2328MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2329 MCContext &C, MCStreamer &Out,
2330 const MCAsmInfo &MAI) {
2331 return new AsmParser(T, SM, C, Out, MAI);
2332}