blob: 0aeff0f0d1af42b9b86e8d098b4edf50017227b2 [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
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Chris Lattneraaec2052010-01-19 19:46:13 +000032
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000033namespace {
34
35/// \brief Generic implementations of directive handling, etc. which is shared
36/// (or the default, at least) for all assembler parser.
37class GenericAsmParser : public MCAsmParserExtension {
38public:
39 GenericAsmParser() {}
40
41 virtual void Initialize(MCAsmParser &Parser) {
42 // Call the base implementation.
43 this->MCAsmParserExtension::Initialize(Parser);
44
45 // Debugging directives.
46 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
47 &GenericAsmParser::ParseDirectiveFile));
48 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
49 &GenericAsmParser::ParseDirectiveLine));
50 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
51 &GenericAsmParser::ParseDirectiveLoc));
52 }
53
54 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
55 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
56 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
57};
58
Daniel Dunbare4749702010-07-12 18:12:02 +000059/// \brief Implementation of directive handling which is shared across all
60/// Darwin targets.
61class DarwinAsmParser : public MCAsmParserExtension {
62public:
63 DarwinAsmParser() {}
64
65 virtual void Initialize(MCAsmParser &Parser) {
66 // Call the base implementation.
67 this->MCAsmParserExtension::Initialize(Parser);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000068
Daniel Dunbar492b7a22010-07-12 19:22:53 +000069 Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler(
70 &DarwinAsmParser::ParseDirectiveDesc));
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000071 Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler(
72 &DarwinAsmParser::ParseDirectiveLsym));
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000073 Parser.AddDirectiveHandler(this, ".subsections_via_symbols",
74 MCAsmParser::DirectiveHandler(
75 &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols));
76 Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler(
77 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
78 Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler(
79 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
80 Parser.AddDirectiveHandler(this, ".secure_log_unique",
81 MCAsmParser::DirectiveHandler(
82 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
83 Parser.AddDirectiveHandler(this, ".secure_log_reset",
84 MCAsmParser::DirectiveHandler(
85 &DarwinAsmParser::ParseDirectiveSecureLogReset));
Daniel Dunbarb6c3a602010-07-12 19:37:35 +000086 Parser.AddDirectiveHandler(this, ".tbss",
87 MCAsmParser::DirectiveHandler(
88 &DarwinAsmParser::ParseDirectiveTBSS));
89 Parser.AddDirectiveHandler(this, ".zerofill",
90 MCAsmParser::DirectiveHandler(
91 &DarwinAsmParser::ParseDirectiveZerofill));
Daniel Dunbare4749702010-07-12 18:12:02 +000092 }
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000093
Daniel Dunbar492b7a22010-07-12 19:22:53 +000094 bool ParseDirectiveDesc(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000095 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000096 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000097 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000098 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
99 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
Daniel Dunbarb6c3a602010-07-12 19:37:35 +0000100 bool ParseDirectiveTBSS(StringRef, SMLoc);
101 bool ParseDirectiveZerofill(StringRef, SMLoc);
Daniel Dunbare4749702010-07-12 18:12:02 +0000102};
103
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000104}
105
Chris Lattneraaec2052010-01-19 19:46:13 +0000106enum { DEFAULT_ADDRSPACE = 0 };
107
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000108AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
109 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000110 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000111 GenericParser(new GenericAsmParser), PlatformParser(0),
112 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000113 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000114
115 // Initialize the generic parser.
116 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000117
118 // Initialize the platform / file format parser.
119 //
120 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
121 // created.
122 if (_MAI.hasSubsectionsViaSymbols()) {
123 PlatformParser = new DarwinAsmParser;
124 PlatformParser->Initialize(*this);
125 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000126}
127
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000128AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000129 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000130 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000131}
132
Daniel Dunbar53131982010-07-12 17:27:45 +0000133void AsmParser::setTargetParser(TargetAsmParser &P) {
134 assert(!TargetParser && "Target parser is already initialized!");
135 TargetParser = &P;
136 TargetParser->Initialize(*this);
137}
138
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000139void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000140 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000141}
142
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000143bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000144 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000145 return true;
146}
147
Sean Callananbf2013e2010-01-20 23:19:55 +0000148void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
149 const char *Type) const {
150 SrcMgr.PrintMessage(Loc, Msg, Type);
151}
Sean Callananfd0b0282010-01-21 00:19:58 +0000152
153bool AsmParser::EnterIncludeFile(const std::string &Filename) {
154 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
155 if (NewBuf == -1)
156 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000157
Sean Callananfd0b0282010-01-21 00:19:58 +0000158 CurBuffer = NewBuf;
159
160 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
161
162 return false;
163}
164
165const AsmToken &AsmParser::Lex() {
166 const AsmToken *tok = &Lexer.Lex();
167
168 if (tok->is(AsmToken::Eof)) {
169 // If this is the end of an included file, pop the parent file off the
170 // include stack.
171 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
172 if (ParentIncludeLoc != SMLoc()) {
173 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
174 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
175 ParentIncludeLoc.getPointer());
176 tok = &Lexer.Lex();
177 }
178 }
179
180 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000181 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000182
Sean Callananfd0b0282010-01-21 00:19:58 +0000183 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000184}
185
Chris Lattner79180e22010-04-05 23:15:42 +0000186bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000187 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000188 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000189 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000190 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000191 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000192 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
193 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000194
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000195 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000196 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000197
Chris Lattnerb717fb02009-07-02 21:53:43 +0000198 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000199
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000200 AsmCond StartingCondState = TheCondState;
201
Chris Lattnerb717fb02009-07-02 21:53:43 +0000202 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000203 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000204 if (!ParseStatement()) continue;
205
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000206 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000207 HadError = true;
208 EatToEndOfStatement();
209 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000210
211 if (TheCondState.TheCond != StartingCondState.TheCond ||
212 TheCondState.Ignore != StartingCondState.Ignore)
213 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000214
Chris Lattner79180e22010-04-05 23:15:42 +0000215 // Finalize the output stream if there are no errors and if the client wants
216 // us to.
217 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000218 Out.Finish();
219
Chris Lattnerb717fb02009-07-02 21:53:43 +0000220 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000221}
222
Chris Lattner2cf5f142009-06-22 01:29:09 +0000223/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
224void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000225 while (Lexer.isNot(AsmToken::EndOfStatement) &&
226 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000227 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000228
229 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000230 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000231 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000232}
233
Chris Lattnerc4193832009-06-22 05:51:26 +0000234
Chris Lattner74ec1a32009-06-22 06:32:03 +0000235/// ParseParenExpr - Parse a paren expression and return it.
236/// NOTE: This assumes the leading '(' has already been consumed.
237///
238/// parenexpr ::= expr)
239///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000240bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000241 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000242 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000243 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000244 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000245 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000246 return false;
247}
Chris Lattnerc4193832009-06-22 05:51:26 +0000248
Daniel Dunbar959fd882009-08-26 22:13:22 +0000249MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000250 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000251 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000252}
253
Chris Lattner74ec1a32009-06-22 06:32:03 +0000254/// ParsePrimaryExpr - Parse a primary expression and return it.
255/// primaryexpr ::= (parenexpr
256/// primaryexpr ::= symbol
257/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000258/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000259/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000260bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000261 switch (Lexer.getKind()) {
262 default:
263 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000264 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000265 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000266 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000267 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000268 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000269 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000270 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000271 case AsmToken::Identifier: {
272 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000273 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
274 MCSymbol *Sym = CreateSymbol(Split.first);
275
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000276 // Mark the symbol as used in an expression.
277 Sym->setUsedInExpr(true);
278
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000279 // Lookup the symbol variant if used.
280 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
281 if (Split.first.size() != getTok().getIdentifier().size())
282 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
283
Chris Lattnerb4307b32010-01-15 19:28:38 +0000284 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000285 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000286
287 // If this is an absolute variable reference, substitute it now to preserve
288 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000289 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000290 if (Variant)
291 return Error(EndLoc, "unexpected modified on variable reference");
292
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000293 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000294 return false;
295 }
296
297 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000298 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000299 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000300 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000301 case AsmToken::Integer: {
302 SMLoc Loc = getTok().getLoc();
303 int64_t IntVal = getTok().getIntVal();
304 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000305 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000306 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000307 // Look for 'b' or 'f' following an Integer as a directional label
308 if (Lexer.getKind() == AsmToken::Identifier) {
309 StringRef IDVal = getTok().getString();
310 if (IDVal == "f" || IDVal == "b"){
311 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
312 IDVal == "f" ? 1 : 0);
313 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
314 getContext());
315 if(IDVal == "b" && Sym->isUndefined())
316 return Error(Loc, "invalid reference to undefined symbol");
317 EndLoc = Lexer.getLoc();
318 Lex(); // Eat identifier.
319 }
320 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000321 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000322 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000323 case AsmToken::Dot: {
324 // This is a '.' reference, which references the current PC. Emit a
325 // temporary label to the streamer and refer to it.
326 MCSymbol *Sym = Ctx.CreateTempSymbol();
327 Out.EmitLabel(Sym);
328 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
329 EndLoc = Lexer.getLoc();
330 Lex(); // Eat identifier.
331 return false;
332 }
333
Daniel Dunbar3f872332009-07-28 16:08:33 +0000334 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000335 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000336 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000337 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000338 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000339 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000341 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000343 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000344 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000345 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000346 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000347 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000349 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000350 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000351 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000353 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000354 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000355 }
356}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000357
Chris Lattnerb4307b32010-01-15 19:28:38 +0000358bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000359 SMLoc EndLoc;
360 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000361}
362
Chris Lattner74ec1a32009-06-22 06:32:03 +0000363/// ParseExpression - Parse an expression and return it.
364///
365/// expr ::= expr +,- expr -> lowest.
366/// expr ::= expr |,^,&,! expr -> middle.
367/// expr ::= expr *,/,%,<<,>> expr -> highest.
368/// expr ::= primaryexpr
369///
Chris Lattner54482b42010-01-15 19:39:23 +0000370bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000371 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000372 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000373 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
374 return true;
375
376 // Try to constant fold it up front, if possible.
377 int64_t Value;
378 if (Res->EvaluateAsAbsolute(Value))
379 Res = MCConstantExpr::Create(Value, getContext());
380
381 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000382}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000383
Chris Lattnerb4307b32010-01-15 19:28:38 +0000384bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000385 Res = 0;
386 return ParseParenExpr(Res, EndLoc) ||
387 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000388}
389
Daniel Dunbar475839e2009-06-29 20:37:27 +0000390bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000391 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000392
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000393 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 if (ParseExpression(Expr))
395 return true;
396
Daniel Dunbare00b0112009-10-16 01:57:52 +0000397 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000398 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000399
400 return false;
401}
402
Daniel Dunbar3f872332009-07-28 16:08:33 +0000403static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000404 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000405 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000406 default:
407 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000408
409 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000410 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000411 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000412 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000413 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000414 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000415 return 1;
416
417 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000418 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000419 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000420 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000421 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000422 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000423 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000424 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000425 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000426 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000427 case AsmToken::ExclaimEqual:
428 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000429 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000430 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000431 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000432 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000433 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000434 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000435 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000436 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000437 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000438 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000439 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000440 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000441 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000442 return 2;
443
444 // Intermediate Precedence: |, &, ^
445 //
446 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000447 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000448 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000449 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000451 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000452 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000454 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000455 return 3;
456
457 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000458 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000459 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000460 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000461 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000462 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000463 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000464 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000465 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000466 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000467 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000468 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000469 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000470 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000471 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000472 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000473 }
474}
475
476
477/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
478/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000479bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
480 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000481 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000482 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000483 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000484
485 // If the next token is lower precedence than we are allowed to eat, return
486 // successfully with what we ate already.
487 if (TokPrec < Precedence)
488 return false;
489
Sean Callanan79ed1a82010-01-19 20:22:31 +0000490 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000491
492 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000493 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000494 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000495
496 // If BinOp binds less tightly with RHS than the operator after RHS, let
497 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000498 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000499 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000500 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000501 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000502 }
503
Daniel Dunbar475839e2009-06-29 20:37:27 +0000504 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000505 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000506 }
507}
508
Chris Lattnerc4193832009-06-22 05:51:26 +0000509
510
511
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000512/// ParseStatement:
513/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000514/// ::= Label* Directive ...Operands... EndOfStatement
515/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000516bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000517 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000518 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000519 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000520 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000521 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000522
523 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000524 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000525 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000526 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000527 int64_t LocalLabelVal = -1;
528 // GUESS allow an integer followed by a ':' as a directional local label
529 if (Lexer.is(AsmToken::Integer)) {
530 LocalLabelVal = getTok().getIntVal();
531 if (LocalLabelVal < 0) {
532 if (!TheCondState.Ignore)
533 return TokError("unexpected token at start of statement");
534 IDVal = "";
535 }
536 else {
537 IDVal = getTok().getString();
538 Lex(); // Consume the integer token to be used as an identifier token.
539 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000540 if (!TheCondState.Ignore)
541 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000542 }
543 }
544 }
545 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000546 if (!TheCondState.Ignore)
547 return TokError("unexpected token at start of statement");
548 IDVal = "";
549 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000550
Chris Lattner7834fac2010-04-17 18:14:27 +0000551 // Handle conditional assembly here before checking for skipping. We
552 // have to do this so that .endif isn't skipped in a ".if 0" block for
553 // example.
554 if (IDVal == ".if")
555 return ParseDirectiveIf(IDLoc);
556 if (IDVal == ".elseif")
557 return ParseDirectiveElseIf(IDLoc);
558 if (IDVal == ".else")
559 return ParseDirectiveElse(IDLoc);
560 if (IDVal == ".endif")
561 return ParseDirectiveEndIf(IDLoc);
562
563 // If we are in a ".if 0" block, ignore this statement.
564 if (TheCondState.Ignore) {
565 EatToEndOfStatement();
566 return false;
567 }
568
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000569 // FIXME: Recurse on local labels?
570
571 // See what kind of statement we have.
572 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000573 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000574 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000575 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000576
577 // Diagnose attempt to use a variable as a label.
578 //
579 // FIXME: Diagnostics. Note the location of the definition as a label.
580 // FIXME: This doesn't diagnose assignment to a symbol which has been
581 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000582 MCSymbol *Sym;
583 if (LocalLabelVal == -1)
584 Sym = CreateSymbol(IDVal);
585 else
586 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000587 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000588 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000589
Daniel Dunbar959fd882009-08-26 22:13:22 +0000590 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000591 Out.EmitLabel(Sym);
592
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000593 // Consume any end of statement token, if present, to avoid spurious
594 // AddBlankLine calls().
595 if (Lexer.is(AsmToken::EndOfStatement)) {
596 Lex();
597 if (Lexer.is(AsmToken::Eof))
598 return false;
599 }
600
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000601 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000602 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000603
Daniel Dunbar3f872332009-07-28 16:08:33 +0000604 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000605 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000606 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000607
Daniel Dunbare2ace502009-08-31 08:09:09 +0000608 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000609
610 default: // Normal instruction or directive.
611 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000612 }
613
614 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000615 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000616 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000618 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000620 // FIXME: This changes behavior based on the -static flag to the
621 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000622 return ParseDirectiveSectionSwitch("__TEXT", "__text",
623 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000625 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000627 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000629 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
630 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000632 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000633 MCSectionMachO::S_4BYTE_LITERALS,
634 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000636 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000637 MCSectionMachO::S_8BYTE_LITERALS,
638 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000640 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 MCSectionMachO::S_16BYTE_LITERALS,
642 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000643 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000644 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000646 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000648 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000650 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
651
652 // FIXME: The assembler manual claims that this has the self modify code
653 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000654 if (IDVal == ".symbol_stub")
655 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
656 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000657 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
658 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000659 0, 16);
660 // FIXME: PowerPC only?
661 if (IDVal == ".picsymbol_stub")
662 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
663 MCSectionMachO::S_SYMBOL_STUBS |
664 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
665 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000667 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000669 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
670
671 // FIXME: The section names of these two are misspelled in the assembler
672 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000674 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
675 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
676 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000678 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
679 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
680 4);
681
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000683 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000685 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000686 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
687 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000689 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000690 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
691 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000693 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000694
695
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000697 return ParseDirectiveSectionSwitch("__OBJC", "__class",
698 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000699 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000700 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
701 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000703 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
704 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000706 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
707 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000709 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
710 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000712 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
713 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000714 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000715 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
716 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000718 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
719 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000721 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
722 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
723 MCSectionMachO::S_LITERAL_POINTERS,
724 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000726 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
727 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
728 MCSectionMachO::S_LITERAL_POINTERS,
729 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000731 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
732 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000734 return ParseDirectiveSectionSwitch("__OBJC", "__category",
735 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000737 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
738 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000740 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
741 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000743 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
744 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000746 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
747 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000749 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
750 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000752 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
753 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000754 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000755 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
756 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000757
Eric Christopherc6177a42010-05-17 22:53:55 +0000758 if (IDVal == ".tdata")
759 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
760 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
761 if (IDVal == ".tlv")
762 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
763 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
764 if (IDVal == ".thread_init_func")
765 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
766 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
767
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000768 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000770 return ParseDirectiveSet();
771
Daniel Dunbara0d14262009-06-24 23:30:00 +0000772 // Data directives
773
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000774 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000775 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000776 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000777 return ParseDirectiveAscii(true);
778
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000779 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000780 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000781 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000782 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000783 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000784 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000785 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000786 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000787
788 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000789 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000790 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000791 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000792 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000793 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000794 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000795 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000796 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000797 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000798 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000799 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000800 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000801 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000802 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000803 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000804 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
805
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000806 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000807 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000808
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000809 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000810 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000811 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000812 return ParseDirectiveSpace();
813
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000814 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000815
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000816 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000817 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000818 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000819 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000820 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000821 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000822 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000823 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000824 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000825 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000826 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000827 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000828 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000829 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000830 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000831 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000832 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000833 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000834 if (IDVal == ".type")
835 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000836 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000837 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000838 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000839 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000840 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000841 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000842 if (IDVal == ".weak_def_can_be_hidden")
843 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000844
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000845 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000846 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000847 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000848 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000849
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000850 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000851 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000852 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000853 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000854
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000855 // Look up the handler in the handler table.
856 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
857 DirectiveMap.lookup(IDVal);
858 if (Handler.first)
859 return (Handler.first->*Handler.second)(IDVal, IDLoc);
860
Kevin Enderby9c656452009-09-10 20:51:44 +0000861 // Target hook for parsing target specific directives.
862 if (!getTargetParser().ParseDirective(ID))
863 return false;
864
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000865 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000866 EatToEndOfStatement();
867 return false;
868 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000869
Chris Lattnera7f13542010-05-19 23:34:33 +0000870 // Canonicalize the opcode to lower case.
871 SmallString<128> Opcode;
872 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
873 Opcode.push_back(tolower(IDVal[i]));
874
Chris Lattner98986712010-01-14 22:21:20 +0000875 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000876 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000877 ParsedOperands);
878 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
879 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000880
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000881 // If parsing succeeded, match the instruction.
882 if (!HadError) {
883 MCInst Inst;
884 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
885 // Emit the instruction on success.
886 Out.EmitInstruction(Inst);
887 } else {
888 // Otherwise emit a diagnostic about the match failure and set the error
889 // flag.
890 //
891 // FIXME: We should give nicer diagnostics about the exact failure.
892 Error(IDLoc, "unrecognized instruction");
893 HadError = true;
894 }
895 }
Chris Lattner98986712010-01-14 22:21:20 +0000896
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000897 // If there was no error, consume the end-of-statement token. Otherwise this
898 // will be done by our caller.
899 if (!HadError)
900 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000901
902 // Free any parsed operands.
903 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
904 delete ParsedOperands[i];
905
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000906 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000907}
Chris Lattner9a023f72009-06-24 04:43:34 +0000908
Daniel Dunbare2ace502009-08-31 08:09:09 +0000909bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000910 // FIXME: Use better location, we should use proper tokens.
911 SMLoc EqualLoc = Lexer.getLoc();
912
Daniel Dunbar821e3332009-08-31 08:09:28 +0000913 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000914 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000915 return true;
916
Daniel Dunbar3f872332009-07-28 16:08:33 +0000917 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000918 return TokError("unexpected token in assignment");
919
920 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000921 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000922
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000923 // Validate that the LHS is allowed to be a variable (either it has not been
924 // used as a symbol, or it is an absolute symbol).
925 MCSymbol *Sym = getContext().LookupSymbol(Name);
926 if (Sym) {
927 // Diagnose assignment to a label.
928 //
929 // FIXME: Diagnostics. Note the location of the definition as a label.
930 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000931 if (Sym->isUndefined() && !Sym->isUsedInExpr())
932 ; // Allow redefinitions of undefined symbols only used in directives.
933 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000934 return Error(EqualLoc, "redefinition of '" + Name + "'");
935 else if (!Sym->isVariable())
936 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000937 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000938 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
939 Name + "'");
940 } else
941 Sym = CreateSymbol(Name);
942
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000943 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000944
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000945 Sym->setUsedInExpr(true);
946
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000947 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000948 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000949
950 return false;
951}
952
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000953/// ParseIdentifier:
954/// ::= identifier
955/// ::= string
956bool AsmParser::ParseIdentifier(StringRef &Res) {
957 if (Lexer.isNot(AsmToken::Identifier) &&
958 Lexer.isNot(AsmToken::String))
959 return true;
960
Sean Callanan18b83232010-01-19 21:44:56 +0000961 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000962
Sean Callanan79ed1a82010-01-19 20:22:31 +0000963 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000964
965 return false;
966}
967
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000968/// ParseDirectiveSet:
969/// ::= .set identifier ',' expression
970bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000971 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000972
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000973 if (ParseIdentifier(Name))
974 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000975
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000976 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000977 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000978 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000979
Daniel Dunbare2ace502009-08-31 08:09:09 +0000980 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000981}
982
Chris Lattner9a023f72009-06-24 04:43:34 +0000983/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000984/// ::= .section identifier (',' identifier)*
985/// FIXME: This should actually parse out the segment, section, attributes and
986/// sizeof_stub fields.
987bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000988 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000989
Daniel Dunbarace63122009-08-11 03:42:33 +0000990 StringRef SectionName;
991 if (ParseIdentifier(SectionName))
992 return Error(Loc, "expected identifier after '.section' directive");
993
994 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000995 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +0000996 return TokError("unexpected token in '.section' directive");
997
Chris Lattnerff4bc462009-08-10 01:39:42 +0000998 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000999 SectionSpec += ",";
1000
1001 // Add all the tokens until the end of the line, ParseSectionSpecifier will
1002 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +00001003 StringRef EOL = Lexer.LexUntilEndOfStatement();
1004 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +00001005
Sean Callanan79ed1a82010-01-19 20:22:31 +00001006 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001007 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +00001008 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001009 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +00001010
Chris Lattnerff4bc462009-08-10 01:39:42 +00001011
1012 StringRef Segment, Section;
1013 unsigned TAA, StubSize;
1014 std::string ErrorStr =
1015 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1016 TAA, StubSize);
1017
1018 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001019 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +00001020
Chris Lattner56594f92009-07-31 17:47:16 +00001021 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001022 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001023 getStreamer().SwitchSection(Ctx.getMachOSection(
1024 Segment, Section, TAA, StubSize,
1025 isText ? SectionKind::getText()
1026 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001027 return false;
1028}
1029
Chris Lattnere15c2d72009-08-10 18:05:55 +00001030/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +00001031bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
1032 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +00001033 unsigned TAA, unsigned Align,
1034 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001035 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001036 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001037 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +00001038
Chris Lattner56594f92009-07-31 17:47:16 +00001039 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001040 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001041 getStreamer().SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001042 isText ? SectionKind::getText()
1043 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001044
1045 // Set the implicit alignment, if any.
1046 //
1047 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1048 // alignment on the section (e.g., if one manually inserts bytes into the
1049 // section, then just issueing the section switch directive will not realign
1050 // the section. However, this is arguably more reasonable behavior, and there
1051 // is no good reason for someone to intentionally emit incorrectly sized
1052 // values into the implicitly aligned sections.
1053 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001054 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001055
Chris Lattner529fb542009-06-24 05:13:15 +00001056 return false;
1057}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001058
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001059bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001060 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001061
1062 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001063 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001064 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1065 if (Str[i] != '\\') {
1066 Data += Str[i];
1067 continue;
1068 }
1069
1070 // Recognize escaped characters. Note that this escape semantics currently
1071 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1072 ++i;
1073 if (i == e)
1074 return TokError("unexpected backslash at end of string");
1075
1076 // Recognize octal sequences.
1077 if ((unsigned) (Str[i] - '0') <= 7) {
1078 // Consume up to three octal characters.
1079 unsigned Value = Str[i] - '0';
1080
1081 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1082 ++i;
1083 Value = Value * 8 + (Str[i] - '0');
1084
1085 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1086 ++i;
1087 Value = Value * 8 + (Str[i] - '0');
1088 }
1089 }
1090
1091 if (Value > 255)
1092 return TokError("invalid octal escape sequence (out of range)");
1093
1094 Data += (unsigned char) Value;
1095 continue;
1096 }
1097
1098 // Otherwise recognize individual escapes.
1099 switch (Str[i]) {
1100 default:
1101 // Just reject invalid escape sequences for now.
1102 return TokError("invalid escape sequence (unrecognized character)");
1103
1104 case 'b': Data += '\b'; break;
1105 case 'f': Data += '\f'; break;
1106 case 'n': Data += '\n'; break;
1107 case 'r': Data += '\r'; break;
1108 case 't': Data += '\t'; break;
1109 case '"': Data += '"'; break;
1110 case '\\': Data += '\\'; break;
1111 }
1112 }
1113
1114 return false;
1115}
1116
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001118/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001120 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001121 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001122 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001123 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001124
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001125 std::string Data;
1126 if (ParseEscapedString(Data))
1127 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001128
1129 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001131 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1132
Sean Callanan79ed1a82010-01-19 20:22:31 +00001133 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001134
1135 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001136 break;
1137
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001138 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001139 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001140 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001141 }
1142 }
1143
Sean Callanan79ed1a82010-01-19 20:22:31 +00001144 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001145 return false;
1146}
1147
1148/// ParseDirectiveValue
1149/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1150bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001151 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001152 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001153 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001154 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001155 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001156 return true;
1157
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001158 // Special case constant expressions to match code generator.
1159 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001160 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001161 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001162 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001163
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001164 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001165 break;
1166
1167 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001168 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001169 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001170 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001171 }
1172 }
1173
Sean Callanan79ed1a82010-01-19 20:22:31 +00001174 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001175 return false;
1176}
1177
1178/// ParseDirectiveSpace
1179/// ::= .space expression [ , expression ]
1180bool AsmParser::ParseDirectiveSpace() {
1181 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001182 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001183 return true;
1184
1185 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001186 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1187 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001188 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001189 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001190
Daniel Dunbar475839e2009-06-29 20:37:27 +00001191 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001192 return true;
1193
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001194 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001195 return TokError("unexpected token in '.space' directive");
1196 }
1197
Sean Callanan79ed1a82010-01-19 20:22:31 +00001198 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001199
1200 if (NumBytes <= 0)
1201 return TokError("invalid number of bytes in '.space' directive");
1202
1203 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001204 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001205
1206 return false;
1207}
1208
1209/// ParseDirectiveFill
1210/// ::= .fill expression , expression , expression
1211bool AsmParser::ParseDirectiveFill() {
1212 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001213 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001214 return true;
1215
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001216 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001217 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001218 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001219
1220 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001221 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001222 return true;
1223
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001224 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001225 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001226 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001227
1228 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001229 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001230 return true;
1231
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001232 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001233 return TokError("unexpected token in '.fill' directive");
1234
Sean Callanan79ed1a82010-01-19 20:22:31 +00001235 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001236
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001237 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1238 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001239
1240 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001241 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001242
1243 return false;
1244}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001245
1246/// ParseDirectiveOrg
1247/// ::= .org expression [ , expression ]
1248bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001249 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001250 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001251 return true;
1252
1253 // Parse optional fill expression.
1254 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001255 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1256 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001257 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001258 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001259
Daniel Dunbar475839e2009-06-29 20:37:27 +00001260 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001261 return true;
1262
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001263 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001264 return TokError("unexpected token in '.org' directive");
1265 }
1266
Sean Callanan79ed1a82010-01-19 20:22:31 +00001267 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001268
1269 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1270 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001271 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001272
1273 return false;
1274}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001275
1276/// ParseDirectiveAlign
1277/// ::= {.align, ...} expression [ , expression [ , expression ]]
1278bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001279 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001280 int64_t Alignment;
1281 if (ParseAbsoluteExpression(Alignment))
1282 return true;
1283
1284 SMLoc MaxBytesLoc;
1285 bool HasFillExpr = false;
1286 int64_t FillExpr = 0;
1287 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001288 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1289 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001290 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001291 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001292
1293 // The fill expression can be omitted while specifying a maximum number of
1294 // alignment bytes, e.g:
1295 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001296 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001297 HasFillExpr = true;
1298 if (ParseAbsoluteExpression(FillExpr))
1299 return true;
1300 }
1301
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001302 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1303 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001304 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001305 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001306
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001307 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001308 if (ParseAbsoluteExpression(MaxBytesToFill))
1309 return true;
1310
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001311 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001312 return TokError("unexpected token in directive");
1313 }
1314 }
1315
Sean Callanan79ed1a82010-01-19 20:22:31 +00001316 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001317
Daniel Dunbar648ac512010-05-17 21:54:30 +00001318 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001319 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001320
1321 // Compute alignment in bytes.
1322 if (IsPow2) {
1323 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001324 if (Alignment >= 32) {
1325 Error(AlignmentLoc, "invalid alignment value");
1326 Alignment = 31;
1327 }
1328
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001329 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001330 }
1331
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001332 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001333 if (MaxBytesLoc.isValid()) {
1334 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001335 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1336 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001337 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001338 }
1339
1340 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001341 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1342 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001343 MaxBytesToFill = 0;
1344 }
1345 }
1346
Daniel Dunbar648ac512010-05-17 21:54:30 +00001347 // Check whether we should use optimal code alignment for this .align
1348 // directive.
1349 //
1350 // FIXME: This should be using a target hook.
1351 bool UseCodeAlign = false;
1352 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001354 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1355 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1356 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001357 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001358 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001359 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001360 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001361 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001362
1363 return false;
1364}
1365
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001366/// ParseDirectiveSymbolAttribute
1367/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001368bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001369 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001370 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001371 StringRef Name;
1372
1373 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001374 return TokError("expected identifier in directive");
1375
Daniel Dunbar959fd882009-08-26 22:13:22 +00001376 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001377
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001378 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001379
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001380 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001381 break;
1382
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001383 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001384 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001385 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001386 }
1387 }
1388
Sean Callanan79ed1a82010-01-19 20:22:31 +00001389 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001390 return false;
1391}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001392
Matt Fleming924c5e52010-05-21 11:36:59 +00001393/// ParseDirectiveELFType
1394/// ::= .type identifier , @attribute
1395bool AsmParser::ParseDirectiveELFType() {
1396 StringRef Name;
1397 if (ParseIdentifier(Name))
1398 return TokError("expected identifier in directive");
1399
1400 // Handle the identifier as the key symbol.
1401 MCSymbol *Sym = CreateSymbol(Name);
1402
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001403 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001404 return TokError("unexpected token in '.type' directive");
1405 Lex();
1406
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001407 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001408 return TokError("expected '@' before type");
1409 Lex();
1410
1411 StringRef Type;
1412 SMLoc TypeLoc;
1413
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001414 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001415 if (ParseIdentifier(Type))
1416 return TokError("expected symbol type in directive");
1417
1418 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1419 .Case("function", MCSA_ELF_TypeFunction)
1420 .Case("object", MCSA_ELF_TypeObject)
1421 .Case("tls_object", MCSA_ELF_TypeTLS)
1422 .Case("common", MCSA_ELF_TypeCommon)
1423 .Case("notype", MCSA_ELF_TypeNoType)
1424 .Default(MCSA_Invalid);
1425
1426 if (Attr == MCSA_Invalid)
1427 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1428
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001429 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001430 return TokError("unexpected token in '.type' directive");
1431
1432 Lex();
1433
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001434 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001435
1436 return false;
1437}
1438
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001439/// ParseDirectiveDesc
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001440/// ::= .desc identifier , expression
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001441bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001442 StringRef Name;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001443 if (getParser().ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001444 return TokError("expected identifier in directive");
1445
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001446 // Handle the identifier as the key symbol.
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001447 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001448
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001449 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001450 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001451 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001452
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001453 int64_t DescValue;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001454 if (getParser().ParseAbsoluteExpression(DescValue))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001455 return true;
1456
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001457 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001458 return TokError("unexpected token in '.desc' directive");
1459
Sean Callanan79ed1a82010-01-19 20:22:31 +00001460 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001461
1462 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001463 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001464
1465 return false;
1466}
1467
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001468/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001469/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1470bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001471 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001472 StringRef Name;
1473 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001474 return TokError("expected identifier in directive");
1475
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001476 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001477 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001478
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001479 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001480 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001481 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001482
1483 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001484 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001485 if (ParseAbsoluteExpression(Size))
1486 return true;
1487
1488 int64_t Pow2Alignment = 0;
1489 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001490 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001491 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001492 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001493 if (ParseAbsoluteExpression(Pow2Alignment))
1494 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001495
1496 // If this target takes alignments in bytes (not log) validate and convert.
1497 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1498 if (!isPowerOf2_64(Pow2Alignment))
1499 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1500 Pow2Alignment = Log2_64(Pow2Alignment);
1501 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001502 }
1503
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001504 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001505 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001506
Sean Callanan79ed1a82010-01-19 20:22:31 +00001507 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001508
Chris Lattner1fc3d752009-07-09 17:25:12 +00001509 // NOTE: a size of zero for a .comm should create a undefined symbol
1510 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001511 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001512 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1513 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001514
Eric Christopherc260a3e2010-05-14 01:38:54 +00001515 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001516 // may internally end up wanting an alignment in bytes.
1517 // FIXME: Diagnose overflow.
1518 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001519 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1520 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001521
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001522 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001523 return Error(IDLoc, "invalid symbol redefinition");
1524
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001525 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001526 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001527 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001528 getStreamer().EmitZerofill(Ctx.getMachOSection(
1529 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1530 0, SectionKind::getBSS()),
1531 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001532 return false;
1533 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001534
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001535 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001536 return false;
1537}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001538
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001539/// ParseDirectiveZerofill
Chris Lattner9be3fee2009-07-10 22:20:30 +00001540/// ::= .zerofill segname , sectname [, identifier , size_expression [
1541/// , align_expression ]]
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001542bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001543 StringRef Segment;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001544 if (getParser().ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001545 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001546
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001547 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001548 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001549 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001550
1551 StringRef Section;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001552 if (getParser().ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001553 return TokError("expected section name after comma in '.zerofill' "
1554 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001555
Chris Lattner9be3fee2009-07-10 22:20:30 +00001556 // If this is the end of the line all that was wanted was to create the
1557 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001558 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001559 // Create the zerofill section but no symbol
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001560 getStreamer().EmitZerofill(getContext().getMachOSection(
1561 Segment, Section, MCSectionMachO::S_ZEROFILL,
1562 0, SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001563 return false;
1564 }
1565
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001566 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001567 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001568 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001569
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001570 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001571 StringRef IDStr;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001572 if (getParser().ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001573 return TokError("expected identifier in directive");
1574
1575 // handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001576 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001577
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001578 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001579 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001580 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001581
1582 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001584 if (getParser().ParseAbsoluteExpression(Size))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001585 return true;
1586
1587 int64_t Pow2Alignment = 0;
1588 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001589 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001590 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001591 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001592 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001593 return true;
1594 }
1595
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001596 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001597 return TokError("unexpected token in '.zerofill' directive");
1598
Sean Callanan79ed1a82010-01-19 20:22:31 +00001599 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001600
1601 if (Size < 0)
1602 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1603 "than zero");
1604
Eric Christopherc260a3e2010-05-14 01:38:54 +00001605 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001606 // may internally end up wanting an alignment in bytes.
1607 // FIXME: Diagnose overflow.
1608 if (Pow2Alignment < 0)
1609 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1610 "can't be less than zero");
1611
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001612 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001613 return Error(IDLoc, "invalid symbol redefinition");
1614
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001615 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001616 //
1617 // FIXME: Arch specific.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001618 getStreamer().EmitZerofill(getContext().getMachOSection(
1619 Segment, Section, MCSectionMachO::S_ZEROFILL,
1620 0, SectionKind::getBSS()),
1621 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001622
1623 return false;
1624}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001625
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001626/// ParseDirectiveTBSS
Eric Christopher482eba02010-05-14 01:50:28 +00001627/// ::= .tbss identifier, size, align
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001628bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001629 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001630 StringRef Name;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001631 if (getParser().ParseIdentifier(Name))
Eric Christopher482eba02010-05-14 01:50:28 +00001632 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001633
Eric Christopher482eba02010-05-14 01:50:28 +00001634 // Handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001635 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001636
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001637 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001638 return TokError("unexpected token in directive");
1639 Lex();
1640
1641 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001642 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001643 if (getParser().ParseAbsoluteExpression(Size))
Eric Christopher482eba02010-05-14 01:50:28 +00001644 return true;
1645
1646 int64_t Pow2Alignment = 0;
1647 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001648 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001649 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001650 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001651 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Eric Christopher482eba02010-05-14 01:50:28 +00001652 return true;
1653 }
1654
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001655 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001656 return TokError("unexpected token in '.tbss' directive");
1657
1658 Lex();
1659
1660 if (Size < 0)
1661 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1662 "zero");
1663
1664 // FIXME: Diagnose overflow.
1665 if (Pow2Alignment < 0)
1666 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1667 "than zero");
1668
1669 if (!Sym->isUndefined())
1670 return Error(IDLoc, "invalid symbol redefinition");
1671
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001672 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001673 "__DATA", "__thread_bss",
1674 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1675 0, SectionKind::getThreadBSS()),
1676 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001677
1678 return false;
1679}
1680
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001681/// ParseDirectiveSubsectionsViaSymbols
Kevin Enderbya5c78322009-07-13 21:03:15 +00001682/// ::= .subsections_via_symbols
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001683bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001685 return TokError("unexpected token in '.subsections_via_symbols' directive");
1686
Sean Callanan79ed1a82010-01-19 20:22:31 +00001687 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001688
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001689 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001690
1691 return false;
1692}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001693
1694/// ParseDirectiveAbort
1695/// ::= .abort [ "abort_string" ]
1696bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001697 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001698 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001699
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001700 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001701 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1702 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001703 return TokError("expected string in '.abort' directive");
1704
Sean Callanan18b83232010-01-19 21:44:56 +00001705 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001706
Sean Callanan79ed1a82010-01-19 20:22:31 +00001707 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001708 }
1709
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001710 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001711 return TokError("unexpected token in '.abort' directive");
1712
Sean Callanan79ed1a82010-01-19 20:22:31 +00001713 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001714
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001715 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001716 if (Str.empty())
1717 Error(Loc, ".abort detected. Assembly stopping.");
1718 else
1719 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001720
1721 return false;
1722}
Kevin Enderby71148242009-07-14 21:35:03 +00001723
1724/// ParseDirectiveLsym
1725/// ::= .lsym identifier , expression
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001726bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001727 StringRef Name;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001728 if (getParser().ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001729 return TokError("expected identifier in directive");
1730
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001731 // Handle the identifier as the key symbol.
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001732 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001733
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001734 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001735 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001736 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001737
Daniel Dunbar821e3332009-08-31 08:09:28 +00001738 const MCExpr *Value;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001739 if (getParser().ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001740 return true;
1741
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001742 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001743 return TokError("unexpected token in '.lsym' directive");
1744
Sean Callanan79ed1a82010-01-19 20:22:31 +00001745 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001746
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001747 // We don't currently support this directive.
1748 //
1749 // FIXME: Diagnostic location!
1750 (void) Sym;
1751 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001752}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001753
1754/// ParseDirectiveInclude
1755/// ::= .include "filename"
1756bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001757 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001758 return TokError("expected string in '.include' directive");
1759
Sean Callanan18b83232010-01-19 21:44:56 +00001760 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001761 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001762 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001763
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001764 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001765 return TokError("unexpected token in '.include' directive");
1766
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001767 // Strip the quotes.
1768 Filename = Filename.substr(1, Filename.size()-2);
1769
1770 // Attempt to switch the lexer to the included file before consuming the end
1771 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001772 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001773 PrintMessage(IncludeLoc,
1774 "Could not find include file '" + Filename + "'",
1775 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001776 return true;
1777 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001778
1779 return false;
1780}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001781
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001782/// ParseDirectiveDumpOrLoad
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001783/// ::= ( .dump | .load ) "filename"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001784bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1785 SMLoc IDLoc) {
1786 bool IsDump = Directive == ".dump";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001787 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001788 return TokError("expected string in '.dump' or '.load' directive");
1789
Sean Callanan79ed1a82010-01-19 20:22:31 +00001790 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001791
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001792 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001793 return TokError("unexpected token in '.dump' or '.load' directive");
1794
Sean Callanan79ed1a82010-01-19 20:22:31 +00001795 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001796
Kevin Enderby5026ae42009-07-20 20:25:37 +00001797 // FIXME: If/when .dump and .load are implemented they will be done in the
1798 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001799 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001800 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001801 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001802 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001803
1804 return false;
1805}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001806
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001807/// ParseDirectiveSecureLogUnique
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001808/// ::= .secure_log_unique "log message"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001809bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001810 std::string LogMessage;
1811
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001812 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001813 LogMessage = "";
1814 else{
1815 LogMessage = getTok().getString();
1816 Lex();
1817 }
1818
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001819 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001820 return TokError("unexpected token in '.secure_log_unique' directive");
1821
1822 if (getContext().getSecureLogUsed() != false)
1823 return Error(IDLoc, ".secure_log_unique specified multiple times");
1824
1825 char *SecureLogFile = getContext().getSecureLogFile();
1826 if (SecureLogFile == NULL)
1827 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1828 "environment variable unset.");
1829
1830 raw_ostream *OS = getContext().getSecureLog();
1831 if (OS == NULL) {
1832 std::string Err;
1833 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1834 if (!Err.empty()) {
1835 delete OS;
1836 return Error(IDLoc, Twine("can't open secure log file: ") +
1837 SecureLogFile + " (" + Err + ")");
1838 }
1839 getContext().setSecureLog(OS);
1840 }
1841
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001842 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
1843 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
1844 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001845 << LogMessage + "\n";
1846
1847 getContext().setSecureLogUsed(true);
1848
1849 return false;
1850}
1851
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001852/// ParseDirectiveSecureLogReset
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001853/// ::= .secure_log_reset
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001854bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001855 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001856 return TokError("unexpected token in '.secure_log_reset' directive");
1857
1858 Lex();
1859
1860 getContext().setSecureLogUsed(false);
1861
1862 return false;
1863}
1864
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001865/// ParseDirectiveIf
1866/// ::= .if expression
1867bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001868 TheCondStack.push_back(TheCondState);
1869 TheCondState.TheCond = AsmCond::IfCond;
1870 if(TheCondState.Ignore) {
1871 EatToEndOfStatement();
1872 }
1873 else {
1874 int64_t ExprValue;
1875 if (ParseAbsoluteExpression(ExprValue))
1876 return true;
1877
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001878 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001879 return TokError("unexpected token in '.if' directive");
1880
Sean Callanan79ed1a82010-01-19 20:22:31 +00001881 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001882
1883 TheCondState.CondMet = ExprValue;
1884 TheCondState.Ignore = !TheCondState.CondMet;
1885 }
1886
1887 return false;
1888}
1889
1890/// ParseDirectiveElseIf
1891/// ::= .elseif expression
1892bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1893 if (TheCondState.TheCond != AsmCond::IfCond &&
1894 TheCondState.TheCond != AsmCond::ElseIfCond)
1895 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1896 " an .elseif");
1897 TheCondState.TheCond = AsmCond::ElseIfCond;
1898
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001899 bool LastIgnoreState = false;
1900 if (!TheCondStack.empty())
1901 LastIgnoreState = TheCondStack.back().Ignore;
1902 if (LastIgnoreState || TheCondState.CondMet) {
1903 TheCondState.Ignore = true;
1904 EatToEndOfStatement();
1905 }
1906 else {
1907 int64_t ExprValue;
1908 if (ParseAbsoluteExpression(ExprValue))
1909 return true;
1910
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001911 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001912 return TokError("unexpected token in '.elseif' directive");
1913
Sean Callanan79ed1a82010-01-19 20:22:31 +00001914 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001915 TheCondState.CondMet = ExprValue;
1916 TheCondState.Ignore = !TheCondState.CondMet;
1917 }
1918
1919 return false;
1920}
1921
1922/// ParseDirectiveElse
1923/// ::= .else
1924bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001925 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001926 return TokError("unexpected token in '.else' directive");
1927
Sean Callanan79ed1a82010-01-19 20:22:31 +00001928 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001929
1930 if (TheCondState.TheCond != AsmCond::IfCond &&
1931 TheCondState.TheCond != AsmCond::ElseIfCond)
1932 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1933 ".elseif");
1934 TheCondState.TheCond = AsmCond::ElseCond;
1935 bool LastIgnoreState = false;
1936 if (!TheCondStack.empty())
1937 LastIgnoreState = TheCondStack.back().Ignore;
1938 if (LastIgnoreState || TheCondState.CondMet)
1939 TheCondState.Ignore = true;
1940 else
1941 TheCondState.Ignore = false;
1942
1943 return false;
1944}
1945
1946/// ParseDirectiveEndIf
1947/// ::= .endif
1948bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001949 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001950 return TokError("unexpected token in '.endif' directive");
1951
Sean Callanan79ed1a82010-01-19 20:22:31 +00001952 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001953
1954 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1955 TheCondStack.empty())
1956 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1957 ".else");
1958 if (!TheCondStack.empty()) {
1959 TheCondState = TheCondStack.back();
1960 TheCondStack.pop_back();
1961 }
1962
1963 return false;
1964}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001965
1966/// ParseDirectiveFile
1967/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001968bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001969 // FIXME: I'm not sure what this is.
1970 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001971 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001972 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001973 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001974
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001975 if (FileNumber < 1)
1976 return TokError("file number less than one");
1977 }
1978
Daniel Dunbareceec052010-07-12 17:45:27 +00001979 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001980 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001981
Chris Lattnerd32e8032010-01-25 19:02:58 +00001982 StringRef Filename = getTok().getString();
1983 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001984 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001985
Daniel Dunbareceec052010-07-12 17:45:27 +00001986 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001987 return TokError("unexpected token in '.file' directive");
1988
Chris Lattnerd32e8032010-01-25 19:02:58 +00001989 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001990 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001991 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001992 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1993
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001994 return false;
1995}
1996
1997/// ParseDirectiveLine
1998/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001999bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002000 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2001 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002002 return TokError("unexpected token in '.line' directive");
2003
Sean Callanan18b83232010-01-19 21:44:56 +00002004 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002005 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002006 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002007
2008 // FIXME: Do something with the .line.
2009 }
2010
Daniel Dunbareceec052010-07-12 17:45:27 +00002011 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002012 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002013
2014 return false;
2015}
2016
2017
2018/// ParseDirectiveLoc
2019/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002020bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002021 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002022 return TokError("unexpected token in '.loc' directive");
2023
2024 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002025 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002026 (void) FileNumber;
2027 // FIXME: Validate file.
2028
Sean Callanan79ed1a82010-01-19 20:22:31 +00002029 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002030 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2031 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002032 return TokError("unexpected token in '.loc' directive");
2033
Sean Callanan18b83232010-01-19 21:44:56 +00002034 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002035 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002036 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002037
Daniel Dunbareceec052010-07-12 17:45:27 +00002038 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2039 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002040 return TokError("unexpected token in '.loc' directive");
2041
Sean Callanan18b83232010-01-19 21:44:56 +00002042 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002043 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002044 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002045
2046 // FIXME: Do something with the .loc.
2047 }
2048 }
2049
Daniel Dunbareceec052010-07-12 17:45:27 +00002050 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002051 return TokError("unexpected token in '.file' directive");
2052
2053 return false;
2054}
2055