blob: b9139fe5af6dc62439b0a3e52de6ab31e80c1b4b [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Chris Lattneraaec2052010-01-19 19:46:13 +000032
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000033namespace {
34
35/// \brief Generic implementations of directive handling, etc. which is shared
36/// (or the default, at least) for all assembler parser.
37class GenericAsmParser : public MCAsmParserExtension {
38public:
39 GenericAsmParser() {}
40
41 virtual void Initialize(MCAsmParser &Parser) {
42 // Call the base implementation.
43 this->MCAsmParserExtension::Initialize(Parser);
44
45 // Debugging directives.
46 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
47 &GenericAsmParser::ParseDirectiveFile));
48 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
49 &GenericAsmParser::ParseDirectiveLine));
50 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
51 &GenericAsmParser::ParseDirectiveLoc));
52 }
53
54 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
55 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
56 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
57};
58
Daniel Dunbare4749702010-07-12 18:12:02 +000059/// \brief Implementation of directive handling which is shared across all
60/// Darwin targets.
61class DarwinAsmParser : public MCAsmParserExtension {
62public:
63 DarwinAsmParser() {}
64
65 virtual void Initialize(MCAsmParser &Parser) {
66 // Call the base implementation.
67 this->MCAsmParserExtension::Initialize(Parser);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000068
Daniel Dunbar492b7a22010-07-12 19:22:53 +000069 Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler(
70 &DarwinAsmParser::ParseDirectiveDesc));
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000071 Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler(
72 &DarwinAsmParser::ParseDirectiveLsym));
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000073 Parser.AddDirectiveHandler(this, ".subsections_via_symbols",
74 MCAsmParser::DirectiveHandler(
75 &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols));
76 Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler(
77 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
78 Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler(
79 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
80 Parser.AddDirectiveHandler(this, ".secure_log_unique",
81 MCAsmParser::DirectiveHandler(
82 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
83 Parser.AddDirectiveHandler(this, ".secure_log_reset",
84 MCAsmParser::DirectiveHandler(
85 &DarwinAsmParser::ParseDirectiveSecureLogReset));
Daniel Dunbare4749702010-07-12 18:12:02 +000086 }
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000087
Daniel Dunbar492b7a22010-07-12 19:22:53 +000088 bool ParseDirectiveDesc(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000089 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000090 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000091 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000092 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
93 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
Daniel Dunbare4749702010-07-12 18:12:02 +000094};
95
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000096}
97
Chris Lattneraaec2052010-01-19 19:46:13 +000098enum { DEFAULT_ADDRSPACE = 0 };
99
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000100AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
101 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000102 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000103 GenericParser(new GenericAsmParser), PlatformParser(0),
104 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000105 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000106
107 // Initialize the generic parser.
108 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000109
110 // Initialize the platform / file format parser.
111 //
112 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
113 // created.
114 if (_MAI.hasSubsectionsViaSymbols()) {
115 PlatformParser = new DarwinAsmParser;
116 PlatformParser->Initialize(*this);
117 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000118}
119
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000120AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000121 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000122 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000123}
124
Daniel Dunbar53131982010-07-12 17:27:45 +0000125void AsmParser::setTargetParser(TargetAsmParser &P) {
126 assert(!TargetParser && "Target parser is already initialized!");
127 TargetParser = &P;
128 TargetParser->Initialize(*this);
129}
130
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000131void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000132 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000133}
134
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000135bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000136 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000137 return true;
138}
139
Sean Callananbf2013e2010-01-20 23:19:55 +0000140void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
141 const char *Type) const {
142 SrcMgr.PrintMessage(Loc, Msg, Type);
143}
Sean Callananfd0b0282010-01-21 00:19:58 +0000144
145bool AsmParser::EnterIncludeFile(const std::string &Filename) {
146 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
147 if (NewBuf == -1)
148 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000149
Sean Callananfd0b0282010-01-21 00:19:58 +0000150 CurBuffer = NewBuf;
151
152 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
153
154 return false;
155}
156
157const AsmToken &AsmParser::Lex() {
158 const AsmToken *tok = &Lexer.Lex();
159
160 if (tok->is(AsmToken::Eof)) {
161 // If this is the end of an included file, pop the parent file off the
162 // include stack.
163 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
164 if (ParentIncludeLoc != SMLoc()) {
165 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
166 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
167 ParentIncludeLoc.getPointer());
168 tok = &Lexer.Lex();
169 }
170 }
171
172 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000173 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000174
Sean Callananfd0b0282010-01-21 00:19:58 +0000175 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000176}
177
Chris Lattner79180e22010-04-05 23:15:42 +0000178bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000179 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000180 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000181 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000182 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000183 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000184 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
185 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000186
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000187 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000188 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000189
Chris Lattnerb717fb02009-07-02 21:53:43 +0000190 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000191
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000192 AsmCond StartingCondState = TheCondState;
193
Chris Lattnerb717fb02009-07-02 21:53:43 +0000194 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000195 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000196 if (!ParseStatement()) continue;
197
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000198 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000199 HadError = true;
200 EatToEndOfStatement();
201 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000202
203 if (TheCondState.TheCond != StartingCondState.TheCond ||
204 TheCondState.Ignore != StartingCondState.Ignore)
205 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000206
Chris Lattner79180e22010-04-05 23:15:42 +0000207 // Finalize the output stream if there are no errors and if the client wants
208 // us to.
209 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000210 Out.Finish();
211
Chris Lattnerb717fb02009-07-02 21:53:43 +0000212 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000213}
214
Chris Lattner2cf5f142009-06-22 01:29:09 +0000215/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
216void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 while (Lexer.isNot(AsmToken::EndOfStatement) &&
218 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000219 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000220
221 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000222 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000223 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000224}
225
Chris Lattnerc4193832009-06-22 05:51:26 +0000226
Chris Lattner74ec1a32009-06-22 06:32:03 +0000227/// ParseParenExpr - Parse a paren expression and return it.
228/// NOTE: This assumes the leading '(' has already been consumed.
229///
230/// parenexpr ::= expr)
231///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000232bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000233 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000234 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000235 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000236 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000237 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000238 return false;
239}
Chris Lattnerc4193832009-06-22 05:51:26 +0000240
Daniel Dunbar959fd882009-08-26 22:13:22 +0000241MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000242 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000243 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000244}
245
Chris Lattner74ec1a32009-06-22 06:32:03 +0000246/// ParsePrimaryExpr - Parse a primary expression and return it.
247/// primaryexpr ::= (parenexpr
248/// primaryexpr ::= symbol
249/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000250/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000251/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000252bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000253 switch (Lexer.getKind()) {
254 default:
255 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000256 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000257 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000258 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000260 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000262 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000263 case AsmToken::Identifier: {
264 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000265 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
266 MCSymbol *Sym = CreateSymbol(Split.first);
267
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000268 // Mark the symbol as used in an expression.
269 Sym->setUsedInExpr(true);
270
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000271 // Lookup the symbol variant if used.
272 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
273 if (Split.first.size() != getTok().getIdentifier().size())
274 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
275
Chris Lattnerb4307b32010-01-15 19:28:38 +0000276 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000277 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000278
279 // If this is an absolute variable reference, substitute it now to preserve
280 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000281 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000282 if (Variant)
283 return Error(EndLoc, "unexpected modified on variable reference");
284
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000285 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000286 return false;
287 }
288
289 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000290 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000291 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000292 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000293 case AsmToken::Integer: {
294 SMLoc Loc = getTok().getLoc();
295 int64_t IntVal = getTok().getIntVal();
296 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000297 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000298 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000299 // Look for 'b' or 'f' following an Integer as a directional label
300 if (Lexer.getKind() == AsmToken::Identifier) {
301 StringRef IDVal = getTok().getString();
302 if (IDVal == "f" || IDVal == "b"){
303 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
304 IDVal == "f" ? 1 : 0);
305 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
306 getContext());
307 if(IDVal == "b" && Sym->isUndefined())
308 return Error(Loc, "invalid reference to undefined symbol");
309 EndLoc = Lexer.getLoc();
310 Lex(); // Eat identifier.
311 }
312 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000313 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000314 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000315 case AsmToken::Dot: {
316 // This is a '.' reference, which references the current PC. Emit a
317 // temporary label to the streamer and refer to it.
318 MCSymbol *Sym = Ctx.CreateTempSymbol();
319 Out.EmitLabel(Sym);
320 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
321 EndLoc = Lexer.getLoc();
322 Lex(); // Eat identifier.
323 return false;
324 }
325
Daniel Dunbar3f872332009-07-28 16:08:33 +0000326 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000327 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000328 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::Minus:
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::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000334 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000335 case AsmToken::Plus:
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::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000340 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000341 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000342 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000343 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000345 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000346 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000347 }
348}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000349
Chris Lattnerb4307b32010-01-15 19:28:38 +0000350bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000351 SMLoc EndLoc;
352 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000353}
354
Chris Lattner74ec1a32009-06-22 06:32:03 +0000355/// ParseExpression - Parse an expression and return it.
356///
357/// expr ::= expr +,- expr -> lowest.
358/// expr ::= expr |,^,&,! expr -> middle.
359/// expr ::= expr *,/,%,<<,>> expr -> highest.
360/// expr ::= primaryexpr
361///
Chris Lattner54482b42010-01-15 19:39:23 +0000362bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000363 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000364 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000365 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
366 return true;
367
368 // Try to constant fold it up front, if possible.
369 int64_t Value;
370 if (Res->EvaluateAsAbsolute(Value))
371 Res = MCConstantExpr::Create(Value, getContext());
372
373 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000374}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000375
Chris Lattnerb4307b32010-01-15 19:28:38 +0000376bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000377 Res = 0;
378 return ParseParenExpr(Res, EndLoc) ||
379 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000380}
381
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000383 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000384
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000385 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000386 if (ParseExpression(Expr))
387 return true;
388
Daniel Dunbare00b0112009-10-16 01:57:52 +0000389 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000390 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391
392 return false;
393}
394
Daniel Dunbar3f872332009-07-28 16:08:33 +0000395static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000396 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000397 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000398 default:
399 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400
401 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000402 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000403 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000404 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000405 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000407 return 1;
408
409 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000410 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000411 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000412 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000413 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000414 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000415 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000416 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000417 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000418 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::ExclaimEqual:
420 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000421 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000422 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000423 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000424 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000425 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000426 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000427 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000428 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000429 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000430 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000431 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000432 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000433 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000434 return 2;
435
436 // Intermediate Precedence: |, &, ^
437 //
438 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000439 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000440 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000441 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000442 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000443 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000444 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000445 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000446 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000447 return 3;
448
449 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000450 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000451 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000452 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000454 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000455 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000456 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000457 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000458 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000459 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000460 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000461 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000463 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000464 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000465 }
466}
467
468
469/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
470/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000471bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
472 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000473 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000474 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000475 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000476
477 // If the next token is lower precedence than we are allowed to eat, return
478 // successfully with what we ate already.
479 if (TokPrec < Precedence)
480 return false;
481
Sean Callanan79ed1a82010-01-19 20:22:31 +0000482 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000483
484 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000485 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000486 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000487
488 // If BinOp binds less tightly with RHS than the operator after RHS, let
489 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000490 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000491 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000492 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000493 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000494 }
495
Daniel Dunbar475839e2009-06-29 20:37:27 +0000496 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000497 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000498 }
499}
500
Chris Lattnerc4193832009-06-22 05:51:26 +0000501
502
503
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000504/// ParseStatement:
505/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000506/// ::= Label* Directive ...Operands... EndOfStatement
507/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000508bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000509 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000510 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000511 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000512 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000513 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000514
515 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000516 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000517 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000518 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000519 int64_t LocalLabelVal = -1;
520 // GUESS allow an integer followed by a ':' as a directional local label
521 if (Lexer.is(AsmToken::Integer)) {
522 LocalLabelVal = getTok().getIntVal();
523 if (LocalLabelVal < 0) {
524 if (!TheCondState.Ignore)
525 return TokError("unexpected token at start of statement");
526 IDVal = "";
527 }
528 else {
529 IDVal = getTok().getString();
530 Lex(); // Consume the integer token to be used as an identifier token.
531 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000532 if (!TheCondState.Ignore)
533 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000534 }
535 }
536 }
537 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000538 if (!TheCondState.Ignore)
539 return TokError("unexpected token at start of statement");
540 IDVal = "";
541 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000542
Chris Lattner7834fac2010-04-17 18:14:27 +0000543 // Handle conditional assembly here before checking for skipping. We
544 // have to do this so that .endif isn't skipped in a ".if 0" block for
545 // example.
546 if (IDVal == ".if")
547 return ParseDirectiveIf(IDLoc);
548 if (IDVal == ".elseif")
549 return ParseDirectiveElseIf(IDLoc);
550 if (IDVal == ".else")
551 return ParseDirectiveElse(IDLoc);
552 if (IDVal == ".endif")
553 return ParseDirectiveEndIf(IDLoc);
554
555 // If we are in a ".if 0" block, ignore this statement.
556 if (TheCondState.Ignore) {
557 EatToEndOfStatement();
558 return false;
559 }
560
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000561 // FIXME: Recurse on local labels?
562
563 // See what kind of statement we have.
564 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000565 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000566 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000567 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000568
569 // Diagnose attempt to use a variable as a label.
570 //
571 // FIXME: Diagnostics. Note the location of the definition as a label.
572 // FIXME: This doesn't diagnose assignment to a symbol which has been
573 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000574 MCSymbol *Sym;
575 if (LocalLabelVal == -1)
576 Sym = CreateSymbol(IDVal);
577 else
578 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000579 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000580 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000581
Daniel Dunbar959fd882009-08-26 22:13:22 +0000582 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000583 Out.EmitLabel(Sym);
584
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000585 // Consume any end of statement token, if present, to avoid spurious
586 // AddBlankLine calls().
587 if (Lexer.is(AsmToken::EndOfStatement)) {
588 Lex();
589 if (Lexer.is(AsmToken::Eof))
590 return false;
591 }
592
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000593 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000594 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000595
Daniel Dunbar3f872332009-07-28 16:08:33 +0000596 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000597 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000598 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000599
Daniel Dunbare2ace502009-08-31 08:09:09 +0000600 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000601
602 default: // Normal instruction or directive.
603 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000604 }
605
606 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000607 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000608 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000610 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000612 // FIXME: This changes behavior based on the -static flag to the
613 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000614 return ParseDirectiveSectionSwitch("__TEXT", "__text",
615 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000617 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000619 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000621 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
622 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000624 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000625 MCSectionMachO::S_4BYTE_LITERALS,
626 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000627 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000628 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000629 MCSectionMachO::S_8BYTE_LITERALS,
630 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000631 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000632 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000633 MCSectionMachO::S_16BYTE_LITERALS,
634 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000636 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000637 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000638 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000640 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000642 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
643
644 // FIXME: The assembler manual claims that this has the self modify code
645 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000646 if (IDVal == ".symbol_stub")
647 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
648 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000649 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
650 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000651 0, 16);
652 // FIXME: PowerPC only?
653 if (IDVal == ".picsymbol_stub")
654 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
655 MCSectionMachO::S_SYMBOL_STUBS |
656 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
657 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000659 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000661 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
662
663 // FIXME: The section names of these two are misspelled in the assembler
664 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000666 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
667 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
668 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000670 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
671 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
672 4);
673
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000674 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000675 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000676 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000677 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000678 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
679 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000681 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000682 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
683 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000685 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000686
687
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000689 return ParseDirectiveSectionSwitch("__OBJC", "__class",
690 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000692 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
693 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000695 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
696 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000698 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
699 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000701 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
702 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000704 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
705 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000707 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
708 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000710 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
711 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000713 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
714 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
715 MCSectionMachO::S_LITERAL_POINTERS,
716 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000718 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
719 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
720 MCSectionMachO::S_LITERAL_POINTERS,
721 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000722 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000723 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
724 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000726 return ParseDirectiveSectionSwitch("__OBJC", "__category",
727 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000729 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
730 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000732 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
733 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000735 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
736 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".objc_class_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_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000741 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
742 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000744 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
745 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000747 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
748 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000749
Eric Christopherc6177a42010-05-17 22:53:55 +0000750 if (IDVal == ".tdata")
751 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
752 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
753 if (IDVal == ".tlv")
754 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
755 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
756 if (IDVal == ".thread_init_func")
757 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
758 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
759
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000760 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000761 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000762 return ParseDirectiveSet();
763
Daniel Dunbara0d14262009-06-24 23:30:00 +0000764 // Data directives
765
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000767 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000768 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000769 return ParseDirectiveAscii(true);
770
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000771 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000772 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000773 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000774 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000775 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000776 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000777 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000778 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000779
780 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000781 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000782 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000783 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000784 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000785 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000786 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000787 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000788 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000789 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000790 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000791 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000792 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000793 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000794 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000795 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000796 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
797
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000798 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000799 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000800
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000801 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000802 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000803 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000804 return ParseDirectiveSpace();
805
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000806 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000807
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000808 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000809 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000810 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000811 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000812 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000813 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000814 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000815 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000816 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000817 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000818 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000819 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000820 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000821 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000822 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000823 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000824 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000825 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000826 if (IDVal == ".type")
827 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000828 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000829 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000830 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000831 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000832 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000833 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000834 if (IDVal == ".weak_def_can_be_hidden")
835 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000836
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000838 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000840 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000841 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000842 return ParseDirectiveDarwinZerofill();
Eric Christopher482eba02010-05-14 01:50:28 +0000843 if (IDVal == ".tbss")
844 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000845
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000846 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000847 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000848 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000849 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000850
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000851 // Look up the handler in the handler table.
852 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
853 DirectiveMap.lookup(IDVal);
854 if (Handler.first)
855 return (Handler.first->*Handler.second)(IDVal, IDLoc);
856
Kevin Enderby9c656452009-09-10 20:51:44 +0000857 // Target hook for parsing target specific directives.
858 if (!getTargetParser().ParseDirective(ID))
859 return false;
860
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000861 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000862 EatToEndOfStatement();
863 return false;
864 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000865
Chris Lattnera7f13542010-05-19 23:34:33 +0000866 // Canonicalize the opcode to lower case.
867 SmallString<128> Opcode;
868 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
869 Opcode.push_back(tolower(IDVal[i]));
870
Chris Lattner98986712010-01-14 22:21:20 +0000871 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000872 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000873 ParsedOperands);
874 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
875 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000876
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000877 // If parsing succeeded, match the instruction.
878 if (!HadError) {
879 MCInst Inst;
880 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
881 // Emit the instruction on success.
882 Out.EmitInstruction(Inst);
883 } else {
884 // Otherwise emit a diagnostic about the match failure and set the error
885 // flag.
886 //
887 // FIXME: We should give nicer diagnostics about the exact failure.
888 Error(IDLoc, "unrecognized instruction");
889 HadError = true;
890 }
891 }
Chris Lattner98986712010-01-14 22:21:20 +0000892
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000893 // If there was no error, consume the end-of-statement token. Otherwise this
894 // will be done by our caller.
895 if (!HadError)
896 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000897
898 // Free any parsed operands.
899 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
900 delete ParsedOperands[i];
901
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000902 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000903}
Chris Lattner9a023f72009-06-24 04:43:34 +0000904
Daniel Dunbare2ace502009-08-31 08:09:09 +0000905bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000906 // FIXME: Use better location, we should use proper tokens.
907 SMLoc EqualLoc = Lexer.getLoc();
908
Daniel Dunbar821e3332009-08-31 08:09:28 +0000909 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000910 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000911 return true;
912
Daniel Dunbar3f872332009-07-28 16:08:33 +0000913 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000914 return TokError("unexpected token in assignment");
915
916 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000917 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000918
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000919 // Validate that the LHS is allowed to be a variable (either it has not been
920 // used as a symbol, or it is an absolute symbol).
921 MCSymbol *Sym = getContext().LookupSymbol(Name);
922 if (Sym) {
923 // Diagnose assignment to a label.
924 //
925 // FIXME: Diagnostics. Note the location of the definition as a label.
926 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000927 if (Sym->isUndefined() && !Sym->isUsedInExpr())
928 ; // Allow redefinitions of undefined symbols only used in directives.
929 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000930 return Error(EqualLoc, "redefinition of '" + Name + "'");
931 else if (!Sym->isVariable())
932 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000933 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000934 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
935 Name + "'");
936 } else
937 Sym = CreateSymbol(Name);
938
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000939 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000940
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000941 Sym->setUsedInExpr(true);
942
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000943 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000944 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000945
946 return false;
947}
948
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000949/// ParseIdentifier:
950/// ::= identifier
951/// ::= string
952bool AsmParser::ParseIdentifier(StringRef &Res) {
953 if (Lexer.isNot(AsmToken::Identifier) &&
954 Lexer.isNot(AsmToken::String))
955 return true;
956
Sean Callanan18b83232010-01-19 21:44:56 +0000957 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000958
Sean Callanan79ed1a82010-01-19 20:22:31 +0000959 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000960
961 return false;
962}
963
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000964/// ParseDirectiveSet:
965/// ::= .set identifier ',' expression
966bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000967 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000968
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000969 if (ParseIdentifier(Name))
970 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000971
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000972 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000973 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000974 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000975
Daniel Dunbare2ace502009-08-31 08:09:09 +0000976 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000977}
978
Chris Lattner9a023f72009-06-24 04:43:34 +0000979/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000980/// ::= .section identifier (',' identifier)*
981/// FIXME: This should actually parse out the segment, section, attributes and
982/// sizeof_stub fields.
983bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000984 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000985
Daniel Dunbarace63122009-08-11 03:42:33 +0000986 StringRef SectionName;
987 if (ParseIdentifier(SectionName))
988 return Error(Loc, "expected identifier after '.section' directive");
989
990 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000991 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +0000992 return TokError("unexpected token in '.section' directive");
993
Chris Lattnerff4bc462009-08-10 01:39:42 +0000994 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000995 SectionSpec += ",";
996
997 // Add all the tokens until the end of the line, ParseSectionSpecifier will
998 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000999 StringRef EOL = Lexer.LexUntilEndOfStatement();
1000 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +00001001
Sean Callanan79ed1a82010-01-19 20:22:31 +00001002 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001003 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +00001004 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001005 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +00001006
Chris Lattnerff4bc462009-08-10 01:39:42 +00001007
1008 StringRef Segment, Section;
1009 unsigned TAA, StubSize;
1010 std::string ErrorStr =
1011 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1012 TAA, StubSize);
1013
1014 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001015 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +00001016
Chris Lattner56594f92009-07-31 17:47:16 +00001017 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001018 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001019 getStreamer().SwitchSection(Ctx.getMachOSection(
1020 Segment, Section, TAA, StubSize,
1021 isText ? SectionKind::getText()
1022 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001023 return false;
1024}
1025
Chris Lattnere15c2d72009-08-10 18:05:55 +00001026/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +00001027bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
1028 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +00001029 unsigned TAA, unsigned Align,
1030 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001031 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001032 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001033 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +00001034
Chris Lattner56594f92009-07-31 17:47:16 +00001035 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001036 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001037 getStreamer().SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001038 isText ? SectionKind::getText()
1039 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001040
1041 // Set the implicit alignment, if any.
1042 //
1043 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1044 // alignment on the section (e.g., if one manually inserts bytes into the
1045 // section, then just issueing the section switch directive will not realign
1046 // the section. However, this is arguably more reasonable behavior, and there
1047 // is no good reason for someone to intentionally emit incorrectly sized
1048 // values into the implicitly aligned sections.
1049 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001050 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001051
Chris Lattner529fb542009-06-24 05:13:15 +00001052 return false;
1053}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001055bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001056 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001057
1058 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001059 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001060 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1061 if (Str[i] != '\\') {
1062 Data += Str[i];
1063 continue;
1064 }
1065
1066 // Recognize escaped characters. Note that this escape semantics currently
1067 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1068 ++i;
1069 if (i == e)
1070 return TokError("unexpected backslash at end of string");
1071
1072 // Recognize octal sequences.
1073 if ((unsigned) (Str[i] - '0') <= 7) {
1074 // Consume up to three octal characters.
1075 unsigned Value = Str[i] - '0';
1076
1077 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1078 ++i;
1079 Value = Value * 8 + (Str[i] - '0');
1080
1081 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1082 ++i;
1083 Value = Value * 8 + (Str[i] - '0');
1084 }
1085 }
1086
1087 if (Value > 255)
1088 return TokError("invalid octal escape sequence (out of range)");
1089
1090 Data += (unsigned char) Value;
1091 continue;
1092 }
1093
1094 // Otherwise recognize individual escapes.
1095 switch (Str[i]) {
1096 default:
1097 // Just reject invalid escape sequences for now.
1098 return TokError("invalid escape sequence (unrecognized character)");
1099
1100 case 'b': Data += '\b'; break;
1101 case 'f': Data += '\f'; break;
1102 case 'n': Data += '\n'; break;
1103 case 'r': Data += '\r'; break;
1104 case 't': Data += '\t'; break;
1105 case '"': Data += '"'; break;
1106 case '\\': Data += '\\'; break;
1107 }
1108 }
1109
1110 return false;
1111}
1112
Daniel Dunbara0d14262009-06-24 23:30:00 +00001113/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001114/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001116 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001117 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001118 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001120
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001121 std::string Data;
1122 if (ParseEscapedString(Data))
1123 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001124
1125 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001127 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1128
Sean Callanan79ed1a82010-01-19 20:22:31 +00001129 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001130
1131 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001132 break;
1133
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001134 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001135 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001136 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001137 }
1138 }
1139
Sean Callanan79ed1a82010-01-19 20:22:31 +00001140 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001141 return false;
1142}
1143
1144/// ParseDirectiveValue
1145/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1146bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001147 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001149 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001150 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001151 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001152 return true;
1153
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001154 // Special case constant expressions to match code generator.
1155 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001156 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001157 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001158 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001159
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001160 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001161 break;
1162
1163 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001164 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001165 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001166 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001167 }
1168 }
1169
Sean Callanan79ed1a82010-01-19 20:22:31 +00001170 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001171 return false;
1172}
1173
1174/// ParseDirectiveSpace
1175/// ::= .space expression [ , expression ]
1176bool AsmParser::ParseDirectiveSpace() {
1177 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001178 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001179 return true;
1180
1181 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001182 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1183 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001184 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001185 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001186
Daniel Dunbar475839e2009-06-29 20:37:27 +00001187 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001188 return true;
1189
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001190 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001191 return TokError("unexpected token in '.space' directive");
1192 }
1193
Sean Callanan79ed1a82010-01-19 20:22:31 +00001194 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001195
1196 if (NumBytes <= 0)
1197 return TokError("invalid number of bytes in '.space' directive");
1198
1199 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001200 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001201
1202 return false;
1203}
1204
1205/// ParseDirectiveFill
1206/// ::= .fill expression , expression , expression
1207bool AsmParser::ParseDirectiveFill() {
1208 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001209 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001210 return true;
1211
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001212 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001213 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001214 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001215
1216 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001217 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001218 return true;
1219
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001220 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001221 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001222 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001223
1224 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001225 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001226 return true;
1227
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001228 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001229 return TokError("unexpected token in '.fill' directive");
1230
Sean Callanan79ed1a82010-01-19 20:22:31 +00001231 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001232
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001233 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1234 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001235
1236 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001237 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001238
1239 return false;
1240}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001241
1242/// ParseDirectiveOrg
1243/// ::= .org expression [ , expression ]
1244bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001245 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001246 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001247 return true;
1248
1249 // Parse optional fill expression.
1250 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001251 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1252 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001253 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001254 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001255
Daniel Dunbar475839e2009-06-29 20:37:27 +00001256 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001257 return true;
1258
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001259 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001260 return TokError("unexpected token in '.org' directive");
1261 }
1262
Sean Callanan79ed1a82010-01-19 20:22:31 +00001263 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001264
1265 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1266 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001267 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001268
1269 return false;
1270}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001271
1272/// ParseDirectiveAlign
1273/// ::= {.align, ...} expression [ , expression [ , expression ]]
1274bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001275 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001276 int64_t Alignment;
1277 if (ParseAbsoluteExpression(Alignment))
1278 return true;
1279
1280 SMLoc MaxBytesLoc;
1281 bool HasFillExpr = false;
1282 int64_t FillExpr = 0;
1283 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001284 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1285 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001286 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001287 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001288
1289 // The fill expression can be omitted while specifying a maximum number of
1290 // alignment bytes, e.g:
1291 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001292 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001293 HasFillExpr = true;
1294 if (ParseAbsoluteExpression(FillExpr))
1295 return true;
1296 }
1297
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001298 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1299 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001300 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001301 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001302
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001303 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001304 if (ParseAbsoluteExpression(MaxBytesToFill))
1305 return true;
1306
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001307 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001308 return TokError("unexpected token in directive");
1309 }
1310 }
1311
Sean Callanan79ed1a82010-01-19 20:22:31 +00001312 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001313
Daniel Dunbar648ac512010-05-17 21:54:30 +00001314 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001315 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001316
1317 // Compute alignment in bytes.
1318 if (IsPow2) {
1319 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001320 if (Alignment >= 32) {
1321 Error(AlignmentLoc, "invalid alignment value");
1322 Alignment = 31;
1323 }
1324
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001325 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001326 }
1327
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001328 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001329 if (MaxBytesLoc.isValid()) {
1330 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001331 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1332 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001333 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001334 }
1335
1336 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001337 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1338 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001339 MaxBytesToFill = 0;
1340 }
1341 }
1342
Daniel Dunbar648ac512010-05-17 21:54:30 +00001343 // Check whether we should use optimal code alignment for this .align
1344 // directive.
1345 //
1346 // FIXME: This should be using a target hook.
1347 bool UseCodeAlign = false;
1348 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001349 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001350 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1351 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1352 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001354 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001355 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001356 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001357 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001358
1359 return false;
1360}
1361
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001362/// ParseDirectiveSymbolAttribute
1363/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001364bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001365 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001366 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001367 StringRef Name;
1368
1369 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001370 return TokError("expected identifier in directive");
1371
Daniel Dunbar959fd882009-08-26 22:13:22 +00001372 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001373
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001374 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001375
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001376 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001377 break;
1378
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001379 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001380 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001381 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001382 }
1383 }
1384
Sean Callanan79ed1a82010-01-19 20:22:31 +00001385 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001386 return false;
1387}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001388
Matt Fleming924c5e52010-05-21 11:36:59 +00001389/// ParseDirectiveELFType
1390/// ::= .type identifier , @attribute
1391bool AsmParser::ParseDirectiveELFType() {
1392 StringRef Name;
1393 if (ParseIdentifier(Name))
1394 return TokError("expected identifier in directive");
1395
1396 // Handle the identifier as the key symbol.
1397 MCSymbol *Sym = CreateSymbol(Name);
1398
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001400 return TokError("unexpected token in '.type' directive");
1401 Lex();
1402
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001403 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001404 return TokError("expected '@' before type");
1405 Lex();
1406
1407 StringRef Type;
1408 SMLoc TypeLoc;
1409
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001410 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001411 if (ParseIdentifier(Type))
1412 return TokError("expected symbol type in directive");
1413
1414 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1415 .Case("function", MCSA_ELF_TypeFunction)
1416 .Case("object", MCSA_ELF_TypeObject)
1417 .Case("tls_object", MCSA_ELF_TypeTLS)
1418 .Case("common", MCSA_ELF_TypeCommon)
1419 .Case("notype", MCSA_ELF_TypeNoType)
1420 .Default(MCSA_Invalid);
1421
1422 if (Attr == MCSA_Invalid)
1423 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1424
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001425 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001426 return TokError("unexpected token in '.type' directive");
1427
1428 Lex();
1429
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001430 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001431
1432 return false;
1433}
1434
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001435/// ParseDirectiveDesc
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001436/// ::= .desc identifier , expression
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001437bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001438 StringRef Name;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001439 if (getParser().ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001440 return TokError("expected identifier in directive");
1441
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001442 // Handle the identifier as the key symbol.
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001443 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001444
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001445 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001446 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001447 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001448
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001449 int64_t DescValue;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001450 if (getParser().ParseAbsoluteExpression(DescValue))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001451 return true;
1452
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001453 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001454 return TokError("unexpected token in '.desc' directive");
1455
Sean Callanan79ed1a82010-01-19 20:22:31 +00001456 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001457
1458 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001459 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001460
1461 return false;
1462}
1463
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001464/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001465/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1466bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001467 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001468 StringRef Name;
1469 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001470 return TokError("expected identifier in directive");
1471
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001472 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001473 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001474
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001475 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001476 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001477 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001478
1479 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001480 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001481 if (ParseAbsoluteExpression(Size))
1482 return true;
1483
1484 int64_t Pow2Alignment = 0;
1485 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001486 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001487 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001488 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001489 if (ParseAbsoluteExpression(Pow2Alignment))
1490 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001491
1492 // If this target takes alignments in bytes (not log) validate and convert.
1493 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1494 if (!isPowerOf2_64(Pow2Alignment))
1495 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1496 Pow2Alignment = Log2_64(Pow2Alignment);
1497 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001498 }
1499
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001500 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001501 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001502
Sean Callanan79ed1a82010-01-19 20:22:31 +00001503 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001504
Chris Lattner1fc3d752009-07-09 17:25:12 +00001505 // NOTE: a size of zero for a .comm should create a undefined symbol
1506 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001507 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001508 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1509 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001510
Eric Christopherc260a3e2010-05-14 01:38:54 +00001511 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001512 // may internally end up wanting an alignment in bytes.
1513 // FIXME: Diagnose overflow.
1514 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001515 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1516 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001517
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001518 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001519 return Error(IDLoc, "invalid symbol redefinition");
1520
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001521 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001522 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001523 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001524 getStreamer().EmitZerofill(Ctx.getMachOSection(
1525 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1526 0, SectionKind::getBSS()),
1527 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001528 return false;
1529 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001530
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001531 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001532 return false;
1533}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001534
1535/// ParseDirectiveDarwinZerofill
1536/// ::= .zerofill segname , sectname [, identifier , size_expression [
1537/// , align_expression ]]
1538bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001539 StringRef Segment;
1540 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001541 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001542
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001543 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001544 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001545 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001546
1547 StringRef Section;
1548 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001549 return TokError("expected section name after comma in '.zerofill' "
1550 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001551
Chris Lattner9be3fee2009-07-10 22:20:30 +00001552 // If this is the end of the line all that was wanted was to create the
1553 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001554 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001555 // Create the zerofill section but no symbol
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001556 getStreamer().EmitZerofill(Ctx.getMachOSection(Segment, Section,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001557 MCSectionMachO::S_ZEROFILL, 0,
1558 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001559 return false;
1560 }
1561
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001562 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001563 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001564 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001565
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001566 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001567 StringRef IDStr;
1568 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001569 return TokError("expected identifier in directive");
1570
1571 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001572 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001573
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001574 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001575 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001576 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001577
1578 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001579 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001580 if (ParseAbsoluteExpression(Size))
1581 return true;
1582
1583 int64_t Pow2Alignment = 0;
1584 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001585 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001586 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001587 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001588 if (ParseAbsoluteExpression(Pow2Alignment))
1589 return true;
1590 }
1591
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001592 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001593 return TokError("unexpected token in '.zerofill' directive");
1594
Sean Callanan79ed1a82010-01-19 20:22:31 +00001595 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001596
1597 if (Size < 0)
1598 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1599 "than zero");
1600
Eric Christopherc260a3e2010-05-14 01:38:54 +00001601 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001602 // may internally end up wanting an alignment in bytes.
1603 // FIXME: Diagnose overflow.
1604 if (Pow2Alignment < 0)
1605 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1606 "can't be less than zero");
1607
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001608 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001609 return Error(IDLoc, "invalid symbol redefinition");
1610
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001611 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001612 //
1613 // FIXME: Arch specific.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001614 getStreamer().EmitZerofill(Ctx.getMachOSection(Segment, Section,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001615 MCSectionMachO::S_ZEROFILL, 0,
1616 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001617 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001618
1619 return false;
1620}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001621
Eric Christopher482eba02010-05-14 01:50:28 +00001622/// ParseDirectiveDarwinTBSS
1623/// ::= .tbss identifier, size, align
1624bool AsmParser::ParseDirectiveDarwinTBSS() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001625 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001626 StringRef Name;
1627 if (ParseIdentifier(Name))
1628 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001629
Eric Christopher482eba02010-05-14 01:50:28 +00001630 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001631 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001632
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001633 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001634 return TokError("unexpected token in directive");
1635 Lex();
1636
1637 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001638 SMLoc SizeLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001639 if (ParseAbsoluteExpression(Size))
1640 return true;
1641
1642 int64_t Pow2Alignment = 0;
1643 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001644 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001645 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001646 Pow2AlignmentLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001647 if (ParseAbsoluteExpression(Pow2Alignment))
1648 return true;
1649 }
1650
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001651 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001652 return TokError("unexpected token in '.tbss' directive");
1653
1654 Lex();
1655
1656 if (Size < 0)
1657 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1658 "zero");
1659
1660 // FIXME: Diagnose overflow.
1661 if (Pow2Alignment < 0)
1662 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1663 "than zero");
1664
1665 if (!Sym->isUndefined())
1666 return Error(IDLoc, "invalid symbol redefinition");
1667
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001668 getStreamer().EmitTBSSSymbol(Ctx.getMachOSection(
1669 "__DATA", "__thread_bss",
1670 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1671 0, SectionKind::getThreadBSS()),
1672 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001673
1674 return false;
1675}
1676
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001677/// ParseDirectiveSubsectionsViaSymbols
Kevin Enderbya5c78322009-07-13 21:03:15 +00001678/// ::= .subsections_via_symbols
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001679bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001680 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001681 return TokError("unexpected token in '.subsections_via_symbols' directive");
1682
Sean Callanan79ed1a82010-01-19 20:22:31 +00001683 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001684
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001685 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001686
1687 return false;
1688}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001689
1690/// ParseDirectiveAbort
1691/// ::= .abort [ "abort_string" ]
1692bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001693 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001694 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001695
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001696 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001697 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1698 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001699 return TokError("expected string in '.abort' directive");
1700
Sean Callanan18b83232010-01-19 21:44:56 +00001701 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001702
Sean Callanan79ed1a82010-01-19 20:22:31 +00001703 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001704 }
1705
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001706 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001707 return TokError("unexpected token in '.abort' directive");
1708
Sean Callanan79ed1a82010-01-19 20:22:31 +00001709 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001710
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001711 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001712 if (Str.empty())
1713 Error(Loc, ".abort detected. Assembly stopping.");
1714 else
1715 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001716
1717 return false;
1718}
Kevin Enderby71148242009-07-14 21:35:03 +00001719
1720/// ParseDirectiveLsym
1721/// ::= .lsym identifier , expression
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001722bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001723 StringRef Name;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001724 if (getParser().ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001725 return TokError("expected identifier in directive");
1726
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001727 // Handle the identifier as the key symbol.
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001728 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001729
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001730 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001731 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001732 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001733
Daniel Dunbar821e3332009-08-31 08:09:28 +00001734 const MCExpr *Value;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001735 if (getParser().ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001736 return true;
1737
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001738 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001739 return TokError("unexpected token in '.lsym' directive");
1740
Sean Callanan79ed1a82010-01-19 20:22:31 +00001741 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001742
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001743 // We don't currently support this directive.
1744 //
1745 // FIXME: Diagnostic location!
1746 (void) Sym;
1747 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001748}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001749
1750/// ParseDirectiveInclude
1751/// ::= .include "filename"
1752bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001753 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001754 return TokError("expected string in '.include' directive");
1755
Sean Callanan18b83232010-01-19 21:44:56 +00001756 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001757 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001758 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001759
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001760 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001761 return TokError("unexpected token in '.include' directive");
1762
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001763 // Strip the quotes.
1764 Filename = Filename.substr(1, Filename.size()-2);
1765
1766 // Attempt to switch the lexer to the included file before consuming the end
1767 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001768 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001769 PrintMessage(IncludeLoc,
1770 "Could not find include file '" + Filename + "'",
1771 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001772 return true;
1773 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001774
1775 return false;
1776}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001777
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001778/// ParseDirectiveDumpOrLoad
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001779/// ::= ( .dump | .load ) "filename"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001780bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1781 SMLoc IDLoc) {
1782 bool IsDump = Directive == ".dump";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001783 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001784 return TokError("expected string in '.dump' or '.load' directive");
1785
Sean Callanan79ed1a82010-01-19 20:22:31 +00001786 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001787
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001788 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001789 return TokError("unexpected token in '.dump' or '.load' directive");
1790
Sean Callanan79ed1a82010-01-19 20:22:31 +00001791 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001792
Kevin Enderby5026ae42009-07-20 20:25:37 +00001793 // FIXME: If/when .dump and .load are implemented they will be done in the
1794 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001795 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001796 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001797 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001798 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001799
1800 return false;
1801}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001802
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001803/// ParseDirectiveSecureLogUnique
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001804/// ::= .secure_log_unique "log message"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001805bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001806 std::string LogMessage;
1807
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001808 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001809 LogMessage = "";
1810 else{
1811 LogMessage = getTok().getString();
1812 Lex();
1813 }
1814
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001815 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001816 return TokError("unexpected token in '.secure_log_unique' directive");
1817
1818 if (getContext().getSecureLogUsed() != false)
1819 return Error(IDLoc, ".secure_log_unique specified multiple times");
1820
1821 char *SecureLogFile = getContext().getSecureLogFile();
1822 if (SecureLogFile == NULL)
1823 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1824 "environment variable unset.");
1825
1826 raw_ostream *OS = getContext().getSecureLog();
1827 if (OS == NULL) {
1828 std::string Err;
1829 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1830 if (!Err.empty()) {
1831 delete OS;
1832 return Error(IDLoc, Twine("can't open secure log file: ") +
1833 SecureLogFile + " (" + Err + ")");
1834 }
1835 getContext().setSecureLog(OS);
1836 }
1837
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001838 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
1839 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
1840 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001841 << LogMessage + "\n";
1842
1843 getContext().setSecureLogUsed(true);
1844
1845 return false;
1846}
1847
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001848/// ParseDirectiveSecureLogReset
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001849/// ::= .secure_log_reset
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001850bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001851 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001852 return TokError("unexpected token in '.secure_log_reset' directive");
1853
1854 Lex();
1855
1856 getContext().setSecureLogUsed(false);
1857
1858 return false;
1859}
1860
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001861/// ParseDirectiveIf
1862/// ::= .if expression
1863bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001864 TheCondStack.push_back(TheCondState);
1865 TheCondState.TheCond = AsmCond::IfCond;
1866 if(TheCondState.Ignore) {
1867 EatToEndOfStatement();
1868 }
1869 else {
1870 int64_t ExprValue;
1871 if (ParseAbsoluteExpression(ExprValue))
1872 return true;
1873
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001874 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001875 return TokError("unexpected token in '.if' directive");
1876
Sean Callanan79ed1a82010-01-19 20:22:31 +00001877 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001878
1879 TheCondState.CondMet = ExprValue;
1880 TheCondState.Ignore = !TheCondState.CondMet;
1881 }
1882
1883 return false;
1884}
1885
1886/// ParseDirectiveElseIf
1887/// ::= .elseif expression
1888bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1889 if (TheCondState.TheCond != AsmCond::IfCond &&
1890 TheCondState.TheCond != AsmCond::ElseIfCond)
1891 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1892 " an .elseif");
1893 TheCondState.TheCond = AsmCond::ElseIfCond;
1894
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001895 bool LastIgnoreState = false;
1896 if (!TheCondStack.empty())
1897 LastIgnoreState = TheCondStack.back().Ignore;
1898 if (LastIgnoreState || TheCondState.CondMet) {
1899 TheCondState.Ignore = true;
1900 EatToEndOfStatement();
1901 }
1902 else {
1903 int64_t ExprValue;
1904 if (ParseAbsoluteExpression(ExprValue))
1905 return true;
1906
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001907 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001908 return TokError("unexpected token in '.elseif' directive");
1909
Sean Callanan79ed1a82010-01-19 20:22:31 +00001910 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001911 TheCondState.CondMet = ExprValue;
1912 TheCondState.Ignore = !TheCondState.CondMet;
1913 }
1914
1915 return false;
1916}
1917
1918/// ParseDirectiveElse
1919/// ::= .else
1920bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001921 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001922 return TokError("unexpected token in '.else' directive");
1923
Sean Callanan79ed1a82010-01-19 20:22:31 +00001924 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001925
1926 if (TheCondState.TheCond != AsmCond::IfCond &&
1927 TheCondState.TheCond != AsmCond::ElseIfCond)
1928 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1929 ".elseif");
1930 TheCondState.TheCond = AsmCond::ElseCond;
1931 bool LastIgnoreState = false;
1932 if (!TheCondStack.empty())
1933 LastIgnoreState = TheCondStack.back().Ignore;
1934 if (LastIgnoreState || TheCondState.CondMet)
1935 TheCondState.Ignore = true;
1936 else
1937 TheCondState.Ignore = false;
1938
1939 return false;
1940}
1941
1942/// ParseDirectiveEndIf
1943/// ::= .endif
1944bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001945 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001946 return TokError("unexpected token in '.endif' directive");
1947
Sean Callanan79ed1a82010-01-19 20:22:31 +00001948 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001949
1950 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1951 TheCondStack.empty())
1952 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1953 ".else");
1954 if (!TheCondStack.empty()) {
1955 TheCondState = TheCondStack.back();
1956 TheCondStack.pop_back();
1957 }
1958
1959 return false;
1960}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001961
1962/// ParseDirectiveFile
1963/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001964bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001965 // FIXME: I'm not sure what this is.
1966 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001967 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001968 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001969 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001970
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001971 if (FileNumber < 1)
1972 return TokError("file number less than one");
1973 }
1974
Daniel Dunbareceec052010-07-12 17:45:27 +00001975 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001976 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001977
Chris Lattnerd32e8032010-01-25 19:02:58 +00001978 StringRef Filename = getTok().getString();
1979 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001980 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001981
Daniel Dunbareceec052010-07-12 17:45:27 +00001982 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001983 return TokError("unexpected token in '.file' directive");
1984
Chris Lattnerd32e8032010-01-25 19:02:58 +00001985 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001986 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001987 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001988 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1989
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001990 return false;
1991}
1992
1993/// ParseDirectiveLine
1994/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001995bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001996 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1997 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001998 return TokError("unexpected token in '.line' directive");
1999
Sean Callanan18b83232010-01-19 21:44:56 +00002000 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002001 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002002 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002003
2004 // FIXME: Do something with the .line.
2005 }
2006
Daniel Dunbareceec052010-07-12 17:45:27 +00002007 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002008 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002009
2010 return false;
2011}
2012
2013
2014/// ParseDirectiveLoc
2015/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002016bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002017 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002018 return TokError("unexpected token in '.loc' directive");
2019
2020 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002021 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002022 (void) FileNumber;
2023 // FIXME: Validate file.
2024
Sean Callanan79ed1a82010-01-19 20:22:31 +00002025 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002026 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2027 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002028 return TokError("unexpected token in '.loc' directive");
2029
Sean Callanan18b83232010-01-19 21:44:56 +00002030 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002031 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002032 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002033
Daniel Dunbareceec052010-07-12 17:45:27 +00002034 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2035 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002036 return TokError("unexpected token in '.loc' directive");
2037
Sean Callanan18b83232010-01-19 21:44:56 +00002038 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002039 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002040 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002041
2042 // FIXME: Do something with the .loc.
2043 }
2044 }
2045
Daniel Dunbareceec052010-07-12 17:45:27 +00002046 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002047 return TokError("unexpected token in '.file' directive");
2048
2049 return false;
2050}
2051