blob: 37cabdb24afc2ef345b49e53044bd05979b302b9 [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"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000024#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000025#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000028#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000029using namespace llvm;
30
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000031namespace {
32
33/// \brief Generic implementations of directive handling, etc. which is shared
34/// (or the default, at least) for all assembler parser.
35class GenericAsmParser : public MCAsmParserExtension {
36public:
37 GenericAsmParser() {}
38
39 virtual void Initialize(MCAsmParser &Parser) {
40 // Call the base implementation.
41 this->MCAsmParserExtension::Initialize(Parser);
42
43 // Debugging directives.
44 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
45 &GenericAsmParser::ParseDirectiveFile));
46 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
47 &GenericAsmParser::ParseDirectiveLine));
48 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
49 &GenericAsmParser::ParseDirectiveLoc));
50 }
51
52 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
53 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
54 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
55};
56
57}
58
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000059namespace llvm {
60
61extern MCAsmParserExtension *createDarwinAsmParser();
Daniel Dunbar5146a092010-07-12 21:23:32 +000062extern MCAsmParserExtension *createELFAsmParser();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000063
64}
65
Chris Lattneraaec2052010-01-19 19:46:13 +000066enum { DEFAULT_ADDRSPACE = 0 };
67
Daniel Dunbar9186fa62010-07-01 20:41:56 +000068AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
69 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000070 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +000071 GenericParser(new GenericAsmParser), PlatformParser(0),
Daniel Dunbard1e3b442010-07-17 02:26:10 +000072 CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +000073 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000074
75 // Initialize the generic parser.
76 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +000077
78 // Initialize the platform / file format parser.
79 //
80 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
81 // created.
82 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000083 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +000084 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +000085 } else {
Daniel Dunbar5146a092010-07-12 21:23:32 +000086 PlatformParser = createELFAsmParser();
Daniel Dunbar7a56fc22010-07-12 20:08:04 +000087 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +000088 }
Chris Lattnerebb89b42009-09-27 21:16:52 +000089}
90
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000091AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +000092 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000093 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000094}
95
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000096void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000097 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000098}
99
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000100bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000101 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000102 return true;
103}
104
Sean Callananbf2013e2010-01-20 23:19:55 +0000105void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
106 const char *Type) const {
107 SrcMgr.PrintMessage(Loc, Msg, Type);
108}
Sean Callananfd0b0282010-01-21 00:19:58 +0000109
110bool AsmParser::EnterIncludeFile(const std::string &Filename) {
111 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
112 if (NewBuf == -1)
113 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000114
Sean Callananfd0b0282010-01-21 00:19:58 +0000115 CurBuffer = NewBuf;
116
117 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
118
119 return false;
120}
121
122const AsmToken &AsmParser::Lex() {
123 const AsmToken *tok = &Lexer.Lex();
124
125 if (tok->is(AsmToken::Eof)) {
126 // If this is the end of an included file, pop the parent file off the
127 // include stack.
128 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
129 if (ParentIncludeLoc != SMLoc()) {
130 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
131 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
132 ParentIncludeLoc.getPointer());
133 tok = &Lexer.Lex();
134 }
135 }
136
137 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000138 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000139
Sean Callananfd0b0282010-01-21 00:19:58 +0000140 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000141}
142
Chris Lattner79180e22010-04-05 23:15:42 +0000143bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000144 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000145 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000146 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000147 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000148 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000149 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
150 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000151
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000152 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000153 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000154
Chris Lattnerb717fb02009-07-02 21:53:43 +0000155 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000156
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000157 AsmCond StartingCondState = TheCondState;
158
Chris Lattnerb717fb02009-07-02 21:53:43 +0000159 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000160 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000161 if (!ParseStatement()) continue;
162
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000163 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000164 HadError = true;
165 EatToEndOfStatement();
166 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000167
168 if (TheCondState.TheCond != StartingCondState.TheCond ||
169 TheCondState.Ignore != StartingCondState.Ignore)
170 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000171
Chris Lattner79180e22010-04-05 23:15:42 +0000172 // Finalize the output stream if there are no errors and if the client wants
173 // us to.
174 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000175 Out.Finish();
176
Chris Lattnerb717fb02009-07-02 21:53:43 +0000177 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000178}
179
Chris Lattner2cf5f142009-06-22 01:29:09 +0000180/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
181void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000182 while (Lexer.isNot(AsmToken::EndOfStatement) &&
183 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000184 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000185
186 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000187 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000188 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000189}
190
Chris Lattnerc4193832009-06-22 05:51:26 +0000191
Chris Lattner74ec1a32009-06-22 06:32:03 +0000192/// ParseParenExpr - Parse a paren expression and return it.
193/// NOTE: This assumes the leading '(' has already been consumed.
194///
195/// parenexpr ::= expr)
196///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000197bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000198 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000199 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000200 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000201 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000202 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000203 return false;
204}
Chris Lattnerc4193832009-06-22 05:51:26 +0000205
Chris Lattner74ec1a32009-06-22 06:32:03 +0000206/// ParsePrimaryExpr - Parse a primary expression and return it.
207/// primaryexpr ::= (parenexpr
208/// primaryexpr ::= symbol
209/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000210/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000211/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000212bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000213 switch (Lexer.getKind()) {
214 default:
215 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000216 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000217 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000218 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000219 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000220 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000221 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000222 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000223 case AsmToken::Identifier: {
224 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000225 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000226 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000227
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000228 // Mark the symbol as used in an expression.
229 Sym->setUsedInExpr(true);
230
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000231 // Lookup the symbol variant if used.
232 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
233 if (Split.first.size() != getTok().getIdentifier().size())
234 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
235
Chris Lattnerb4307b32010-01-15 19:28:38 +0000236 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000237 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000238
239 // If this is an absolute variable reference, substitute it now to preserve
240 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000241 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000242 if (Variant)
243 return Error(EndLoc, "unexpected modified on variable reference");
244
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000245 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000246 return false;
247 }
248
249 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000250 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000251 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000252 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000253 case AsmToken::Integer: {
254 SMLoc Loc = getTok().getLoc();
255 int64_t IntVal = getTok().getIntVal();
256 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000257 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000258 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000259 // Look for 'b' or 'f' following an Integer as a directional label
260 if (Lexer.getKind() == AsmToken::Identifier) {
261 StringRef IDVal = getTok().getString();
262 if (IDVal == "f" || IDVal == "b"){
263 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
264 IDVal == "f" ? 1 : 0);
265 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
266 getContext());
267 if(IDVal == "b" && Sym->isUndefined())
268 return Error(Loc, "invalid reference to undefined symbol");
269 EndLoc = Lexer.getLoc();
270 Lex(); // Eat identifier.
271 }
272 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000273 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000274 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000275 case AsmToken::Dot: {
276 // This is a '.' reference, which references the current PC. Emit a
277 // temporary label to the streamer and refer to it.
278 MCSymbol *Sym = Ctx.CreateTempSymbol();
279 Out.EmitLabel(Sym);
280 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
281 EndLoc = Lexer.getLoc();
282 Lex(); // Eat identifier.
283 return false;
284 }
285
Daniel Dunbar3f872332009-07-28 16:08:33 +0000286 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000287 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000288 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000289 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000290 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000291 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000292 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000293 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000294 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000296 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000297 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000298 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000299 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000300 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000302 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000303 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000304 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000305 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000306 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000307 }
308}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000309
Chris Lattnerb4307b32010-01-15 19:28:38 +0000310bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000311 SMLoc EndLoc;
312 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000313}
314
Chris Lattner74ec1a32009-06-22 06:32:03 +0000315/// ParseExpression - Parse an expression and return it.
316///
317/// expr ::= expr +,- expr -> lowest.
318/// expr ::= expr |,^,&,! expr -> middle.
319/// expr ::= expr *,/,%,<<,>> expr -> highest.
320/// expr ::= primaryexpr
321///
Chris Lattner54482b42010-01-15 19:39:23 +0000322bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000323 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000324 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000325 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
326 return true;
327
328 // Try to constant fold it up front, if possible.
329 int64_t Value;
330 if (Res->EvaluateAsAbsolute(Value))
331 Res = MCConstantExpr::Create(Value, getContext());
332
333 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000334}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000335
Chris Lattnerb4307b32010-01-15 19:28:38 +0000336bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000337 Res = 0;
338 return ParseParenExpr(Res, EndLoc) ||
339 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000340}
341
Daniel Dunbar475839e2009-06-29 20:37:27 +0000342bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000343 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000344
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000345 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000346 if (ParseExpression(Expr))
347 return true;
348
Daniel Dunbare00b0112009-10-16 01:57:52 +0000349 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000350 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351
352 return false;
353}
354
Daniel Dunbar3f872332009-07-28 16:08:33 +0000355static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000356 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000357 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000358 default:
359 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360
361 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000363 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000364 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000366 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 return 1;
368
369 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000370 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000371 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000372 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000373 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000374 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000375 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000376 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000377 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000378 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000379 case AsmToken::ExclaimEqual:
380 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000381 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000383 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000384 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000386 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000387 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000392 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000393 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 return 2;
395
396 // Intermediate Precedence: |, &, ^
397 //
398 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000399 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000400 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000401 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000402 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000403 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000404 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000405 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000407 return 3;
408
409 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000410 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000411 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000412 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000413 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000414 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000415 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000416 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000417 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000418 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000420 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000423 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000424 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000425 }
426}
427
428
429/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
430/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000431bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
432 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000433 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000434 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000435 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000436
437 // If the next token is lower precedence than we are allowed to eat, return
438 // successfully with what we ate already.
439 if (TokPrec < Precedence)
440 return false;
441
Sean Callanan79ed1a82010-01-19 20:22:31 +0000442 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000443
444 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000445 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000446 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000447
448 // If BinOp binds less tightly with RHS than the operator after RHS, let
449 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000450 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000451 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000452 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000453 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000454 }
455
Daniel Dunbar475839e2009-06-29 20:37:27 +0000456 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000457 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000458 }
459}
460
Chris Lattnerc4193832009-06-22 05:51:26 +0000461
462
463
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000464/// ParseStatement:
465/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000466/// ::= Label* Directive ...Operands... EndOfStatement
467/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000468bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000469 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000470 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000471 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000472 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000473 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000474
475 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000476 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000477 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000478 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000479 int64_t LocalLabelVal = -1;
480 // GUESS allow an integer followed by a ':' as a directional local label
481 if (Lexer.is(AsmToken::Integer)) {
482 LocalLabelVal = getTok().getIntVal();
483 if (LocalLabelVal < 0) {
484 if (!TheCondState.Ignore)
485 return TokError("unexpected token at start of statement");
486 IDVal = "";
487 }
488 else {
489 IDVal = getTok().getString();
490 Lex(); // Consume the integer token to be used as an identifier token.
491 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000492 if (!TheCondState.Ignore)
493 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000494 }
495 }
496 }
497 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000498 if (!TheCondState.Ignore)
499 return TokError("unexpected token at start of statement");
500 IDVal = "";
501 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000502
Chris Lattner7834fac2010-04-17 18:14:27 +0000503 // Handle conditional assembly here before checking for skipping. We
504 // have to do this so that .endif isn't skipped in a ".if 0" block for
505 // example.
506 if (IDVal == ".if")
507 return ParseDirectiveIf(IDLoc);
508 if (IDVal == ".elseif")
509 return ParseDirectiveElseIf(IDLoc);
510 if (IDVal == ".else")
511 return ParseDirectiveElse(IDLoc);
512 if (IDVal == ".endif")
513 return ParseDirectiveEndIf(IDLoc);
514
515 // If we are in a ".if 0" block, ignore this statement.
516 if (TheCondState.Ignore) {
517 EatToEndOfStatement();
518 return false;
519 }
520
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000521 // FIXME: Recurse on local labels?
522
523 // See what kind of statement we have.
524 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000525 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000526 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000527 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000528
529 // Diagnose attempt to use a variable as a label.
530 //
531 // FIXME: Diagnostics. Note the location of the definition as a label.
532 // FIXME: This doesn't diagnose assignment to a symbol which has been
533 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000534 MCSymbol *Sym;
535 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000536 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000537 else
538 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000539 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000540 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000541
Daniel Dunbar959fd882009-08-26 22:13:22 +0000542 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000543 Out.EmitLabel(Sym);
544
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000545 // Consume any end of statement token, if present, to avoid spurious
546 // AddBlankLine calls().
547 if (Lexer.is(AsmToken::EndOfStatement)) {
548 Lex();
549 if (Lexer.is(AsmToken::Eof))
550 return false;
551 }
552
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000553 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000554 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000555
Daniel Dunbar3f872332009-07-28 16:08:33 +0000556 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000557 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000558 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000559
Daniel Dunbare2ace502009-08-31 08:09:09 +0000560 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000561
562 default: // Normal instruction or directive.
563 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000564 }
565
566 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000567 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000568 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000569 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000570 return ParseDirectiveSet();
571
Daniel Dunbara0d14262009-06-24 23:30:00 +0000572 // Data directives
573
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000574 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000575 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000576 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000577 return ParseDirectiveAscii(true);
578
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000579 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000580 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000581 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000582 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000583 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000584 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000586 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000587
588 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000590 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000592 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000594 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000596 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000598 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000600 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000602 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000604 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
605
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000606 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000607 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000608
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000610 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000612 return ParseDirectiveSpace();
613
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000614 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000615
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000616 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000617 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000619 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000621 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000623 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000625 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000627 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000629 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000631 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000633 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000634 if (IDVal == ".type")
635 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000637 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000639 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000641 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000642 if (IDVal == ".weak_def_can_be_hidden")
643 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000644
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000646 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000648 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000649
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000650 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000651 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000653 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000654
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000655 // Look up the handler in the handler table.
656 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
657 DirectiveMap.lookup(IDVal);
658 if (Handler.first)
659 return (Handler.first->*Handler.second)(IDVal, IDLoc);
660
Kevin Enderby9c656452009-09-10 20:51:44 +0000661 // Target hook for parsing target specific directives.
662 if (!getTargetParser().ParseDirective(ID))
663 return false;
664
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000665 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000666 EatToEndOfStatement();
667 return false;
668 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000669
Chris Lattnera7f13542010-05-19 23:34:33 +0000670 // Canonicalize the opcode to lower case.
671 SmallString<128> Opcode;
672 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
673 Opcode.push_back(tolower(IDVal[i]));
674
Chris Lattner98986712010-01-14 22:21:20 +0000675 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000676 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000677 ParsedOperands);
678 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
679 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000680
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000681 // If parsing succeeded, match the instruction.
682 if (!HadError) {
683 MCInst Inst;
684 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
685 // Emit the instruction on success.
686 Out.EmitInstruction(Inst);
687 } else {
688 // Otherwise emit a diagnostic about the match failure and set the error
689 // flag.
690 //
691 // FIXME: We should give nicer diagnostics about the exact failure.
692 Error(IDLoc, "unrecognized instruction");
693 HadError = true;
694 }
695 }
Chris Lattner98986712010-01-14 22:21:20 +0000696
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000697 // If there was no error, consume the end-of-statement token. Otherwise this
698 // will be done by our caller.
699 if (!HadError)
700 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000701
702 // Free any parsed operands.
703 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
704 delete ParsedOperands[i];
705
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000706 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000707}
Chris Lattner9a023f72009-06-24 04:43:34 +0000708
Benjamin Kramer38e59892010-07-14 22:38:02 +0000709bool AsmParser::ParseAssignment(StringRef Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000710 // FIXME: Use better location, we should use proper tokens.
711 SMLoc EqualLoc = Lexer.getLoc();
712
Daniel Dunbar821e3332009-08-31 08:09:28 +0000713 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000714 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000715 return true;
716
Daniel Dunbar3f872332009-07-28 16:08:33 +0000717 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000718 return TokError("unexpected token in assignment");
719
720 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000721 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000722
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000723 // Validate that the LHS is allowed to be a variable (either it has not been
724 // used as a symbol, or it is an absolute symbol).
725 MCSymbol *Sym = getContext().LookupSymbol(Name);
726 if (Sym) {
727 // Diagnose assignment to a label.
728 //
729 // FIXME: Diagnostics. Note the location of the definition as a label.
730 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000731 if (Sym->isUndefined() && !Sym->isUsedInExpr())
732 ; // Allow redefinitions of undefined symbols only used in directives.
733 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000734 return Error(EqualLoc, "redefinition of '" + Name + "'");
735 else if (!Sym->isVariable())
736 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000737 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000738 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
739 Name + "'");
740 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000741 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000742
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000743 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000744
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000745 Sym->setUsedInExpr(true);
746
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000747 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000748 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000749
750 return false;
751}
752
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000753/// ParseIdentifier:
754/// ::= identifier
755/// ::= string
756bool AsmParser::ParseIdentifier(StringRef &Res) {
757 if (Lexer.isNot(AsmToken::Identifier) &&
758 Lexer.isNot(AsmToken::String))
759 return true;
760
Sean Callanan18b83232010-01-19 21:44:56 +0000761 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000762
Sean Callanan79ed1a82010-01-19 20:22:31 +0000763 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000764
765 return false;
766}
767
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000768/// ParseDirectiveSet:
769/// ::= .set identifier ',' expression
770bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000771 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000772
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000773 if (ParseIdentifier(Name))
774 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000775
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000776 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000777 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000778 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000779
Daniel Dunbare2ace502009-08-31 08:09:09 +0000780 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000781}
782
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000783bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000784 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000785
786 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000787 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000788 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
789 if (Str[i] != '\\') {
790 Data += Str[i];
791 continue;
792 }
793
794 // Recognize escaped characters. Note that this escape semantics currently
795 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
796 ++i;
797 if (i == e)
798 return TokError("unexpected backslash at end of string");
799
800 // Recognize octal sequences.
801 if ((unsigned) (Str[i] - '0') <= 7) {
802 // Consume up to three octal characters.
803 unsigned Value = Str[i] - '0';
804
805 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
806 ++i;
807 Value = Value * 8 + (Str[i] - '0');
808
809 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
810 ++i;
811 Value = Value * 8 + (Str[i] - '0');
812 }
813 }
814
815 if (Value > 255)
816 return TokError("invalid octal escape sequence (out of range)");
817
818 Data += (unsigned char) Value;
819 continue;
820 }
821
822 // Otherwise recognize individual escapes.
823 switch (Str[i]) {
824 default:
825 // Just reject invalid escape sequences for now.
826 return TokError("invalid escape sequence (unrecognized character)");
827
828 case 'b': Data += '\b'; break;
829 case 'f': Data += '\f'; break;
830 case 'n': Data += '\n'; break;
831 case 'r': Data += '\r'; break;
832 case 't': Data += '\t'; break;
833 case '"': Data += '"'; break;
834 case '\\': Data += '\\'; break;
835 }
836 }
837
838 return false;
839}
840
Daniel Dunbara0d14262009-06-24 23:30:00 +0000841/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000842/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000843bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000844 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000845 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000846 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000847 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000848
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000849 std::string Data;
850 if (ParseEscapedString(Data))
851 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000852
853 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000854 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000855 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
856
Sean Callanan79ed1a82010-01-19 20:22:31 +0000857 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000858
859 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000860 break;
861
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000862 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000863 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000864 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000865 }
866 }
867
Sean Callanan79ed1a82010-01-19 20:22:31 +0000868 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000869 return false;
870}
871
872/// ParseDirectiveValue
873/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
874bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000875 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000876 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000877 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000878 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000879 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000880 return true;
881
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000882 // Special case constant expressions to match code generator.
883 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000884 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000885 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000886 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000887
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000888 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000889 break;
890
891 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000892 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000893 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000894 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000895 }
896 }
897
Sean Callanan79ed1a82010-01-19 20:22:31 +0000898 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000899 return false;
900}
901
902/// ParseDirectiveSpace
903/// ::= .space expression [ , expression ]
904bool AsmParser::ParseDirectiveSpace() {
905 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000906 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000907 return true;
908
909 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000910 if (getLexer().isNot(AsmToken::EndOfStatement)) {
911 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000912 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000913 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000914
Daniel Dunbar475839e2009-06-29 20:37:27 +0000915 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000916 return true;
917
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000918 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000919 return TokError("unexpected token in '.space' directive");
920 }
921
Sean Callanan79ed1a82010-01-19 20:22:31 +0000922 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923
924 if (NumBytes <= 0)
925 return TokError("invalid number of bytes in '.space' directive");
926
927 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000928 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929
930 return false;
931}
932
933/// ParseDirectiveFill
934/// ::= .fill expression , expression , expression
935bool AsmParser::ParseDirectiveFill() {
936 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000937 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000938 return true;
939
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000940 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000941 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000942 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000943
944 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000945 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000946 return true;
947
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000948 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000949 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000950 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000951
952 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000953 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000954 return true;
955
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000956 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000957 return TokError("unexpected token in '.fill' directive");
958
Sean Callanan79ed1a82010-01-19 20:22:31 +0000959 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960
Daniel Dunbarbc38ca72009-08-21 15:43:35 +0000961 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
962 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963
964 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000965 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966
967 return false;
968}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000969
970/// ParseDirectiveOrg
971/// ::= .org expression [ , expression ]
972bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000973 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000974 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000975 return true;
976
977 // Parse optional fill expression.
978 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000979 if (getLexer().isNot(AsmToken::EndOfStatement)) {
980 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000981 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000982 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +0000983
Daniel Dunbar475839e2009-06-29 20:37:27 +0000984 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000985 return true;
986
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000987 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000988 return TokError("unexpected token in '.org' directive");
989 }
990
Sean Callanan79ed1a82010-01-19 20:22:31 +0000991 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000992
993 // FIXME: Only limited forms of relocatable expressions are accepted here, it
994 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000995 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +0000996
997 return false;
998}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000999
1000/// ParseDirectiveAlign
1001/// ::= {.align, ...} expression [ , expression [ , expression ]]
1002bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001003 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001004 int64_t Alignment;
1005 if (ParseAbsoluteExpression(Alignment))
1006 return true;
1007
1008 SMLoc MaxBytesLoc;
1009 bool HasFillExpr = false;
1010 int64_t FillExpr = 0;
1011 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001012 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1013 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001014 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001015 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001016
1017 // The fill expression can be omitted while specifying a maximum number of
1018 // alignment bytes, e.g:
1019 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001020 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001021 HasFillExpr = true;
1022 if (ParseAbsoluteExpression(FillExpr))
1023 return true;
1024 }
1025
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001026 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1027 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001028 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001029 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001030
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001031 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001032 if (ParseAbsoluteExpression(MaxBytesToFill))
1033 return true;
1034
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001035 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001036 return TokError("unexpected token in directive");
1037 }
1038 }
1039
Sean Callanan79ed1a82010-01-19 20:22:31 +00001040 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001041
Daniel Dunbar648ac512010-05-17 21:54:30 +00001042 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001043 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001044
1045 // Compute alignment in bytes.
1046 if (IsPow2) {
1047 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001048 if (Alignment >= 32) {
1049 Error(AlignmentLoc, "invalid alignment value");
1050 Alignment = 31;
1051 }
1052
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001053 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001054 }
1055
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001056 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001057 if (MaxBytesLoc.isValid()) {
1058 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001059 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1060 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001061 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001062 }
1063
1064 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001065 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1066 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001067 MaxBytesToFill = 0;
1068 }
1069 }
1070
Daniel Dunbar648ac512010-05-17 21:54:30 +00001071 // Check whether we should use optimal code alignment for this .align
1072 // directive.
1073 //
1074 // FIXME: This should be using a target hook.
1075 bool UseCodeAlign = false;
1076 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001077 getStreamer().getCurrentSection()))
Chris Lattnera9558532010-07-15 21:19:31 +00001078 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001079 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1080 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001081 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001082 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001083 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Chris Lattnera9558532010-07-15 21:19:31 +00001084 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize,
1085 MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001086 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001087
1088 return false;
1089}
1090
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001091/// ParseDirectiveSymbolAttribute
1092/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001093bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001094 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001095 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001096 StringRef Name;
1097
1098 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001099 return TokError("expected identifier in directive");
1100
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001101 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001102
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001103 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001104
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001105 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001106 break;
1107
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001108 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001109 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001110 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001111 }
1112 }
1113
Sean Callanan79ed1a82010-01-19 20:22:31 +00001114 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001115 return false;
1116}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001117
Matt Fleming924c5e52010-05-21 11:36:59 +00001118/// ParseDirectiveELFType
1119/// ::= .type identifier , @attribute
1120bool AsmParser::ParseDirectiveELFType() {
1121 StringRef Name;
1122 if (ParseIdentifier(Name))
1123 return TokError("expected identifier in directive");
1124
1125 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001126 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001127
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001128 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001129 return TokError("unexpected token in '.type' directive");
1130 Lex();
1131
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001132 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001133 return TokError("expected '@' before type");
1134 Lex();
1135
1136 StringRef Type;
1137 SMLoc TypeLoc;
1138
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001139 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001140 if (ParseIdentifier(Type))
1141 return TokError("expected symbol type in directive");
1142
1143 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1144 .Case("function", MCSA_ELF_TypeFunction)
1145 .Case("object", MCSA_ELF_TypeObject)
1146 .Case("tls_object", MCSA_ELF_TypeTLS)
1147 .Case("common", MCSA_ELF_TypeCommon)
1148 .Case("notype", MCSA_ELF_TypeNoType)
1149 .Default(MCSA_Invalid);
1150
1151 if (Attr == MCSA_Invalid)
1152 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1153
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001154 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001155 return TokError("unexpected token in '.type' directive");
1156
1157 Lex();
1158
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001159 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001160
1161 return false;
1162}
1163
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001164/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001165/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1166bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001167 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001168 StringRef Name;
1169 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001170 return TokError("expected identifier in directive");
1171
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001172 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001173 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001174
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001175 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001176 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001177 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001178
1179 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001180 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001181 if (ParseAbsoluteExpression(Size))
1182 return true;
1183
1184 int64_t Pow2Alignment = 0;
1185 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001186 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001187 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001188 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001189 if (ParseAbsoluteExpression(Pow2Alignment))
1190 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001191
1192 // If this target takes alignments in bytes (not log) validate and convert.
1193 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1194 if (!isPowerOf2_64(Pow2Alignment))
1195 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1196 Pow2Alignment = Log2_64(Pow2Alignment);
1197 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001198 }
1199
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001200 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001201 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001202
Sean Callanan79ed1a82010-01-19 20:22:31 +00001203 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001204
Chris Lattner1fc3d752009-07-09 17:25:12 +00001205 // NOTE: a size of zero for a .comm should create a undefined symbol
1206 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001207 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001208 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1209 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001210
Eric Christopherc260a3e2010-05-14 01:38:54 +00001211 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001212 // may internally end up wanting an alignment in bytes.
1213 // FIXME: Diagnose overflow.
1214 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001215 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1216 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001217
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001218 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001219 return Error(IDLoc, "invalid symbol redefinition");
1220
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001221 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001222 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001223 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001224 getStreamer().EmitZerofill(Ctx.getMachOSection(
1225 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1226 0, SectionKind::getBSS()),
1227 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001228 return false;
1229 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001230
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001231 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001232 return false;
1233}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001234
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001235/// ParseDirectiveAbort
1236/// ::= .abort [ "abort_string" ]
1237bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001238 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001239 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001240
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001241 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001242 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1243 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001244 return TokError("expected string in '.abort' directive");
1245
Sean Callanan18b83232010-01-19 21:44:56 +00001246 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001247
Sean Callanan79ed1a82010-01-19 20:22:31 +00001248 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001249 }
1250
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001251 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001252 return TokError("unexpected token in '.abort' directive");
1253
Sean Callanan79ed1a82010-01-19 20:22:31 +00001254 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001255
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001256 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001257 if (Str.empty())
1258 Error(Loc, ".abort detected. Assembly stopping.");
1259 else
1260 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001261
1262 return false;
1263}
Kevin Enderby71148242009-07-14 21:35:03 +00001264
Kevin Enderby1f049b22009-07-14 23:21:55 +00001265/// ParseDirectiveInclude
1266/// ::= .include "filename"
1267bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001268 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001269 return TokError("expected string in '.include' directive");
1270
Sean Callanan18b83232010-01-19 21:44:56 +00001271 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001272 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001273 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001274
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001275 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001276 return TokError("unexpected token in '.include' directive");
1277
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001278 // Strip the quotes.
1279 Filename = Filename.substr(1, Filename.size()-2);
1280
1281 // Attempt to switch the lexer to the included file before consuming the end
1282 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001283 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001284 PrintMessage(IncludeLoc,
1285 "Could not find include file '" + Filename + "'",
1286 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001287 return true;
1288 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001289
1290 return false;
1291}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001292
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001293/// ParseDirectiveIf
1294/// ::= .if expression
1295bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001296 TheCondStack.push_back(TheCondState);
1297 TheCondState.TheCond = AsmCond::IfCond;
1298 if(TheCondState.Ignore) {
1299 EatToEndOfStatement();
1300 }
1301 else {
1302 int64_t ExprValue;
1303 if (ParseAbsoluteExpression(ExprValue))
1304 return true;
1305
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001306 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001307 return TokError("unexpected token in '.if' directive");
1308
Sean Callanan79ed1a82010-01-19 20:22:31 +00001309 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001310
1311 TheCondState.CondMet = ExprValue;
1312 TheCondState.Ignore = !TheCondState.CondMet;
1313 }
1314
1315 return false;
1316}
1317
1318/// ParseDirectiveElseIf
1319/// ::= .elseif expression
1320bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1321 if (TheCondState.TheCond != AsmCond::IfCond &&
1322 TheCondState.TheCond != AsmCond::ElseIfCond)
1323 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1324 " an .elseif");
1325 TheCondState.TheCond = AsmCond::ElseIfCond;
1326
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001327 bool LastIgnoreState = false;
1328 if (!TheCondStack.empty())
1329 LastIgnoreState = TheCondStack.back().Ignore;
1330 if (LastIgnoreState || TheCondState.CondMet) {
1331 TheCondState.Ignore = true;
1332 EatToEndOfStatement();
1333 }
1334 else {
1335 int64_t ExprValue;
1336 if (ParseAbsoluteExpression(ExprValue))
1337 return true;
1338
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001339 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001340 return TokError("unexpected token in '.elseif' directive");
1341
Sean Callanan79ed1a82010-01-19 20:22:31 +00001342 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001343 TheCondState.CondMet = ExprValue;
1344 TheCondState.Ignore = !TheCondState.CondMet;
1345 }
1346
1347 return false;
1348}
1349
1350/// ParseDirectiveElse
1351/// ::= .else
1352bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001353 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001354 return TokError("unexpected token in '.else' directive");
1355
Sean Callanan79ed1a82010-01-19 20:22:31 +00001356 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001357
1358 if (TheCondState.TheCond != AsmCond::IfCond &&
1359 TheCondState.TheCond != AsmCond::ElseIfCond)
1360 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1361 ".elseif");
1362 TheCondState.TheCond = AsmCond::ElseCond;
1363 bool LastIgnoreState = false;
1364 if (!TheCondStack.empty())
1365 LastIgnoreState = TheCondStack.back().Ignore;
1366 if (LastIgnoreState || TheCondState.CondMet)
1367 TheCondState.Ignore = true;
1368 else
1369 TheCondState.Ignore = false;
1370
1371 return false;
1372}
1373
1374/// ParseDirectiveEndIf
1375/// ::= .endif
1376bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001377 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001378 return TokError("unexpected token in '.endif' directive");
1379
Sean Callanan79ed1a82010-01-19 20:22:31 +00001380 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001381
1382 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1383 TheCondStack.empty())
1384 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1385 ".else");
1386 if (!TheCondStack.empty()) {
1387 TheCondState = TheCondStack.back();
1388 TheCondStack.pop_back();
1389 }
1390
1391 return false;
1392}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001393
1394/// ParseDirectiveFile
1395/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001396bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001397 // FIXME: I'm not sure what this is.
1398 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001399 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001400 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001401 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001402
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001403 if (FileNumber < 1)
1404 return TokError("file number less than one");
1405 }
1406
Daniel Dunbareceec052010-07-12 17:45:27 +00001407 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001408 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001409
Chris Lattnerd32e8032010-01-25 19:02:58 +00001410 StringRef Filename = getTok().getString();
1411 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001412 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001413
Daniel Dunbareceec052010-07-12 17:45:27 +00001414 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001415 return TokError("unexpected token in '.file' directive");
1416
Chris Lattnerd32e8032010-01-25 19:02:58 +00001417 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001418 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001419 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001420 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1421
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001422 return false;
1423}
1424
1425/// ParseDirectiveLine
1426/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001427bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001428 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1429 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001430 return TokError("unexpected token in '.line' directive");
1431
Sean Callanan18b83232010-01-19 21:44:56 +00001432 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001433 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001434 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001435
1436 // FIXME: Do something with the .line.
1437 }
1438
Daniel Dunbareceec052010-07-12 17:45:27 +00001439 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001440 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001441
1442 return false;
1443}
1444
1445
1446/// ParseDirectiveLoc
1447/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001448bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001449 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001450 return TokError("unexpected token in '.loc' directive");
1451
1452 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001453 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001454 (void) FileNumber;
1455 // FIXME: Validate file.
1456
Sean Callanan79ed1a82010-01-19 20:22:31 +00001457 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001458 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1459 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001460 return TokError("unexpected token in '.loc' directive");
1461
Sean Callanan18b83232010-01-19 21:44:56 +00001462 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001463 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001464 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001465
Daniel Dunbareceec052010-07-12 17:45:27 +00001466 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1467 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001468 return TokError("unexpected token in '.loc' directive");
1469
Sean Callanan18b83232010-01-19 21:44:56 +00001470 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001471 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001472 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001473
1474 // FIXME: Do something with the .loc.
1475 }
1476 }
1477
Daniel Dunbareceec052010-07-12 17:45:27 +00001478 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001479 return TokError("unexpected token in '.file' directive");
1480
1481 return false;
1482}
1483
Daniel Dunbard1e3b442010-07-17 02:26:10 +00001484
1485/// \brief Create an MCAsmParser instance.
1486MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
1487 MCContext &C, MCStreamer &Out,
1488 const MCAsmInfo &MAI) {
1489 return new AsmParser(T, SM, C, Out, MAI);
1490}