blob: c4c3ce1450648c34678cd1e1c1e9dfe3bbf13ec8 [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);
68 }
69};
70
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000071}
72
Chris Lattneraaec2052010-01-19 19:46:13 +000073enum { DEFAULT_ADDRSPACE = 0 };
74
Daniel Dunbar9186fa62010-07-01 20:41:56 +000075AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
76 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000077 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +000078 GenericParser(new GenericAsmParser), PlatformParser(0),
79 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000080 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000081
82 // Initialize the generic parser.
83 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +000084
85 // Initialize the platform / file format parser.
86 //
87 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
88 // created.
89 if (_MAI.hasSubsectionsViaSymbols()) {
90 PlatformParser = new DarwinAsmParser;
91 PlatformParser->Initialize(*this);
92 }
Chris Lattnerebb89b42009-09-27 21:16:52 +000093}
94
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000095AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +000096 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000097 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000098}
99
Daniel Dunbar53131982010-07-12 17:27:45 +0000100void AsmParser::setTargetParser(TargetAsmParser &P) {
101 assert(!TargetParser && "Target parser is already initialized!");
102 TargetParser = &P;
103 TargetParser->Initialize(*this);
104}
105
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000106void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000107 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000108}
109
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000110bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000111 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000112 return true;
113}
114
Sean Callananbf2013e2010-01-20 23:19:55 +0000115void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
116 const char *Type) const {
117 SrcMgr.PrintMessage(Loc, Msg, Type);
118}
Sean Callananfd0b0282010-01-21 00:19:58 +0000119
120bool AsmParser::EnterIncludeFile(const std::string &Filename) {
121 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
122 if (NewBuf == -1)
123 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000124
Sean Callananfd0b0282010-01-21 00:19:58 +0000125 CurBuffer = NewBuf;
126
127 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
128
129 return false;
130}
131
132const AsmToken &AsmParser::Lex() {
133 const AsmToken *tok = &Lexer.Lex();
134
135 if (tok->is(AsmToken::Eof)) {
136 // If this is the end of an included file, pop the parent file off the
137 // include stack.
138 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
139 if (ParentIncludeLoc != SMLoc()) {
140 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
141 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
142 ParentIncludeLoc.getPointer());
143 tok = &Lexer.Lex();
144 }
145 }
146
147 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000148 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000149
Sean Callananfd0b0282010-01-21 00:19:58 +0000150 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000151}
152
Chris Lattner79180e22010-04-05 23:15:42 +0000153bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000154 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000155 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000156 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000157 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000158 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000159 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
160 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000161
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000162 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000163 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000164
Chris Lattnerb717fb02009-07-02 21:53:43 +0000165 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000166
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000167 AsmCond StartingCondState = TheCondState;
168
Chris Lattnerb717fb02009-07-02 21:53:43 +0000169 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000170 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000171 if (!ParseStatement()) continue;
172
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000173 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000174 HadError = true;
175 EatToEndOfStatement();
176 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000177
178 if (TheCondState.TheCond != StartingCondState.TheCond ||
179 TheCondState.Ignore != StartingCondState.Ignore)
180 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000181
Chris Lattner79180e22010-04-05 23:15:42 +0000182 // Finalize the output stream if there are no errors and if the client wants
183 // us to.
184 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000185 Out.Finish();
186
Chris Lattnerb717fb02009-07-02 21:53:43 +0000187 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000188}
189
Chris Lattner2cf5f142009-06-22 01:29:09 +0000190/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
191void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000192 while (Lexer.isNot(AsmToken::EndOfStatement) &&
193 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000194 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000195
196 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000197 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000198 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000199}
200
Chris Lattnerc4193832009-06-22 05:51:26 +0000201
Chris Lattner74ec1a32009-06-22 06:32:03 +0000202/// ParseParenExpr - Parse a paren expression and return it.
203/// NOTE: This assumes the leading '(' has already been consumed.
204///
205/// parenexpr ::= expr)
206///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000207bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000208 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000209 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000210 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000211 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000212 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000213 return false;
214}
Chris Lattnerc4193832009-06-22 05:51:26 +0000215
Daniel Dunbar959fd882009-08-26 22:13:22 +0000216MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Chris Lattner9b97a732010-03-30 18:10:53 +0000217 // FIXME: Inline into callers.
Chris Lattner00685bb2010-03-10 01:29:27 +0000218 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000219}
220
Chris Lattner74ec1a32009-06-22 06:32:03 +0000221/// ParsePrimaryExpr - Parse a primary expression and return it.
222/// primaryexpr ::= (parenexpr
223/// primaryexpr ::= symbol
224/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000225/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000226/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000227bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000228 switch (Lexer.getKind()) {
229 default:
230 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000231 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000232 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000233 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000234 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000235 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000236 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000237 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000238 case AsmToken::Identifier: {
239 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000240 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
241 MCSymbol *Sym = CreateSymbol(Split.first);
242
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000243 // Mark the symbol as used in an expression.
244 Sym->setUsedInExpr(true);
245
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000246 // Lookup the symbol variant if used.
247 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
248 if (Split.first.size() != getTok().getIdentifier().size())
249 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
250
Chris Lattnerb4307b32010-01-15 19:28:38 +0000251 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000252 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000253
254 // If this is an absolute variable reference, substitute it now to preserve
255 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000256 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000257 if (Variant)
258 return Error(EndLoc, "unexpected modified on variable reference");
259
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000260 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000261 return false;
262 }
263
264 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000265 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000266 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000267 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000268 case AsmToken::Integer: {
269 SMLoc Loc = getTok().getLoc();
270 int64_t IntVal = getTok().getIntVal();
271 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000272 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000273 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000274 // Look for 'b' or 'f' following an Integer as a directional label
275 if (Lexer.getKind() == AsmToken::Identifier) {
276 StringRef IDVal = getTok().getString();
277 if (IDVal == "f" || IDVal == "b"){
278 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
279 IDVal == "f" ? 1 : 0);
280 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
281 getContext());
282 if(IDVal == "b" && Sym->isUndefined())
283 return Error(Loc, "invalid reference to undefined symbol");
284 EndLoc = Lexer.getLoc();
285 Lex(); // Eat identifier.
286 }
287 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000288 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000289 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000290 case AsmToken::Dot: {
291 // This is a '.' reference, which references the current PC. Emit a
292 // temporary label to the streamer and refer to it.
293 MCSymbol *Sym = Ctx.CreateTempSymbol();
294 Out.EmitLabel(Sym);
295 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
296 EndLoc = Lexer.getLoc();
297 Lex(); // Eat identifier.
298 return false;
299 }
300
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000302 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000303 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000304 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000305 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000306 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000308 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000310 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000311 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000312 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000313 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000314 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000316 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000317 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000318 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000319 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000320 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000321 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000322 }
323}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000324
Chris Lattnerb4307b32010-01-15 19:28:38 +0000325bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000326 SMLoc EndLoc;
327 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000328}
329
Chris Lattner74ec1a32009-06-22 06:32:03 +0000330/// ParseExpression - Parse an expression and return it.
331///
332/// expr ::= expr +,- expr -> lowest.
333/// expr ::= expr |,^,&,! expr -> middle.
334/// expr ::= expr *,/,%,<<,>> expr -> highest.
335/// expr ::= primaryexpr
336///
Chris Lattner54482b42010-01-15 19:39:23 +0000337bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000338 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000339 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000340 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
341 return true;
342
343 // Try to constant fold it up front, if possible.
344 int64_t Value;
345 if (Res->EvaluateAsAbsolute(Value))
346 Res = MCConstantExpr::Create(Value, getContext());
347
348 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000349}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000350
Chris Lattnerb4307b32010-01-15 19:28:38 +0000351bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000352 Res = 0;
353 return ParseParenExpr(Res, EndLoc) ||
354 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000355}
356
Daniel Dunbar475839e2009-06-29 20:37:27 +0000357bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000358 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000360 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000361 if (ParseExpression(Expr))
362 return true;
363
Daniel Dunbare00b0112009-10-16 01:57:52 +0000364 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000365 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000366
367 return false;
368}
369
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000371 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000372 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000373 default:
374 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000375
376 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000377 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000378 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000379 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000380 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return 1;
383
384 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000386 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000387 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000388 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000389 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000390 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000391 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000392 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000393 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000394 case AsmToken::ExclaimEqual:
395 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000396 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000397 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000398 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000399 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000401 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000402 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000404 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000405 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000406 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000407 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000408 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000409 return 2;
410
411 // Intermediate Precedence: |, &, ^
412 //
413 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000414 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000416 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000417 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000418 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000419 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000420 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000421 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000422 return 3;
423
424 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000425 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000426 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000427 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000429 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000430 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000431 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000432 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000433 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000434 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000435 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000436 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000437 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000438 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000439 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000440 }
441}
442
443
444/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
445/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000446bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
447 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000448 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000449 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000450 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000451
452 // If the next token is lower precedence than we are allowed to eat, return
453 // successfully with what we ate already.
454 if (TokPrec < Precedence)
455 return false;
456
Sean Callanan79ed1a82010-01-19 20:22:31 +0000457 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000458
459 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000460 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000461 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000462
463 // If BinOp binds less tightly with RHS than the operator after RHS, let
464 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000465 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000466 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000467 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000468 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000469 }
470
Daniel Dunbar475839e2009-06-29 20:37:27 +0000471 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000472 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000473 }
474}
475
Chris Lattnerc4193832009-06-22 05:51:26 +0000476
477
478
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000479/// ParseStatement:
480/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000481/// ::= Label* Directive ...Operands... EndOfStatement
482/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000483bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000484 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000485 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000486 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000487 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000488 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000489
490 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000491 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000492 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000493 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000494 int64_t LocalLabelVal = -1;
495 // GUESS allow an integer followed by a ':' as a directional local label
496 if (Lexer.is(AsmToken::Integer)) {
497 LocalLabelVal = getTok().getIntVal();
498 if (LocalLabelVal < 0) {
499 if (!TheCondState.Ignore)
500 return TokError("unexpected token at start of statement");
501 IDVal = "";
502 }
503 else {
504 IDVal = getTok().getString();
505 Lex(); // Consume the integer token to be used as an identifier token.
506 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000507 if (!TheCondState.Ignore)
508 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000509 }
510 }
511 }
512 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000513 if (!TheCondState.Ignore)
514 return TokError("unexpected token at start of statement");
515 IDVal = "";
516 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000517
Chris Lattner7834fac2010-04-17 18:14:27 +0000518 // Handle conditional assembly here before checking for skipping. We
519 // have to do this so that .endif isn't skipped in a ".if 0" block for
520 // example.
521 if (IDVal == ".if")
522 return ParseDirectiveIf(IDLoc);
523 if (IDVal == ".elseif")
524 return ParseDirectiveElseIf(IDLoc);
525 if (IDVal == ".else")
526 return ParseDirectiveElse(IDLoc);
527 if (IDVal == ".endif")
528 return ParseDirectiveEndIf(IDLoc);
529
530 // If we are in a ".if 0" block, ignore this statement.
531 if (TheCondState.Ignore) {
532 EatToEndOfStatement();
533 return false;
534 }
535
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000536 // FIXME: Recurse on local labels?
537
538 // See what kind of statement we have.
539 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000540 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000541 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000542 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000543
544 // Diagnose attempt to use a variable as a label.
545 //
546 // FIXME: Diagnostics. Note the location of the definition as a label.
547 // FIXME: This doesn't diagnose assignment to a symbol which has been
548 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000549 MCSymbol *Sym;
550 if (LocalLabelVal == -1)
551 Sym = CreateSymbol(IDVal);
552 else
553 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000554 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000555 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000556
Daniel Dunbar959fd882009-08-26 22:13:22 +0000557 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000558 Out.EmitLabel(Sym);
559
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000560 // Consume any end of statement token, if present, to avoid spurious
561 // AddBlankLine calls().
562 if (Lexer.is(AsmToken::EndOfStatement)) {
563 Lex();
564 if (Lexer.is(AsmToken::Eof))
565 return false;
566 }
567
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000568 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000569 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000570
Daniel Dunbar3f872332009-07-28 16:08:33 +0000571 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000572 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000573 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000574
Daniel Dunbare2ace502009-08-31 08:09:09 +0000575 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000576
577 default: // Normal instruction or directive.
578 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000579 }
580
581 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000582 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000583 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000584 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000585 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000586 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000587 // FIXME: This changes behavior based on the -static flag to the
588 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000589 return ParseDirectiveSectionSwitch("__TEXT", "__text",
590 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000594 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000596 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
597 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000598 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000599 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000600 MCSectionMachO::S_4BYTE_LITERALS,
601 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000602 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000603 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000604 MCSectionMachO::S_8BYTE_LITERALS,
605 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000607 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000608 MCSectionMachO::S_16BYTE_LITERALS,
609 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000610 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000611 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000613 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000615 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000617 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
618
619 // FIXME: The assembler manual claims that this has the self modify code
620 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000621 if (IDVal == ".symbol_stub")
622 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
623 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000624 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
625 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000626 0, 16);
627 // FIXME: PowerPC only?
628 if (IDVal == ".picsymbol_stub")
629 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
630 MCSectionMachO::S_SYMBOL_STUBS |
631 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
632 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000633 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000634 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000635 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000636 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
637
638 // FIXME: The section names of these two are misspelled in the assembler
639 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
642 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
643 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000645 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
646 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
647 4);
648
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000649 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000650 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000652 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000653 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
654 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000656 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000657 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
658 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000660 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000661
662
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000664 return ParseDirectiveSectionSwitch("__OBJC", "__class",
665 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000666 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000667 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
668 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000669 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000670 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
671 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000673 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
674 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000676 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
677 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000679 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
680 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000681 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000682 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
683 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000685 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
686 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000688 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
689 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
690 MCSectionMachO::S_LITERAL_POINTERS,
691 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000693 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
694 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
695 MCSectionMachO::S_LITERAL_POINTERS,
696 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000698 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
699 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000701 return ParseDirectiveSectionSwitch("__OBJC", "__category",
702 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000704 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
705 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000707 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
708 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000710 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
711 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000713 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
714 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000716 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
717 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000718 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000719 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
720 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000722 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
723 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000724
Eric Christopherc6177a42010-05-17 22:53:55 +0000725 if (IDVal == ".tdata")
726 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
727 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
728 if (IDVal == ".tlv")
729 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
730 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
731 if (IDVal == ".thread_init_func")
732 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
733 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
734
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000735 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000737 return ParseDirectiveSet();
738
Daniel Dunbara0d14262009-06-24 23:30:00 +0000739 // Data directives
740
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000741 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000742 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000744 return ParseDirectiveAscii(true);
745
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000747 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000748 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000749 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000750 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000751 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000752 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000753 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000754
755 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000756 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000757 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000758 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000759 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000760 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000761 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000762 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000763 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000765 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000766 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000767 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000768 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000769 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000770 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000771 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
772
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000773 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000774 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000775
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000776 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000777 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000778 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000779 return ParseDirectiveSpace();
780
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000781 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000782
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000783 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000784 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000785 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000786 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000787 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000788 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000789 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000790 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000791 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000792 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000793 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000794 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000795 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000796 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000797 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000798 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000799 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000800 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000801 if (IDVal == ".type")
802 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000803 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000804 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000805 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000806 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000807 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000808 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000809 if (IDVal == ".weak_def_can_be_hidden")
810 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000811
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000812 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000813 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000814 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000815 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000816 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000817 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000818 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000819 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000820 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000821 return ParseDirectiveDarwinLsym();
Eric Christopher482eba02010-05-14 01:50:28 +0000822 if (IDVal == ".tbss")
823 return ParseDirectiveDarwinTBSS();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000824
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000825 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000826 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000827 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000828 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000829 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000830 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000832 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000833 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000834 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbyf187ac52010-06-28 21:45:58 +0000835 if (IDVal == ".secure_log_unique")
836 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
837 if (IDVal == ".secure_log_reset")
838 return ParseDirectiveDarwinSecureLogReset(IDLoc);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000839
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000840 // Look up the handler in the handler table.
841 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
842 DirectiveMap.lookup(IDVal);
843 if (Handler.first)
844 return (Handler.first->*Handler.second)(IDVal, IDLoc);
845
Kevin Enderby9c656452009-09-10 20:51:44 +0000846 // Target hook for parsing target specific directives.
847 if (!getTargetParser().ParseDirective(ID))
848 return false;
849
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000850 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000851 EatToEndOfStatement();
852 return false;
853 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000854
Chris Lattnera7f13542010-05-19 23:34:33 +0000855 // Canonicalize the opcode to lower case.
856 SmallString<128> Opcode;
857 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
858 Opcode.push_back(tolower(IDVal[i]));
859
Chris Lattner98986712010-01-14 22:21:20 +0000860 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000861 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000862 ParsedOperands);
863 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
864 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000865
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000866 // If parsing succeeded, match the instruction.
867 if (!HadError) {
868 MCInst Inst;
869 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
870 // Emit the instruction on success.
871 Out.EmitInstruction(Inst);
872 } else {
873 // Otherwise emit a diagnostic about the match failure and set the error
874 // flag.
875 //
876 // FIXME: We should give nicer diagnostics about the exact failure.
877 Error(IDLoc, "unrecognized instruction");
878 HadError = true;
879 }
880 }
Chris Lattner98986712010-01-14 22:21:20 +0000881
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000882 // If there was no error, consume the end-of-statement token. Otherwise this
883 // will be done by our caller.
884 if (!HadError)
885 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000886
887 // Free any parsed operands.
888 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
889 delete ParsedOperands[i];
890
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000891 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000892}
Chris Lattner9a023f72009-06-24 04:43:34 +0000893
Daniel Dunbare2ace502009-08-31 08:09:09 +0000894bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000895 // FIXME: Use better location, we should use proper tokens.
896 SMLoc EqualLoc = Lexer.getLoc();
897
Daniel Dunbar821e3332009-08-31 08:09:28 +0000898 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000899 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000900 return true;
901
Daniel Dunbar3f872332009-07-28 16:08:33 +0000902 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000903 return TokError("unexpected token in assignment");
904
905 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000906 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000907
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000908 // Validate that the LHS is allowed to be a variable (either it has not been
909 // used as a symbol, or it is an absolute symbol).
910 MCSymbol *Sym = getContext().LookupSymbol(Name);
911 if (Sym) {
912 // Diagnose assignment to a label.
913 //
914 // FIXME: Diagnostics. Note the location of the definition as a label.
915 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000916 if (Sym->isUndefined() && !Sym->isUsedInExpr())
917 ; // Allow redefinitions of undefined symbols only used in directives.
918 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000919 return Error(EqualLoc, "redefinition of '" + Name + "'");
920 else if (!Sym->isVariable())
921 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000922 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000923 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
924 Name + "'");
925 } else
926 Sym = CreateSymbol(Name);
927
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000928 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000929
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000930 Sym->setUsedInExpr(true);
931
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000932 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000933 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000934
935 return false;
936}
937
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000938/// ParseIdentifier:
939/// ::= identifier
940/// ::= string
941bool AsmParser::ParseIdentifier(StringRef &Res) {
942 if (Lexer.isNot(AsmToken::Identifier) &&
943 Lexer.isNot(AsmToken::String))
944 return true;
945
Sean Callanan18b83232010-01-19 21:44:56 +0000946 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000947
Sean Callanan79ed1a82010-01-19 20:22:31 +0000948 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000949
950 return false;
951}
952
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000953/// ParseDirectiveSet:
954/// ::= .set identifier ',' expression
955bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000956 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000957
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000958 if (ParseIdentifier(Name))
959 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000960
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000961 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000962 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000963 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000964
Daniel Dunbare2ace502009-08-31 08:09:09 +0000965 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000966}
967
Chris Lattner9a023f72009-06-24 04:43:34 +0000968/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000969/// ::= .section identifier (',' identifier)*
970/// FIXME: This should actually parse out the segment, section, attributes and
971/// sizeof_stub fields.
972bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000973 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000974
Daniel Dunbarace63122009-08-11 03:42:33 +0000975 StringRef SectionName;
976 if (ParseIdentifier(SectionName))
977 return Error(Loc, "expected identifier after '.section' directive");
978
979 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000980 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +0000981 return TokError("unexpected token in '.section' directive");
982
Chris Lattnerff4bc462009-08-10 01:39:42 +0000983 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000984 SectionSpec += ",";
985
986 // Add all the tokens until the end of the line, ParseSectionSpecifier will
987 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000988 StringRef EOL = Lexer.LexUntilEndOfStatement();
989 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000990
Sean Callanan79ed1a82010-01-19 20:22:31 +0000991 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000992 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000993 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000994 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000995
Chris Lattnerff4bc462009-08-10 01:39:42 +0000996
997 StringRef Segment, Section;
998 unsigned TAA, StubSize;
999 std::string ErrorStr =
1000 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1001 TAA, StubSize);
1002
1003 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001004 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +00001005
Chris Lattner56594f92009-07-31 17:47:16 +00001006 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001007 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001008 getStreamer().SwitchSection(Ctx.getMachOSection(
1009 Segment, Section, TAA, StubSize,
1010 isText ? SectionKind::getText()
1011 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001012 return false;
1013}
1014
Chris Lattnere15c2d72009-08-10 18:05:55 +00001015/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +00001016bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
1017 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +00001018 unsigned TAA, unsigned Align,
1019 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001020 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001021 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001022 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +00001023
Chris Lattner56594f92009-07-31 17:47:16 +00001024 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001025 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001026 getStreamer().SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001027 isText ? SectionKind::getText()
1028 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001029
1030 // Set the implicit alignment, if any.
1031 //
1032 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1033 // alignment on the section (e.g., if one manually inserts bytes into the
1034 // section, then just issueing the section switch directive will not realign
1035 // the section. However, this is arguably more reasonable behavior, and there
1036 // is no good reason for someone to intentionally emit incorrectly sized
1037 // values into the implicitly aligned sections.
1038 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001039 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001040
Chris Lattner529fb542009-06-24 05:13:15 +00001041 return false;
1042}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001044bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001045 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001046
1047 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001048 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001049 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1050 if (Str[i] != '\\') {
1051 Data += Str[i];
1052 continue;
1053 }
1054
1055 // Recognize escaped characters. Note that this escape semantics currently
1056 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1057 ++i;
1058 if (i == e)
1059 return TokError("unexpected backslash at end of string");
1060
1061 // Recognize octal sequences.
1062 if ((unsigned) (Str[i] - '0') <= 7) {
1063 // Consume up to three octal characters.
1064 unsigned Value = Str[i] - '0';
1065
1066 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1067 ++i;
1068 Value = Value * 8 + (Str[i] - '0');
1069
1070 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1071 ++i;
1072 Value = Value * 8 + (Str[i] - '0');
1073 }
1074 }
1075
1076 if (Value > 255)
1077 return TokError("invalid octal escape sequence (out of range)");
1078
1079 Data += (unsigned char) Value;
1080 continue;
1081 }
1082
1083 // Otherwise recognize individual escapes.
1084 switch (Str[i]) {
1085 default:
1086 // Just reject invalid escape sequences for now.
1087 return TokError("invalid escape sequence (unrecognized character)");
1088
1089 case 'b': Data += '\b'; break;
1090 case 'f': Data += '\f'; break;
1091 case 'n': Data += '\n'; break;
1092 case 'r': Data += '\r'; break;
1093 case 't': Data += '\t'; break;
1094 case '"': Data += '"'; break;
1095 case '\\': Data += '\\'; break;
1096 }
1097 }
1098
1099 return false;
1100}
1101
Daniel Dunbara0d14262009-06-24 23:30:00 +00001102/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001103/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001104bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001105 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001106 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001107 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001108 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001109
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001110 std::string Data;
1111 if (ParseEscapedString(Data))
1112 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001113
1114 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001115 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001116 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1117
Sean Callanan79ed1a82010-01-19 20:22:31 +00001118 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001119
1120 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001121 break;
1122
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001123 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001125 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001126 }
1127 }
1128
Sean Callanan79ed1a82010-01-19 20:22:31 +00001129 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130 return false;
1131}
1132
1133/// ParseDirectiveValue
1134/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1135bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001136 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001137 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001138 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001139 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001140 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001141 return true;
1142
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001143 // Special case constant expressions to match code generator.
1144 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001145 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001146 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001147 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001148
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001149 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001150 break;
1151
1152 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001153 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001154 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001155 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001156 }
1157 }
1158
Sean Callanan79ed1a82010-01-19 20:22:31 +00001159 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001160 return false;
1161}
1162
1163/// ParseDirectiveSpace
1164/// ::= .space expression [ , expression ]
1165bool AsmParser::ParseDirectiveSpace() {
1166 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001167 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001168 return true;
1169
1170 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001171 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1172 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001173 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001174 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001175
Daniel Dunbar475839e2009-06-29 20:37:27 +00001176 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001177 return true;
1178
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001179 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001180 return TokError("unexpected token in '.space' directive");
1181 }
1182
Sean Callanan79ed1a82010-01-19 20:22:31 +00001183 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001184
1185 if (NumBytes <= 0)
1186 return TokError("invalid number of bytes in '.space' directive");
1187
1188 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001189 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001190
1191 return false;
1192}
1193
1194/// ParseDirectiveFill
1195/// ::= .fill expression , expression , expression
1196bool AsmParser::ParseDirectiveFill() {
1197 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001198 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001199 return true;
1200
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001201 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001202 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001203 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001204
1205 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001206 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001207 return true;
1208
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001209 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001210 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001211 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001212
1213 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001214 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001215 return true;
1216
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001217 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001218 return TokError("unexpected token in '.fill' directive");
1219
Sean Callanan79ed1a82010-01-19 20:22:31 +00001220 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001221
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001222 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1223 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001224
1225 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001226 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001227
1228 return false;
1229}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001230
1231/// ParseDirectiveOrg
1232/// ::= .org expression [ , expression ]
1233bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001234 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001235 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001236 return true;
1237
1238 // Parse optional fill expression.
1239 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001240 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1241 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001242 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001243 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001244
Daniel Dunbar475839e2009-06-29 20:37:27 +00001245 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001246 return true;
1247
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001248 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001249 return TokError("unexpected token in '.org' directive");
1250 }
1251
Sean Callanan79ed1a82010-01-19 20:22:31 +00001252 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001253
1254 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1255 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001256 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001257
1258 return false;
1259}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001260
1261/// ParseDirectiveAlign
1262/// ::= {.align, ...} expression [ , expression [ , expression ]]
1263bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001264 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001265 int64_t Alignment;
1266 if (ParseAbsoluteExpression(Alignment))
1267 return true;
1268
1269 SMLoc MaxBytesLoc;
1270 bool HasFillExpr = false;
1271 int64_t FillExpr = 0;
1272 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001273 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1274 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001275 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001276 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001277
1278 // The fill expression can be omitted while specifying a maximum number of
1279 // alignment bytes, e.g:
1280 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001281 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001282 HasFillExpr = true;
1283 if (ParseAbsoluteExpression(FillExpr))
1284 return true;
1285 }
1286
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001287 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1288 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001289 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001290 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001291
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001292 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001293 if (ParseAbsoluteExpression(MaxBytesToFill))
1294 return true;
1295
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001296 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001297 return TokError("unexpected token in directive");
1298 }
1299 }
1300
Sean Callanan79ed1a82010-01-19 20:22:31 +00001301 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001302
Daniel Dunbar648ac512010-05-17 21:54:30 +00001303 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001304 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001305
1306 // Compute alignment in bytes.
1307 if (IsPow2) {
1308 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001309 if (Alignment >= 32) {
1310 Error(AlignmentLoc, "invalid alignment value");
1311 Alignment = 31;
1312 }
1313
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001314 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001315 }
1316
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001317 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001318 if (MaxBytesLoc.isValid()) {
1319 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001320 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1321 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001322 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001323 }
1324
1325 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001326 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1327 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001328 MaxBytesToFill = 0;
1329 }
1330 }
1331
Daniel Dunbar648ac512010-05-17 21:54:30 +00001332 // Check whether we should use optimal code alignment for this .align
1333 // directive.
1334 //
1335 // FIXME: This should be using a target hook.
1336 bool UseCodeAlign = false;
1337 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001338 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001339 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1340 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1341 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001342 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001343 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001344 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001345 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001346 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001347
1348 return false;
1349}
1350
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001351/// ParseDirectiveSymbolAttribute
1352/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001353bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001354 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001355 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001356 StringRef Name;
1357
1358 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001359 return TokError("expected identifier in directive");
1360
Daniel Dunbar959fd882009-08-26 22:13:22 +00001361 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001362
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001363 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001364
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001365 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001366 break;
1367
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001368 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001369 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001370 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001371 }
1372 }
1373
Sean Callanan79ed1a82010-01-19 20:22:31 +00001374 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001375 return false;
1376}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001377
Matt Fleming924c5e52010-05-21 11:36:59 +00001378/// ParseDirectiveELFType
1379/// ::= .type identifier , @attribute
1380bool AsmParser::ParseDirectiveELFType() {
1381 StringRef Name;
1382 if (ParseIdentifier(Name))
1383 return TokError("expected identifier in directive");
1384
1385 // Handle the identifier as the key symbol.
1386 MCSymbol *Sym = CreateSymbol(Name);
1387
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001388 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001389 return TokError("unexpected token in '.type' directive");
1390 Lex();
1391
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001392 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001393 return TokError("expected '@' before type");
1394 Lex();
1395
1396 StringRef Type;
1397 SMLoc TypeLoc;
1398
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001400 if (ParseIdentifier(Type))
1401 return TokError("expected symbol type in directive");
1402
1403 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1404 .Case("function", MCSA_ELF_TypeFunction)
1405 .Case("object", MCSA_ELF_TypeObject)
1406 .Case("tls_object", MCSA_ELF_TypeTLS)
1407 .Case("common", MCSA_ELF_TypeCommon)
1408 .Case("notype", MCSA_ELF_TypeNoType)
1409 .Default(MCSA_Invalid);
1410
1411 if (Attr == MCSA_Invalid)
1412 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1413
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001414 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001415 return TokError("unexpected token in '.type' directive");
1416
1417 Lex();
1418
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001419 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001420
1421 return false;
1422}
1423
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001424/// ParseDirectiveDarwinSymbolDesc
1425/// ::= .desc identifier , expression
1426bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001427 StringRef Name;
1428 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001429 return TokError("expected identifier in directive");
1430
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001431 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001432 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001433
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001434 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001435 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001436 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001437
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001438 int64_t DescValue;
1439 if (ParseAbsoluteExpression(DescValue))
1440 return true;
1441
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001442 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001443 return TokError("unexpected token in '.desc' directive");
1444
Sean Callanan79ed1a82010-01-19 20:22:31 +00001445 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001446
1447 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001448 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001449
1450 return false;
1451}
1452
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001453/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001454/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1455bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001456 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001457 StringRef Name;
1458 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001459 return TokError("expected identifier in directive");
1460
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001461 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001462 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001463
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001464 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001465 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001466 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001467
1468 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001469 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001470 if (ParseAbsoluteExpression(Size))
1471 return true;
1472
1473 int64_t Pow2Alignment = 0;
1474 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001475 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001476 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001478 if (ParseAbsoluteExpression(Pow2Alignment))
1479 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001480
1481 // If this target takes alignments in bytes (not log) validate and convert.
1482 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1483 if (!isPowerOf2_64(Pow2Alignment))
1484 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1485 Pow2Alignment = Log2_64(Pow2Alignment);
1486 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001487 }
1488
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001489 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001490 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001491
Sean Callanan79ed1a82010-01-19 20:22:31 +00001492 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001493
Chris Lattner1fc3d752009-07-09 17:25:12 +00001494 // NOTE: a size of zero for a .comm should create a undefined symbol
1495 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001496 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001497 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1498 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001499
Eric Christopherc260a3e2010-05-14 01:38:54 +00001500 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001501 // may internally end up wanting an alignment in bytes.
1502 // FIXME: Diagnose overflow.
1503 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001504 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1505 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001506
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001507 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001508 return Error(IDLoc, "invalid symbol redefinition");
1509
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001510 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001511 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001512 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001513 getStreamer().EmitZerofill(Ctx.getMachOSection(
1514 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1515 0, SectionKind::getBSS()),
1516 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001517 return false;
1518 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001519
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001520 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001521 return false;
1522}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001523
1524/// ParseDirectiveDarwinZerofill
1525/// ::= .zerofill segname , sectname [, identifier , size_expression [
1526/// , align_expression ]]
1527bool AsmParser::ParseDirectiveDarwinZerofill() {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001528 StringRef Segment;
1529 if (ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001530 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001531
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001533 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001534 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001535
1536 StringRef Section;
1537 if (ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001538 return TokError("expected section name after comma in '.zerofill' "
1539 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001540
Chris Lattner9be3fee2009-07-10 22:20:30 +00001541 // If this is the end of the line all that was wanted was to create the
1542 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001543 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001544 // Create the zerofill section but no symbol
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001545 getStreamer().EmitZerofill(Ctx.getMachOSection(Segment, Section,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001546 MCSectionMachO::S_ZEROFILL, 0,
1547 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001548 return false;
1549 }
1550
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001551 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001552 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001553 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001554
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001556 StringRef IDStr;
1557 if (ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001558 return TokError("expected identifier in directive");
1559
1560 // handle the identifier as the key symbol.
Chris Lattner7bb7c552010-05-13 00:10:34 +00001561 MCSymbol *Sym = CreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001562
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001563 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001564 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001565 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001566
1567 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001568 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001569 if (ParseAbsoluteExpression(Size))
1570 return true;
1571
1572 int64_t Pow2Alignment = 0;
1573 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001574 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001575 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001576 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001577 if (ParseAbsoluteExpression(Pow2Alignment))
1578 return true;
1579 }
1580
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001581 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001582 return TokError("unexpected token in '.zerofill' directive");
1583
Sean Callanan79ed1a82010-01-19 20:22:31 +00001584 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001585
1586 if (Size < 0)
1587 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1588 "than zero");
1589
Eric Christopherc260a3e2010-05-14 01:38:54 +00001590 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001591 // may internally end up wanting an alignment in bytes.
1592 // FIXME: Diagnose overflow.
1593 if (Pow2Alignment < 0)
1594 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1595 "can't be less than zero");
1596
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001597 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001598 return Error(IDLoc, "invalid symbol redefinition");
1599
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001600 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001601 //
1602 // FIXME: Arch specific.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001603 getStreamer().EmitZerofill(Ctx.getMachOSection(Segment, Section,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001604 MCSectionMachO::S_ZEROFILL, 0,
1605 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001606 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001607
1608 return false;
1609}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001610
Eric Christopher482eba02010-05-14 01:50:28 +00001611/// ParseDirectiveDarwinTBSS
1612/// ::= .tbss identifier, size, align
1613bool AsmParser::ParseDirectiveDarwinTBSS() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001614 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001615 StringRef Name;
1616 if (ParseIdentifier(Name))
1617 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001618
Eric Christopher482eba02010-05-14 01:50:28 +00001619 // Handle the identifier as the key symbol.
Eric Christopherd04d98d2010-05-17 02:13:02 +00001620 MCSymbol *Sym = CreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001621
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001622 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001623 return TokError("unexpected token in directive");
1624 Lex();
1625
1626 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001627 SMLoc SizeLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001628 if (ParseAbsoluteExpression(Size))
1629 return true;
1630
1631 int64_t Pow2Alignment = 0;
1632 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001633 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001634 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001635 Pow2AlignmentLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001636 if (ParseAbsoluteExpression(Pow2Alignment))
1637 return true;
1638 }
1639
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001640 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001641 return TokError("unexpected token in '.tbss' directive");
1642
1643 Lex();
1644
1645 if (Size < 0)
1646 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1647 "zero");
1648
1649 // FIXME: Diagnose overflow.
1650 if (Pow2Alignment < 0)
1651 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1652 "than zero");
1653
1654 if (!Sym->isUndefined())
1655 return Error(IDLoc, "invalid symbol redefinition");
1656
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001657 getStreamer().EmitTBSSSymbol(Ctx.getMachOSection(
1658 "__DATA", "__thread_bss",
1659 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1660 0, SectionKind::getThreadBSS()),
1661 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001662
1663 return false;
1664}
1665
Kevin Enderbya5c78322009-07-13 21:03:15 +00001666/// ParseDirectiveDarwinSubsectionsViaSymbols
1667/// ::= .subsections_via_symbols
1668bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001669 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001670 return TokError("unexpected token in '.subsections_via_symbols' directive");
1671
Sean Callanan79ed1a82010-01-19 20:22:31 +00001672 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001673
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001674 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001675
1676 return false;
1677}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001678
1679/// ParseDirectiveAbort
1680/// ::= .abort [ "abort_string" ]
1681bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001682 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001683 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001684
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001685 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001686 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1687 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001688 return TokError("expected string in '.abort' directive");
1689
Sean Callanan18b83232010-01-19 21:44:56 +00001690 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001691
Sean Callanan79ed1a82010-01-19 20:22:31 +00001692 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001693 }
1694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001696 return TokError("unexpected token in '.abort' directive");
1697
Sean Callanan79ed1a82010-01-19 20:22:31 +00001698 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001699
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001700 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001701 if (Str.empty())
1702 Error(Loc, ".abort detected. Assembly stopping.");
1703 else
1704 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001705
1706 return false;
1707}
Kevin Enderby71148242009-07-14 21:35:03 +00001708
1709/// ParseDirectiveLsym
1710/// ::= .lsym identifier , expression
1711bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001712 StringRef Name;
1713 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001714 return TokError("expected identifier in directive");
1715
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001716 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001717 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001718
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001719 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001720 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001721 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001722
Daniel Dunbar821e3332009-08-31 08:09:28 +00001723 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001724 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001725 return true;
1726
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001727 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001728 return TokError("unexpected token in '.lsym' directive");
1729
Sean Callanan79ed1a82010-01-19 20:22:31 +00001730 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001731
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001732 // We don't currently support this directive.
1733 //
1734 // FIXME: Diagnostic location!
1735 (void) Sym;
1736 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001737}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001738
1739/// ParseDirectiveInclude
1740/// ::= .include "filename"
1741bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001742 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001743 return TokError("expected string in '.include' directive");
1744
Sean Callanan18b83232010-01-19 21:44:56 +00001745 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001746 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001747 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001748
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001749 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001750 return TokError("unexpected token in '.include' directive");
1751
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001752 // Strip the quotes.
1753 Filename = Filename.substr(1, Filename.size()-2);
1754
1755 // Attempt to switch the lexer to the included file before consuming the end
1756 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001757 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001758 PrintMessage(IncludeLoc,
1759 "Could not find include file '" + Filename + "'",
1760 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001761 return true;
1762 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001763
1764 return false;
1765}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001766
1767/// ParseDirectiveDarwinDumpOrLoad
1768/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001769bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001770 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001771 return TokError("expected string in '.dump' or '.load' directive");
1772
Sean Callanan79ed1a82010-01-19 20:22:31 +00001773 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001774
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001775 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001776 return TokError("unexpected token in '.dump' or '.load' directive");
1777
Sean Callanan79ed1a82010-01-19 20:22:31 +00001778 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001779
Kevin Enderby5026ae42009-07-20 20:25:37 +00001780 // FIXME: If/when .dump and .load are implemented they will be done in the
1781 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001782 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001783 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001784 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001785 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001786
1787 return false;
1788}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001789
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001790/// ParseDirectiveDarwinSecureLogUnique
1791/// ::= .secure_log_unique "log message"
1792bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1793 std::string LogMessage;
1794
1795 if (Lexer.isNot(AsmToken::String))
1796 LogMessage = "";
1797 else{
1798 LogMessage = getTok().getString();
1799 Lex();
1800 }
1801
1802 if (Lexer.isNot(AsmToken::EndOfStatement))
1803 return TokError("unexpected token in '.secure_log_unique' directive");
1804
1805 if (getContext().getSecureLogUsed() != false)
1806 return Error(IDLoc, ".secure_log_unique specified multiple times");
1807
1808 char *SecureLogFile = getContext().getSecureLogFile();
1809 if (SecureLogFile == NULL)
1810 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1811 "environment variable unset.");
1812
1813 raw_ostream *OS = getContext().getSecureLog();
1814 if (OS == NULL) {
1815 std::string Err;
1816 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1817 if (!Err.empty()) {
1818 delete OS;
1819 return Error(IDLoc, Twine("can't open secure log file: ") +
1820 SecureLogFile + " (" + Err + ")");
1821 }
1822 getContext().setSecureLog(OS);
1823 }
1824
1825 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1826 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1827 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1828 << LogMessage + "\n";
1829
1830 getContext().setSecureLogUsed(true);
1831
1832 return false;
1833}
1834
1835/// ParseDirectiveDarwinSecureLogReset
1836/// ::= .secure_log_reset
1837bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001838 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001839 return TokError("unexpected token in '.secure_log_reset' directive");
1840
1841 Lex();
1842
1843 getContext().setSecureLogUsed(false);
1844
1845 return false;
1846}
1847
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001848/// ParseDirectiveIf
1849/// ::= .if expression
1850bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001851 TheCondStack.push_back(TheCondState);
1852 TheCondState.TheCond = AsmCond::IfCond;
1853 if(TheCondState.Ignore) {
1854 EatToEndOfStatement();
1855 }
1856 else {
1857 int64_t ExprValue;
1858 if (ParseAbsoluteExpression(ExprValue))
1859 return true;
1860
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001861 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001862 return TokError("unexpected token in '.if' directive");
1863
Sean Callanan79ed1a82010-01-19 20:22:31 +00001864 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001865
1866 TheCondState.CondMet = ExprValue;
1867 TheCondState.Ignore = !TheCondState.CondMet;
1868 }
1869
1870 return false;
1871}
1872
1873/// ParseDirectiveElseIf
1874/// ::= .elseif expression
1875bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1876 if (TheCondState.TheCond != AsmCond::IfCond &&
1877 TheCondState.TheCond != AsmCond::ElseIfCond)
1878 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1879 " an .elseif");
1880 TheCondState.TheCond = AsmCond::ElseIfCond;
1881
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001882 bool LastIgnoreState = false;
1883 if (!TheCondStack.empty())
1884 LastIgnoreState = TheCondStack.back().Ignore;
1885 if (LastIgnoreState || TheCondState.CondMet) {
1886 TheCondState.Ignore = true;
1887 EatToEndOfStatement();
1888 }
1889 else {
1890 int64_t ExprValue;
1891 if (ParseAbsoluteExpression(ExprValue))
1892 return true;
1893
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001894 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001895 return TokError("unexpected token in '.elseif' directive");
1896
Sean Callanan79ed1a82010-01-19 20:22:31 +00001897 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001898 TheCondState.CondMet = ExprValue;
1899 TheCondState.Ignore = !TheCondState.CondMet;
1900 }
1901
1902 return false;
1903}
1904
1905/// ParseDirectiveElse
1906/// ::= .else
1907bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001908 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001909 return TokError("unexpected token in '.else' directive");
1910
Sean Callanan79ed1a82010-01-19 20:22:31 +00001911 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001912
1913 if (TheCondState.TheCond != AsmCond::IfCond &&
1914 TheCondState.TheCond != AsmCond::ElseIfCond)
1915 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1916 ".elseif");
1917 TheCondState.TheCond = AsmCond::ElseCond;
1918 bool LastIgnoreState = false;
1919 if (!TheCondStack.empty())
1920 LastIgnoreState = TheCondStack.back().Ignore;
1921 if (LastIgnoreState || TheCondState.CondMet)
1922 TheCondState.Ignore = true;
1923 else
1924 TheCondState.Ignore = false;
1925
1926 return false;
1927}
1928
1929/// ParseDirectiveEndIf
1930/// ::= .endif
1931bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001933 return TokError("unexpected token in '.endif' directive");
1934
Sean Callanan79ed1a82010-01-19 20:22:31 +00001935 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001936
1937 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1938 TheCondStack.empty())
1939 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1940 ".else");
1941 if (!TheCondStack.empty()) {
1942 TheCondState = TheCondStack.back();
1943 TheCondStack.pop_back();
1944 }
1945
1946 return false;
1947}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001948
1949/// ParseDirectiveFile
1950/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001951bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001952 // FIXME: I'm not sure what this is.
1953 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001954 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001955 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001956 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001957
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001958 if (FileNumber < 1)
1959 return TokError("file number less than one");
1960 }
1961
Daniel Dunbareceec052010-07-12 17:45:27 +00001962 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001963 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001964
Chris Lattnerd32e8032010-01-25 19:02:58 +00001965 StringRef Filename = getTok().getString();
1966 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001967 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001968
Daniel Dunbareceec052010-07-12 17:45:27 +00001969 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001970 return TokError("unexpected token in '.file' directive");
1971
Chris Lattnerd32e8032010-01-25 19:02:58 +00001972 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001973 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001974 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001975 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1976
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001977 return false;
1978}
1979
1980/// ParseDirectiveLine
1981/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001982bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001983 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1984 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001985 return TokError("unexpected token in '.line' directive");
1986
Sean Callanan18b83232010-01-19 21:44:56 +00001987 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001988 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001989 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001990
1991 // FIXME: Do something with the .line.
1992 }
1993
Daniel Dunbareceec052010-07-12 17:45:27 +00001994 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001995 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001996
1997 return false;
1998}
1999
2000
2001/// ParseDirectiveLoc
2002/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002003bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002004 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002005 return TokError("unexpected token in '.loc' directive");
2006
2007 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002008 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002009 (void) FileNumber;
2010 // FIXME: Validate file.
2011
Sean Callanan79ed1a82010-01-19 20:22:31 +00002012 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002013 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2014 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002015 return TokError("unexpected token in '.loc' directive");
2016
Sean Callanan18b83232010-01-19 21:44:56 +00002017 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002018 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002019 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002020
Daniel Dunbareceec052010-07-12 17:45:27 +00002021 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2022 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002023 return TokError("unexpected token in '.loc' directive");
2024
Sean Callanan18b83232010-01-19 21:44:56 +00002025 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002026 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002027 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002028
2029 // FIXME: Do something with the .loc.
2030 }
2031 }
2032
Daniel Dunbareceec052010-07-12 17:45:27 +00002033 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002034 return TokError("unexpected token in '.file' directive");
2035
2036 return false;
2037}
2038