blob: cf11ca9c5a8cb1ff3f408d97bb4f992c33c17f16 [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"
Nick Lewycky476b2422010-12-19 20:43:38 +000034#include <cctype>
Daniel Dunbaraef87e32010-07-18 18:31:38 +000035#include <vector>
Chris Lattner27aa7d22009-06-21 20:16:42 +000036using namespace llvm;
37
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000038namespace {
39
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000040/// \brief Helper class for tracking macro definitions.
41struct Macro {
42 StringRef Name;
43 StringRef Body;
44
45public:
46 Macro(StringRef N, StringRef B) : Name(N), Body(B) {}
47};
48
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000049/// \brief Helper class for storing information about an active macro
50/// instantiation.
51struct MacroInstantiation {
52 /// The macro being instantiated.
53 const Macro *TheMacro;
54
55 /// The macro instantiation with substitutions.
56 MemoryBuffer *Instantiation;
57
58 /// The location of the instantiation.
59 SMLoc InstantiationLoc;
60
61 /// The location where parsing should resume upon instantiation completion.
62 SMLoc ExitLoc;
63
64public:
Daniel Dunbar7a570d02010-07-18 19:00:10 +000065 MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
66 const std::vector<std::vector<AsmToken> > &A);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000067};
68
Daniel Dunbaraef87e32010-07-18 18:31:38 +000069/// \brief The concrete assembly parser instance.
70class AsmParser : public MCAsmParser {
Daniel Dunbar3c802de2010-07-18 18:38:02 +000071 friend class GenericAsmParser;
72
Daniel Dunbaraef87e32010-07-18 18:31:38 +000073 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
74 void operator=(const AsmParser &); // DO NOT IMPLEMENT
75private:
76 AsmLexer Lexer;
77 MCContext &Ctx;
78 MCStreamer &Out;
79 SourceMgr &SrcMgr;
80 MCAsmParserExtension *GenericParser;
81 MCAsmParserExtension *PlatformParser;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000082
Daniel Dunbaraef87e32010-07-18 18:31:38 +000083 /// This is the current buffer index we're lexing from as managed by the
84 /// SourceMgr object.
85 int CurBuffer;
86
87 AsmCond TheCondState;
88 std::vector<AsmCond> TheCondStack;
89
90 /// DirectiveMap - This is a table handlers for directives. Each handler is
91 /// invoked after the directive identifier is read and is responsible for
92 /// parsing and validating the rest of the directive. The handler is passed
93 /// in the directive name and the location of the directive keyword.
94 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
Daniel Dunbar3c802de2010-07-18 18:38:02 +000095
Daniel Dunbar6d8cf082010-07-18 18:47:21 +000096 /// MacroMap - Map of currently defined macros.
97 StringMap<Macro*> MacroMap;
98
Daniel Dunbarc64a0d72010-07-18 18:54:11 +000099 /// ActiveMacros - Stack of active macro instantiations.
100 std::vector<MacroInstantiation*> ActiveMacros;
101
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000102 /// Boolean tracking whether macro substitution is enabled.
103 unsigned MacrosEnabled : 1;
104
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000105 /// Flag tracking whether any errors have been encountered.
106 unsigned HadError : 1;
107
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000108public:
109 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
110 const MCAsmInfo &MAI);
111 ~AsmParser();
112
113 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
114
115 void AddDirectiveHandler(MCAsmParserExtension *Object,
116 StringRef Directive,
117 DirectiveHandler Handler) {
118 DirectiveMap[Directive] = std::make_pair(Object, Handler);
119 }
120
121public:
122 /// @name MCAsmParser Interface
123 /// {
124
125 virtual SourceMgr &getSourceManager() { return SrcMgr; }
126 virtual MCAsmLexer &getLexer() { return Lexer; }
127 virtual MCContext &getContext() { return Ctx; }
128 virtual MCStreamer &getStreamer() { return Out; }
129
130 virtual void Warning(SMLoc L, const Twine &Meg);
131 virtual bool Error(SMLoc L, const Twine &Msg);
132
133 const AsmToken &Lex();
134
135 bool ParseExpression(const MCExpr *&Res);
136 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
137 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
138 virtual bool ParseAbsoluteExpression(int64_t &Res);
139
140 /// }
141
142private:
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000143 void CheckForValidSection();
144
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000145 bool ParseStatement();
146
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000147 bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
148 void HandleMacroExit();
149
150 void PrintMacroInstantiations();
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000151 void PrintMessage(SMLoc Loc, const Twine &Msg, const char *Type) const {
152 SrcMgr.PrintMessage(Loc, Msg, Type);
153 }
154
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000155 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
156 bool EnterIncludeFile(const std::string &Filename);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000157
158 /// \brief Reset the current lexer position to that given by \arg Loc. The
159 /// current token is not set; clients should ensure Lex() is called
160 /// subsequently.
161 void JumpToLoc(SMLoc Loc);
162
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000163 void EatToEndOfStatement();
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000164
165 /// \brief Parse up to the end of statement and a return the contents from the
166 /// current token until the end of the statement; the current token on exit
167 /// will be either the EndOfStatement or EOF.
168 StringRef ParseStringToEndOfStatement();
169
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000170 bool ParseAssignment(StringRef Name);
171
172 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
173 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
174 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
175
176 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
177 /// and set \arg Res to the identifier contents.
178 bool ParseIdentifier(StringRef &Res);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000179
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000180 // Directive Parsing.
Rafael Espindola787c3372010-10-28 20:02:27 +0000181
182 // ".ascii", ".asciiz", ".string"
183 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000184 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000185 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ...
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000186 bool ParseDirectiveFill(); // ".fill"
187 bool ParseDirectiveSpace(); // ".space"
Rafael Espindola2ea2ac72010-09-16 15:03:59 +0000188 bool ParseDirectiveZero(); // ".zero"
Roman Divacky50e7a782010-10-28 16:22:58 +0000189 bool ParseDirectiveSet(StringRef IDVal); // ".set" or ".equ"
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000190 bool ParseDirectiveOrg(); // ".org"
191 // ".align{,32}", ".p2align{,w,l}"
192 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
193
194 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
195 /// accepts a single symbol (which should be a label or an external).
196 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000197
198 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
199
200 bool ParseDirectiveAbort(); // ".abort"
201 bool ParseDirectiveInclude(); // ".include"
202
203 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
204 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
205 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
206 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
207
208 /// ParseEscapedString - Parse the current token as a string which may include
209 /// escaped characters and return the string contents.
210 bool ParseEscapedString(std::string &Data);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000211
212 const MCExpr *ApplyModifierToExpr(const MCExpr *E,
213 MCSymbolRefExpr::VariantKind Variant);
Daniel Dunbaraef87e32010-07-18 18:31:38 +0000214};
215
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000216/// \brief Generic implementations of directive handling, etc. which is shared
217/// (or the default, at least) for all assembler parser.
218class GenericAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000219 template<bool (GenericAsmParser::*Handler)(StringRef, SMLoc)>
220 void AddDirectiveHandler(StringRef Directive) {
221 getParser().AddDirectiveHandler(this, Directive,
222 HandleDirective<GenericAsmParser, Handler>);
223 }
224
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000225public:
226 GenericAsmParser() {}
227
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000228 AsmParser &getParser() {
229 return (AsmParser&) this->MCAsmParserExtension::getParser();
230 }
231
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000232 virtual void Initialize(MCAsmParser &Parser) {
233 // Call the base implementation.
234 this->MCAsmParserExtension::Initialize(Parser);
235
236 // Debugging directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000237 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveFile>(".file");
238 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLine>(".line");
239 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLoc>(".loc");
Daniel Dunbar138abae2010-10-16 04:56:42 +0000240 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveStabs>(".stabs");
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000241
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000242 // CFI directives.
243 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIStartProc>(
244 ".cfi_startproc");
245 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIEndProc>(
246 ".cfi_endproc");
247 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaOffset>(
248 ".cfi_def_cfa_offset");
249 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIDefCfaRegister>(
250 ".cfi_def_cfa_register");
251 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveCFIOffset>(
252 ".cfi_offset");
253 AddDirectiveHandler<
254 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_personality");
255 AddDirectiveHandler<
256 &GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda>(".cfi_lsda");
257
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000258 // Macro directives.
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +0000259 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
260 ".macros_on");
261 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacrosOnOff>(
262 ".macros_off");
263 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
264 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
265 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000266
267 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
268 AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000269 }
270
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000271 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
272 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc);
273 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar138abae2010-10-16 04:56:42 +0000274 bool ParseDirectiveStabs(StringRef, SMLoc DirectiveLoc);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +0000275 bool ParseDirectiveCFIStartProc(StringRef, SMLoc DirectiveLoc);
276 bool ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc);
277 bool ParseDirectiveCFIDefCfaOffset(StringRef, SMLoc DirectiveLoc);
278 bool ParseDirectiveCFIDefCfaRegister(StringRef, SMLoc DirectiveLoc);
279 bool ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc);
280 bool ParseDirectiveCFIPersonalityOrLsda(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000281
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000282 bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
Daniel Dunbar6d8cf082010-07-18 18:47:21 +0000283 bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
284 bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000285
Rafael Espindolab98ac2a2010-09-11 16:45:15 +0000286 bool ParseDirectiveLEB128(StringRef, SMLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000287};
288
289}
290
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000291namespace llvm {
292
293extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +0000294extern MCAsmParserExtension *createELFAsmParser();
Michael J. Spencer7d490042010-10-09 11:01:07 +0000295extern MCAsmParserExtension *createCOFFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000296
297}
298
Chris Lattneraaec2052010-01-19 19:46:13 +0000299enum { DEFAULT_ADDRSPACE = 0 };
300
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000301AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
302 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000303 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000304 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbar3c802de2010-07-18 18:38:02 +0000305 CurBuffer(0), MacrosEnabled(true) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000306 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000307
308 // Initialize the generic parser.
309 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000310
311 // Initialize the platform / file format parser.
312 //
313 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
314 // created.
Michael J. Spencer7d490042010-10-09 11:01:07 +0000315 if (_MAI.hasMicrosoftFastStdCallMangling()) {
316 PlatformParser = createCOFFAsmParser();
317 PlatformParser->Initialize(*this);
318 } else if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000319 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000320 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000321 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +0000322 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000323 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000324 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000325}
326
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000327AsmParser::~AsmParser() {
Daniel Dunbar56491302010-07-29 01:51:55 +0000328 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!");
329
330 // Destroy any macros.
331 for (StringMap<Macro*>::iterator it = MacroMap.begin(),
332 ie = MacroMap.end(); it != ie; ++it)
333 delete it->getValue();
334
Daniel Dunbare4749702010-07-12 18:12:02 +0000335 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000336 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000337}
338
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000339void AsmParser::PrintMacroInstantiations() {
340 // Print the active macro instantiation stack.
341 for (std::vector<MacroInstantiation*>::const_reverse_iterator
342 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it)
343 PrintMessage((*it)->InstantiationLoc, "while in macro instantiation",
344 "note");
345}
346
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000347void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000348 PrintMessage(L, Msg, "warning");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000349 PrintMacroInstantiations();
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000350}
351
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000352bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000353 HadError = true;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000354 PrintMessage(L, Msg, "error");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000355 PrintMacroInstantiations();
Chris Lattner14ee48a2009-06-21 21:22:11 +0000356 return true;
357}
358
Sean Callananfd0b0282010-01-21 00:19:58 +0000359bool AsmParser::EnterIncludeFile(const std::string &Filename) {
360 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
361 if (NewBuf == -1)
362 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000363
Sean Callananfd0b0282010-01-21 00:19:58 +0000364 CurBuffer = NewBuf;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000365
Sean Callananfd0b0282010-01-21 00:19:58 +0000366 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000367
Sean Callananfd0b0282010-01-21 00:19:58 +0000368 return false;
369}
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000370
371void AsmParser::JumpToLoc(SMLoc Loc) {
372 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
373 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
374}
375
Sean Callananfd0b0282010-01-21 00:19:58 +0000376const AsmToken &AsmParser::Lex() {
377 const AsmToken *tok = &Lexer.Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000378
Sean Callananfd0b0282010-01-21 00:19:58 +0000379 if (tok->is(AsmToken::Eof)) {
380 // If this is the end of an included file, pop the parent file off the
381 // include stack.
382 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
383 if (ParentIncludeLoc != SMLoc()) {
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000384 JumpToLoc(ParentIncludeLoc);
Sean Callananfd0b0282010-01-21 00:19:58 +0000385 tok = &Lexer.Lex();
386 }
387 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000388
Sean Callananfd0b0282010-01-21 00:19:58 +0000389 if (tok->is(AsmToken::Error))
Daniel Dunbar275ce392010-07-18 18:31:45 +0000390 Error(Lexer.getErrLoc(), Lexer.getErr());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000391
Sean Callananfd0b0282010-01-21 00:19:58 +0000392 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000393}
394
Chris Lattner79180e22010-04-05 23:15:42 +0000395bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000396 // Create the initial section, if requested.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000397 if (!NoInitialTextSection)
Rafael Espindolad80781b2010-09-15 21:48:40 +0000398 Out.InitSections();
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000399
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000400 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000401 Lex();
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000402
403 HadError = false;
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000404 AsmCond StartingCondState = TheCondState;
405
Chris Lattnerb717fb02009-07-02 21:53:43 +0000406 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000407 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000408 if (!ParseStatement()) continue;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000409
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000410 // We had an error, validate that one was emitted and recover by skipping to
411 // the next line.
412 assert(HadError && "Parse statement returned an error, but none emitted!");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000413 EatToEndOfStatement();
414 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000415
416 if (TheCondState.TheCond != StartingCondState.TheCond ||
417 TheCondState.Ignore != StartingCondState.Ignore)
418 return TokError("unmatched .ifs or .elses");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000419
420 // Check to see there are no empty DwarfFile slots.
421 const std::vector<MCDwarfFile *> &MCDwarfFiles =
422 getContext().getMCDwarfFiles();
423 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Daniel Dunbar93bd4d12010-09-09 22:42:56 +0000424 if (!MCDwarfFiles[i])
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000425 TokError("unassigned file number: " + Twine(i) + " for .file directives");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000426 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000427
Chris Lattner79180e22010-04-05 23:15:42 +0000428 // Finalize the output stream if there are no errors and if the client wants
429 // us to.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000430 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000431 Out.Finish();
432
Chris Lattnerb717fb02009-07-02 21:53:43 +0000433 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000434}
435
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000436void AsmParser::CheckForValidSection() {
437 if (!getStreamer().getCurrentSection()) {
438 TokError("expected section directive before assembly directive");
439 Out.SwitchSection(Ctx.getMachOSection(
440 "__TEXT", "__text",
441 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
442 0, SectionKind::getText()));
443 }
444}
445
Chris Lattner2cf5f142009-06-22 01:29:09 +0000446/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
447void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 while (Lexer.isNot(AsmToken::EndOfStatement) &&
449 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000450 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000451
Chris Lattner2cf5f142009-06-22 01:29:09 +0000452 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000454 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000455}
456
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000457StringRef AsmParser::ParseStringToEndOfStatement() {
458 const char *Start = getTok().getLoc().getPointer();
459
460 while (Lexer.isNot(AsmToken::EndOfStatement) &&
461 Lexer.isNot(AsmToken::Eof))
462 Lex();
463
464 const char *End = getTok().getLoc().getPointer();
465 return StringRef(Start, End - Start);
466}
Chris Lattnerc4193832009-06-22 05:51:26 +0000467
Chris Lattner74ec1a32009-06-22 06:32:03 +0000468/// ParseParenExpr - Parse a paren expression and return it.
469/// NOTE: This assumes the leading '(' has already been consumed.
470///
471/// parenexpr ::= expr)
472///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000473bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000474 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000475 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000476 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000477 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000478 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000479 return false;
480}
Chris Lattnerc4193832009-06-22 05:51:26 +0000481
Chris Lattner74ec1a32009-06-22 06:32:03 +0000482/// ParsePrimaryExpr - Parse a primary expression and return it.
483/// primaryexpr ::= (parenexpr
484/// primaryexpr ::= symbol
485/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000486/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000487/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000488bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000489 switch (Lexer.getKind()) {
490 default:
491 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000492 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000493 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000494 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000495 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000496 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000497 return false;
Daniel Dunbare17edff2010-08-24 19:13:42 +0000498 case AsmToken::Dollar:
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000499 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000500 case AsmToken::Identifier: {
Daniel Dunbare17edff2010-08-24 19:13:42 +0000501 EndLoc = Lexer.getLoc();
502
503 StringRef Identifier;
504 if (ParseIdentifier(Identifier))
505 return false;
506
Daniel Dunbarfffff912009-10-16 01:34:54 +0000507 // This is a symbol reference.
Daniel Dunbare17edff2010-08-24 19:13:42 +0000508 std::pair<StringRef, StringRef> Split = Identifier.split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000509 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000510
511 // Lookup the symbol variant if used.
512 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000513 if (Split.first.size() != Identifier.size()) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000514 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
Daniel Dunbarcceba832010-09-17 02:47:07 +0000515 if (Variant == MCSymbolRefExpr::VK_Invalid) {
516 Variant = MCSymbolRefExpr::VK_None;
517 TokError("invalid variant '" + Split.second + "'");
518 }
519 }
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000520
Daniel Dunbarfffff912009-10-16 01:34:54 +0000521 // If this is an absolute variable reference, substitute it now to preserve
522 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000523 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000524 if (Variant)
Daniel Dunbar603abd52010-11-08 17:53:02 +0000525 return Error(EndLoc, "unexpected modifier on variable reference");
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000526
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000527 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000528 return false;
529 }
530
531 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000532 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000533 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000534 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000535 case AsmToken::Integer: {
536 SMLoc Loc = getTok().getLoc();
537 int64_t IntVal = getTok().getIntVal();
538 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000539 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000540 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000541 // Look for 'b' or 'f' following an Integer as a directional label
542 if (Lexer.getKind() == AsmToken::Identifier) {
543 StringRef IDVal = getTok().getString();
544 if (IDVal == "f" || IDVal == "b"){
545 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
546 IDVal == "f" ? 1 : 0);
547 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
548 getContext());
549 if(IDVal == "b" && Sym->isUndefined())
550 return Error(Loc, "invalid reference to undefined symbol");
551 EndLoc = Lexer.getLoc();
552 Lex(); // Eat identifier.
553 }
554 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000555 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000556 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000557 case AsmToken::Dot: {
558 // This is a '.' reference, which references the current PC. Emit a
559 // temporary label to the streamer and refer to it.
560 MCSymbol *Sym = Ctx.CreateTempSymbol();
561 Out.EmitLabel(Sym);
562 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
563 EndLoc = Lexer.getLoc();
564 Lex(); // Eat identifier.
565 return false;
566 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000567
Daniel Dunbar3f872332009-07-28 16:08:33 +0000568 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000569 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000570 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000571 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000572 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000573 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000574 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000575 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000576 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000577 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000578 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000579 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000580 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000581 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000582 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000583 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000584 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000585 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000586 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000587 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000588 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000589 }
590}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000591
Chris Lattnerb4307b32010-01-15 19:28:38 +0000592bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000593 SMLoc EndLoc;
594 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000595}
596
Daniel Dunbarcceba832010-09-17 02:47:07 +0000597const MCExpr *
598AsmParser::ApplyModifierToExpr(const MCExpr *E,
599 MCSymbolRefExpr::VariantKind Variant) {
600 // Recurse over the given expression, rebuilding it to apply the given variant
601 // if there is exactly one symbol.
602 switch (E->getKind()) {
603 case MCExpr::Target:
604 case MCExpr::Constant:
605 return 0;
606
607 case MCExpr::SymbolRef: {
608 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
609
610 if (SRE->getKind() != MCSymbolRefExpr::VK_None) {
611 TokError("invalid variant on expression '" +
612 getTok().getIdentifier() + "' (already modified)");
613 return E;
614 }
615
616 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
617 }
618
619 case MCExpr::Unary: {
620 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
621 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant);
622 if (!Sub)
623 return 0;
624 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext());
625 }
626
627 case MCExpr::Binary: {
628 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
629 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant);
630 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant);
631
632 if (!LHS && !RHS)
633 return 0;
634
635 if (!LHS) LHS = BE->getLHS();
636 if (!RHS) RHS = BE->getRHS();
637
638 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
639 }
640 }
Daniel Dunbarf3f95c92010-09-17 16:34:24 +0000641
642 assert(0 && "Invalid expression kind!");
643 return 0;
Daniel Dunbarcceba832010-09-17 02:47:07 +0000644}
645
Chris Lattner74ec1a32009-06-22 06:32:03 +0000646/// ParseExpression - Parse an expression and return it.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000647///
Chris Lattner74ec1a32009-06-22 06:32:03 +0000648/// expr ::= expr +,- expr -> lowest.
649/// expr ::= expr |,^,&,! expr -> middle.
650/// expr ::= expr *,/,%,<<,>> expr -> highest.
651/// expr ::= primaryexpr
652///
Chris Lattner54482b42010-01-15 19:39:23 +0000653bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000654 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000655 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000656 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
657 return true;
658
Daniel Dunbarcceba832010-09-17 02:47:07 +0000659 // As a special case, we support 'a op b @ modifier' by rewriting the
660 // expression to include the modifier. This is inefficient, but in general we
661 // expect users to use 'a@modifier op b'.
662 if (Lexer.getKind() == AsmToken::At) {
663 Lex();
664
665 if (Lexer.isNot(AsmToken::Identifier))
666 return TokError("unexpected symbol modifier following '@'");
667
668 MCSymbolRefExpr::VariantKind Variant =
669 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier());
670 if (Variant == MCSymbolRefExpr::VK_Invalid)
671 return TokError("invalid variant '" + getTok().getIdentifier() + "'");
672
673 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant);
674 if (!ModifiedRes) {
675 return TokError("invalid modifier '" + getTok().getIdentifier() +
676 "' (no symbols present)");
677 return true;
678 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000679
Daniel Dunbarcceba832010-09-17 02:47:07 +0000680 Res = ModifiedRes;
681 Lex();
682 }
683
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000684 // Try to constant fold it up front, if possible.
685 int64_t Value;
686 if (Res->EvaluateAsAbsolute(Value))
687 Res = MCConstantExpr::Create(Value, getContext());
688
689 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000690}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000691
Chris Lattnerb4307b32010-01-15 19:28:38 +0000692bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000693 Res = 0;
694 return ParseParenExpr(Res, EndLoc) ||
695 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000696}
697
Daniel Dunbar475839e2009-06-29 20:37:27 +0000698bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000699 const MCExpr *Expr;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000700
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000701 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000702 if (ParseExpression(Expr))
703 return true;
704
Daniel Dunbare00b0112009-10-16 01:57:52 +0000705 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000706 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000707
708 return false;
709}
710
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000711static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000712 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000713 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000714 default:
715 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000716
Daniel Dunbarcceba832010-09-17 02:47:07 +0000717 // Lowest Precedence: &&, ||, @
Daniel Dunbar3f872332009-07-28 16:08:33 +0000718 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000719 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000720 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000721 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000722 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000723 return 1;
724
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000725
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000726 // Low Precedence: |, &, ^
Daniel Dunbar475839e2009-06-29 20:37:27 +0000727 //
728 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000729 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000730 Kind = MCBinaryExpr::Or;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000731 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000732 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000733 Kind = MCBinaryExpr::Xor;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000734 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000735 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000736 Kind = MCBinaryExpr::And;
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000737 return 2;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000738
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000739 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >=
Chris Lattnerf7d4da02010-09-22 05:05:16 +0000740 case AsmToken::EqualEqual:
741 Kind = MCBinaryExpr::EQ;
742 return 3;
743 case AsmToken::ExclaimEqual:
744 case AsmToken::LessGreater:
745 Kind = MCBinaryExpr::NE;
746 return 3;
747 case AsmToken::Less:
748 Kind = MCBinaryExpr::LT;
749 return 3;
750 case AsmToken::LessEqual:
751 Kind = MCBinaryExpr::LTE;
752 return 3;
753 case AsmToken::Greater:
754 Kind = MCBinaryExpr::GT;
755 return 3;
756 case AsmToken::GreaterEqual:
757 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000758 return 3;
759
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000760 // High Intermediate Precedence: +, -
761 case AsmToken::Plus:
762 Kind = MCBinaryExpr::Add;
763 return 4;
764 case AsmToken::Minus:
765 Kind = MCBinaryExpr::Sub;
766 return 4;
767
Daniel Dunbar475839e2009-06-29 20:37:27 +0000768 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000769 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000770 Kind = MCBinaryExpr::Mul;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000771 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000772 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000773 Kind = MCBinaryExpr::Div;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000774 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000775 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000776 Kind = MCBinaryExpr::Mod;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000777 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000778 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000779 Kind = MCBinaryExpr::Shl;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000780 return 5;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000782 Kind = MCBinaryExpr::Shr;
Daniel Dunbarb1e0f762010-10-25 20:18:56 +0000783 return 5;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000784 }
785}
786
787
788/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
789/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000790bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
791 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000792 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000793 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000794 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000795
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000796 // If the next token is lower precedence than we are allowed to eat, return
797 // successfully with what we ate already.
798 if (TokPrec < Precedence)
799 return false;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000800
Sean Callanan79ed1a82010-01-19 20:22:31 +0000801 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000802
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000803 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000804 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000805 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000806
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000807 // If BinOp binds less tightly with RHS than the operator after RHS, let
808 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000809 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000810 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000811 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000812 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000813 }
814
Daniel Dunbar475839e2009-06-29 20:37:27 +0000815 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000816 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000817 }
818}
819
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000820
821
822
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000823/// ParseStatement:
824/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000825/// ::= Label* Directive ...Operands... EndOfStatement
826/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000827bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000828 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000829 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000830 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000831 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000832 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000833
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000834 // Statements always start with an identifier or are a full line comment.
Sean Callanan18b83232010-01-19 21:44:56 +0000835 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000836 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000837 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000838 int64_t LocalLabelVal = -1;
Kevin Enderbyd82ed5b2010-12-24 00:12:02 +0000839 // A full line comment is a '#' as the first token.
840 if (Lexer.is(AsmToken::Hash)) {
841 EatToEndOfStatement();
842 return false;
843 }
844 // Allow an integer followed by a ':' as a directional local label.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000845 if (Lexer.is(AsmToken::Integer)) {
846 LocalLabelVal = getTok().getIntVal();
847 if (LocalLabelVal < 0) {
848 if (!TheCondState.Ignore)
849 return TokError("unexpected token at start of statement");
850 IDVal = "";
851 }
852 else {
853 IDVal = getTok().getString();
854 Lex(); // Consume the integer token to be used as an identifier token.
855 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000856 if (!TheCondState.Ignore)
857 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000858 }
859 }
860 }
861 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000862 if (!TheCondState.Ignore)
863 return TokError("unexpected token at start of statement");
864 IDVal = "";
865 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000866
Chris Lattner7834fac2010-04-17 18:14:27 +0000867 // Handle conditional assembly here before checking for skipping. We
868 // have to do this so that .endif isn't skipped in a ".if 0" block for
869 // example.
870 if (IDVal == ".if")
871 return ParseDirectiveIf(IDLoc);
872 if (IDVal == ".elseif")
873 return ParseDirectiveElseIf(IDLoc);
874 if (IDVal == ".else")
875 return ParseDirectiveElse(IDLoc);
876 if (IDVal == ".endif")
877 return ParseDirectiveEndIf(IDLoc);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000878
Chris Lattner7834fac2010-04-17 18:14:27 +0000879 // If we are in a ".if 0" block, ignore this statement.
880 if (TheCondState.Ignore) {
881 EatToEndOfStatement();
882 return false;
883 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000884
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000885 // FIXME: Recurse on local labels?
886
887 // See what kind of statement we have.
888 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000889 case AsmToken::Colon: {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +0000890 CheckForValidSection();
891
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000892 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000893 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000894
895 // Diagnose attempt to use a variable as a label.
896 //
897 // FIXME: Diagnostics. Note the location of the definition as a label.
898 // FIXME: This doesn't diagnose assignment to a symbol which has been
899 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000900 MCSymbol *Sym;
901 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000902 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000903 else
904 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000905 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000906 return Error(IDLoc, "invalid symbol redefinition");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000907
Daniel Dunbar959fd882009-08-26 22:13:22 +0000908 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000909 Out.EmitLabel(Sym);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000910
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000911 // Consume any end of statement token, if present, to avoid spurious
912 // AddBlankLine calls().
913 if (Lexer.is(AsmToken::EndOfStatement)) {
914 Lex();
915 if (Lexer.is(AsmToken::Eof))
916 return false;
917 }
918
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000919 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000920 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000921
Daniel Dunbar3f872332009-07-28 16:08:33 +0000922 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000923 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000924 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000925
Daniel Dunbare2ace502009-08-31 08:09:09 +0000926 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000927
928 default: // Normal instruction or directive.
929 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000930 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +0000931
932 // If macros are enabled, check to see if this is a macro instantiation.
933 if (MacrosEnabled)
934 if (const Macro *M = MacroMap.lookup(IDVal))
935 return HandleMacroEntry(IDVal, IDLoc, M);
936
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000937 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000938 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000939 // Assembler features
Roman Divacky50e7a782010-10-28 16:22:58 +0000940 if (IDVal == ".set" || IDVal == ".equ")
941 return ParseDirectiveSet(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000942
Daniel Dunbara0d14262009-06-24 23:30:00 +0000943 // Data directives
944
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000945 if (IDVal == ".ascii")
Rafael Espindola787c3372010-10-28 20:02:27 +0000946 return ParseDirectiveAscii(IDVal, false);
947 if (IDVal == ".asciz" || IDVal == ".string")
948 return ParseDirectiveAscii(IDVal, true);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000949
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000950 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000951 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000952 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000953 return ParseDirectiveValue(2);
Rafael Espindolacc3acee2010-11-01 15:29:07 +0000954 if (IDVal == ".value")
955 return ParseDirectiveValue(2);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000956 if (IDVal == ".2byte")
957 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000958 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959 return ParseDirectiveValue(4);
Rafael Espindola435279b2010-11-17 16:24:40 +0000960 if (IDVal == ".int")
961 return ParseDirectiveValue(4);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000962 if (IDVal == ".4byte")
963 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000964 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000965 return ParseDirectiveValue(8);
Rafael Espindola110f22a2010-11-17 16:15:42 +0000966 if (IDVal == ".8byte")
967 return ParseDirectiveValue(8);
Daniel Dunbarb95a0792010-09-24 01:59:56 +0000968 if (IDVal == ".single")
969 return ParseDirectiveRealValue(APFloat::IEEEsingle);
970 if (IDVal == ".double")
971 return ParseDirectiveRealValue(APFloat::IEEEdouble);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000972
Eli Friedman5d68ec22010-07-19 04:17:25 +0000973 if (IDVal == ".align") {
974 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
975 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1);
976 }
977 if (IDVal == ".align32") {
978 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes();
979 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4);
980 }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000981 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000982 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000984 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000985 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000986 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000987 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000988 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000990 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000992 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
993
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000994 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000995 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000996
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000997 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000998 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000999 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +00001000 return ParseDirectiveSpace();
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001001 if (IDVal == ".zero")
1002 return ParseDirectiveZero();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001003
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001004 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001005
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001006 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001007 return ParseDirectiveSymbolAttribute(MCSA_Global);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001008 // ELF only? Should it be here?
1009 if (IDVal == ".local")
1010 return ParseDirectiveSymbolAttribute(MCSA_Local);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001011 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001012 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001013 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001014 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001015 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001016 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001017 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001018 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001019 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001020 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Kevin Enderbye8e98d72010-11-19 18:39:33 +00001021 if (IDVal == ".symbol_resolver")
1022 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001023 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001024 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001025 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001026 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001027 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001028 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001029 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001030 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001032 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001033 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001034 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001035 if (IDVal == ".weak_def_can_be_hidden")
1036 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001037
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001038 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001039 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001040 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001041 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001042
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001043 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001044 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001045 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001046 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001047
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001048 // Look up the handler in the handler table.
1049 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1050 DirectiveMap.lookup(IDVal);
1051 if (Handler.first)
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +00001052 return (*Handler.second)(Handler.first, IDVal, IDLoc);
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001053
Kevin Enderby9c656452009-09-10 20:51:44 +00001054 // Target hook for parsing target specific directives.
1055 if (!getTargetParser().ParseDirective(ID))
1056 return false;
1057
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001058 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001059 EatToEndOfStatement();
1060 return false;
1061 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001062
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001063 CheckForValidSection();
1064
Chris Lattnera7f13542010-05-19 23:34:33 +00001065 // Canonicalize the opcode to lower case.
1066 SmallString<128> Opcode;
1067 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1068 Opcode.push_back(tolower(IDVal[i]));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001069
Chris Lattner98986712010-01-14 22:21:20 +00001070 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001071 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001072 ParsedOperands);
Chris Lattner2cf5f142009-06-22 01:29:09 +00001073
Daniel Dunbar3c14ca42010-08-11 06:37:09 +00001074 // Dump the parsed representation, if requested.
1075 if (getShowParsedOperands()) {
1076 SmallString<256> Str;
1077 raw_svector_ostream OS(Str);
1078 OS << "parsed instruction: [";
1079 for (unsigned i = 0; i != ParsedOperands.size(); ++i) {
1080 if (i != 0)
1081 OS << ", ";
1082 ParsedOperands[i]->dump(OS);
1083 }
1084 OS << "]";
1085
1086 PrintMessage(IDLoc, OS.str(), "note");
1087 }
1088
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001089 // If parsing succeeded, match the instruction.
Chris Lattner7036f8b2010-09-29 01:42:58 +00001090 if (!HadError)
1091 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, ParsedOperands,
1092 Out);
Chris Lattner98986712010-01-14 22:21:20 +00001093
Chris Lattner98986712010-01-14 22:21:20 +00001094 // Free any parsed operands.
1095 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1096 delete ParsedOperands[i];
1097
Chris Lattnercbf8a982010-09-11 16:18:25 +00001098 // Don't skip the rest of the line, the instruction parser is responsible for
1099 // that.
1100 return false;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001101}
Chris Lattner9a023f72009-06-24 04:43:34 +00001102
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001103MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL,
1104 const std::vector<std::vector<AsmToken> > &A)
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001105 : TheMacro(M), InstantiationLoc(IL), ExitLoc(EL)
1106{
1107 // Macro instantiation is lexical, unfortunately. We construct a new buffer
1108 // to hold the macro body with substitutions.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001109 SmallString<256> Buf;
1110 raw_svector_ostream OS(Buf);
1111
1112 StringRef Body = M->Body;
1113 while (!Body.empty()) {
1114 // Scan for the next substitution.
1115 std::size_t End = Body.size(), Pos = 0;
1116 for (; Pos != End; ++Pos) {
1117 // Check for a substitution or escape.
1118 if (Body[Pos] != '$' || Pos + 1 == End)
1119 continue;
1120
1121 char Next = Body[Pos + 1];
1122 if (Next == '$' || Next == 'n' || isdigit(Next))
1123 break;
1124 }
1125
1126 // Add the prefix.
1127 OS << Body.slice(0, Pos);
1128
1129 // Check if we reached the end.
1130 if (Pos == End)
1131 break;
1132
1133 switch (Body[Pos+1]) {
1134 // $$ => $
1135 case '$':
1136 OS << '$';
1137 break;
1138
1139 // $n => number of arguments
1140 case 'n':
1141 OS << A.size();
1142 break;
1143
1144 // $[0-9] => argument
1145 default: {
1146 // Missing arguments are ignored.
1147 unsigned Index = Body[Pos+1] - '0';
1148 if (Index >= A.size())
1149 break;
1150
1151 // Otherwise substitute with the token values, with spaces eliminated.
1152 for (std::vector<AsmToken>::const_iterator it = A[Index].begin(),
1153 ie = A[Index].end(); it != ie; ++it)
1154 OS << it->getString();
1155 break;
1156 }
1157 }
1158
1159 // Update the scan point.
1160 Body = Body.substr(Pos + 2);
1161 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001162
1163 // We include the .endmacro in the buffer as our queue to exit the macro
1164 // instantiation.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001165 OS << ".endmacro\n";
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001166
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001167 Instantiation = MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001168}
1169
1170bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc,
1171 const Macro *M) {
1172 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate
1173 // this, although we should protect against infinite loops.
1174 if (ActiveMacros.size() == 20)
1175 return TokError("macros cannot be nested more than 20 levels deep");
1176
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001177 // Parse the macro instantiation arguments.
1178 std::vector<std::vector<AsmToken> > MacroArguments;
1179 MacroArguments.push_back(std::vector<AsmToken>());
1180 unsigned ParenLevel = 0;
1181 for (;;) {
1182 if (Lexer.is(AsmToken::Eof))
1183 return TokError("unexpected token in macro instantiation");
1184 if (Lexer.is(AsmToken::EndOfStatement))
1185 break;
1186
1187 // If we aren't inside parentheses and this is a comma, start a new token
1188 // list.
1189 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) {
1190 MacroArguments.push_back(std::vector<AsmToken>());
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001191 } else {
Daniel Dunbare25c6b92010-08-10 17:38:52 +00001192 // Adjust the current parentheses level.
1193 if (Lexer.is(AsmToken::LParen))
1194 ++ParenLevel;
1195 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
1196 --ParenLevel;
1197
1198 // Append the token to the current argument list.
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001199 MacroArguments.back().push_back(getTok());
1200 }
1201 Lex();
1202 }
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001203
1204 // Create the macro instantiation object and add to the current macro
1205 // instantiation stack.
1206 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc,
Daniel Dunbar7a570d02010-07-18 19:00:10 +00001207 getTok().getLoc(),
1208 MacroArguments);
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00001209 ActiveMacros.push_back(MI);
1210
1211 // Jump to the macro instantiation and prime the lexer.
1212 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc());
1213 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
1214 Lex();
1215
1216 return false;
1217}
1218
1219void AsmParser::HandleMacroExit() {
1220 // Jump to the EndOfStatement we should return to, and consume it.
1221 JumpToLoc(ActiveMacros.back()->ExitLoc);
1222 Lex();
1223
1224 // Pop the instantiation entry.
1225 delete ActiveMacros.back();
1226 ActiveMacros.pop_back();
1227}
1228
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001229static void MarkUsed(const MCExpr *Value) {
1230 switch (Value->getKind()) {
1231 case MCExpr::Binary:
1232 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getLHS());
1233 MarkUsed(static_cast<const MCBinaryExpr*>(Value)->getRHS());
1234 break;
1235 case MCExpr::Target:
1236 case MCExpr::Constant:
1237 break;
1238 case MCExpr::SymbolRef: {
1239 static_cast<const MCSymbolRefExpr*>(Value)->getSymbol().setUsed(true);
1240 break;
1241 }
1242 case MCExpr::Unary:
1243 MarkUsed(static_cast<const MCUnaryExpr*>(Value)->getSubExpr());
1244 break;
1245 }
1246}
1247
Benjamin Kramer38e59892010-07-14 22:38:02 +00001248bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001249 // FIXME: Use better location, we should use proper tokens.
1250 SMLoc EqualLoc = Lexer.getLoc();
1251
Daniel Dunbar821e3332009-08-31 08:09:28 +00001252 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001253 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001254 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001255
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001256 MarkUsed(Value);
1257
Daniel Dunbar3f872332009-07-28 16:08:33 +00001258 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001259 return TokError("unexpected token in assignment");
1260
1261 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001262 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001263
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001264 // Validate that the LHS is allowed to be a variable (either it has not been
1265 // used as a symbol, or it is an absolute symbol).
1266 MCSymbol *Sym = getContext().LookupSymbol(Name);
1267 if (Sym) {
1268 // Diagnose assignment to a label.
1269 //
1270 // FIXME: Diagnostics. Note the location of the definition as a label.
1271 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001272 if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable())
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001273 ; // Allow redefinitions of undefined symbols only used in directives.
1274 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001275 return Error(EqualLoc, "redefinition of '" + Name + "'");
1276 else if (!Sym->isVariable())
1277 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001278 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001279 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1280 Name + "'");
Rafael Espindoladb9835d2010-11-15 14:40:36 +00001281
1282 // Don't count these checks as uses.
1283 Sym->setUsed(false);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001284 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001285 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001286
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001287 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001288
1289 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001290 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001291
1292 return false;
1293}
1294
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001295/// ParseIdentifier:
1296/// ::= identifier
1297/// ::= string
1298bool AsmParser::ParseIdentifier(StringRef &Res) {
Daniel Dunbar1f1b8652010-08-24 18:12:12 +00001299 // The assembler has relaxed rules for accepting identifiers, in particular we
1300 // allow things like '.globl $foo', which would normally be separate
1301 // tokens. At this level, we have already lexed so we cannot (currently)
1302 // handle this as a context dependent token, instead we detect adjacent tokens
1303 // and return the combined identifier.
1304 if (Lexer.is(AsmToken::Dollar)) {
1305 SMLoc DollarLoc = getLexer().getLoc();
1306
1307 // Consume the dollar sign, and check for a following identifier.
1308 Lex();
1309 if (Lexer.isNot(AsmToken::Identifier))
1310 return true;
1311
1312 // We have a '$' followed by an identifier, make sure they are adjacent.
1313 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1314 return true;
1315
1316 // Construct the joined identifier and consume the token.
1317 Res = StringRef(DollarLoc.getPointer(),
1318 getTok().getIdentifier().size() + 1);
1319 Lex();
1320 return false;
1321 }
1322
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001323 if (Lexer.isNot(AsmToken::Identifier) &&
1324 Lexer.isNot(AsmToken::String))
1325 return true;
1326
Sean Callanan18b83232010-01-19 21:44:56 +00001327 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001328
Sean Callanan79ed1a82010-01-19 20:22:31 +00001329 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001330
1331 return false;
1332}
1333
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001334/// ParseDirectiveSet:
1335/// ::= .set identifier ',' expression
Roman Divacky50e7a782010-10-28 16:22:58 +00001336bool AsmParser::ParseDirectiveSet(StringRef IDVal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001337 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001338
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001339 if (ParseIdentifier(Name))
Roman Divackyf9d17522010-10-28 16:57:58 +00001340 return TokError("expected identifier after '" + Twine(IDVal) + "'");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001341
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001342 if (getLexer().isNot(AsmToken::Comma))
Roman Divackyf9d17522010-10-28 16:57:58 +00001343 return TokError("unexpected token in '" + Twine(IDVal) + "'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001344 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001345
Daniel Dunbare2ace502009-08-31 08:09:09 +00001346 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001347}
1348
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001349bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001350 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001351
1352 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001353 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001354 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1355 if (Str[i] != '\\') {
1356 Data += Str[i];
1357 continue;
1358 }
1359
1360 // Recognize escaped characters. Note that this escape semantics currently
1361 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1362 ++i;
1363 if (i == e)
1364 return TokError("unexpected backslash at end of string");
1365
1366 // Recognize octal sequences.
1367 if ((unsigned) (Str[i] - '0') <= 7) {
1368 // Consume up to three octal characters.
1369 unsigned Value = Str[i] - '0';
1370
1371 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1372 ++i;
1373 Value = Value * 8 + (Str[i] - '0');
1374
1375 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1376 ++i;
1377 Value = Value * 8 + (Str[i] - '0');
1378 }
1379 }
1380
1381 if (Value > 255)
1382 return TokError("invalid octal escape sequence (out of range)");
1383
1384 Data += (unsigned char) Value;
1385 continue;
1386 }
1387
1388 // Otherwise recognize individual escapes.
1389 switch (Str[i]) {
1390 default:
1391 // Just reject invalid escape sequences for now.
1392 return TokError("invalid escape sequence (unrecognized character)");
1393
1394 case 'b': Data += '\b'; break;
1395 case 'f': Data += '\f'; break;
1396 case 'n': Data += '\n'; break;
1397 case 'r': Data += '\r'; break;
1398 case 't': Data += '\t'; break;
1399 case '"': Data += '"'; break;
1400 case '\\': Data += '\\'; break;
1401 }
1402 }
1403
1404 return false;
1405}
1406
Daniel Dunbara0d14262009-06-24 23:30:00 +00001407/// ParseDirectiveAscii:
Rafael Espindola787c3372010-10-28 20:02:27 +00001408/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
1409bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001410 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001411 CheckForValidSection();
1412
Daniel Dunbara0d14262009-06-24 23:30:00 +00001413 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001414 if (getLexer().isNot(AsmToken::String))
Rafael Espindola787c3372010-10-28 20:02:27 +00001415 return TokError("expected string in '" + Twine(IDVal) + "' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001416
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001417 std::string Data;
1418 if (ParseEscapedString(Data))
1419 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001420
1421 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001422 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001423 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1424
Sean Callanan79ed1a82010-01-19 20:22:31 +00001425 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001426
1427 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001428 break;
1429
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001430 if (getLexer().isNot(AsmToken::Comma))
Rafael Espindola787c3372010-10-28 20:02:27 +00001431 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001432 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001433 }
1434 }
1435
Sean Callanan79ed1a82010-01-19 20:22:31 +00001436 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001437 return false;
1438}
1439
1440/// ParseDirectiveValue
1441/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1442bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001443 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001444 CheckForValidSection();
1445
Daniel Dunbara0d14262009-06-24 23:30:00 +00001446 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001447 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001448 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001449 return true;
1450
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001451 // Special case constant expressions to match code generator.
1452 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001453 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001454 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001455 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001456
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001457 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001458 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001459
Daniel Dunbara0d14262009-06-24 23:30:00 +00001460 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001461 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001462 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001463 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001464 }
1465 }
1466
Sean Callanan79ed1a82010-01-19 20:22:31 +00001467 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001468 return false;
1469}
1470
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001471/// ParseDirectiveRealValue
1472/// ::= (.single | .double) [ expression (, expression)* ]
1473bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) {
1474 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1475 CheckForValidSection();
1476
1477 for (;;) {
1478 // We don't truly support arithmetic on floating point expressions, so we
1479 // have to manually parse unary prefixes.
1480 bool IsNeg = false;
1481 if (getLexer().is(AsmToken::Minus)) {
1482 Lex();
1483 IsNeg = true;
1484 } else if (getLexer().is(AsmToken::Plus))
1485 Lex();
1486
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001487 if (getLexer().isNot(AsmToken::Integer) &&
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001488 getLexer().isNot(AsmToken::Real))
1489 return TokError("unexpected token in directive");
1490
1491 // Convert to an APFloat.
1492 APFloat Value(Semantics);
1493 if (Value.convertFromString(getTok().getString(),
1494 APFloat::rmNearestTiesToEven) ==
1495 APFloat::opInvalidOp)
1496 return TokError("invalid floating point literal");
1497 if (IsNeg)
1498 Value.changeSign();
1499
1500 // Consume the numeric token.
1501 Lex();
1502
1503 // Emit the value as an integer.
1504 APInt AsInt = Value.bitcastToAPInt();
1505 getStreamer().EmitIntValue(AsInt.getLimitedValue(),
1506 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE);
1507
1508 if (getLexer().is(AsmToken::EndOfStatement))
1509 break;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001510
Daniel Dunbarb95a0792010-09-24 01:59:56 +00001511 if (getLexer().isNot(AsmToken::Comma))
1512 return TokError("unexpected token in directive");
1513 Lex();
1514 }
1515 }
1516
1517 Lex();
1518 return false;
1519}
1520
Daniel Dunbara0d14262009-06-24 23:30:00 +00001521/// ParseDirectiveSpace
1522/// ::= .space expression [ , expression ]
1523bool AsmParser::ParseDirectiveSpace() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001524 CheckForValidSection();
1525
Daniel Dunbara0d14262009-06-24 23:30:00 +00001526 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001527 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001528 return true;
1529
1530 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001531 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1532 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001533 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001534 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001535
Daniel Dunbar475839e2009-06-29 20:37:27 +00001536 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001537 return true;
1538
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001539 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001540 return TokError("unexpected token in '.space' directive");
1541 }
1542
Sean Callanan79ed1a82010-01-19 20:22:31 +00001543 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001544
1545 if (NumBytes <= 0)
1546 return TokError("invalid number of bytes in '.space' directive");
1547
1548 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001549 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001550
1551 return false;
1552}
1553
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001554/// ParseDirectiveZero
1555/// ::= .zero expression
1556bool AsmParser::ParseDirectiveZero() {
1557 CheckForValidSection();
1558
1559 int64_t NumBytes;
1560 if (ParseAbsoluteExpression(NumBytes))
1561 return true;
1562
Rafael Espindolae452b172010-10-05 19:42:57 +00001563 int64_t Val = 0;
1564 if (getLexer().is(AsmToken::Comma)) {
1565 Lex();
1566 if (ParseAbsoluteExpression(Val))
1567 return true;
1568 }
1569
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001570 if (getLexer().isNot(AsmToken::EndOfStatement))
1571 return TokError("unexpected token in '.zero' directive");
1572
1573 Lex();
1574
Rafael Espindolae452b172010-10-05 19:42:57 +00001575 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE);
Rafael Espindola2ea2ac72010-09-16 15:03:59 +00001576
1577 return false;
1578}
1579
Daniel Dunbara0d14262009-06-24 23:30:00 +00001580/// ParseDirectiveFill
1581/// ::= .fill expression , expression , expression
1582bool AsmParser::ParseDirectiveFill() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001583 CheckForValidSection();
1584
Daniel Dunbara0d14262009-06-24 23:30:00 +00001585 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001586 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001587 return true;
1588
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001589 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001590 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001591 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001592
Daniel Dunbara0d14262009-06-24 23:30:00 +00001593 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001594 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001595 return true;
1596
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001597 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001598 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001599 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001600
Daniel Dunbara0d14262009-06-24 23:30:00 +00001601 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001602 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001603 return true;
1604
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001605 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001606 return TokError("unexpected token in '.fill' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001607
Sean Callanan79ed1a82010-01-19 20:22:31 +00001608 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001609
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001610 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1611 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001612
1613 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001614 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001615
1616 return false;
1617}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001618
1619/// ParseDirectiveOrg
1620/// ::= .org expression [ , expression ]
1621bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001622 CheckForValidSection();
1623
Daniel Dunbar821e3332009-08-31 08:09:28 +00001624 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001625 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001626 return true;
1627
1628 // Parse optional fill expression.
1629 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001630 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1631 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001632 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001633 Lex();
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001634
Daniel Dunbar475839e2009-06-29 20:37:27 +00001635 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001636 return true;
1637
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001638 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001639 return TokError("unexpected token in '.org' directive");
1640 }
1641
Sean Callanan79ed1a82010-01-19 20:22:31 +00001642 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001643
1644 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1645 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001646 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001647
1648 return false;
1649}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001650
1651/// ParseDirectiveAlign
1652/// ::= {.align, ...} expression [ , expression [ , expression ]]
1653bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001654 CheckForValidSection();
1655
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001656 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001657 int64_t Alignment;
1658 if (ParseAbsoluteExpression(Alignment))
1659 return true;
1660
1661 SMLoc MaxBytesLoc;
1662 bool HasFillExpr = false;
1663 int64_t FillExpr = 0;
1664 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001665 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1666 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001667 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001668 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001669
1670 // The fill expression can be omitted while specifying a maximum number of
1671 // alignment bytes, e.g:
1672 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001673 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001674 HasFillExpr = true;
1675 if (ParseAbsoluteExpression(FillExpr))
1676 return true;
1677 }
1678
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1680 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001681 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001682 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001683
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001685 if (ParseAbsoluteExpression(MaxBytesToFill))
1686 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001687
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001688 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001689 return TokError("unexpected token in directive");
1690 }
1691 }
1692
Sean Callanan79ed1a82010-01-19 20:22:31 +00001693 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001694
Daniel Dunbar648ac512010-05-17 21:54:30 +00001695 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001696 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001697
1698 // Compute alignment in bytes.
1699 if (IsPow2) {
1700 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001701 if (Alignment >= 32) {
1702 Error(AlignmentLoc, "invalid alignment value");
1703 Alignment = 31;
1704 }
1705
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001706 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001707 }
1708
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001709 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001710 if (MaxBytesLoc.isValid()) {
1711 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001712 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1713 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001714 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001715 }
1716
1717 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001718 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1719 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001720 MaxBytesToFill = 0;
1721 }
1722 }
1723
Daniel Dunbar648ac512010-05-17 21:54:30 +00001724 // Check whether we should use optimal code alignment for this .align
1725 // directive.
Jan Wen Voung083cf152010-10-04 17:32:41 +00001726 bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign();
Daniel Dunbar648ac512010-05-17 21:54:30 +00001727 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1728 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001729 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001730 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001731 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001732 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1733 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001734 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001735
1736 return false;
1737}
1738
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001739/// ParseDirectiveSymbolAttribute
1740/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001741bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001742 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001743 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001744 StringRef Name;
1745
1746 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001747 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001748
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001749 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001750
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001752
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001753 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001754 break;
1755
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001757 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001758 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001759 }
1760 }
1761
Sean Callanan79ed1a82010-01-19 20:22:31 +00001762 Lex();
Jan Wen Vounga854a4b2010-09-30 01:09:20 +00001763 return false;
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001764}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001765
1766/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001767/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1768bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar1ab6f2f2010-09-09 22:42:59 +00001769 CheckForValidSection();
1770
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001771 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001772 StringRef Name;
1773 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001774 return TokError("expected identifier in directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001775
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001776 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001777 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001778
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001779 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001780 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001781 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001782
1783 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001784 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001785 if (ParseAbsoluteExpression(Size))
1786 return true;
1787
1788 int64_t Pow2Alignment = 0;
1789 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001790 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001791 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001792 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001793 if (ParseAbsoluteExpression(Pow2Alignment))
1794 return true;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001795
Chris Lattner258281d2010-01-19 06:22:22 +00001796 // If this target takes alignments in bytes (not log) validate and convert.
1797 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1798 if (!isPowerOf2_64(Pow2Alignment))
1799 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1800 Pow2Alignment = Log2_64(Pow2Alignment);
1801 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001802 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001803
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001804 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001805 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001806
Sean Callanan79ed1a82010-01-19 20:22:31 +00001807 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001808
Chris Lattner1fc3d752009-07-09 17:25:12 +00001809 // NOTE: a size of zero for a .comm should create a undefined symbol
1810 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001811 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001812 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1813 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001814
Eric Christopherc260a3e2010-05-14 01:38:54 +00001815 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001816 // may internally end up wanting an alignment in bytes.
1817 // FIXME: Diagnose overflow.
1818 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001819 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1820 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001821
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001822 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001823 return Error(IDLoc, "invalid symbol redefinition");
1824
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001825 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001826 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001827 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001828 getStreamer().EmitZerofill(Ctx.getMachOSection(
1829 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1830 0, SectionKind::getBSS()),
1831 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001832 return false;
1833 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001834
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001835 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001836 return false;
1837}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001838
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001839/// ParseDirectiveAbort
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001840/// ::= .abort [... message ...]
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001841bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001842 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001843 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001844
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001845 StringRef Str = ParseStringToEndOfStatement();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001846 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001847 return TokError("unexpected token in '.abort' directive");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001848
Sean Callanan79ed1a82010-01-19 20:22:31 +00001849 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001850
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001851 if (Str.empty())
1852 Error(Loc, ".abort detected. Assembly stopping.");
1853 else
1854 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Daniel Dunbar6a46d572010-07-18 20:15:59 +00001855 // FIXME: Actually abort assembly here.
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001856
1857 return false;
1858}
Kevin Enderby71148242009-07-14 21:35:03 +00001859
Kevin Enderby1f049b22009-07-14 23:21:55 +00001860/// ParseDirectiveInclude
1861/// ::= .include "filename"
1862bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001863 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001864 return TokError("expected string in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001865
Sean Callanan18b83232010-01-19 21:44:56 +00001866 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001867 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001868 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001869
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001870 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001871 return TokError("unexpected token in '.include' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001872
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001873 // Strip the quotes.
1874 Filename = Filename.substr(1, Filename.size()-2);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001875
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001876 // Attempt to switch the lexer to the included file before consuming the end
1877 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001878 if (EnterIncludeFile(Filename)) {
Daniel Dunbar275ce392010-07-18 18:31:45 +00001879 Error(IncludeLoc, "Could not find include file '" + Filename + "'");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001880 return true;
1881 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001882
1883 return false;
1884}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001885
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001886/// ParseDirectiveIf
1887/// ::= .if expression
1888bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001889 TheCondStack.push_back(TheCondState);
1890 TheCondState.TheCond = AsmCond::IfCond;
1891 if(TheCondState.Ignore) {
1892 EatToEndOfStatement();
1893 }
1894 else {
1895 int64_t ExprValue;
1896 if (ParseAbsoluteExpression(ExprValue))
1897 return true;
1898
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001899 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001900 return TokError("unexpected token in '.if' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001901
Sean Callanan79ed1a82010-01-19 20:22:31 +00001902 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001903
1904 TheCondState.CondMet = ExprValue;
1905 TheCondState.Ignore = !TheCondState.CondMet;
1906 }
1907
1908 return false;
1909}
1910
1911/// ParseDirectiveElseIf
1912/// ::= .elseif expression
1913bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1914 if (TheCondState.TheCond != AsmCond::IfCond &&
1915 TheCondState.TheCond != AsmCond::ElseIfCond)
1916 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1917 " an .elseif");
1918 TheCondState.TheCond = AsmCond::ElseIfCond;
1919
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001920 bool LastIgnoreState = false;
1921 if (!TheCondStack.empty())
1922 LastIgnoreState = TheCondStack.back().Ignore;
1923 if (LastIgnoreState || TheCondState.CondMet) {
1924 TheCondState.Ignore = true;
1925 EatToEndOfStatement();
1926 }
1927 else {
1928 int64_t ExprValue;
1929 if (ParseAbsoluteExpression(ExprValue))
1930 return true;
1931
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001933 return TokError("unexpected token in '.elseif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001934
Sean Callanan79ed1a82010-01-19 20:22:31 +00001935 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001936 TheCondState.CondMet = ExprValue;
1937 TheCondState.Ignore = !TheCondState.CondMet;
1938 }
1939
1940 return false;
1941}
1942
1943/// ParseDirectiveElse
1944/// ::= .else
1945bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001946 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001947 return TokError("unexpected token in '.else' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001948
Sean Callanan79ed1a82010-01-19 20:22:31 +00001949 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001950
1951 if (TheCondState.TheCond != AsmCond::IfCond &&
1952 TheCondState.TheCond != AsmCond::ElseIfCond)
1953 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1954 ".elseif");
1955 TheCondState.TheCond = AsmCond::ElseCond;
1956 bool LastIgnoreState = false;
1957 if (!TheCondStack.empty())
1958 LastIgnoreState = TheCondStack.back().Ignore;
1959 if (LastIgnoreState || TheCondState.CondMet)
1960 TheCondState.Ignore = true;
1961 else
1962 TheCondState.Ignore = false;
1963
1964 return false;
1965}
1966
1967/// ParseDirectiveEndIf
1968/// ::= .endif
1969bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001970 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001971 return TokError("unexpected token in '.endif' directive");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001972
Sean Callanan79ed1a82010-01-19 20:22:31 +00001973 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001974
1975 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1976 TheCondStack.empty())
1977 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1978 ".else");
1979 if (!TheCondStack.empty()) {
1980 TheCondState = TheCondStack.back();
1981 TheCondStack.pop_back();
1982 }
1983
1984 return false;
1985}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001986
1987/// ParseDirectiveFile
1988/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001989bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001990 // FIXME: I'm not sure what this is.
1991 int64_t FileNumber = -1;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00001992 SMLoc FileNumberLoc = getLexer().getLoc();
Daniel Dunbareceec052010-07-12 17:45:27 +00001993 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001994 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001995 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001996
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001997 if (FileNumber < 1)
1998 return TokError("file number less than one");
1999 }
2000
Daniel Dunbareceec052010-07-12 17:45:27 +00002001 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002002 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002003
Chris Lattnerd32e8032010-01-25 19:02:58 +00002004 StringRef Filename = getTok().getString();
2005 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002006 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002007
Daniel Dunbareceec052010-07-12 17:45:27 +00002008 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002009 return TokError("unexpected token in '.file' directive");
2010
Chris Lattnerd32e8032010-01-25 19:02:58 +00002011 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002012 getStreamer().EmitFileDirective(Filename);
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002013 else {
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002014 if (getStreamer().EmitDwarfFileDirective(FileNumber, Filename))
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002015 Error(FileNumberLoc, "file number already allocated");
Kevin Enderby7cbf73a2010-07-28 20:55:35 +00002016 }
Daniel Dunbareceec052010-07-12 17:45:27 +00002017
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002018 return false;
2019}
2020
2021/// ParseDirectiveLine
2022/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002023bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002024 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2025 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002026 return TokError("unexpected token in '.line' directive");
2027
Sean Callanan18b83232010-01-19 21:44:56 +00002028 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002029 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002030 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002031
2032 // FIXME: Do something with the .line.
2033 }
2034
Daniel Dunbareceec052010-07-12 17:45:27 +00002035 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002036 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002037
2038 return false;
2039}
2040
2041
2042/// ParseDirectiveLoc
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002043/// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end]
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002044/// [epilogue_begin] [is_stmt VALUE] [isa VALUE]
2045/// The first number is a file number, must have been previously assigned with
2046/// a .file directive, the second number is the line number and optionally the
2047/// third number is a column position (zero if not specified). The remaining
2048/// optional items are .loc sub-directives.
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002049bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002050
Daniel Dunbareceec052010-07-12 17:45:27 +00002051 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002052 return TokError("unexpected token in '.loc' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002053 int64_t FileNumber = getTok().getIntVal();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002054 if (FileNumber < 1)
2055 return TokError("file number less than one in '.loc' directive");
Kevin Enderby3f55c242010-10-04 20:17:24 +00002056 if (!getContext().isValidDwarfFileNumber(FileNumber))
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002057 return TokError("unassigned file number in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002058 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002059
Kevin Enderby6d8f1a92010-08-24 21:14:47 +00002060 int64_t LineNumber = 0;
2061 if (getLexer().is(AsmToken::Integer)) {
2062 LineNumber = getTok().getIntVal();
2063 if (LineNumber < 1)
2064 return TokError("line number less than one in '.loc' directive");
2065 Lex();
2066 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002067
2068 int64_t ColumnPos = 0;
2069 if (getLexer().is(AsmToken::Integer)) {
2070 ColumnPos = getTok().getIntVal();
2071 if (ColumnPos < 0)
2072 return TokError("column position less than zero in '.loc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00002073 Lex();
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002074 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002075
Kevin Enderbyc0957932010-09-30 16:52:03 +00002076 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002077 unsigned Isa = 0;
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002078 int64_t Discriminator = 0;
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002079 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2080 for (;;) {
2081 if (getLexer().is(AsmToken::EndOfStatement))
2082 break;
2083
2084 StringRef Name;
2085 SMLoc Loc = getTok().getLoc();
2086 if (getParser().ParseIdentifier(Name))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002087 return TokError("unexpected token in '.loc' directive");
2088
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002089 if (Name == "basic_block")
2090 Flags |= DWARF2_FLAG_BASIC_BLOCK;
2091 else if (Name == "prologue_end")
2092 Flags |= DWARF2_FLAG_PROLOGUE_END;
2093 else if (Name == "epilogue_begin")
2094 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN;
2095 else if (Name == "is_stmt") {
2096 SMLoc Loc = getTok().getLoc();
2097 const MCExpr *Value;
2098 if (getParser().ParseExpression(Value))
2099 return true;
2100 // The expression must be the constant 0 or 1.
2101 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2102 int Value = MCE->getValue();
2103 if (Value == 0)
2104 Flags &= ~DWARF2_FLAG_IS_STMT;
2105 else if (Value == 1)
2106 Flags |= DWARF2_FLAG_IS_STMT;
2107 else
2108 return Error(Loc, "is_stmt value not 0 or 1");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002109 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002110 else {
2111 return Error(Loc, "is_stmt value not the constant value of 0 or 1");
2112 }
2113 }
2114 else if (Name == "isa") {
2115 SMLoc Loc = getTok().getLoc();
2116 const MCExpr *Value;
2117 if (getParser().ParseExpression(Value))
2118 return true;
2119 // The expression must be a constant greater or equal to 0.
2120 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
2121 int Value = MCE->getValue();
2122 if (Value < 0)
2123 return Error(Loc, "isa number less than zero");
2124 Isa = Value;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002125 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002126 else {
2127 return Error(Loc, "isa number not a constant value");
2128 }
2129 }
Rafael Espindolac50a0fd2010-11-13 03:18:27 +00002130 else if (Name == "discriminator") {
2131 if (getParser().ParseAbsoluteExpression(Discriminator))
2132 return true;
2133 }
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002134 else {
2135 return Error(Loc, "unknown sub-directive in '.loc' directive");
2136 }
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002137
Kevin Enderbyc1840b32010-08-24 20:32:42 +00002138 if (getLexer().is(AsmToken::EndOfStatement))
2139 break;
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002140 }
2141 }
2142
Rafael Espindolaaf6b58082010-11-16 21:20:32 +00002143 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags,
2144 Isa, Discriminator);
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002145
2146 return false;
2147}
2148
Daniel Dunbar138abae2010-10-16 04:56:42 +00002149/// ParseDirectiveStabs
2150/// ::= .stabs string, number, number, number
2151bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive,
2152 SMLoc DirectiveLoc) {
2153 return TokError("unsupported directive '" + Directive + "'");
2154}
2155
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002156/// ParseDirectiveCFIStartProc
2157/// ::= .cfi_startproc
2158bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef,
2159 SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002160 return getStreamer().EmitCFIStartProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002161}
2162
2163/// ParseDirectiveCFIEndProc
2164/// ::= .cfi_endproc
2165bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) {
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002166 return getStreamer().EmitCFIEndProc();
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002167}
2168
2169/// ParseDirectiveCFIDefCfaOffset
2170/// ::= .cfi_def_cfa_offset offset
2171bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef,
2172 SMLoc DirectiveLoc) {
2173 int64_t Offset = 0;
2174 if (getParser().ParseAbsoluteExpression(Offset))
2175 return true;
2176
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002177 return getStreamer().EmitCFIDefCfaOffset(Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002178}
2179
2180/// ParseDirectiveCFIDefCfaRegister
2181/// ::= .cfi_def_cfa_register register
2182bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef,
2183 SMLoc DirectiveLoc) {
2184 int64_t Register = 0;
2185 if (getParser().ParseAbsoluteExpression(Register))
2186 return true;
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002187
2188 return getStreamer().EmitCFIDefCfaRegister(Register);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002189}
2190
2191/// ParseDirectiveCFIOffset
2192/// ::= .cfi_off register, offset
2193bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) {
2194 int64_t Register = 0;
2195 int64_t Offset = 0;
2196 if (getParser().ParseAbsoluteExpression(Register))
2197 return true;
2198
2199 if (getLexer().isNot(AsmToken::Comma))
2200 return TokError("unexpected token in directive");
2201 Lex();
2202
2203 if (getParser().ParseAbsoluteExpression(Offset))
2204 return true;
2205
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002206 return getStreamer().EmitCFIOffset(Register, Offset);
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002207}
2208
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002209static bool isValidEncoding(int64_t Encoding) {
2210 if (Encoding & ~0xff)
2211 return false;
2212
2213 if (Encoding == dwarf::DW_EH_PE_omit)
2214 return true;
2215
2216 const unsigned Format = Encoding & 0xf;
2217 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 &&
2218 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 &&
2219 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 &&
2220 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed)
2221 return false;
2222
2223 const unsigned Application = Encoding & 0xf0;
2224 if (Application != dwarf::DW_EH_PE_absptr &&
2225 Application != dwarf::DW_EH_PE_pcrel &&
2226 Application != dwarf::DW_EH_PE_indirect)
2227 return false;
2228
2229 return true;
2230}
2231
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002232/// ParseDirectiveCFIPersonalityOrLsda
2233/// ::= .cfi_personality encoding, [symbol_name]
2234/// ::= .cfi_lsda encoding, [symbol_name]
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002235bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal,
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002236 SMLoc DirectiveLoc) {
2237 int64_t Encoding = 0;
2238 if (getParser().ParseAbsoluteExpression(Encoding))
2239 return true;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002240 if (Encoding == dwarf::DW_EH_PE_omit)
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002241 return false;
2242
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00002243 if (!isValidEncoding(Encoding))
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002244 return TokError("unsupported encoding.");
2245
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002246 if (getLexer().isNot(AsmToken::Comma))
2247 return TokError("unexpected token in directive");
2248 Lex();
2249
2250 StringRef Name;
2251 if (getParser().ParseIdentifier(Name))
2252 return TokError("expected identifier in directive");
Rafael Espindolacdfecc82010-11-22 14:27:24 +00002253
2254 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2255
2256 if (IDVal == ".cfi_personality")
2257 return getStreamer().EmitCFIPersonality(Sym);
2258 else {
2259 assert(IDVal == ".cfi_lsda");
2260 return getStreamer().EmitCFILsda(Sym);
2261 }
Rafael Espindola1fdfbc42010-11-16 18:34:07 +00002262}
2263
Daniel Dunbar3c802de2010-07-18 18:38:02 +00002264/// ParseDirectiveMacrosOnOff
2265/// ::= .macros_on
2266/// ::= .macros_off
2267bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive,
2268 SMLoc DirectiveLoc) {
2269 if (getLexer().isNot(AsmToken::EndOfStatement))
2270 return Error(getLexer().getLoc(),
2271 "unexpected token in '" + Directive + "' directive");
2272
2273 getParser().MacrosEnabled = Directive == ".macros_on";
2274
2275 return false;
2276}
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002277
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002278/// ParseDirectiveMacro
2279/// ::= .macro name
2280bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive,
2281 SMLoc DirectiveLoc) {
2282 StringRef Name;
2283 if (getParser().ParseIdentifier(Name))
2284 return TokError("expected identifier in directive");
2285
2286 if (getLexer().isNot(AsmToken::EndOfStatement))
2287 return TokError("unexpected token in '.macro' directive");
2288
2289 // Eat the end of statement.
2290 Lex();
2291
2292 AsmToken EndToken, StartToken = getTok();
2293
2294 // Lex the macro definition.
2295 for (;;) {
2296 // Check whether we have reached the end of the file.
2297 if (getLexer().is(AsmToken::Eof))
2298 return Error(DirectiveLoc, "no matching '.endmacro' in definition");
2299
2300 // Otherwise, check whether we have reach the .endmacro.
2301 if (getLexer().is(AsmToken::Identifier) &&
2302 (getTok().getIdentifier() == ".endm" ||
2303 getTok().getIdentifier() == ".endmacro")) {
2304 EndToken = getTok();
2305 Lex();
2306 if (getLexer().isNot(AsmToken::EndOfStatement))
2307 return TokError("unexpected token in '" + EndToken.getIdentifier() +
2308 "' directive");
2309 break;
2310 }
2311
2312 // Otherwise, scan til the end of the statement.
2313 getParser().EatToEndOfStatement();
2314 }
2315
2316 if (getParser().MacroMap.lookup(Name)) {
2317 return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
2318 }
2319
2320 const char *BodyStart = StartToken.getLoc().getPointer();
2321 const char *BodyEnd = EndToken.getLoc().getPointer();
2322 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
2323 getParser().MacroMap[Name] = new Macro(Name, Body);
2324 return false;
2325}
2326
2327/// ParseDirectiveEndMacro
2328/// ::= .endm
2329/// ::= .endmacro
2330bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive,
2331 SMLoc DirectiveLoc) {
2332 if (getLexer().isNot(AsmToken::EndOfStatement))
2333 return TokError("unexpected token in '" + Directive + "' directive");
2334
Daniel Dunbarc64a0d72010-07-18 18:54:11 +00002335 // If we are inside a macro instantiation, terminate the current
2336 // instantiation.
2337 if (!getParser().ActiveMacros.empty()) {
2338 getParser().HandleMacroExit();
2339 return false;
2340 }
2341
2342 // Otherwise, this .endmacro is a stray entry in the file; well formed
2343 // .endmacro directives are handled during the macro definition parsing.
Daniel Dunbar6d8cf082010-07-18 18:47:21 +00002344 return TokError("unexpected '" + Directive + "' in file, "
2345 "no current macro definition");
2346}
2347
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002348bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
Rafael Espindola3ff57092010-11-02 17:22:24 +00002349 getParser().CheckForValidSection();
2350
2351 const MCExpr *Value;
2352
2353 if (getParser().ParseExpression(Value))
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002354 return true;
2355
2356 if (getLexer().isNot(AsmToken::EndOfStatement))
2357 return TokError("unexpected token in directive");
2358
2359 if (DirName[1] == 's')
Rafael Espindola3ff57092010-11-02 17:22:24 +00002360 getStreamer().EmitSLEB128Value(Value);
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002361 else
Rafael Espindola3ff57092010-11-02 17:22:24 +00002362 getStreamer().EmitULEB128Value(Value);
2363
Rafael Espindolab98ac2a2010-09-11 16:45:15 +00002364 return false;
2365}
2366
2367
Daniel Dunbard1e3b442010-07-17 02:26:10 +00002368/// \brief Create an MCAsmParser instance.
2369MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
2370 MCContext &C, MCStreamer &Out,
2371 const MCAsmInfo &MAI) {
2372 return new AsmParser(T, SM, C, Out, MAI);
2373}