blob: 3a1cfc41a478521153e86aa5827bf7110ebebc45 [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
69 Parser.AddDirectiveHandler(this, ".subsections_via_symbols",
70 MCAsmParser::DirectiveHandler(
71 &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols));
72 Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler(
73 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
74 Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler(
75 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
76 Parser.AddDirectiveHandler(this, ".secure_log_unique",
77 MCAsmParser::DirectiveHandler(
78 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
79 Parser.AddDirectiveHandler(this, ".secure_log_reset",
80 MCAsmParser::DirectiveHandler(
81 &DarwinAsmParser::ParseDirectiveSecureLogReset));
Daniel Dunbare4749702010-07-12 18:12:02 +000082 }
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000083
84 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
85 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
86 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
87 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
Daniel Dunbare4749702010-07-12 18:12:02 +000088};
89
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000090}
91
Chris Lattneraaec2052010-01-19 19:46:13 +000092enum { DEFAULT_ADDRSPACE = 0 };
93
Daniel Dunbar9186fa62010-07-01 20:41:56 +000094AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
95 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000096 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +000097 GenericParser(new GenericAsmParser), PlatformParser(0),
98 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000099 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000100
101 // Initialize the generic parser.
102 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000103
104 // Initialize the platform / file format parser.
105 //
106 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
107 // created.
108 if (_MAI.hasSubsectionsViaSymbols()) {
109 PlatformParser = new DarwinAsmParser;
110 PlatformParser->Initialize(*this);
111 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000112}
113
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000114AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000115 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000116 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000117}
118
Daniel Dunbar53131982010-07-12 17:27:45 +0000119void AsmParser::setTargetParser(TargetAsmParser &P) {
120 assert(!TargetParser && "Target parser is already initialized!");
121 TargetParser = &P;
122 TargetParser->Initialize(*this);
123}
124
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000125void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000126 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000127}
128
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000129bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000130 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000131 return true;
132}
133
Sean Callananbf2013e2010-01-20 23:19:55 +0000134void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
135 const char *Type) const {
136 SrcMgr.PrintMessage(Loc, Msg, Type);
137}
Sean Callananfd0b0282010-01-21 00:19:58 +0000138
139bool AsmParser::EnterIncludeFile(const std::string &Filename) {
140 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
141 if (NewBuf == -1)
142 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000143
Sean Callananfd0b0282010-01-21 00:19:58 +0000144 CurBuffer = NewBuf;
145
146 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
147
148 return false;
149}
150
151const AsmToken &AsmParser::Lex() {
152 const AsmToken *tok = &Lexer.Lex();
153
154 if (tok->is(AsmToken::Eof)) {
155 // If this is the end of an included file, pop the parent file off the
156 // include stack.
157 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
158 if (ParentIncludeLoc != SMLoc()) {
159 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
160 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
161 ParentIncludeLoc.getPointer());
162 tok = &Lexer.Lex();
163 }
164 }
165
166 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000167 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000168
Sean Callananfd0b0282010-01-21 00:19:58 +0000169 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000170}
171
Chris Lattner79180e22010-04-05 23:15:42 +0000172bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000173 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000174 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000175 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000176 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000177 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000178 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
179 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000180
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000181 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000182 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000183
Chris Lattnerb717fb02009-07-02 21:53:43 +0000184 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000185
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000186 AsmCond StartingCondState = TheCondState;
187
Chris Lattnerb717fb02009-07-02 21:53:43 +0000188 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000189 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000190 if (!ParseStatement()) continue;
191
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000192 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000193 HadError = true;
194 EatToEndOfStatement();
195 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000196
197 if (TheCondState.TheCond != StartingCondState.TheCond ||
198 TheCondState.Ignore != StartingCondState.Ignore)
199 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000200
Chris Lattner79180e22010-04-05 23:15:42 +0000201 // Finalize the output stream if there are no errors and if the client wants
202 // us to.
203 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000204 Out.Finish();
205
Chris Lattnerb717fb02009-07-02 21:53:43 +0000206 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000207}
208
Chris Lattner2cf5f142009-06-22 01:29:09 +0000209/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
210void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000211 while (Lexer.isNot(AsmToken::EndOfStatement) &&
212 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000213 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000214
215 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000216 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000217 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000218}
219
Chris Lattnerc4193832009-06-22 05:51:26 +0000220
Chris Lattner74ec1a32009-06-22 06:32:03 +0000221/// ParseParenExpr - Parse a paren expression and return it.
222/// NOTE: This assumes the leading '(' has already been consumed.
223///
224/// parenexpr ::= expr)
225///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000226bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000227 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000228 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000229 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000230 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000231 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000232 return false;
233}
Chris Lattnerc4193832009-06-22 05:51:26 +0000234
Daniel Dunbar959fd882009-08-26 22:13:22 +0000235MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000236 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000237 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000238}
239
Chris Lattner74ec1a32009-06-22 06:32:03 +0000240/// ParsePrimaryExpr - Parse a primary expression and return it.
241/// primaryexpr ::= (parenexpr
242/// primaryexpr ::= symbol
243/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000244/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000245/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000246bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000247 switch (Lexer.getKind()) {
248 default:
249 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000250 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000251 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000252 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000253 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000254 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000255 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000256 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000257 case AsmToken::Identifier: {
258 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000259 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
260 MCSymbol *Sym = CreateSymbol(Split.first);
261
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000262 // Mark the symbol as used in an expression.
263 Sym->setUsedInExpr(true);
264
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000265 // Lookup the symbol variant if used.
266 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
267 if (Split.first.size() != getTok().getIdentifier().size())
268 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
269
Chris Lattnerb4307b32010-01-15 19:28:38 +0000270 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000271 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000272
273 // If this is an absolute variable reference, substitute it now to preserve
274 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000275 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000276 if (Variant)
277 return Error(EndLoc, "unexpected modified on variable reference");
278
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000279 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000280 return false;
281 }
282
283 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000284 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000285 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000286 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000287 case AsmToken::Integer: {
288 SMLoc Loc = getTok().getLoc();
289 int64_t IntVal = getTok().getIntVal();
290 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000291 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000292 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000293 // Look for 'b' or 'f' following an Integer as a directional label
294 if (Lexer.getKind() == AsmToken::Identifier) {
295 StringRef IDVal = getTok().getString();
296 if (IDVal == "f" || IDVal == "b"){
297 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
298 IDVal == "f" ? 1 : 0);
299 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
300 getContext());
301 if(IDVal == "b" && Sym->isUndefined())
302 return Error(Loc, "invalid reference to undefined symbol");
303 EndLoc = Lexer.getLoc();
304 Lex(); // Eat identifier.
305 }
306 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000307 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000308 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000309 case AsmToken::Dot: {
310 // This is a '.' reference, which references the current PC. Emit a
311 // temporary label to the streamer and refer to it.
312 MCSymbol *Sym = Ctx.CreateTempSymbol();
313 Out.EmitLabel(Sym);
314 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
315 EndLoc = Lexer.getLoc();
316 Lex(); // Eat identifier.
317 return false;
318 }
319
Daniel Dunbar3f872332009-07-28 16:08:33 +0000320 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000321 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000322 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000323 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000324 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000325 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000326 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000327 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000328 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000330 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000331 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000332 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000333 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000336 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000337 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000338 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000339 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000341 }
342}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000343
Chris Lattnerb4307b32010-01-15 19:28:38 +0000344bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000345 SMLoc EndLoc;
346 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000347}
348
Chris Lattner74ec1a32009-06-22 06:32:03 +0000349/// ParseExpression - Parse an expression and return it.
350///
351/// expr ::= expr +,- expr -> lowest.
352/// expr ::= expr |,^,&,! expr -> middle.
353/// expr ::= expr *,/,%,<<,>> expr -> highest.
354/// expr ::= primaryexpr
355///
Chris Lattner54482b42010-01-15 19:39:23 +0000356bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000357 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000358 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000359 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
360 return true;
361
362 // Try to constant fold it up front, if possible.
363 int64_t Value;
364 if (Res->EvaluateAsAbsolute(Value))
365 Res = MCConstantExpr::Create(Value, getContext());
366
367 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000368}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000369
Chris Lattnerb4307b32010-01-15 19:28:38 +0000370bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000371 Res = 0;
372 return ParseParenExpr(Res, EndLoc) ||
373 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000374}
375
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000377 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000378
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000379 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 if (ParseExpression(Expr))
381 return true;
382
Daniel Dunbare00b0112009-10-16 01:57:52 +0000383 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000384 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385
386 return false;
387}
388
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000391 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000392 default:
393 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394
395 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000396 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000397 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000398 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000399 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000400 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000401 return 1;
402
403 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000404 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000405 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000406 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000407 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000408 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000409 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000410 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000411 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000412 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000413 case AsmToken::ExclaimEqual:
414 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000416 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000417 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000418 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000419 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000420 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000421 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000422 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000423 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000424 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000425 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000426 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000427 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000428 return 2;
429
430 // Intermediate Precedence: |, &, ^
431 //
432 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000433 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000434 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000435 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000436 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000437 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000438 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000439 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000440 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000441 return 3;
442
443 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000444 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000445 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000446 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000447 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000448 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000449 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000451 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000452 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000454 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000455 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000456 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000457 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000458 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000459 }
460}
461
462
463/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
464/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000465bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
466 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000467 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000468 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000469 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000470
471 // If the next token is lower precedence than we are allowed to eat, return
472 // successfully with what we ate already.
473 if (TokPrec < Precedence)
474 return false;
475
Sean Callanan79ed1a82010-01-19 20:22:31 +0000476 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000477
478 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000479 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000480 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000481
482 // If BinOp binds less tightly with RHS than the operator after RHS, let
483 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000484 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000485 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000486 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000487 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000488 }
489
Daniel Dunbar475839e2009-06-29 20:37:27 +0000490 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000491 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000492 }
493}
494
Chris Lattnerc4193832009-06-22 05:51:26 +0000495
496
497
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000498/// ParseStatement:
499/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000500/// ::= Label* Directive ...Operands... EndOfStatement
501/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000502bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000503 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000504 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000505 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000506 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000507 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000508
509 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000510 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000511 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000512 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000513 int64_t LocalLabelVal = -1;
514 // GUESS allow an integer followed by a ':' as a directional local label
515 if (Lexer.is(AsmToken::Integer)) {
516 LocalLabelVal = getTok().getIntVal();
517 if (LocalLabelVal < 0) {
518 if (!TheCondState.Ignore)
519 return TokError("unexpected token at start of statement");
520 IDVal = "";
521 }
522 else {
523 IDVal = getTok().getString();
524 Lex(); // Consume the integer token to be used as an identifier token.
525 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000526 if (!TheCondState.Ignore)
527 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000528 }
529 }
530 }
531 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000532 if (!TheCondState.Ignore)
533 return TokError("unexpected token at start of statement");
534 IDVal = "";
535 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000536
Chris Lattner7834fac2010-04-17 18:14:27 +0000537 // Handle conditional assembly here before checking for skipping. We
538 // have to do this so that .endif isn't skipped in a ".if 0" block for
539 // example.
540 if (IDVal == ".if")
541 return ParseDirectiveIf(IDLoc);
542 if (IDVal == ".elseif")
543 return ParseDirectiveElseIf(IDLoc);
544 if (IDVal == ".else")
545 return ParseDirectiveElse(IDLoc);
546 if (IDVal == ".endif")
547 return ParseDirectiveEndIf(IDLoc);
548
549 // If we are in a ".if 0" block, ignore this statement.
550 if (TheCondState.Ignore) {
551 EatToEndOfStatement();
552 return false;
553 }
554
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000555 // FIXME: Recurse on local labels?
556
557 // See what kind of statement we have.
558 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000559 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000560 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000561 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000562
563 // Diagnose attempt to use a variable as a label.
564 //
565 // FIXME: Diagnostics. Note the location of the definition as a label.
566 // FIXME: This doesn't diagnose assignment to a symbol which has been
567 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000568 MCSymbol *Sym;
569 if (LocalLabelVal == -1)
570 Sym = CreateSymbol(IDVal);
571 else
572 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000573 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000574 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000575
Daniel Dunbar959fd882009-08-26 22:13:22 +0000576 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000577 Out.EmitLabel(Sym);
578
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000579 // Consume any end of statement token, if present, to avoid spurious
580 // AddBlankLine calls().
581 if (Lexer.is(AsmToken::EndOfStatement)) {
582 Lex();
583 if (Lexer.is(AsmToken::Eof))
584 return false;
585 }
586
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000587 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000588 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000589
Daniel Dunbar3f872332009-07-28 16:08:33 +0000590 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000591 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000592 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000593
Daniel Dunbare2ace502009-08-31 08:09:09 +0000594 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000595
596 default: // Normal instruction or directive.
597 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000598 }
599
600 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000601 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000602 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000604 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000606 // FIXME: This changes behavior based on the -static flag to the
607 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000608 return ParseDirectiveSectionSwitch("__TEXT", "__text",
609 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000615 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
616 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000618 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000619 MCSectionMachO::S_4BYTE_LITERALS,
620 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000622 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000623 MCSectionMachO::S_8BYTE_LITERALS,
624 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000625 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000626 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000627 MCSectionMachO::S_16BYTE_LITERALS,
628 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000630 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000632 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000634 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000636 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
637
638 // FIXME: The assembler manual claims that this has the self modify code
639 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000640 if (IDVal == ".symbol_stub")
641 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
642 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000643 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
644 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000645 0, 16);
646 // FIXME: PowerPC only?
647 if (IDVal == ".picsymbol_stub")
648 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
649 MCSectionMachO::S_SYMBOL_STUBS |
650 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
651 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000653 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000655 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
656
657 // FIXME: The section names of these two are misspelled in the assembler
658 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000660 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
661 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
662 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000664 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
665 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
666 4);
667
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000669 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000670 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000671 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000672 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
673 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000675 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000676 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
677 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000679 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000680
681
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000683 return ParseDirectiveSectionSwitch("__OBJC", "__class",
684 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000686 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
687 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000689 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
690 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000692 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
693 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000695 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
696 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000698 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
699 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000701 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
702 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000704 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
705 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000707 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
708 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
709 MCSectionMachO::S_LITERAL_POINTERS,
710 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000711 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000712 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
713 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
714 MCSectionMachO::S_LITERAL_POINTERS,
715 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000717 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
718 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000720 return ParseDirectiveSectionSwitch("__OBJC", "__category",
721 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000723 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
724 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000726 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
727 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000729 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
730 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000732 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
733 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000735 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
736 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000738 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
739 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000741 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
742 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000743
Eric Christopherc6177a42010-05-17 22:53:55 +0000744 if (IDVal == ".tdata")
745 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
746 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
747 if (IDVal == ".tlv")
748 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
749 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
750 if (IDVal == ".thread_init_func")
751 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
752 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
753
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000754 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000756 return ParseDirectiveSet();
757
Daniel Dunbara0d14262009-06-24 23:30:00 +0000758 // Data directives
759
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000761 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000763 return ParseDirectiveAscii(true);
764
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000765 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000766 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000767 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000768 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000770 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000771 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000772 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000773
774 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000775 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000776 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000777 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000778 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000779 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000780 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000781 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000782 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000783 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000784 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000785 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000786 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000787 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000788 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000789 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000790 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
791
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000792 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000793 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000794
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000795 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000796 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000797 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000798 return ParseDirectiveSpace();
799
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000800 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000801
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000802 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000803 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000805 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000806 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000807 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000808 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000809 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000810 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000811 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000812 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000813 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000814 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000815 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000816 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000817 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000818 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000819 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000820 if (IDVal == ".type")
821 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000822 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000823 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000824 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000825 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000826 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000827 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000828 if (IDVal == ".weak_def_can_be_hidden")
829 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000830
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000832 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000833 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000834 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000835 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000836 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000838 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000840 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000841 if (IDVal == ".tbss")
842 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000843
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000844 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000845 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000846 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000847 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000848
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000849 // Look up the handler in the handler table.
850 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
851 DirectiveMap.lookup(IDVal);
852 if (Handler.first)
853 return (Handler.first->*Handler.second)(IDVal, IDLoc);
854
Kevin Enderby9c656452009-09-10 20:51:44 +0000855 // Target hook for parsing target specific directives.
856 if (!getTargetParser().ParseDirective(ID))
857 return false;
858
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000859 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000860 EatToEndOfStatement();
861 return false;
862 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000863
Chris Lattnera7f13542010-05-19 23:34:33 +0000864 // Canonicalize the opcode to lower case.
865 SmallString<128> Opcode;
866 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
867 Opcode.push_back(tolower(IDVal[i]));
868
Chris Lattner98986712010-01-14 22:21:20 +0000869 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000870 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000871 ParsedOperands);
872 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
873 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000874
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000875 // If parsing succeeded, match the instruction.
876 if (!HadError) {
877 MCInst Inst;
878 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
879 // Emit the instruction on success.
880 Out.EmitInstruction(Inst);
881 } else {
882 // Otherwise emit a diagnostic about the match failure and set the error
883 // flag.
884 //
885 // FIXME: We should give nicer diagnostics about the exact failure.
886 Error(IDLoc, "unrecognized instruction");
887 HadError = true;
888 }
889 }
Chris Lattner98986712010-01-14 22:21:20 +0000890
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000891 // If there was no error, consume the end-of-statement token. Otherwise this
892 // will be done by our caller.
893 if (!HadError)
894 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000895
896 // Free any parsed operands.
897 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
898 delete ParsedOperands[i];
899
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000900 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000901}
Chris Lattner9a023f72009-06-24 04:43:34 +0000902
Daniel Dunbare2ace502009-08-31 08:09:09 +0000903bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000904 // FIXME: Use better location, we should use proper tokens.
905 SMLoc EqualLoc = Lexer.getLoc();
906
Daniel Dunbar821e3332009-08-31 08:09:28 +0000907 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000908 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000909 return true;
910
Daniel Dunbar3f872332009-07-28 16:08:33 +0000911 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000912 return TokError("unexpected token in assignment");
913
914 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000915 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000916
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000917 // Validate that the LHS is allowed to be a variable (either it has not been
918 // used as a symbol, or it is an absolute symbol).
919 MCSymbol *Sym = getContext().LookupSymbol(Name);
920 if (Sym) {
921 // Diagnose assignment to a label.
922 //
923 // FIXME: Diagnostics. Note the location of the definition as a label.
924 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000925 if (Sym->isUndefined() && !Sym->isUsedInExpr())
926 ; // Allow redefinitions of undefined symbols only used in directives.
927 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000928 return Error(EqualLoc, "redefinition of '" + Name + "'");
929 else if (!Sym->isVariable())
930 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000931 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000932 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
933 Name + "'");
934 } else
935 Sym = CreateSymbol(Name);
936
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000937 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000938
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000939 Sym->setUsedInExpr(true);
940
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000941 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000942 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000943
944 return false;
945}
946
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000947/// ParseIdentifier:
948/// ::= identifier
949/// ::= string
950bool AsmParser::ParseIdentifier(StringRef &Res) {
951 if (Lexer.isNot(AsmToken::Identifier) &&
952 Lexer.isNot(AsmToken::String))
953 return true;
954
Sean Callanan18b83232010-01-19 21:44:56 +0000955 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000956
Sean Callanan79ed1a82010-01-19 20:22:31 +0000957 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000958
959 return false;
960}
961
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000962/// ParseDirectiveSet:
963/// ::= .set identifier ',' expression
964bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000965 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000966
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000967 if (ParseIdentifier(Name))
968 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000969
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000970 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000971 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000972 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000973
Daniel Dunbare2ace502009-08-31 08:09:09 +0000974 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000975}
976
Chris Lattner9a023f72009-06-24 04:43:34 +0000977/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000978/// ::= .section identifier (',' identifier)*
979/// FIXME: This should actually parse out the segment, section, attributes and
980/// sizeof_stub fields.
981bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000982 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000983
Daniel Dunbarace63122009-08-11 03:42:33 +0000984 StringRef SectionName;
985 if (ParseIdentifier(SectionName))
986 return Error(Loc, "expected identifier after '.section' directive");
987
988 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000989 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +0000990 return TokError("unexpected token in '.section' directive");
991
Chris Lattnerff4bc462009-08-10 01:39:42 +0000992 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000993 SectionSpec += ",";
994
995 // Add all the tokens until the end of the line, ParseSectionSpecifier will
996 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000997 StringRef EOL = Lexer.LexUntilEndOfStatement();
998 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000999
Sean Callanan79ed1a82010-01-19 20:22:31 +00001000 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001001 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +00001002 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001003 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +00001004
Chris Lattnerff4bc462009-08-10 01:39:42 +00001005
1006 StringRef Segment, Section;
1007 unsigned TAA, StubSize;
1008 std::string ErrorStr =
1009 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1010 TAA, StubSize);
1011
1012 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001013 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +00001014
Chris Lattner56594f92009-07-31 17:47:16 +00001015 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001016 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001017 getStreamer().SwitchSection(Ctx.getMachOSection(
1018 Segment, Section, TAA, StubSize,
1019 isText ? SectionKind::getText()
1020 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001021 return false;
1022}
1023
Chris Lattnere15c2d72009-08-10 18:05:55 +00001024/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +00001025bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
1026 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +00001027 unsigned TAA, unsigned Align,
1028 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001029 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001030 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001031 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +00001032
Chris Lattner56594f92009-07-31 17:47:16 +00001033 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001034 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001035 getStreamer().SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001036 isText ? SectionKind::getText()
1037 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001038
1039 // Set the implicit alignment, if any.
1040 //
1041 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1042 // alignment on the section (e.g., if one manually inserts bytes into the
1043 // section, then just issueing the section switch directive will not realign
1044 // the section. However, this is arguably more reasonable behavior, and there
1045 // is no good reason for someone to intentionally emit incorrectly sized
1046 // values into the implicitly aligned sections.
1047 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001048 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001049
Chris Lattner529fb542009-06-24 05:13:15 +00001050 return false;
1051}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001052
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001053bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001054 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001055
1056 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001057 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001058 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1059 if (Str[i] != '\\') {
1060 Data += Str[i];
1061 continue;
1062 }
1063
1064 // Recognize escaped characters. Note that this escape semantics currently
1065 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1066 ++i;
1067 if (i == e)
1068 return TokError("unexpected backslash at end of string");
1069
1070 // Recognize octal sequences.
1071 if ((unsigned) (Str[i] - '0') <= 7) {
1072 // Consume up to three octal characters.
1073 unsigned Value = Str[i] - '0';
1074
1075 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1076 ++i;
1077 Value = Value * 8 + (Str[i] - '0');
1078
1079 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1080 ++i;
1081 Value = Value * 8 + (Str[i] - '0');
1082 }
1083 }
1084
1085 if (Value > 255)
1086 return TokError("invalid octal escape sequence (out of range)");
1087
1088 Data += (unsigned char) Value;
1089 continue;
1090 }
1091
1092 // Otherwise recognize individual escapes.
1093 switch (Str[i]) {
1094 default:
1095 // Just reject invalid escape sequences for now.
1096 return TokError("invalid escape sequence (unrecognized character)");
1097
1098 case 'b': Data += '\b'; break;
1099 case 'f': Data += '\f'; break;
1100 case 'n': Data += '\n'; break;
1101 case 'r': Data += '\r'; break;
1102 case 't': Data += '\t'; break;
1103 case '"': Data += '"'; break;
1104 case '\\': Data += '\\'; break;
1105 }
1106 }
1107
1108 return false;
1109}
1110
Daniel Dunbara0d14262009-06-24 23:30:00 +00001111/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001112/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001114 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001116 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001118
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001119 std::string Data;
1120 if (ParseEscapedString(Data))
1121 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001122
1123 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001125 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1126
Sean Callanan79ed1a82010-01-19 20:22:31 +00001127 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001128
1129 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130 break;
1131
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001132 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001133 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001134 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001135 }
1136 }
1137
Sean Callanan79ed1a82010-01-19 20:22:31 +00001138 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001139 return false;
1140}
1141
1142/// ParseDirectiveValue
1143/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1144bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001145 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001146 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001147 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001148 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001149 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001150 return true;
1151
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001152 // Special case constant expressions to match code generator.
1153 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001154 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001155 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001156 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001157
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001158 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001159 break;
1160
1161 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001162 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001163 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001164 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001165 }
1166 }
1167
Sean Callanan79ed1a82010-01-19 20:22:31 +00001168 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001169 return false;
1170}
1171
1172/// ParseDirectiveSpace
1173/// ::= .space expression [ , expression ]
1174bool AsmParser::ParseDirectiveSpace() {
1175 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001176 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001177 return true;
1178
1179 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001180 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1181 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001182 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001183 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001184
Daniel Dunbar475839e2009-06-29 20:37:27 +00001185 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001186 return true;
1187
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001188 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001189 return TokError("unexpected token in '.space' directive");
1190 }
1191
Sean Callanan79ed1a82010-01-19 20:22:31 +00001192 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001193
1194 if (NumBytes <= 0)
1195 return TokError("invalid number of bytes in '.space' directive");
1196
1197 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001198 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001199
1200 return false;
1201}
1202
1203/// ParseDirectiveFill
1204/// ::= .fill expression , expression , expression
1205bool AsmParser::ParseDirectiveFill() {
1206 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001207 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001208 return true;
1209
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001210 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001211 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001212 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001213
1214 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001215 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001216 return true;
1217
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001218 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001219 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001220 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001221
1222 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001223 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001224 return true;
1225
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001226 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001227 return TokError("unexpected token in '.fill' directive");
1228
Sean Callanan79ed1a82010-01-19 20:22:31 +00001229 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001230
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001231 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1232 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001233
1234 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001235 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001236
1237 return false;
1238}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001239
1240/// ParseDirectiveOrg
1241/// ::= .org expression [ , expression ]
1242bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001243 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001244 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001245 return true;
1246
1247 // Parse optional fill expression.
1248 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001249 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1250 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001251 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001252 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001253
Daniel Dunbar475839e2009-06-29 20:37:27 +00001254 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001255 return true;
1256
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001257 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001258 return TokError("unexpected token in '.org' directive");
1259 }
1260
Sean Callanan79ed1a82010-01-19 20:22:31 +00001261 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001262
1263 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1264 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001265 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001266
1267 return false;
1268}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001269
1270/// ParseDirectiveAlign
1271/// ::= {.align, ...} expression [ , expression [ , expression ]]
1272bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001273 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001274 int64_t Alignment;
1275 if (ParseAbsoluteExpression(Alignment))
1276 return true;
1277
1278 SMLoc MaxBytesLoc;
1279 bool HasFillExpr = false;
1280 int64_t FillExpr = 0;
1281 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001282 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1283 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001284 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001285 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001286
1287 // The fill expression can be omitted while specifying a maximum number of
1288 // alignment bytes, e.g:
1289 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001290 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001291 HasFillExpr = true;
1292 if (ParseAbsoluteExpression(FillExpr))
1293 return true;
1294 }
1295
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001296 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1297 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001298 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001299 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001300
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001301 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001302 if (ParseAbsoluteExpression(MaxBytesToFill))
1303 return true;
1304
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001305 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001306 return TokError("unexpected token in directive");
1307 }
1308 }
1309
Sean Callanan79ed1a82010-01-19 20:22:31 +00001310 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001311
Daniel Dunbar648ac512010-05-17 21:54:30 +00001312 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001313 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001314
1315 // Compute alignment in bytes.
1316 if (IsPow2) {
1317 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001318 if (Alignment >= 32) {
1319 Error(AlignmentLoc, "invalid alignment value");
1320 Alignment = 31;
1321 }
1322
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001323 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001324 }
1325
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001326 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001327 if (MaxBytesLoc.isValid()) {
1328 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001329 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1330 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001331 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001332 }
1333
1334 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001335 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1336 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001337 MaxBytesToFill = 0;
1338 }
1339 }
1340
Daniel Dunbar648ac512010-05-17 21:54:30 +00001341 // Check whether we should use optimal code alignment for this .align
1342 // directive.
1343 //
1344 // FIXME: This should be using a target hook.
1345 bool UseCodeAlign = false;
1346 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001347 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001348 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1349 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1350 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001351 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001352 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001353 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001354 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001355 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001356
1357 return false;
1358}
1359
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001360/// ParseDirectiveSymbolAttribute
1361/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001362bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001363 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001364 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001365 StringRef Name;
1366
1367 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001368 return TokError("expected identifier in directive");
1369
Daniel Dunbar959fd882009-08-26 22:13:22 +00001370 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001371
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001372 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001373
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001374 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001375 break;
1376
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001377 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001378 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001379 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001380 }
1381 }
1382
Sean Callanan79ed1a82010-01-19 20:22:31 +00001383 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001384 return false;
1385}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001386
Matt Fleming924c5e52010-05-21 11:36:59 +00001387/// ParseDirectiveELFType
1388/// ::= .type identifier , @attribute
1389bool AsmParser::ParseDirectiveELFType() {
1390 StringRef Name;
1391 if (ParseIdentifier(Name))
1392 return TokError("expected identifier in directive");
1393
1394 // Handle the identifier as the key symbol.
1395 MCSymbol *Sym = CreateSymbol(Name);
1396
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001397 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001398 return TokError("unexpected token in '.type' directive");
1399 Lex();
1400
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001401 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001402 return TokError("expected '@' before type");
1403 Lex();
1404
1405 StringRef Type;
1406 SMLoc TypeLoc;
1407
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001408 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001409 if (ParseIdentifier(Type))
1410 return TokError("expected symbol type in directive");
1411
1412 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1413 .Case("function", MCSA_ELF_TypeFunction)
1414 .Case("object", MCSA_ELF_TypeObject)
1415 .Case("tls_object", MCSA_ELF_TypeTLS)
1416 .Case("common", MCSA_ELF_TypeCommon)
1417 .Case("notype", MCSA_ELF_TypeNoType)
1418 .Default(MCSA_Invalid);
1419
1420 if (Attr == MCSA_Invalid)
1421 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1422
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001423 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001424 return TokError("unexpected token in '.type' directive");
1425
1426 Lex();
1427
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001428 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001429
1430 return false;
1431}
1432
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001433/// ParseDirectiveDarwinSymbolDesc
1434/// ::= .desc identifier , expression
1435bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001436 StringRef Name;
1437 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001438 return TokError("expected identifier in directive");
1439
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001440 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001441 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001442
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001443 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001444 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001445 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001446
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001447 int64_t DescValue;
1448 if (ParseAbsoluteExpression(DescValue))
1449 return true;
1450
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001451 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001452 return TokError("unexpected token in '.desc' directive");
1453
Sean Callanan79ed1a82010-01-19 20:22:31 +00001454 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001455
1456 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001457 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001458
1459 return false;
1460}
1461
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001462/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001463/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1464bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001465 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001466 StringRef Name;
1467 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001468 return TokError("expected identifier in directive");
1469
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001470 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001471 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001472
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001473 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001474 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001475 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001476
1477 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001478 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001479 if (ParseAbsoluteExpression(Size))
1480 return true;
1481
1482 int64_t Pow2Alignment = 0;
1483 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001484 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001485 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001486 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001487 if (ParseAbsoluteExpression(Pow2Alignment))
1488 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001489
1490 // If this target takes alignments in bytes (not log) validate and convert.
1491 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1492 if (!isPowerOf2_64(Pow2Alignment))
1493 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1494 Pow2Alignment = Log2_64(Pow2Alignment);
1495 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001496 }
1497
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001498 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001499 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001500
Sean Callanan79ed1a82010-01-19 20:22:31 +00001501 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001502
Chris Lattner1fc3d752009-07-09 17:25:12 +00001503 // NOTE: a size of zero for a .comm should create a undefined symbol
1504 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001505 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001506 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1507 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001508
Eric Christopherc260a3e2010-05-14 01:38:54 +00001509 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001510 // may internally end up wanting an alignment in bytes.
1511 // FIXME: Diagnose overflow.
1512 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001513 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1514 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001515
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001516 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001517 return Error(IDLoc, "invalid symbol redefinition");
1518
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001519 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001520 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001521 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001522 getStreamer().EmitZerofill(Ctx.getMachOSection(
1523 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1524 0, SectionKind::getBSS()),
1525 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001526 return false;
1527 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001528
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001529 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001530 return false;
1531}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001532
1533/// ParseDirectiveDarwinZerofill
1534/// ::= .zerofill segname , sectname [, identifier , size_expression [
1535/// , align_expression ]]
1536bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001537 StringRef Segment;
1538 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001539 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001540
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001541 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001542 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001543 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001544
1545 StringRef Section;
1546 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001547 return TokError("expected section name after comma in '.zerofill' "
1548 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001549
Chris Lattner9be3fee2009-07-10 22:20:30 +00001550 // If this is the end of the line all that was wanted was to create the
1551 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001552 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001553 // Create the zerofill section but no symbol
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001554 getStreamer().EmitZerofill(Ctx.getMachOSection(Segment, Section,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001555 MCSectionMachO::S_ZEROFILL, 0,
1556 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001557 return false;
1558 }
1559
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001560 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001561 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001562 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001563
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001564 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001565 StringRef IDStr;
1566 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001567 return TokError("expected identifier in directive");
1568
1569 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001570 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001571
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001572 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001573 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001574 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001575
1576 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001577 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001578 if (ParseAbsoluteExpression(Size))
1579 return true;
1580
1581 int64_t Pow2Alignment = 0;
1582 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001583 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001584 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001585 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001586 if (ParseAbsoluteExpression(Pow2Alignment))
1587 return true;
1588 }
1589
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001590 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001591 return TokError("unexpected token in '.zerofill' directive");
1592
Sean Callanan79ed1a82010-01-19 20:22:31 +00001593 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001594
1595 if (Size < 0)
1596 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1597 "than zero");
1598
Eric Christopherc260a3e2010-05-14 01:38:54 +00001599 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001600 // may internally end up wanting an alignment in bytes.
1601 // FIXME: Diagnose overflow.
1602 if (Pow2Alignment < 0)
1603 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1604 "can't be less than zero");
1605
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001606 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001607 return Error(IDLoc, "invalid symbol redefinition");
1608
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001609 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001610 //
1611 // FIXME: Arch specific.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001612 getStreamer().EmitZerofill(Ctx.getMachOSection(Segment, Section,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001613 MCSectionMachO::S_ZEROFILL, 0,
1614 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001615 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001616
1617 return false;
1618}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001619
Eric Christopher482eba02010-05-14 01:50:28 +00001620/// ParseDirectiveDarwinTBSS
1621/// ::= .tbss identifier, size, align
1622bool AsmParser::ParseDirectiveDarwinTBSS() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001623 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001624 StringRef Name;
1625 if (ParseIdentifier(Name))
1626 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001627
Eric Christopher482eba02010-05-14 01:50:28 +00001628 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001629 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001630
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001631 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001632 return TokError("unexpected token in directive");
1633 Lex();
1634
1635 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 SMLoc SizeLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001637 if (ParseAbsoluteExpression(Size))
1638 return true;
1639
1640 int64_t Pow2Alignment = 0;
1641 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001642 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001643 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001644 Pow2AlignmentLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001645 if (ParseAbsoluteExpression(Pow2Alignment))
1646 return true;
1647 }
1648
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001649 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001650 return TokError("unexpected token in '.tbss' directive");
1651
1652 Lex();
1653
1654 if (Size < 0)
1655 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1656 "zero");
1657
1658 // FIXME: Diagnose overflow.
1659 if (Pow2Alignment < 0)
1660 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1661 "than zero");
1662
1663 if (!Sym->isUndefined())
1664 return Error(IDLoc, "invalid symbol redefinition");
1665
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001666 getStreamer().EmitTBSSSymbol(Ctx.getMachOSection(
1667 "__DATA", "__thread_bss",
1668 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1669 0, SectionKind::getThreadBSS()),
1670 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001671
1672 return false;
1673}
1674
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001675/// ParseDirectiveSubsectionsViaSymbols
Kevin Enderbya5c78322009-07-13 21:03:15 +00001676/// ::= .subsections_via_symbols
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001677bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001678 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001679 return TokError("unexpected token in '.subsections_via_symbols' directive");
1680
Sean Callanan79ed1a82010-01-19 20:22:31 +00001681 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001682
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001684
1685 return false;
1686}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001687
1688/// ParseDirectiveAbort
1689/// ::= .abort [ "abort_string" ]
1690bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001691 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001692 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001693
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001694 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1696 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001697 return TokError("expected string in '.abort' directive");
1698
Sean Callanan18b83232010-01-19 21:44:56 +00001699 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001700
Sean Callanan79ed1a82010-01-19 20:22:31 +00001701 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001702 }
1703
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001704 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001705 return TokError("unexpected token in '.abort' directive");
1706
Sean Callanan79ed1a82010-01-19 20:22:31 +00001707 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001708
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001709 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001710 if (Str.empty())
1711 Error(Loc, ".abort detected. Assembly stopping.");
1712 else
1713 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001714
1715 return false;
1716}
Kevin Enderby71148242009-07-14 21:35:03 +00001717
1718/// ParseDirectiveLsym
1719/// ::= .lsym identifier , expression
1720bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001721 StringRef Name;
1722 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001723 return TokError("expected identifier in directive");
1724
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001725 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001726 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001727
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001728 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001729 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001730 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001731
Daniel Dunbar821e3332009-08-31 08:09:28 +00001732 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001733 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001734 return true;
1735
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001736 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001737 return TokError("unexpected token in '.lsym' directive");
1738
Sean Callanan79ed1a82010-01-19 20:22:31 +00001739 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001740
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001741 // We don't currently support this directive.
1742 //
1743 // FIXME: Diagnostic location!
1744 (void) Sym;
1745 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001746}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001747
1748/// ParseDirectiveInclude
1749/// ::= .include "filename"
1750bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001751 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001752 return TokError("expected string in '.include' directive");
1753
Sean Callanan18b83232010-01-19 21:44:56 +00001754 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001755 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001756 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001757
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001758 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001759 return TokError("unexpected token in '.include' directive");
1760
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001761 // Strip the quotes.
1762 Filename = Filename.substr(1, Filename.size()-2);
1763
1764 // Attempt to switch the lexer to the included file before consuming the end
1765 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001766 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001767 PrintMessage(IncludeLoc,
1768 "Could not find include file '" + Filename + "'",
1769 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001770 return true;
1771 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001772
1773 return false;
1774}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001775
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001776/// ParseDirectiveDumpOrLoad
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001777/// ::= ( .dump | .load ) "filename"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001778bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1779 SMLoc IDLoc) {
1780 bool IsDump = Directive == ".dump";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001782 return TokError("expected string in '.dump' or '.load' directive");
1783
Sean Callanan79ed1a82010-01-19 20:22:31 +00001784 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001785
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001786 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001787 return TokError("unexpected token in '.dump' or '.load' directive");
1788
Sean Callanan79ed1a82010-01-19 20:22:31 +00001789 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001790
Kevin Enderby5026ae42009-07-20 20:25:37 +00001791 // FIXME: If/when .dump and .load are implemented they will be done in the
1792 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001793 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001794 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001795 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001796 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001797
1798 return false;
1799}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001800
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001801/// ParseDirectiveSecureLogUnique
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001802/// ::= .secure_log_unique "log message"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001803bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001804 std::string LogMessage;
1805
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001806 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001807 LogMessage = "";
1808 else{
1809 LogMessage = getTok().getString();
1810 Lex();
1811 }
1812
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001813 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001814 return TokError("unexpected token in '.secure_log_unique' directive");
1815
1816 if (getContext().getSecureLogUsed() != false)
1817 return Error(IDLoc, ".secure_log_unique specified multiple times");
1818
1819 char *SecureLogFile = getContext().getSecureLogFile();
1820 if (SecureLogFile == NULL)
1821 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1822 "environment variable unset.");
1823
1824 raw_ostream *OS = getContext().getSecureLog();
1825 if (OS == NULL) {
1826 std::string Err;
1827 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1828 if (!Err.empty()) {
1829 delete OS;
1830 return Error(IDLoc, Twine("can't open secure log file: ") +
1831 SecureLogFile + " (" + Err + ")");
1832 }
1833 getContext().setSecureLog(OS);
1834 }
1835
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001836 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
1837 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
1838 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001839 << LogMessage + "\n";
1840
1841 getContext().setSecureLogUsed(true);
1842
1843 return false;
1844}
1845
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001846/// ParseDirectiveSecureLogReset
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001847/// ::= .secure_log_reset
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001848bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001849 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001850 return TokError("unexpected token in '.secure_log_reset' directive");
1851
1852 Lex();
1853
1854 getContext().setSecureLogUsed(false);
1855
1856 return false;
1857}
1858
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001859/// ParseDirectiveIf
1860/// ::= .if expression
1861bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001862 TheCondStack.push_back(TheCondState);
1863 TheCondState.TheCond = AsmCond::IfCond;
1864 if(TheCondState.Ignore) {
1865 EatToEndOfStatement();
1866 }
1867 else {
1868 int64_t ExprValue;
1869 if (ParseAbsoluteExpression(ExprValue))
1870 return true;
1871
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001872 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001873 return TokError("unexpected token in '.if' directive");
1874
Sean Callanan79ed1a82010-01-19 20:22:31 +00001875 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001876
1877 TheCondState.CondMet = ExprValue;
1878 TheCondState.Ignore = !TheCondState.CondMet;
1879 }
1880
1881 return false;
1882}
1883
1884/// ParseDirectiveElseIf
1885/// ::= .elseif expression
1886bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1887 if (TheCondState.TheCond != AsmCond::IfCond &&
1888 TheCondState.TheCond != AsmCond::ElseIfCond)
1889 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1890 " an .elseif");
1891 TheCondState.TheCond = AsmCond::ElseIfCond;
1892
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001893 bool LastIgnoreState = false;
1894 if (!TheCondStack.empty())
1895 LastIgnoreState = TheCondStack.back().Ignore;
1896 if (LastIgnoreState || TheCondState.CondMet) {
1897 TheCondState.Ignore = true;
1898 EatToEndOfStatement();
1899 }
1900 else {
1901 int64_t ExprValue;
1902 if (ParseAbsoluteExpression(ExprValue))
1903 return true;
1904
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001905 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001906 return TokError("unexpected token in '.elseif' directive");
1907
Sean Callanan79ed1a82010-01-19 20:22:31 +00001908 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001909 TheCondState.CondMet = ExprValue;
1910 TheCondState.Ignore = !TheCondState.CondMet;
1911 }
1912
1913 return false;
1914}
1915
1916/// ParseDirectiveElse
1917/// ::= .else
1918bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001919 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001920 return TokError("unexpected token in '.else' directive");
1921
Sean Callanan79ed1a82010-01-19 20:22:31 +00001922 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001923
1924 if (TheCondState.TheCond != AsmCond::IfCond &&
1925 TheCondState.TheCond != AsmCond::ElseIfCond)
1926 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1927 ".elseif");
1928 TheCondState.TheCond = AsmCond::ElseCond;
1929 bool LastIgnoreState = false;
1930 if (!TheCondStack.empty())
1931 LastIgnoreState = TheCondStack.back().Ignore;
1932 if (LastIgnoreState || TheCondState.CondMet)
1933 TheCondState.Ignore = true;
1934 else
1935 TheCondState.Ignore = false;
1936
1937 return false;
1938}
1939
1940/// ParseDirectiveEndIf
1941/// ::= .endif
1942bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001943 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001944 return TokError("unexpected token in '.endif' directive");
1945
Sean Callanan79ed1a82010-01-19 20:22:31 +00001946 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001947
1948 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1949 TheCondStack.empty())
1950 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1951 ".else");
1952 if (!TheCondStack.empty()) {
1953 TheCondState = TheCondStack.back();
1954 TheCondStack.pop_back();
1955 }
1956
1957 return false;
1958}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001959
1960/// ParseDirectiveFile
1961/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001962bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001963 // FIXME: I'm not sure what this is.
1964 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001965 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001966 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001967 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001968
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001969 if (FileNumber < 1)
1970 return TokError("file number less than one");
1971 }
1972
Daniel Dunbareceec052010-07-12 17:45:27 +00001973 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001974 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001975
Chris Lattnerd32e8032010-01-25 19:02:58 +00001976 StringRef Filename = getTok().getString();
1977 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001978 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001979
Daniel Dunbareceec052010-07-12 17:45:27 +00001980 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001981 return TokError("unexpected token in '.file' directive");
1982
Chris Lattnerd32e8032010-01-25 19:02:58 +00001983 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001984 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001985 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001986 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1987
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001988 return false;
1989}
1990
1991/// ParseDirectiveLine
1992/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001993bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001994 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1995 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001996 return TokError("unexpected token in '.line' directive");
1997
Sean Callanan18b83232010-01-19 21:44:56 +00001998 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001999 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002000 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002001
2002 // FIXME: Do something with the .line.
2003 }
2004
Daniel Dunbareceec052010-07-12 17:45:27 +00002005 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002006 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002007
2008 return false;
2009}
2010
2011
2012/// ParseDirectiveLoc
2013/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002014bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002015 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002016 return TokError("unexpected token in '.loc' directive");
2017
2018 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002019 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002020 (void) FileNumber;
2021 // FIXME: Validate file.
2022
Sean Callanan79ed1a82010-01-19 20:22:31 +00002023 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002024 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2025 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002026 return TokError("unexpected token in '.loc' directive");
2027
Sean Callanan18b83232010-01-19 21:44:56 +00002028 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002029 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002030 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002031
Daniel Dunbareceec052010-07-12 17:45:27 +00002032 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2033 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002034 return TokError("unexpected token in '.loc' directive");
2035
Sean Callanan18b83232010-01-19 21:44:56 +00002036 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002037 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002038 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002039
2040 // FIXME: Do something with the .loc.
2041 }
2042 }
2043
Daniel Dunbareceec052010-07-12 17:45:27 +00002044 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002045 return TokError("unexpected token in '.file' directive");
2046
2047 return false;
2048}
2049