blob: 66786ad826653ac714d0e0806168cfe5999a31c8 [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 Dunbar7a56fc22010-07-12 20:08:04 +000021#include "llvm/MC/MCSectionELF.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000032namespace {
33
34/// \brief Generic implementations of directive handling, etc. which is shared
35/// (or the default, at least) for all assembler parser.
36class GenericAsmParser : public MCAsmParserExtension {
37public:
38 GenericAsmParser() {}
39
40 virtual void Initialize(MCAsmParser &Parser) {
41 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser);
43
44 // Debugging directives.
45 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
46 &GenericAsmParser::ParseDirectiveFile));
47 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
48 &GenericAsmParser::ParseDirectiveLine));
49 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
50 &GenericAsmParser::ParseDirectiveLoc));
51 }
52
53 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
54 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
55 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
56};
57
Daniel Dunbar7a56fc22010-07-12 20:08:04 +000058class ELFAsmParser : public MCAsmParserExtension {
59 bool ParseSectionSwitch(StringRef Section, unsigned Type,
60 unsigned Flags, SectionKind Kind);
61
62public:
63 ELFAsmParser() {}
64
65 virtual void Initialize(MCAsmParser &Parser) {
66 // Call the base implementation.
67 this->MCAsmParserExtension::Initialize(Parser);
68
69 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
70 &ELFAsmParser::ParseSectionDirectiveData));
71 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
72 &ELFAsmParser::ParseSectionDirectiveText));
73 }
74
75 bool ParseSectionDirectiveData(StringRef, SMLoc) {
76 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
77 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
78 SectionKind::getDataRel());
79 }
80 bool ParseSectionDirectiveText(StringRef, SMLoc) {
81 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
82 MCSectionELF::SHF_EXECINSTR |
83 MCSectionELF::SHF_ALLOC, SectionKind::getText());
84 }
85};
86
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000087}
88
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +000089namespace llvm {
90
91extern MCAsmParserExtension *createDarwinAsmParser();
92
93}
94
Chris Lattneraaec2052010-01-19 19:46:13 +000095enum { DEFAULT_ADDRSPACE = 0 };
96
Daniel Dunbar9186fa62010-07-01 20:41:56 +000097AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
98 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000099 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000100 GenericParser(new GenericAsmParser), PlatformParser(0),
101 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000102 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000103
104 // Initialize the generic parser.
105 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000106
107 // Initialize the platform / file format parser.
108 //
109 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
110 // created.
111 if (_MAI.hasSubsectionsViaSymbols()) {
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000112 PlatformParser = createDarwinAsmParser();
Daniel Dunbare4749702010-07-12 18:12:02 +0000113 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000114 } else {
115 PlatformParser = new ELFAsmParser;
116 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000117 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000118}
119
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000120AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000121 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000122 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000123}
124
Daniel Dunbar53131982010-07-12 17:27:45 +0000125void AsmParser::setTargetParser(TargetAsmParser &P) {
126 assert(!TargetParser && "Target parser is already initialized!");
127 TargetParser = &P;
128 TargetParser->Initialize(*this);
129}
130
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000131void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000132 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000133}
134
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000135bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000136 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000137 return true;
138}
139
Sean Callananbf2013e2010-01-20 23:19:55 +0000140void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
141 const char *Type) const {
142 SrcMgr.PrintMessage(Loc, Msg, Type);
143}
Sean Callananfd0b0282010-01-21 00:19:58 +0000144
145bool AsmParser::EnterIncludeFile(const std::string &Filename) {
146 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
147 if (NewBuf == -1)
148 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000149
Sean Callananfd0b0282010-01-21 00:19:58 +0000150 CurBuffer = NewBuf;
151
152 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
153
154 return false;
155}
156
157const AsmToken &AsmParser::Lex() {
158 const AsmToken *tok = &Lexer.Lex();
159
160 if (tok->is(AsmToken::Eof)) {
161 // If this is the end of an included file, pop the parent file off the
162 // include stack.
163 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
164 if (ParentIncludeLoc != SMLoc()) {
165 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
166 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
167 ParentIncludeLoc.getPointer());
168 tok = &Lexer.Lex();
169 }
170 }
171
172 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000173 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000174
Sean Callananfd0b0282010-01-21 00:19:58 +0000175 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000176}
177
Chris Lattner79180e22010-04-05 23:15:42 +0000178bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000179 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000180 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000181 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000182 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000183 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000184 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
185 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000186
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000187 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000188 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000189
Chris Lattnerb717fb02009-07-02 21:53:43 +0000190 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000191
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000192 AsmCond StartingCondState = TheCondState;
193
Chris Lattnerb717fb02009-07-02 21:53:43 +0000194 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000195 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000196 if (!ParseStatement()) continue;
197
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000198 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000199 HadError = true;
200 EatToEndOfStatement();
201 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000202
203 if (TheCondState.TheCond != StartingCondState.TheCond ||
204 TheCondState.Ignore != StartingCondState.Ignore)
205 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000206
Chris Lattner79180e22010-04-05 23:15:42 +0000207 // Finalize the output stream if there are no errors and if the client wants
208 // us to.
209 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000210 Out.Finish();
211
Chris Lattnerb717fb02009-07-02 21:53:43 +0000212 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000213}
214
Chris Lattner2cf5f142009-06-22 01:29:09 +0000215/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
216void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000217 while (Lexer.isNot(AsmToken::EndOfStatement) &&
218 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000219 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000220
221 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000222 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000223 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000224}
225
Chris Lattnerc4193832009-06-22 05:51:26 +0000226
Chris Lattner74ec1a32009-06-22 06:32:03 +0000227/// ParseParenExpr - Parse a paren expression and return it.
228/// NOTE: This assumes the leading '(' has already been consumed.
229///
230/// parenexpr ::= expr)
231///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000232bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000233 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000234 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000235 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000236 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000237 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000238 return false;
239}
Chris Lattnerc4193832009-06-22 05:51:26 +0000240
Chris Lattner74ec1a32009-06-22 06:32:03 +0000241/// ParsePrimaryExpr - Parse a primary expression and return it.
242/// primaryexpr ::= (parenexpr
243/// primaryexpr ::= symbol
244/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000245/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000246/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000247bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000248 switch (Lexer.getKind()) {
249 default:
250 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000251 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000252 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000253 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000254 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000255 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000256 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000257 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000258 case AsmToken::Identifier: {
259 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000260 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000261 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000262
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000263 // Mark the symbol as used in an expression.
264 Sym->setUsedInExpr(true);
265
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000266 // Lookup the symbol variant if used.
267 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
268 if (Split.first.size() != getTok().getIdentifier().size())
269 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
270
Chris Lattnerb4307b32010-01-15 19:28:38 +0000271 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000272 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000273
274 // If this is an absolute variable reference, substitute it now to preserve
275 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000276 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000277 if (Variant)
278 return Error(EndLoc, "unexpected modified on variable reference");
279
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000280 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000281 return false;
282 }
283
284 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000285 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000286 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000287 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000288 case AsmToken::Integer: {
289 SMLoc Loc = getTok().getLoc();
290 int64_t IntVal = getTok().getIntVal();
291 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000292 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000293 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000294 // Look for 'b' or 'f' following an Integer as a directional label
295 if (Lexer.getKind() == AsmToken::Identifier) {
296 StringRef IDVal = getTok().getString();
297 if (IDVal == "f" || IDVal == "b"){
298 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
299 IDVal == "f" ? 1 : 0);
300 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
301 getContext());
302 if(IDVal == "b" && Sym->isUndefined())
303 return Error(Loc, "invalid reference to undefined symbol");
304 EndLoc = Lexer.getLoc();
305 Lex(); // Eat identifier.
306 }
307 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000308 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000309 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000310 case AsmToken::Dot: {
311 // This is a '.' reference, which references the current PC. Emit a
312 // temporary label to the streamer and refer to it.
313 MCSymbol *Sym = Ctx.CreateTempSymbol();
314 Out.EmitLabel(Sym);
315 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
316 EndLoc = Lexer.getLoc();
317 Lex(); // Eat identifier.
318 return false;
319 }
320
Daniel Dunbar3f872332009-07-28 16:08:33 +0000321 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000322 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000323 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000324 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000325 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000326 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000327 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000328 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000329 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000330 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000331 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000332 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000334 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000336 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000337 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000338 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000339 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000340 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000342 }
343}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000344
Chris Lattnerb4307b32010-01-15 19:28:38 +0000345bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000346 SMLoc EndLoc;
347 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000348}
349
Chris Lattner74ec1a32009-06-22 06:32:03 +0000350/// ParseExpression - Parse an expression and return it.
351///
352/// expr ::= expr +,- expr -> lowest.
353/// expr ::= expr |,^,&,! expr -> middle.
354/// expr ::= expr *,/,%,<<,>> expr -> highest.
355/// expr ::= primaryexpr
356///
Chris Lattner54482b42010-01-15 19:39:23 +0000357bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000358 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000359 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000360 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
361 return true;
362
363 // Try to constant fold it up front, if possible.
364 int64_t Value;
365 if (Res->EvaluateAsAbsolute(Value))
366 Res = MCConstantExpr::Create(Value, getContext());
367
368 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000369}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000370
Chris Lattnerb4307b32010-01-15 19:28:38 +0000371bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000372 Res = 0;
373 return ParseParenExpr(Res, EndLoc) ||
374 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000375}
376
Daniel Dunbar475839e2009-06-29 20:37:27 +0000377bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000378 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000379
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000380 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000381 if (ParseExpression(Expr))
382 return true;
383
Daniel Dunbare00b0112009-10-16 01:57:52 +0000384 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000385 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000386
387 return false;
388}
389
Daniel Dunbar3f872332009-07-28 16:08:33 +0000390static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000391 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000392 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000393 default:
394 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000395
396 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000397 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000398 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000399 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000400 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000401 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000402 return 1;
403
404 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000405 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000407 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000408 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000409 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000410 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000412 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000413 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000414 case AsmToken::ExclaimEqual:
415 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000416 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000417 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000418 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000419 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000420 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000421 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000422 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000423 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000424 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000425 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000426 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000427 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000428 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000429 return 2;
430
431 // Intermediate Precedence: |, &, ^
432 //
433 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000434 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000435 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000436 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000437 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000438 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000439 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000440 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000441 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000442 return 3;
443
444 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000445 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000446 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000447 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000449 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000450 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000451 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000452 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000453 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000454 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000455 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000456 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000457 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000458 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000459 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000460 }
461}
462
463
464/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
465/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000466bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
467 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000468 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000469 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000470 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000471
472 // If the next token is lower precedence than we are allowed to eat, return
473 // successfully with what we ate already.
474 if (TokPrec < Precedence)
475 return false;
476
Sean Callanan79ed1a82010-01-19 20:22:31 +0000477 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000478
479 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000480 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000481 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000482
483 // If BinOp binds less tightly with RHS than the operator after RHS, let
484 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000485 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000486 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000487 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000488 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000489 }
490
Daniel Dunbar475839e2009-06-29 20:37:27 +0000491 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000492 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000493 }
494}
495
Chris Lattnerc4193832009-06-22 05:51:26 +0000496
497
498
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000499/// ParseStatement:
500/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000501/// ::= Label* Directive ...Operands... EndOfStatement
502/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000503bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000504 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000505 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000506 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000507 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000508 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000509
510 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000511 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000512 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000513 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000514 int64_t LocalLabelVal = -1;
515 // GUESS allow an integer followed by a ':' as a directional local label
516 if (Lexer.is(AsmToken::Integer)) {
517 LocalLabelVal = getTok().getIntVal();
518 if (LocalLabelVal < 0) {
519 if (!TheCondState.Ignore)
520 return TokError("unexpected token at start of statement");
521 IDVal = "";
522 }
523 else {
524 IDVal = getTok().getString();
525 Lex(); // Consume the integer token to be used as an identifier token.
526 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000527 if (!TheCondState.Ignore)
528 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000529 }
530 }
531 }
532 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000533 if (!TheCondState.Ignore)
534 return TokError("unexpected token at start of statement");
535 IDVal = "";
536 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000537
Chris Lattner7834fac2010-04-17 18:14:27 +0000538 // Handle conditional assembly here before checking for skipping. We
539 // have to do this so that .endif isn't skipped in a ".if 0" block for
540 // example.
541 if (IDVal == ".if")
542 return ParseDirectiveIf(IDLoc);
543 if (IDVal == ".elseif")
544 return ParseDirectiveElseIf(IDLoc);
545 if (IDVal == ".else")
546 return ParseDirectiveElse(IDLoc);
547 if (IDVal == ".endif")
548 return ParseDirectiveEndIf(IDLoc);
549
550 // If we are in a ".if 0" block, ignore this statement.
551 if (TheCondState.Ignore) {
552 EatToEndOfStatement();
553 return false;
554 }
555
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000556 // FIXME: Recurse on local labels?
557
558 // See what kind of statement we have.
559 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000560 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000561 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000562 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000563
564 // Diagnose attempt to use a variable as a label.
565 //
566 // FIXME: Diagnostics. Note the location of the definition as a label.
567 // FIXME: This doesn't diagnose assignment to a symbol which has been
568 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000569 MCSymbol *Sym;
570 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000571 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000572 else
573 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000574 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000575 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000576
Daniel Dunbar959fd882009-08-26 22:13:22 +0000577 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000578 Out.EmitLabel(Sym);
579
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000580 // Consume any end of statement token, if present, to avoid spurious
581 // AddBlankLine calls().
582 if (Lexer.is(AsmToken::EndOfStatement)) {
583 Lex();
584 if (Lexer.is(AsmToken::Eof))
585 return false;
586 }
587
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000588 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000589 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000590
Daniel Dunbar3f872332009-07-28 16:08:33 +0000591 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000592 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000593 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000594
Daniel Dunbare2ace502009-08-31 08:09:09 +0000595 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000596
597 default: // Normal instruction or directive.
598 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000599 }
600
601 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000602 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000603 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000604 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000605 return ParseDirectiveSet();
606
Daniel Dunbara0d14262009-06-24 23:30:00 +0000607 // Data directives
608
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000609 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000610 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000612 return ParseDirectiveAscii(true);
613
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000615 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000616 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000617 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000618 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000619 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000621 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000622
623 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000624 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000625 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000627 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000628 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000629 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000631 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000632 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000633 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000635 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000636 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000637 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000639 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
640
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000641 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000642 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000643
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000645 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000646 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000647 return ParseDirectiveSpace();
648
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000649 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000650
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000652 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000653 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000654 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000655 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000656 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000658 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000660 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000662 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000664 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000665 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000666 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000668 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000669 if (IDVal == ".type")
670 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000672 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000674 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000676 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000677 if (IDVal == ".weak_def_can_be_hidden")
678 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000679
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000680 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000681 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000683 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000684
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000685 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000686 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000688 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000689
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000690 // Look up the handler in the handler table.
691 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
692 DirectiveMap.lookup(IDVal);
693 if (Handler.first)
694 return (Handler.first->*Handler.second)(IDVal, IDLoc);
695
Kevin Enderby9c656452009-09-10 20:51:44 +0000696 // Target hook for parsing target specific directives.
697 if (!getTargetParser().ParseDirective(ID))
698 return false;
699
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000700 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000701 EatToEndOfStatement();
702 return false;
703 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000704
Chris Lattnera7f13542010-05-19 23:34:33 +0000705 // Canonicalize the opcode to lower case.
706 SmallString<128> Opcode;
707 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
708 Opcode.push_back(tolower(IDVal[i]));
709
Chris Lattner98986712010-01-14 22:21:20 +0000710 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000711 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000712 ParsedOperands);
713 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
714 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000715
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000716 // If parsing succeeded, match the instruction.
717 if (!HadError) {
718 MCInst Inst;
719 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
720 // Emit the instruction on success.
721 Out.EmitInstruction(Inst);
722 } else {
723 // Otherwise emit a diagnostic about the match failure and set the error
724 // flag.
725 //
726 // FIXME: We should give nicer diagnostics about the exact failure.
727 Error(IDLoc, "unrecognized instruction");
728 HadError = true;
729 }
730 }
Chris Lattner98986712010-01-14 22:21:20 +0000731
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000732 // If there was no error, consume the end-of-statement token. Otherwise this
733 // will be done by our caller.
734 if (!HadError)
735 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000736
737 // Free any parsed operands.
738 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
739 delete ParsedOperands[i];
740
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000741 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000742}
Chris Lattner9a023f72009-06-24 04:43:34 +0000743
Daniel Dunbare2ace502009-08-31 08:09:09 +0000744bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000745 // FIXME: Use better location, we should use proper tokens.
746 SMLoc EqualLoc = Lexer.getLoc();
747
Daniel Dunbar821e3332009-08-31 08:09:28 +0000748 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000749 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000750 return true;
751
Daniel Dunbar3f872332009-07-28 16:08:33 +0000752 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000753 return TokError("unexpected token in assignment");
754
755 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000756 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000757
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000758 // Validate that the LHS is allowed to be a variable (either it has not been
759 // used as a symbol, or it is an absolute symbol).
760 MCSymbol *Sym = getContext().LookupSymbol(Name);
761 if (Sym) {
762 // Diagnose assignment to a label.
763 //
764 // FIXME: Diagnostics. Note the location of the definition as a label.
765 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000766 if (Sym->isUndefined() && !Sym->isUsedInExpr())
767 ; // Allow redefinitions of undefined symbols only used in directives.
768 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000769 return Error(EqualLoc, "redefinition of '" + Name + "'");
770 else if (!Sym->isVariable())
771 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000772 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000773 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
774 Name + "'");
775 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000776 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000777
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000778 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000779
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000780 Sym->setUsedInExpr(true);
781
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000782 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000783 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000784
785 return false;
786}
787
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000788/// ParseIdentifier:
789/// ::= identifier
790/// ::= string
791bool AsmParser::ParseIdentifier(StringRef &Res) {
792 if (Lexer.isNot(AsmToken::Identifier) &&
793 Lexer.isNot(AsmToken::String))
794 return true;
795
Sean Callanan18b83232010-01-19 21:44:56 +0000796 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000797
Sean Callanan79ed1a82010-01-19 20:22:31 +0000798 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000799
800 return false;
801}
802
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000803/// ParseDirectiveSet:
804/// ::= .set identifier ',' expression
805bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000806 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000807
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000808 if (ParseIdentifier(Name))
809 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000810
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000811 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000812 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000813 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000814
Daniel Dunbare2ace502009-08-31 08:09:09 +0000815 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000816}
817
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000818bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
819 unsigned Flags, SectionKind Kind) {
820 if (getLexer().isNot(AsmToken::EndOfStatement))
821 return TokError("unexpected token in section switching directive");
822 Lex();
823
824 getStreamer().SwitchSection(getContext().getELFSection(
825 Section, Type, Flags, Kind));
826
827 return false;
828}
829
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000830bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000831 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000832
833 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000834 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000835 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
836 if (Str[i] != '\\') {
837 Data += Str[i];
838 continue;
839 }
840
841 // Recognize escaped characters. Note that this escape semantics currently
842 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
843 ++i;
844 if (i == e)
845 return TokError("unexpected backslash at end of string");
846
847 // Recognize octal sequences.
848 if ((unsigned) (Str[i] - '0') <= 7) {
849 // Consume up to three octal characters.
850 unsigned Value = Str[i] - '0';
851
852 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
853 ++i;
854 Value = Value * 8 + (Str[i] - '0');
855
856 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
857 ++i;
858 Value = Value * 8 + (Str[i] - '0');
859 }
860 }
861
862 if (Value > 255)
863 return TokError("invalid octal escape sequence (out of range)");
864
865 Data += (unsigned char) Value;
866 continue;
867 }
868
869 // Otherwise recognize individual escapes.
870 switch (Str[i]) {
871 default:
872 // Just reject invalid escape sequences for now.
873 return TokError("invalid escape sequence (unrecognized character)");
874
875 case 'b': Data += '\b'; break;
876 case 'f': Data += '\f'; break;
877 case 'n': Data += '\n'; break;
878 case 'r': Data += '\r'; break;
879 case 't': Data += '\t'; break;
880 case '"': Data += '"'; break;
881 case '\\': Data += '\\'; break;
882 }
883 }
884
885 return false;
886}
887
Daniel Dunbara0d14262009-06-24 23:30:00 +0000888/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +0000889/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +0000890bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000891 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000892 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000893 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000894 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000895
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000896 std::string Data;
897 if (ParseEscapedString(Data))
898 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000899
900 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000901 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000902 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
903
Sean Callanan79ed1a82010-01-19 20:22:31 +0000904 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000905
906 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000907 break;
908
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000909 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000910 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000911 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000912 }
913 }
914
Sean Callanan79ed1a82010-01-19 20:22:31 +0000915 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000916 return false;
917}
918
919/// ParseDirectiveValue
920/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
921bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000922 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +0000923 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +0000924 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000925 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000926 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000927 return true;
928
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000929 // Special case constant expressions to match code generator.
930 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000931 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +0000932 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000933 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000934
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000935 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000936 break;
937
938 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000939 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000940 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000941 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000942 }
943 }
944
Sean Callanan79ed1a82010-01-19 20:22:31 +0000945 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000946 return false;
947}
948
949/// ParseDirectiveSpace
950/// ::= .space expression [ , expression ]
951bool AsmParser::ParseDirectiveSpace() {
952 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000953 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000954 return true;
955
956 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000957 if (getLexer().isNot(AsmToken::EndOfStatement)) {
958 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000959 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000960 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000961
Daniel Dunbar475839e2009-06-29 20:37:27 +0000962 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963 return true;
964
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000965 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966 return TokError("unexpected token in '.space' directive");
967 }
968
Sean Callanan79ed1a82010-01-19 20:22:31 +0000969 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000970
971 if (NumBytes <= 0)
972 return TokError("invalid number of bytes in '.space' directive");
973
974 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000975 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +0000976
977 return false;
978}
979
980/// ParseDirectiveFill
981/// ::= .fill expression , expression , expression
982bool AsmParser::ParseDirectiveFill() {
983 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000984 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000985 return true;
986
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000987 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000988 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000989 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000990
991 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000992 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000993 return true;
994
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000995 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +0000996 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000997 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998
999 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001000 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001001 return true;
1002
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001003 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001004 return TokError("unexpected token in '.fill' directive");
1005
Sean Callanan79ed1a82010-01-19 20:22:31 +00001006 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001007
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001008 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1009 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001010
1011 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001012 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001013
1014 return false;
1015}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001016
1017/// ParseDirectiveOrg
1018/// ::= .org expression [ , expression ]
1019bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001020 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001021 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001022 return true;
1023
1024 // Parse optional fill expression.
1025 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001026 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1027 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001028 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001029 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001030
Daniel Dunbar475839e2009-06-29 20:37:27 +00001031 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001032 return true;
1033
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001034 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001035 return TokError("unexpected token in '.org' directive");
1036 }
1037
Sean Callanan79ed1a82010-01-19 20:22:31 +00001038 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001039
1040 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1041 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001042 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001043
1044 return false;
1045}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001046
1047/// ParseDirectiveAlign
1048/// ::= {.align, ...} expression [ , expression [ , expression ]]
1049bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001050 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001051 int64_t Alignment;
1052 if (ParseAbsoluteExpression(Alignment))
1053 return true;
1054
1055 SMLoc MaxBytesLoc;
1056 bool HasFillExpr = false;
1057 int64_t FillExpr = 0;
1058 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001059 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1060 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001061 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001062 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001063
1064 // The fill expression can be omitted while specifying a maximum number of
1065 // alignment bytes, e.g:
1066 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001067 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001068 HasFillExpr = true;
1069 if (ParseAbsoluteExpression(FillExpr))
1070 return true;
1071 }
1072
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001073 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1074 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001075 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001076 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001077
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001078 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001079 if (ParseAbsoluteExpression(MaxBytesToFill))
1080 return true;
1081
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001082 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001083 return TokError("unexpected token in directive");
1084 }
1085 }
1086
Sean Callanan79ed1a82010-01-19 20:22:31 +00001087 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001088
Daniel Dunbar648ac512010-05-17 21:54:30 +00001089 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001090 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001091
1092 // Compute alignment in bytes.
1093 if (IsPow2) {
1094 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001095 if (Alignment >= 32) {
1096 Error(AlignmentLoc, "invalid alignment value");
1097 Alignment = 31;
1098 }
1099
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001100 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001101 }
1102
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001103 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001104 if (MaxBytesLoc.isValid()) {
1105 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001106 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1107 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001108 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001109 }
1110
1111 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001112 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1113 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001114 MaxBytesToFill = 0;
1115 }
1116 }
1117
Daniel Dunbar648ac512010-05-17 21:54:30 +00001118 // Check whether we should use optimal code alignment for this .align
1119 // directive.
1120 //
1121 // FIXME: This should be using a target hook.
1122 bool UseCodeAlign = false;
1123 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001124 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001125 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1126 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1127 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001128 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001129 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001130 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001131 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001132 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001133
1134 return false;
1135}
1136
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001137/// ParseDirectiveSymbolAttribute
1138/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001139bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001140 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001141 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001142 StringRef Name;
1143
1144 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001145 return TokError("expected identifier in directive");
1146
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001147 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001148
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001149 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001150
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001151 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001152 break;
1153
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001154 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001155 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001156 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001157 }
1158 }
1159
Sean Callanan79ed1a82010-01-19 20:22:31 +00001160 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001161 return false;
1162}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001163
Matt Fleming924c5e52010-05-21 11:36:59 +00001164/// ParseDirectiveELFType
1165/// ::= .type identifier , @attribute
1166bool AsmParser::ParseDirectiveELFType() {
1167 StringRef Name;
1168 if (ParseIdentifier(Name))
1169 return TokError("expected identifier in directive");
1170
1171 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001172 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001173
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001174 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001175 return TokError("unexpected token in '.type' directive");
1176 Lex();
1177
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001178 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001179 return TokError("expected '@' before type");
1180 Lex();
1181
1182 StringRef Type;
1183 SMLoc TypeLoc;
1184
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001185 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001186 if (ParseIdentifier(Type))
1187 return TokError("expected symbol type in directive");
1188
1189 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1190 .Case("function", MCSA_ELF_TypeFunction)
1191 .Case("object", MCSA_ELF_TypeObject)
1192 .Case("tls_object", MCSA_ELF_TypeTLS)
1193 .Case("common", MCSA_ELF_TypeCommon)
1194 .Case("notype", MCSA_ELF_TypeNoType)
1195 .Default(MCSA_Invalid);
1196
1197 if (Attr == MCSA_Invalid)
1198 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1199
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001200 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001201 return TokError("unexpected token in '.type' directive");
1202
1203 Lex();
1204
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001205 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001206
1207 return false;
1208}
1209
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001210/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001211/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1212bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001213 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001214 StringRef Name;
1215 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001216 return TokError("expected identifier in directive");
1217
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001218 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001219 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001220
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001221 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001222 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001223 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001224
1225 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001226 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001227 if (ParseAbsoluteExpression(Size))
1228 return true;
1229
1230 int64_t Pow2Alignment = 0;
1231 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001232 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001233 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001234 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001235 if (ParseAbsoluteExpression(Pow2Alignment))
1236 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001237
1238 // If this target takes alignments in bytes (not log) validate and convert.
1239 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1240 if (!isPowerOf2_64(Pow2Alignment))
1241 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1242 Pow2Alignment = Log2_64(Pow2Alignment);
1243 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001244 }
1245
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001246 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001247 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001248
Sean Callanan79ed1a82010-01-19 20:22:31 +00001249 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001250
Chris Lattner1fc3d752009-07-09 17:25:12 +00001251 // NOTE: a size of zero for a .comm should create a undefined symbol
1252 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001253 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001254 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1255 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001256
Eric Christopherc260a3e2010-05-14 01:38:54 +00001257 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001258 // may internally end up wanting an alignment in bytes.
1259 // FIXME: Diagnose overflow.
1260 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001261 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1262 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001263
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001264 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001265 return Error(IDLoc, "invalid symbol redefinition");
1266
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001267 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001268 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001269 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001270 getStreamer().EmitZerofill(Ctx.getMachOSection(
1271 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1272 0, SectionKind::getBSS()),
1273 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001274 return false;
1275 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001276
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001277 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001278 return false;
1279}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001280
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001281/// ParseDirectiveAbort
1282/// ::= .abort [ "abort_string" ]
1283bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001284 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001285 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001286
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001287 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001288 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1289 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001290 return TokError("expected string in '.abort' directive");
1291
Sean Callanan18b83232010-01-19 21:44:56 +00001292 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001293
Sean Callanan79ed1a82010-01-19 20:22:31 +00001294 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001295 }
1296
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001297 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001298 return TokError("unexpected token in '.abort' directive");
1299
Sean Callanan79ed1a82010-01-19 20:22:31 +00001300 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001301
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001302 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001303 if (Str.empty())
1304 Error(Loc, ".abort detected. Assembly stopping.");
1305 else
1306 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001307
1308 return false;
1309}
Kevin Enderby71148242009-07-14 21:35:03 +00001310
Kevin Enderby1f049b22009-07-14 23:21:55 +00001311/// ParseDirectiveInclude
1312/// ::= .include "filename"
1313bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001314 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001315 return TokError("expected string in '.include' directive");
1316
Sean Callanan18b83232010-01-19 21:44:56 +00001317 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001318 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001319 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001320
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001321 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001322 return TokError("unexpected token in '.include' directive");
1323
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001324 // Strip the quotes.
1325 Filename = Filename.substr(1, Filename.size()-2);
1326
1327 // Attempt to switch the lexer to the included file before consuming the end
1328 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001329 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001330 PrintMessage(IncludeLoc,
1331 "Could not find include file '" + Filename + "'",
1332 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001333 return true;
1334 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001335
1336 return false;
1337}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001338
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001339/// ParseDirectiveIf
1340/// ::= .if expression
1341bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001342 TheCondStack.push_back(TheCondState);
1343 TheCondState.TheCond = AsmCond::IfCond;
1344 if(TheCondState.Ignore) {
1345 EatToEndOfStatement();
1346 }
1347 else {
1348 int64_t ExprValue;
1349 if (ParseAbsoluteExpression(ExprValue))
1350 return true;
1351
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001352 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001353 return TokError("unexpected token in '.if' directive");
1354
Sean Callanan79ed1a82010-01-19 20:22:31 +00001355 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001356
1357 TheCondState.CondMet = ExprValue;
1358 TheCondState.Ignore = !TheCondState.CondMet;
1359 }
1360
1361 return false;
1362}
1363
1364/// ParseDirectiveElseIf
1365/// ::= .elseif expression
1366bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1367 if (TheCondState.TheCond != AsmCond::IfCond &&
1368 TheCondState.TheCond != AsmCond::ElseIfCond)
1369 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1370 " an .elseif");
1371 TheCondState.TheCond = AsmCond::ElseIfCond;
1372
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001373 bool LastIgnoreState = false;
1374 if (!TheCondStack.empty())
1375 LastIgnoreState = TheCondStack.back().Ignore;
1376 if (LastIgnoreState || TheCondState.CondMet) {
1377 TheCondState.Ignore = true;
1378 EatToEndOfStatement();
1379 }
1380 else {
1381 int64_t ExprValue;
1382 if (ParseAbsoluteExpression(ExprValue))
1383 return true;
1384
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001385 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001386 return TokError("unexpected token in '.elseif' directive");
1387
Sean Callanan79ed1a82010-01-19 20:22:31 +00001388 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001389 TheCondState.CondMet = ExprValue;
1390 TheCondState.Ignore = !TheCondState.CondMet;
1391 }
1392
1393 return false;
1394}
1395
1396/// ParseDirectiveElse
1397/// ::= .else
1398bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001400 return TokError("unexpected token in '.else' directive");
1401
Sean Callanan79ed1a82010-01-19 20:22:31 +00001402 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001403
1404 if (TheCondState.TheCond != AsmCond::IfCond &&
1405 TheCondState.TheCond != AsmCond::ElseIfCond)
1406 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1407 ".elseif");
1408 TheCondState.TheCond = AsmCond::ElseCond;
1409 bool LastIgnoreState = false;
1410 if (!TheCondStack.empty())
1411 LastIgnoreState = TheCondStack.back().Ignore;
1412 if (LastIgnoreState || TheCondState.CondMet)
1413 TheCondState.Ignore = true;
1414 else
1415 TheCondState.Ignore = false;
1416
1417 return false;
1418}
1419
1420/// ParseDirectiveEndIf
1421/// ::= .endif
1422bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001423 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001424 return TokError("unexpected token in '.endif' directive");
1425
Sean Callanan79ed1a82010-01-19 20:22:31 +00001426 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001427
1428 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1429 TheCondStack.empty())
1430 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1431 ".else");
1432 if (!TheCondStack.empty()) {
1433 TheCondState = TheCondStack.back();
1434 TheCondStack.pop_back();
1435 }
1436
1437 return false;
1438}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001439
1440/// ParseDirectiveFile
1441/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001442bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001443 // FIXME: I'm not sure what this is.
1444 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001445 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001446 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001447 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001448
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001449 if (FileNumber < 1)
1450 return TokError("file number less than one");
1451 }
1452
Daniel Dunbareceec052010-07-12 17:45:27 +00001453 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001454 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001455
Chris Lattnerd32e8032010-01-25 19:02:58 +00001456 StringRef Filename = getTok().getString();
1457 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001458 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001459
Daniel Dunbareceec052010-07-12 17:45:27 +00001460 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001461 return TokError("unexpected token in '.file' directive");
1462
Chris Lattnerd32e8032010-01-25 19:02:58 +00001463 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001464 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001465 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001466 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1467
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001468 return false;
1469}
1470
1471/// ParseDirectiveLine
1472/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001473bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001474 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1475 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001476 return TokError("unexpected token in '.line' directive");
1477
Sean Callanan18b83232010-01-19 21:44:56 +00001478 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001479 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001480 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001481
1482 // FIXME: Do something with the .line.
1483 }
1484
Daniel Dunbareceec052010-07-12 17:45:27 +00001485 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00001486 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001487
1488 return false;
1489}
1490
1491
1492/// ParseDirectiveLoc
1493/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001494bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001495 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001496 return TokError("unexpected token in '.loc' directive");
1497
1498 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001499 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001500 (void) FileNumber;
1501 // FIXME: Validate file.
1502
Sean Callanan79ed1a82010-01-19 20:22:31 +00001503 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001504 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1505 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001506 return TokError("unexpected token in '.loc' directive");
1507
Sean Callanan18b83232010-01-19 21:44:56 +00001508 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001509 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001510 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001511
Daniel Dunbareceec052010-07-12 17:45:27 +00001512 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1513 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001514 return TokError("unexpected token in '.loc' directive");
1515
Sean Callanan18b83232010-01-19 21:44:56 +00001516 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001517 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001518 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001519
1520 // FIXME: Do something with the .loc.
1521 }
1522 }
1523
Daniel Dunbareceec052010-07-12 17:45:27 +00001524 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001525 return TokError("unexpected token in '.file' directive");
1526
1527 return false;
1528}
1529