blob: 793f3c7ab7424102037775546d828dc251a12bc9 [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),
72 TargetParser(0), 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 Dunbar53131982010-07-12 17:27:45 +000096void AsmParser::setTargetParser(TargetAsmParser &P) {
97 assert(!TargetParser && "Target parser is already initialized!");
98 TargetParser = &P;
99 TargetParser->Initialize(*this);
100}
101
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000102void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000103 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000104}
105
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000106bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000107 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000108 return true;
109}
110
Sean Callananbf2013e2010-01-20 23:19:55 +0000111void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
112 const char *Type) const {
113 SrcMgr.PrintMessage(Loc, Msg, Type);
114}
Sean Callananfd0b0282010-01-21 00:19:58 +0000115
116bool AsmParser::EnterIncludeFile(const std::string &Filename) {
117 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
118 if (NewBuf == -1)
119 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000120
Sean Callananfd0b0282010-01-21 00:19:58 +0000121 CurBuffer = NewBuf;
122
123 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
124
125 return false;
126}
127
128const AsmToken &AsmParser::Lex() {
129 const AsmToken *tok = &Lexer.Lex();
130
131 if (tok->is(AsmToken::Eof)) {
132 // If this is the end of an included file, pop the parent file off the
133 // include stack.
134 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
135 if (ParentIncludeLoc != SMLoc()) {
136 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
137 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
138 ParentIncludeLoc.getPointer());
139 tok = &Lexer.Lex();
140 }
141 }
142
143 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000144 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000145
Sean Callananfd0b0282010-01-21 00:19:58 +0000146 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000147}
148
Chris Lattner79180e22010-04-05 23:15:42 +0000149bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000150 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000151 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000152 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000153 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000154 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000155 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
156 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000157
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000158 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000159 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000160
Chris Lattnerb717fb02009-07-02 21:53:43 +0000161 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000162
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000163 AsmCond StartingCondState = TheCondState;
164
Chris Lattnerb717fb02009-07-02 21:53:43 +0000165 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000166 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000167 if (!ParseStatement()) continue;
168
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000169 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000170 HadError = true;
171 EatToEndOfStatement();
172 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000173
174 if (TheCondState.TheCond != StartingCondState.TheCond ||
175 TheCondState.Ignore != StartingCondState.Ignore)
176 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000177
Chris Lattner79180e22010-04-05 23:15:42 +0000178 // Finalize the output stream if there are no errors and if the client wants
179 // us to.
180 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000181 Out.Finish();
182
Chris Lattnerb717fb02009-07-02 21:53:43 +0000183 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000184}
185
Chris Lattner2cf5f142009-06-22 01:29:09 +0000186/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
187void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000188 while (Lexer.isNot(AsmToken::EndOfStatement) &&
189 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000190 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000191
192 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000193 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000194 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000195}
196
Chris Lattnerc4193832009-06-22 05:51:26 +0000197
Chris Lattner74ec1a32009-06-22 06:32:03 +0000198/// ParseParenExpr - Parse a paren expression and return it.
199/// NOTE: This assumes the leading '(' has already been consumed.
200///
201/// parenexpr ::= expr)
202///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000203bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000204 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000205 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000206 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000207 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000208 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000209 return false;
210}
Chris Lattnerc4193832009-06-22 05:51:26 +0000211
Chris Lattner74ec1a32009-06-22 06:32:03 +0000212/// ParsePrimaryExpr - Parse a primary expression and return it.
213/// primaryexpr ::= (parenexpr
214/// primaryexpr ::= symbol
215/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000216/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000217/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000218bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000219 switch (Lexer.getKind()) {
220 default:
221 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000222 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000223 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000224 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000225 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000226 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000227 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000228 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000229 case AsmToken::Identifier: {
230 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000231 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000232 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000233
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000234 // Mark the symbol as used in an expression.
235 Sym->setUsedInExpr(true);
236
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000237 // Lookup the symbol variant if used.
238 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
239 if (Split.first.size() != getTok().getIdentifier().size())
240 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
241
Chris Lattnerb4307b32010-01-15 19:28:38 +0000242 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000243 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000244
245 // If this is an absolute variable reference, substitute it now to preserve
246 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000247 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000248 if (Variant)
249 return Error(EndLoc, "unexpected modified on variable reference");
250
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000251 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000252 return false;
253 }
254
255 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000256 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000257 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000258 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000259 case AsmToken::Integer: {
260 SMLoc Loc = getTok().getLoc();
261 int64_t IntVal = getTok().getIntVal();
262 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000263 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000264 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000265 // Look for 'b' or 'f' following an Integer as a directional label
266 if (Lexer.getKind() == AsmToken::Identifier) {
267 StringRef IDVal = getTok().getString();
268 if (IDVal == "f" || IDVal == "b"){
269 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
270 IDVal == "f" ? 1 : 0);
271 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
272 getContext());
273 if(IDVal == "b" && Sym->isUndefined())
274 return Error(Loc, "invalid reference to undefined symbol");
275 EndLoc = Lexer.getLoc();
276 Lex(); // Eat identifier.
277 }
278 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000279 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000280 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000281 case AsmToken::Dot: {
282 // This is a '.' reference, which references the current PC. Emit a
283 // temporary label to the streamer and refer to it.
284 MCSymbol *Sym = Ctx.CreateTempSymbol();
285 Out.EmitLabel(Sym);
286 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
287 EndLoc = Lexer.getLoc();
288 Lex(); // Eat identifier.
289 return false;
290 }
291
Daniel Dunbar3f872332009-07-28 16:08:33 +0000292 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000293 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000294 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::Minus:
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::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000300 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000301 case AsmToken::Plus:
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::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000306 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000307 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000308 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000309 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000311 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000312 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000313 }
314}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000315
Chris Lattnerb4307b32010-01-15 19:28:38 +0000316bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000317 SMLoc EndLoc;
318 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000319}
320
Chris Lattner74ec1a32009-06-22 06:32:03 +0000321/// ParseExpression - Parse an expression and return it.
322///
323/// expr ::= expr +,- expr -> lowest.
324/// expr ::= expr |,^,&,! expr -> middle.
325/// expr ::= expr *,/,%,<<,>> expr -> highest.
326/// expr ::= primaryexpr
327///
Chris Lattner54482b42010-01-15 19:39:23 +0000328bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000329 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000330 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000331 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
332 return true;
333
334 // Try to constant fold it up front, if possible.
335 int64_t Value;
336 if (Res->EvaluateAsAbsolute(Value))
337 Res = MCConstantExpr::Create(Value, getContext());
338
339 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000340}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000341
Chris Lattnerb4307b32010-01-15 19:28:38 +0000342bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000343 Res = 0;
344 return ParseParenExpr(Res, EndLoc) ||
345 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000346}
347
Daniel Dunbar475839e2009-06-29 20:37:27 +0000348bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000349 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000350
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000351 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000352 if (ParseExpression(Expr))
353 return true;
354
Daniel Dunbare00b0112009-10-16 01:57:52 +0000355 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000356 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000357
358 return false;
359}
360
Daniel Dunbar3f872332009-07-28 16:08:33 +0000361static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000362 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000363 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000364 default:
365 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000366
367 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000368 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000369 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000370 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000372 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000373 return 1;
374
375 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000376 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000377 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000378 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000379 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000380 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000381 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000382 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000383 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000384 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 case AsmToken::ExclaimEqual:
386 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000387 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000388 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000389 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000392 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000393 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000395 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000396 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000397 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000398 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000399 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 return 2;
401
402 // Intermediate Precedence: |, &, ^
403 //
404 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000405 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000407 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000408 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000409 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000410 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000412 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000413 return 3;
414
415 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000416 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000417 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000418 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000420 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000423 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000424 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000425 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000426 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000427 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000429 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000430 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000431 }
432}
433
434
435/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
436/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000437bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
438 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000439 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000440 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000441 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000442
443 // If the next token is lower precedence than we are allowed to eat, return
444 // successfully with what we ate already.
445 if (TokPrec < Precedence)
446 return false;
447
Sean Callanan79ed1a82010-01-19 20:22:31 +0000448 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000449
450 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000451 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000452 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000453
454 // If BinOp binds less tightly with RHS than the operator after RHS, let
455 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000456 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000457 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000458 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000459 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000460 }
461
Daniel Dunbar475839e2009-06-29 20:37:27 +0000462 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000463 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000464 }
465}
466
Chris Lattnerc4193832009-06-22 05:51:26 +0000467
468
469
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000470/// ParseStatement:
471/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000472/// ::= Label* Directive ...Operands... EndOfStatement
473/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000474bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000475 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000476 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000477 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000478 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000479 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000480
481 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000482 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000483 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000484 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000485 int64_t LocalLabelVal = -1;
486 // GUESS allow an integer followed by a ':' as a directional local label
487 if (Lexer.is(AsmToken::Integer)) {
488 LocalLabelVal = getTok().getIntVal();
489 if (LocalLabelVal < 0) {
490 if (!TheCondState.Ignore)
491 return TokError("unexpected token at start of statement");
492 IDVal = "";
493 }
494 else {
495 IDVal = getTok().getString();
496 Lex(); // Consume the integer token to be used as an identifier token.
497 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000498 if (!TheCondState.Ignore)
499 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000500 }
501 }
502 }
503 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000504 if (!TheCondState.Ignore)
505 return TokError("unexpected token at start of statement");
506 IDVal = "";
507 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000508
Chris Lattner7834fac2010-04-17 18:14:27 +0000509 // Handle conditional assembly here before checking for skipping. We
510 // have to do this so that .endif isn't skipped in a ".if 0" block for
511 // example.
512 if (IDVal == ".if")
513 return ParseDirectiveIf(IDLoc);
514 if (IDVal == ".elseif")
515 return ParseDirectiveElseIf(IDLoc);
516 if (IDVal == ".else")
517 return ParseDirectiveElse(IDLoc);
518 if (IDVal == ".endif")
519 return ParseDirectiveEndIf(IDLoc);
520
521 // If we are in a ".if 0" block, ignore this statement.
522 if (TheCondState.Ignore) {
523 EatToEndOfStatement();
524 return false;
525 }
526
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000527 // FIXME: Recurse on local labels?
528
529 // See what kind of statement we have.
530 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000531 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000532 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000533 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000534
535 // Diagnose attempt to use a variable as a label.
536 //
537 // FIXME: Diagnostics. Note the location of the definition as a label.
538 // FIXME: This doesn't diagnose assignment to a symbol which has been
539 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000540 MCSymbol *Sym;
541 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000542 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000543 else
544 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000545 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000546 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000547
Daniel Dunbar959fd882009-08-26 22:13:22 +0000548 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000549 Out.EmitLabel(Sym);
550
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000551 // Consume any end of statement token, if present, to avoid spurious
552 // AddBlankLine calls().
553 if (Lexer.is(AsmToken::EndOfStatement)) {
554 Lex();
555 if (Lexer.is(AsmToken::Eof))
556 return false;
557 }
558
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000559 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000560 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000561
Daniel Dunbar3f872332009-07-28 16:08:33 +0000562 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000563 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000564 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000565
Daniel Dunbare2ace502009-08-31 08:09:09 +0000566 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000567
568 default: // Normal instruction or directive.
569 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000570 }
571
572 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000573 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000574 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000576 return ParseDirectiveSet();
577
Daniel Dunbara0d14262009-06-24 23:30:00 +0000578 // Data directives
579
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000580 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000581 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000583 return ParseDirectiveAscii(true);
584
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000585 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000586 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000587 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000588 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000589 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000590 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000592 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000593
594 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000595 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000596 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000598 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000599 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000600 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000602 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000603 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000604 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000606 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000607 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000608 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000610 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
611
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000613 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000614
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000615 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000616 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000618 return ParseDirectiveSpace();
619
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000620 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000621
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000622 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000623 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000625 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000627 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000629 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000631 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000633 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000635 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000637 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000639 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000640 if (IDVal == ".type")
641 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000643 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000645 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000647 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000648 if (IDVal == ".weak_def_can_be_hidden")
649 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000650
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000652 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000654 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000655
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000657 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000658 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000659 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000660
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000661 // Look up the handler in the handler table.
662 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
663 DirectiveMap.lookup(IDVal);
664 if (Handler.first)
665 return (Handler.first->*Handler.second)(IDVal, IDLoc);
666
Kevin Enderby9c656452009-09-10 20:51:44 +0000667 // Target hook for parsing target specific directives.
668 if (!getTargetParser().ParseDirective(ID))
669 return false;
670
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000671 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000672 EatToEndOfStatement();
673 return false;
674 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000675
Chris Lattnera7f13542010-05-19 23:34:33 +0000676 // Canonicalize the opcode to lower case.
677 SmallString<128> Opcode;
678 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
679 Opcode.push_back(tolower(IDVal[i]));
680
Chris Lattner98986712010-01-14 22:21:20 +0000681 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000682 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000683 ParsedOperands);
684 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
685 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000686
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000687 // If parsing succeeded, match the instruction.
688 if (!HadError) {
689 MCInst Inst;
690 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
691 // Emit the instruction on success.
692 Out.EmitInstruction(Inst);
693 } else {
694 // Otherwise emit a diagnostic about the match failure and set the error
695 // flag.
696 //
697 // FIXME: We should give nicer diagnostics about the exact failure.
698 Error(IDLoc, "unrecognized instruction");
699 HadError = true;
700 }
701 }
Chris Lattner98986712010-01-14 22:21:20 +0000702
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000703 // If there was no error, consume the end-of-statement token. Otherwise this
704 // will be done by our caller.
705 if (!HadError)
706 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000707
708 // Free any parsed operands.
709 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
710 delete ParsedOperands[i];
711
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000712 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000713}
Chris Lattner9a023f72009-06-24 04:43:34 +0000714
Daniel Dunbare2ace502009-08-31 08:09:09 +0000715bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000716 // FIXME: Use better location, we should use proper tokens.
717 SMLoc EqualLoc = Lexer.getLoc();
718
Daniel Dunbar821e3332009-08-31 08:09:28 +0000719 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000720 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000721 return true;
722
Daniel Dunbar3f872332009-07-28 16:08:33 +0000723 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000724 return TokError("unexpected token in assignment");
725
726 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000727 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000728
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000729 // Validate that the LHS is allowed to be a variable (either it has not been
730 // used as a symbol, or it is an absolute symbol).
731 MCSymbol *Sym = getContext().LookupSymbol(Name);
732 if (Sym) {
733 // Diagnose assignment to a label.
734 //
735 // FIXME: Diagnostics. Note the location of the definition as a label.
736 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000737 if (Sym->isUndefined() && !Sym->isUsedInExpr())
738 ; // Allow redefinitions of undefined symbols only used in directives.
739 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000740 return Error(EqualLoc, "redefinition of '" + Name + "'");
741 else if (!Sym->isVariable())
742 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000743 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000744 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
745 Name + "'");
746 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000747 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000748
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000749 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000750
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000751 Sym->setUsedInExpr(true);
752
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000753 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000754 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000755
756 return false;
757}
758
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000759/// ParseIdentifier:
760/// ::= identifier
761/// ::= string
762bool AsmParser::ParseIdentifier(StringRef &Res) {
763 if (Lexer.isNot(AsmToken::Identifier) &&
764 Lexer.isNot(AsmToken::String))
765 return true;
766
Sean Callanan18b83232010-01-19 21:44:56 +0000767 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000768
Sean Callanan79ed1a82010-01-19 20:22:31 +0000769 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000770
771 return false;
772}
773
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000774/// ParseDirectiveSet:
775/// ::= .set identifier ',' expression
776bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000777 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000778
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000779 if (ParseIdentifier(Name))
780 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000781
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000782 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000783 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000784 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000785
Daniel Dunbare2ace502009-08-31 08:09:09 +0000786 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000787}
788
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000789bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000790 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000791
792 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000793 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000794 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
795 if (Str[i] != '\\') {
796 Data += Str[i];
797 continue;
798 }
799
800 // Recognize escaped characters. Note that this escape semantics currently
801 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
802 ++i;
803 if (i == e)
804 return TokError("unexpected backslash at end of string");
805
806 // Recognize octal sequences.
807 if ((unsigned) (Str[i] - '0') <= 7) {
808 // Consume up to three octal characters.
809 unsigned Value = Str[i] - '0';
810
811 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
812 ++i;
813 Value = Value * 8 + (Str[i] - '0');
814
815 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
816 ++i;
817 Value = Value * 8 + (Str[i] - '0');
818 }
819 }
820
821 if (Value > 255)
822 return TokError("invalid octal escape sequence (out of range)");
823
824 Data += (unsigned char) Value;
825 continue;
826 }
827
828 // Otherwise recognize individual escapes.
829 switch (Str[i]) {
830 default:
831 // Just reject invalid escape sequences for now.
832 return TokError("invalid escape sequence (unrecognized character)");
833
834 case 'b': Data += '\b'; break;
835 case 'f': Data += '\f'; break;
836 case 'n': Data += '\n'; break;
837 case 'r': Data += '\r'; break;
838 case 't': Data += '\t'; break;
839 case '"': Data += '"'; break;
840 case '\\': Data += '\\'; break;
841 }
842 }
843
844 return false;
845}
846
Daniel Dunbara0d14262009-06-24 23:30:00 +0000847/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000848/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000849bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000850 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000851 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000852 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000853 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000854
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000855 std::string Data;
856 if (ParseEscapedString(Data))
857 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000858
859 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000860 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000861 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
862
Sean Callanan79ed1a82010-01-19 20:22:31 +0000863 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000864
865 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000866 break;
867
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000868 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000869 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000870 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000871 }
872 }
873
Sean Callanan79ed1a82010-01-19 20:22:31 +0000874 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000875 return false;
876}
877
878/// ParseDirectiveValue
879/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
880bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000881 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000882 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000883 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000884 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000885 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000886 return true;
887
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000888 // Special case constant expressions to match code generator.
889 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000890 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000891 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000892 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000893
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000894 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000895 break;
896
897 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000898 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000899 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000900 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000901 }
902 }
903
Sean Callanan79ed1a82010-01-19 20:22:31 +0000904 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000905 return false;
906}
907
908/// ParseDirectiveSpace
909/// ::= .space expression [ , expression ]
910bool AsmParser::ParseDirectiveSpace() {
911 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000912 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000913 return true;
914
915 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000916 if (getLexer().isNot(AsmToken::EndOfStatement)) {
917 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000918 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000919 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000920
Daniel Dunbar475839e2009-06-29 20:37:27 +0000921 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000922 return true;
923
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000924 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000925 return TokError("unexpected token in '.space' directive");
926 }
927
Sean Callanan79ed1a82010-01-19 20:22:31 +0000928 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000929
930 if (NumBytes <= 0)
931 return TokError("invalid number of bytes in '.space' directive");
932
933 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000934 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000935
936 return false;
937}
938
939/// ParseDirectiveFill
940/// ::= .fill expression , expression , expression
941bool AsmParser::ParseDirectiveFill() {
942 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000943 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000944 return true;
945
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000946 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000947 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000948 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000949
950 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000951 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000952 return true;
953
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000954 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000955 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000956 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000957
958 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000959 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960 return true;
961
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000962 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963 return TokError("unexpected token in '.fill' directive");
964
Sean Callanan79ed1a82010-01-19 20:22:31 +0000965 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966
Daniel Dunbarbc38ca72009-08-21 15:43:35 +0000967 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
968 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969
970 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000971 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000972
973 return false;
974}
Daniel Dunbarc238b582009-06-25 22:44:51 +0000975
976/// ParseDirectiveOrg
977/// ::= .org expression [ , expression ]
978bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000979 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000980 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000981 return true;
982
983 // Parse optional fill expression.
984 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000985 if (getLexer().isNot(AsmToken::EndOfStatement)) {
986 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000987 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000988 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +0000989
Daniel Dunbar475839e2009-06-29 20:37:27 +0000990 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000991 return true;
992
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000993 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +0000994 return TokError("unexpected token in '.org' directive");
995 }
996
Sean Callanan79ed1a82010-01-19 20:22:31 +0000997 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000998
999 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1000 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001001 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001002
1003 return false;
1004}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001005
1006/// ParseDirectiveAlign
1007/// ::= {.align, ...} expression [ , expression [ , expression ]]
1008bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001009 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001010 int64_t Alignment;
1011 if (ParseAbsoluteExpression(Alignment))
1012 return true;
1013
1014 SMLoc MaxBytesLoc;
1015 bool HasFillExpr = false;
1016 int64_t FillExpr = 0;
1017 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001018 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1019 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001020 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001021 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001022
1023 // The fill expression can be omitted while specifying a maximum number of
1024 // alignment bytes, e.g:
1025 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001026 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001027 HasFillExpr = true;
1028 if (ParseAbsoluteExpression(FillExpr))
1029 return true;
1030 }
1031
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001032 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1033 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001034 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001035 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001036
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001037 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001038 if (ParseAbsoluteExpression(MaxBytesToFill))
1039 return true;
1040
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001041 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001042 return TokError("unexpected token in directive");
1043 }
1044 }
1045
Sean Callanan79ed1a82010-01-19 20:22:31 +00001046 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001047
Daniel Dunbar648ac512010-05-17 21:54:30 +00001048 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001049 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001050
1051 // Compute alignment in bytes.
1052 if (IsPow2) {
1053 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001054 if (Alignment >= 32) {
1055 Error(AlignmentLoc, "invalid alignment value");
1056 Alignment = 31;
1057 }
1058
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001059 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001060 }
1061
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001062 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001063 if (MaxBytesLoc.isValid()) {
1064 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001065 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1066 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001067 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001068 }
1069
1070 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001071 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1072 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001073 MaxBytesToFill = 0;
1074 }
1075 }
1076
Daniel Dunbar648ac512010-05-17 21:54:30 +00001077 // Check whether we should use optimal code alignment for this .align
1078 // directive.
1079 //
1080 // FIXME: This should be using a target hook.
1081 bool UseCodeAlign = false;
1082 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001083 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001084 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1085 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1086 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001087 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001088 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001089 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001090 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001091 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001092
1093 return false;
1094}
1095
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001096/// ParseDirectiveSymbolAttribute
1097/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001098bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001099 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001100 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001101 StringRef Name;
1102
1103 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001104 return TokError("expected identifier in directive");
1105
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001106 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001107
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001108 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001109
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001110 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001111 break;
1112
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001113 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001114 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001115 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001116 }
1117 }
1118
Sean Callanan79ed1a82010-01-19 20:22:31 +00001119 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001120 return false;
1121}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001122
Matt Fleming924c5e52010-05-21 11:36:59 +00001123/// ParseDirectiveELFType
1124/// ::= .type identifier , @attribute
1125bool AsmParser::ParseDirectiveELFType() {
1126 StringRef Name;
1127 if (ParseIdentifier(Name))
1128 return TokError("expected identifier in directive");
1129
1130 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001131 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001132
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001133 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001134 return TokError("unexpected token in '.type' directive");
1135 Lex();
1136
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001137 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001138 return TokError("expected '@' before type");
1139 Lex();
1140
1141 StringRef Type;
1142 SMLoc TypeLoc;
1143
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001144 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001145 if (ParseIdentifier(Type))
1146 return TokError("expected symbol type in directive");
1147
1148 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1149 .Case("function", MCSA_ELF_TypeFunction)
1150 .Case("object", MCSA_ELF_TypeObject)
1151 .Case("tls_object", MCSA_ELF_TypeTLS)
1152 .Case("common", MCSA_ELF_TypeCommon)
1153 .Case("notype", MCSA_ELF_TypeNoType)
1154 .Default(MCSA_Invalid);
1155
1156 if (Attr == MCSA_Invalid)
1157 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1158
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001159 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001160 return TokError("unexpected token in '.type' directive");
1161
1162 Lex();
1163
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001164 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001165
1166 return false;
1167}
1168
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001169/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001170/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1171bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001172 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001173 StringRef Name;
1174 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001175 return TokError("expected identifier in directive");
1176
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001177 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001178 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001179
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001180 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001181 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001182 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001183
1184 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001185 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001186 if (ParseAbsoluteExpression(Size))
1187 return true;
1188
1189 int64_t Pow2Alignment = 0;
1190 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001191 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001192 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001193 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001194 if (ParseAbsoluteExpression(Pow2Alignment))
1195 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001196
1197 // If this target takes alignments in bytes (not log) validate and convert.
1198 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1199 if (!isPowerOf2_64(Pow2Alignment))
1200 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1201 Pow2Alignment = Log2_64(Pow2Alignment);
1202 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001203 }
1204
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001205 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001206 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001207
Sean Callanan79ed1a82010-01-19 20:22:31 +00001208 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001209
Chris Lattner1fc3d752009-07-09 17:25:12 +00001210 // NOTE: a size of zero for a .comm should create a undefined symbol
1211 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001212 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001213 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1214 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001215
Eric Christopherc260a3e2010-05-14 01:38:54 +00001216 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001217 // may internally end up wanting an alignment in bytes.
1218 // FIXME: Diagnose overflow.
1219 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001220 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1221 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001222
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001223 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001224 return Error(IDLoc, "invalid symbol redefinition");
1225
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001226 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001227 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001228 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001229 getStreamer().EmitZerofill(Ctx.getMachOSection(
1230 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1231 0, SectionKind::getBSS()),
1232 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001233 return false;
1234 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001235
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001236 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001237 return false;
1238}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001239
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001240/// ParseDirectiveAbort
1241/// ::= .abort [ "abort_string" ]
1242bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001243 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001244 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001245
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001246 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001247 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1248 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001249 return TokError("expected string in '.abort' directive");
1250
Sean Callanan18b83232010-01-19 21:44:56 +00001251 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001252
Sean Callanan79ed1a82010-01-19 20:22:31 +00001253 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001254 }
1255
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001256 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001257 return TokError("unexpected token in '.abort' directive");
1258
Sean Callanan79ed1a82010-01-19 20:22:31 +00001259 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001260
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001261 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001262 if (Str.empty())
1263 Error(Loc, ".abort detected. Assembly stopping.");
1264 else
1265 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001266
1267 return false;
1268}
Kevin Enderby71148242009-07-14 21:35:03 +00001269
Kevin Enderby1f049b22009-07-14 23:21:55 +00001270/// ParseDirectiveInclude
1271/// ::= .include "filename"
1272bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001273 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001274 return TokError("expected string in '.include' directive");
1275
Sean Callanan18b83232010-01-19 21:44:56 +00001276 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001277 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001278 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001279
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001280 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001281 return TokError("unexpected token in '.include' directive");
1282
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001283 // Strip the quotes.
1284 Filename = Filename.substr(1, Filename.size()-2);
1285
1286 // Attempt to switch the lexer to the included file before consuming the end
1287 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001288 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001289 PrintMessage(IncludeLoc,
1290 "Could not find include file '" + Filename + "'",
1291 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001292 return true;
1293 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001294
1295 return false;
1296}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001297
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001298/// ParseDirectiveIf
1299/// ::= .if expression
1300bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001301 TheCondStack.push_back(TheCondState);
1302 TheCondState.TheCond = AsmCond::IfCond;
1303 if(TheCondState.Ignore) {
1304 EatToEndOfStatement();
1305 }
1306 else {
1307 int64_t ExprValue;
1308 if (ParseAbsoluteExpression(ExprValue))
1309 return true;
1310
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001311 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001312 return TokError("unexpected token in '.if' directive");
1313
Sean Callanan79ed1a82010-01-19 20:22:31 +00001314 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001315
1316 TheCondState.CondMet = ExprValue;
1317 TheCondState.Ignore = !TheCondState.CondMet;
1318 }
1319
1320 return false;
1321}
1322
1323/// ParseDirectiveElseIf
1324/// ::= .elseif expression
1325bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1326 if (TheCondState.TheCond != AsmCond::IfCond &&
1327 TheCondState.TheCond != AsmCond::ElseIfCond)
1328 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1329 " an .elseif");
1330 TheCondState.TheCond = AsmCond::ElseIfCond;
1331
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001332 bool LastIgnoreState = false;
1333 if (!TheCondStack.empty())
1334 LastIgnoreState = TheCondStack.back().Ignore;
1335 if (LastIgnoreState || TheCondState.CondMet) {
1336 TheCondState.Ignore = true;
1337 EatToEndOfStatement();
1338 }
1339 else {
1340 int64_t ExprValue;
1341 if (ParseAbsoluteExpression(ExprValue))
1342 return true;
1343
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001344 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001345 return TokError("unexpected token in '.elseif' directive");
1346
Sean Callanan79ed1a82010-01-19 20:22:31 +00001347 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001348 TheCondState.CondMet = ExprValue;
1349 TheCondState.Ignore = !TheCondState.CondMet;
1350 }
1351
1352 return false;
1353}
1354
1355/// ParseDirectiveElse
1356/// ::= .else
1357bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001358 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001359 return TokError("unexpected token in '.else' directive");
1360
Sean Callanan79ed1a82010-01-19 20:22:31 +00001361 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001362
1363 if (TheCondState.TheCond != AsmCond::IfCond &&
1364 TheCondState.TheCond != AsmCond::ElseIfCond)
1365 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1366 ".elseif");
1367 TheCondState.TheCond = AsmCond::ElseCond;
1368 bool LastIgnoreState = false;
1369 if (!TheCondStack.empty())
1370 LastIgnoreState = TheCondStack.back().Ignore;
1371 if (LastIgnoreState || TheCondState.CondMet)
1372 TheCondState.Ignore = true;
1373 else
1374 TheCondState.Ignore = false;
1375
1376 return false;
1377}
1378
1379/// ParseDirectiveEndIf
1380/// ::= .endif
1381bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001382 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001383 return TokError("unexpected token in '.endif' directive");
1384
Sean Callanan79ed1a82010-01-19 20:22:31 +00001385 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001386
1387 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1388 TheCondStack.empty())
1389 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1390 ".else");
1391 if (!TheCondStack.empty()) {
1392 TheCondState = TheCondStack.back();
1393 TheCondStack.pop_back();
1394 }
1395
1396 return false;
1397}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001398
1399/// ParseDirectiveFile
1400/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001401bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001402 // FIXME: I'm not sure what this is.
1403 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001404 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001405 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001406 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001407
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001408 if (FileNumber < 1)
1409 return TokError("file number less than one");
1410 }
1411
Daniel Dunbareceec052010-07-12 17:45:27 +00001412 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001413 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001414
Chris Lattnerd32e8032010-01-25 19:02:58 +00001415 StringRef Filename = getTok().getString();
1416 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001417 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001418
Daniel Dunbareceec052010-07-12 17:45:27 +00001419 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001420 return TokError("unexpected token in '.file' directive");
1421
Chris Lattnerd32e8032010-01-25 19:02:58 +00001422 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001423 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001424 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001425 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1426
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001427 return false;
1428}
1429
1430/// ParseDirectiveLine
1431/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001432bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001433 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1434 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001435 return TokError("unexpected token in '.line' directive");
1436
Sean Callanan18b83232010-01-19 21:44:56 +00001437 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001438 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001439 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001440
1441 // FIXME: Do something with the .line.
1442 }
1443
Daniel Dunbareceec052010-07-12 17:45:27 +00001444 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001445 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001446
1447 return false;
1448}
1449
1450
1451/// ParseDirectiveLoc
1452/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001453bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001454 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001455 return TokError("unexpected token in '.loc' directive");
1456
1457 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001458 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001459 (void) FileNumber;
1460 // FIXME: Validate file.
1461
Sean Callanan79ed1a82010-01-19 20:22:31 +00001462 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001463 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1464 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001465 return TokError("unexpected token in '.loc' directive");
1466
Sean Callanan18b83232010-01-19 21:44:56 +00001467 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001468 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001469 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001470
Daniel Dunbareceec052010-07-12 17:45:27 +00001471 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1472 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001473 return TokError("unexpected token in '.loc' directive");
1474
Sean Callanan18b83232010-01-19 21:44:56 +00001475 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001476 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001477 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001478
1479 // FIXME: Do something with the .loc.
1480 }
1481 }
1482
Daniel Dunbareceec052010-07-12 17:45:27 +00001483 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001484 return TokError("unexpected token in '.file' directive");
1485
1486 return false;
1487}
1488