blob: 3af962330d78cee643f7c85c7d58e34225628ec9 [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"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000022#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000027#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000028#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000030#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000031using namespace llvm;
32
Chris Lattneraaec2052010-01-19 19:46:13 +000033
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000034namespace {
35
36/// \brief Generic implementations of directive handling, etc. which is shared
37/// (or the default, at least) for all assembler parser.
38class GenericAsmParser : public MCAsmParserExtension {
39public:
40 GenericAsmParser() {}
41
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
45
46 // Debugging directives.
47 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
48 &GenericAsmParser::ParseDirectiveFile));
49 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
50 &GenericAsmParser::ParseDirectiveLine));
51 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
52 &GenericAsmParser::ParseDirectiveLoc));
53 }
54
55 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
56 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
57 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
58};
59
Daniel Dunbare4749702010-07-12 18:12:02 +000060/// \brief Implementation of directive handling which is shared across all
61/// Darwin targets.
62class DarwinAsmParser : public MCAsmParserExtension {
63public:
64 DarwinAsmParser() {}
65
66 virtual void Initialize(MCAsmParser &Parser) {
67 // Call the base implementation.
68 this->MCAsmParserExtension::Initialize(Parser);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000069
Daniel Dunbar492b7a22010-07-12 19:22:53 +000070 Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler(
71 &DarwinAsmParser::ParseDirectiveDesc));
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000072 Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler(
73 &DarwinAsmParser::ParseDirectiveLsym));
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000074 Parser.AddDirectiveHandler(this, ".subsections_via_symbols",
75 MCAsmParser::DirectiveHandler(
76 &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols));
77 Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler(
78 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
79 Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler(
80 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
81 Parser.AddDirectiveHandler(this, ".secure_log_unique",
82 MCAsmParser::DirectiveHandler(
83 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
84 Parser.AddDirectiveHandler(this, ".secure_log_reset",
85 MCAsmParser::DirectiveHandler(
86 &DarwinAsmParser::ParseDirectiveSecureLogReset));
Daniel Dunbarb6c3a602010-07-12 19:37:35 +000087 Parser.AddDirectiveHandler(this, ".tbss",
88 MCAsmParser::DirectiveHandler(
89 &DarwinAsmParser::ParseDirectiveTBSS));
90 Parser.AddDirectiveHandler(this, ".zerofill",
91 MCAsmParser::DirectiveHandler(
92 &DarwinAsmParser::ParseDirectiveZerofill));
Daniel Dunbare4749702010-07-12 18:12:02 +000093 }
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000094
Daniel Dunbar492b7a22010-07-12 19:22:53 +000095 bool ParseDirectiveDesc(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000096 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000097 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000098 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000099 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
100 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
Daniel Dunbarb6c3a602010-07-12 19:37:35 +0000101 bool ParseDirectiveTBSS(StringRef, SMLoc);
102 bool ParseDirectiveZerofill(StringRef, SMLoc);
Daniel Dunbare4749702010-07-12 18:12:02 +0000103};
104
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000105class ELFAsmParser : public MCAsmParserExtension {
106 bool ParseSectionSwitch(StringRef Section, unsigned Type,
107 unsigned Flags, SectionKind Kind);
108
109public:
110 ELFAsmParser() {}
111
112 virtual void Initialize(MCAsmParser &Parser) {
113 // Call the base implementation.
114 this->MCAsmParserExtension::Initialize(Parser);
115
116 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
117 &ELFAsmParser::ParseSectionDirectiveData));
118 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
119 &ELFAsmParser::ParseSectionDirectiveText));
120 }
121
122 bool ParseSectionDirectiveData(StringRef, SMLoc) {
123 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
124 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
125 SectionKind::getDataRel());
126 }
127 bool ParseSectionDirectiveText(StringRef, SMLoc) {
128 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
129 MCSectionELF::SHF_EXECINSTR |
130 MCSectionELF::SHF_ALLOC, SectionKind::getText());
131 }
132};
133
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000134}
135
Chris Lattneraaec2052010-01-19 19:46:13 +0000136enum { DEFAULT_ADDRSPACE = 0 };
137
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000138AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
139 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000140 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000141 GenericParser(new GenericAsmParser), PlatformParser(0),
142 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000143 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000144
145 // Initialize the generic parser.
146 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000147
148 // Initialize the platform / file format parser.
149 //
150 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
151 // created.
152 if (_MAI.hasSubsectionsViaSymbols()) {
153 PlatformParser = new DarwinAsmParser;
154 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000155 } else {
156 PlatformParser = new ELFAsmParser;
157 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000158 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000159}
160
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000161AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000162 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000163 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000164}
165
Daniel Dunbar53131982010-07-12 17:27:45 +0000166void AsmParser::setTargetParser(TargetAsmParser &P) {
167 assert(!TargetParser && "Target parser is already initialized!");
168 TargetParser = &P;
169 TargetParser->Initialize(*this);
170}
171
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000172void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000173 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000174}
175
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000176bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000177 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000178 return true;
179}
180
Sean Callananbf2013e2010-01-20 23:19:55 +0000181void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
182 const char *Type) const {
183 SrcMgr.PrintMessage(Loc, Msg, Type);
184}
Sean Callananfd0b0282010-01-21 00:19:58 +0000185
186bool AsmParser::EnterIncludeFile(const std::string &Filename) {
187 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
188 if (NewBuf == -1)
189 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000190
Sean Callananfd0b0282010-01-21 00:19:58 +0000191 CurBuffer = NewBuf;
192
193 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
194
195 return false;
196}
197
198const AsmToken &AsmParser::Lex() {
199 const AsmToken *tok = &Lexer.Lex();
200
201 if (tok->is(AsmToken::Eof)) {
202 // If this is the end of an included file, pop the parent file off the
203 // include stack.
204 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
205 if (ParentIncludeLoc != SMLoc()) {
206 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
207 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
208 ParentIncludeLoc.getPointer());
209 tok = &Lexer.Lex();
210 }
211 }
212
213 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000214 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000215
Sean Callananfd0b0282010-01-21 00:19:58 +0000216 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000217}
218
Chris Lattner79180e22010-04-05 23:15:42 +0000219bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000220 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000221 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000222 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000223 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000224 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000225 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
226 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000227
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000228 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000229 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000230
Chris Lattnerb717fb02009-07-02 21:53:43 +0000231 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000232
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000233 AsmCond StartingCondState = TheCondState;
234
Chris Lattnerb717fb02009-07-02 21:53:43 +0000235 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000236 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000237 if (!ParseStatement()) continue;
238
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000239 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000240 HadError = true;
241 EatToEndOfStatement();
242 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000243
244 if (TheCondState.TheCond != StartingCondState.TheCond ||
245 TheCondState.Ignore != StartingCondState.Ignore)
246 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000247
Chris Lattner79180e22010-04-05 23:15:42 +0000248 // Finalize the output stream if there are no errors and if the client wants
249 // us to.
250 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000251 Out.Finish();
252
Chris Lattnerb717fb02009-07-02 21:53:43 +0000253 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000254}
255
Chris Lattner2cf5f142009-06-22 01:29:09 +0000256/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
257void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000258 while (Lexer.isNot(AsmToken::EndOfStatement) &&
259 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000260 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000261
262 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000263 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000264 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000265}
266
Chris Lattnerc4193832009-06-22 05:51:26 +0000267
Chris Lattner74ec1a32009-06-22 06:32:03 +0000268/// ParseParenExpr - Parse a paren expression and return it.
269/// NOTE: This assumes the leading '(' has already been consumed.
270///
271/// parenexpr ::= expr)
272///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000273bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000274 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000275 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000276 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000277 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000278 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000279 return false;
280}
Chris Lattnerc4193832009-06-22 05:51:26 +0000281
Chris Lattner74ec1a32009-06-22 06:32:03 +0000282/// ParsePrimaryExpr - Parse a primary expression and return it.
283/// primaryexpr ::= (parenexpr
284/// primaryexpr ::= symbol
285/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000286/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000287/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000288bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000289 switch (Lexer.getKind()) {
290 default:
291 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000292 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000293 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000294 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000295 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000296 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000297 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000298 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000299 case AsmToken::Identifier: {
300 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000301 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000302 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000303
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000304 // Mark the symbol as used in an expression.
305 Sym->setUsedInExpr(true);
306
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000307 // Lookup the symbol variant if used.
308 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
309 if (Split.first.size() != getTok().getIdentifier().size())
310 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
311
Chris Lattnerb4307b32010-01-15 19:28:38 +0000312 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000313 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000314
315 // If this is an absolute variable reference, substitute it now to preserve
316 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000317 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000318 if (Variant)
319 return Error(EndLoc, "unexpected modified on variable reference");
320
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000321 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000322 return false;
323 }
324
325 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000326 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000327 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000328 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000329 case AsmToken::Integer: {
330 SMLoc Loc = getTok().getLoc();
331 int64_t IntVal = getTok().getIntVal();
332 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000333 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000334 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000335 // Look for 'b' or 'f' following an Integer as a directional label
336 if (Lexer.getKind() == AsmToken::Identifier) {
337 StringRef IDVal = getTok().getString();
338 if (IDVal == "f" || IDVal == "b"){
339 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
340 IDVal == "f" ? 1 : 0);
341 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
342 getContext());
343 if(IDVal == "b" && Sym->isUndefined())
344 return Error(Loc, "invalid reference to undefined symbol");
345 EndLoc = Lexer.getLoc();
346 Lex(); // Eat identifier.
347 }
348 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000349 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000350 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000351 case AsmToken::Dot: {
352 // This is a '.' reference, which references the current PC. Emit a
353 // temporary label to the streamer and refer to it.
354 MCSymbol *Sym = Ctx.CreateTempSymbol();
355 Out.EmitLabel(Sym);
356 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
357 EndLoc = Lexer.getLoc();
358 Lex(); // Eat identifier.
359 return false;
360 }
361
Daniel Dunbar3f872332009-07-28 16:08:33 +0000362 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000363 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000364 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000365 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000366 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000367 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000368 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000369 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000370 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000372 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000373 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000374 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000375 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000377 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000378 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000379 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000380 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000381 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000382 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000383 }
384}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000385
Chris Lattnerb4307b32010-01-15 19:28:38 +0000386bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000387 SMLoc EndLoc;
388 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000389}
390
Chris Lattner74ec1a32009-06-22 06:32:03 +0000391/// ParseExpression - Parse an expression and return it.
392///
393/// expr ::= expr +,- expr -> lowest.
394/// expr ::= expr |,^,&,! expr -> middle.
395/// expr ::= expr *,/,%,<<,>> expr -> highest.
396/// expr ::= primaryexpr
397///
Chris Lattner54482b42010-01-15 19:39:23 +0000398bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000399 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000401 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
402 return true;
403
404 // Try to constant fold it up front, if possible.
405 int64_t Value;
406 if (Res->EvaluateAsAbsolute(Value))
407 Res = MCConstantExpr::Create(Value, getContext());
408
409 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000410}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000411
Chris Lattnerb4307b32010-01-15 19:28:38 +0000412bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000413 Res = 0;
414 return ParseParenExpr(Res, EndLoc) ||
415 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000416}
417
Daniel Dunbar475839e2009-06-29 20:37:27 +0000418bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000419 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000420
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000421 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000422 if (ParseExpression(Expr))
423 return true;
424
Daniel Dunbare00b0112009-10-16 01:57:52 +0000425 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000426 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000427
428 return false;
429}
430
Daniel Dunbar3f872332009-07-28 16:08:33 +0000431static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000432 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000433 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000434 default:
435 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000436
437 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000438 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000439 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000440 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000441 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000442 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000443 return 1;
444
445 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000446 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000447 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000448 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000449 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000450 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000451 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000452 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000453 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000454 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000455 case AsmToken::ExclaimEqual:
456 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000457 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000458 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000459 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000460 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000461 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000463 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000464 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000465 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000466 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000467 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000468 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000469 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000470 return 2;
471
472 // Intermediate Precedence: |, &, ^
473 //
474 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000475 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000476 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000477 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000478 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000479 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000480 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000481 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000482 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000483 return 3;
484
485 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000486 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000487 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000488 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000489 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000490 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000491 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000492 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000493 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000494 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000495 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000496 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000497 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000498 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000499 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000500 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000501 }
502}
503
504
505/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
506/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000507bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
508 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000509 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000510 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000511 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000512
513 // If the next token is lower precedence than we are allowed to eat, return
514 // successfully with what we ate already.
515 if (TokPrec < Precedence)
516 return false;
517
Sean Callanan79ed1a82010-01-19 20:22:31 +0000518 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000519
520 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000521 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000522 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000523
524 // If BinOp binds less tightly with RHS than the operator after RHS, let
525 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000526 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000527 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000528 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000529 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000530 }
531
Daniel Dunbar475839e2009-06-29 20:37:27 +0000532 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000533 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000534 }
535}
536
Chris Lattnerc4193832009-06-22 05:51:26 +0000537
538
539
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000540/// ParseStatement:
541/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000542/// ::= Label* Directive ...Operands... EndOfStatement
543/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000544bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000545 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000546 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000547 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000548 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000549 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000550
551 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000552 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000553 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000554 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000555 int64_t LocalLabelVal = -1;
556 // GUESS allow an integer followed by a ':' as a directional local label
557 if (Lexer.is(AsmToken::Integer)) {
558 LocalLabelVal = getTok().getIntVal();
559 if (LocalLabelVal < 0) {
560 if (!TheCondState.Ignore)
561 return TokError("unexpected token at start of statement");
562 IDVal = "";
563 }
564 else {
565 IDVal = getTok().getString();
566 Lex(); // Consume the integer token to be used as an identifier token.
567 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000568 if (!TheCondState.Ignore)
569 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000570 }
571 }
572 }
573 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000574 if (!TheCondState.Ignore)
575 return TokError("unexpected token at start of statement");
576 IDVal = "";
577 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000578
Chris Lattner7834fac2010-04-17 18:14:27 +0000579 // Handle conditional assembly here before checking for skipping. We
580 // have to do this so that .endif isn't skipped in a ".if 0" block for
581 // example.
582 if (IDVal == ".if")
583 return ParseDirectiveIf(IDLoc);
584 if (IDVal == ".elseif")
585 return ParseDirectiveElseIf(IDLoc);
586 if (IDVal == ".else")
587 return ParseDirectiveElse(IDLoc);
588 if (IDVal == ".endif")
589 return ParseDirectiveEndIf(IDLoc);
590
591 // If we are in a ".if 0" block, ignore this statement.
592 if (TheCondState.Ignore) {
593 EatToEndOfStatement();
594 return false;
595 }
596
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000597 // FIXME: Recurse on local labels?
598
599 // See what kind of statement we have.
600 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000601 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000602 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000603 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000604
605 // Diagnose attempt to use a variable as a label.
606 //
607 // FIXME: Diagnostics. Note the location of the definition as a label.
608 // FIXME: This doesn't diagnose assignment to a symbol which has been
609 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000610 MCSymbol *Sym;
611 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000612 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000613 else
614 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000615 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000616 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000617
Daniel Dunbar959fd882009-08-26 22:13:22 +0000618 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000619 Out.EmitLabel(Sym);
620
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000621 // Consume any end of statement token, if present, to avoid spurious
622 // AddBlankLine calls().
623 if (Lexer.is(AsmToken::EndOfStatement)) {
624 Lex();
625 if (Lexer.is(AsmToken::Eof))
626 return false;
627 }
628
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000629 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000630 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000631
Daniel Dunbar3f872332009-07-28 16:08:33 +0000632 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000633 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000634 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000635
Daniel Dunbare2ace502009-08-31 08:09:09 +0000636 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000637
638 default: // Normal instruction or directive.
639 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000640 }
641
642 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000643 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000644 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000646 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000647 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000648 // FIXME: This changes behavior based on the -static flag to the
649 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000650 return ParseDirectiveSectionSwitch("__TEXT", "__text",
651 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000652 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000653 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000655 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000656 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000657 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
658 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000659 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000660 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000661 MCSectionMachO::S_4BYTE_LITERALS,
662 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000664 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000665 MCSectionMachO::S_8BYTE_LITERALS,
666 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000667 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000668 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000669 MCSectionMachO::S_16BYTE_LITERALS,
670 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000671 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000672 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000674 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000676 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000678 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
679
680 // FIXME: The assembler manual claims that this has the self modify code
681 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000682 if (IDVal == ".symbol_stub")
683 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
684 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000685 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
686 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000687 0, 16);
688 // FIXME: PowerPC only?
689 if (IDVal == ".picsymbol_stub")
690 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
691 MCSectionMachO::S_SYMBOL_STUBS |
692 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
693 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000695 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000697 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
698
699 // FIXME: The section names of these two are misspelled in the assembler
700 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000701 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000702 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
703 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
704 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000706 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
707 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
708 4);
709
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000711 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000713 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000714 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
715 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000716 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000717 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000718 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
719 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000721 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000722
723
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000724 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000725 return ParseDirectiveSectionSwitch("__OBJC", "__class",
726 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000728 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
729 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000730 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000731 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
732 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000734 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
735 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000736 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000737 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
738 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000739 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000740 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
741 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000743 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
744 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000745 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000746 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
747 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000749 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
750 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
751 MCSectionMachO::S_LITERAL_POINTERS,
752 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000754 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
755 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
756 MCSectionMachO::S_LITERAL_POINTERS,
757 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000758 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000759 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
760 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000761 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000762 return ParseDirectiveSectionSwitch("__OBJC", "__category",
763 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000765 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
766 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000767 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000768 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
769 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000770 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000771 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
772 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000773 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000774 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
775 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000776 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000777 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
778 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000779 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000780 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
781 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000782 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000783 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
784 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000785
Eric Christopherc6177a42010-05-17 22:53:55 +0000786 if (IDVal == ".tdata")
787 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
788 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
789 if (IDVal == ".tlv")
790 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
791 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
792 if (IDVal == ".thread_init_func")
793 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
794 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
795
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000796 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000797 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000798 return ParseDirectiveSet();
799
Daniel Dunbara0d14262009-06-24 23:30:00 +0000800 // Data directives
801
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000802 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000803 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000805 return ParseDirectiveAscii(true);
806
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000807 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000808 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000809 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000810 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000811 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000812 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000813 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000814 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000815
816 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000817 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000818 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000819 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000820 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000821 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000822 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000823 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000824 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000825 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000826 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000827 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000828 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000829 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000830 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000832 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
833
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000834 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000835 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000836
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000837 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000838 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000839 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000840 return ParseDirectiveSpace();
841
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000842 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000843
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000844 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000845 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000846 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000847 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000848 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000849 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000850 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000851 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000852 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000853 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000854 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000855 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000856 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000857 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000858 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000859 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000860 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000861 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000862 if (IDVal == ".type")
863 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000864 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000865 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000866 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000867 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000868 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000869 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000870 if (IDVal == ".weak_def_can_be_hidden")
871 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000872
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000873 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000874 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000875 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000876 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000877
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000878 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000879 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000880 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000881 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000882
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000883 // Look up the handler in the handler table.
884 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
885 DirectiveMap.lookup(IDVal);
886 if (Handler.first)
887 return (Handler.first->*Handler.second)(IDVal, IDLoc);
888
Kevin Enderby9c656452009-09-10 20:51:44 +0000889 // Target hook for parsing target specific directives.
890 if (!getTargetParser().ParseDirective(ID))
891 return false;
892
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000893 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000894 EatToEndOfStatement();
895 return false;
896 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000897
Chris Lattnera7f13542010-05-19 23:34:33 +0000898 // Canonicalize the opcode to lower case.
899 SmallString<128> Opcode;
900 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
901 Opcode.push_back(tolower(IDVal[i]));
902
Chris Lattner98986712010-01-14 22:21:20 +0000903 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000904 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000905 ParsedOperands);
906 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
907 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000908
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000909 // If parsing succeeded, match the instruction.
910 if (!HadError) {
911 MCInst Inst;
912 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
913 // Emit the instruction on success.
914 Out.EmitInstruction(Inst);
915 } else {
916 // Otherwise emit a diagnostic about the match failure and set the error
917 // flag.
918 //
919 // FIXME: We should give nicer diagnostics about the exact failure.
920 Error(IDLoc, "unrecognized instruction");
921 HadError = true;
922 }
923 }
Chris Lattner98986712010-01-14 22:21:20 +0000924
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000925 // If there was no error, consume the end-of-statement token. Otherwise this
926 // will be done by our caller.
927 if (!HadError)
928 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000929
930 // Free any parsed operands.
931 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
932 delete ParsedOperands[i];
933
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000934 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000935}
Chris Lattner9a023f72009-06-24 04:43:34 +0000936
Daniel Dunbare2ace502009-08-31 08:09:09 +0000937bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000938 // FIXME: Use better location, we should use proper tokens.
939 SMLoc EqualLoc = Lexer.getLoc();
940
Daniel Dunbar821e3332009-08-31 08:09:28 +0000941 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000942 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000943 return true;
944
Daniel Dunbar3f872332009-07-28 16:08:33 +0000945 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000946 return TokError("unexpected token in assignment");
947
948 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000949 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000950
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000951 // Validate that the LHS is allowed to be a variable (either it has not been
952 // used as a symbol, or it is an absolute symbol).
953 MCSymbol *Sym = getContext().LookupSymbol(Name);
954 if (Sym) {
955 // Diagnose assignment to a label.
956 //
957 // FIXME: Diagnostics. Note the location of the definition as a label.
958 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000959 if (Sym->isUndefined() && !Sym->isUsedInExpr())
960 ; // Allow redefinitions of undefined symbols only used in directives.
961 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000962 return Error(EqualLoc, "redefinition of '" + Name + "'");
963 else if (!Sym->isVariable())
964 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000965 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000966 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
967 Name + "'");
968 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000969 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000970
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000971 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000972
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000973 Sym->setUsedInExpr(true);
974
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000975 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000976 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000977
978 return false;
979}
980
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000981/// ParseIdentifier:
982/// ::= identifier
983/// ::= string
984bool AsmParser::ParseIdentifier(StringRef &Res) {
985 if (Lexer.isNot(AsmToken::Identifier) &&
986 Lexer.isNot(AsmToken::String))
987 return true;
988
Sean Callanan18b83232010-01-19 21:44:56 +0000989 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000990
Sean Callanan79ed1a82010-01-19 20:22:31 +0000991 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000992
993 return false;
994}
995
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000996/// ParseDirectiveSet:
997/// ::= .set identifier ',' expression
998bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000999 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001000
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001001 if (ParseIdentifier(Name))
1002 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001003
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001004 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001005 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001006 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001007
Daniel Dunbare2ace502009-08-31 08:09:09 +00001008 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001009}
1010
Chris Lattner9a023f72009-06-24 04:43:34 +00001011/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +00001012/// ::= .section identifier (',' identifier)*
1013/// FIXME: This should actually parse out the segment, section, attributes and
1014/// sizeof_stub fields.
1015bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001016 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001017
Daniel Dunbarace63122009-08-11 03:42:33 +00001018 StringRef SectionName;
1019 if (ParseIdentifier(SectionName))
1020 return Error(Loc, "expected identifier after '.section' directive");
1021
1022 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001023 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +00001024 return TokError("unexpected token in '.section' directive");
1025
Chris Lattnerff4bc462009-08-10 01:39:42 +00001026 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +00001027 SectionSpec += ",";
1028
1029 // Add all the tokens until the end of the line, ParseSectionSpecifier will
1030 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +00001031 StringRef EOL = Lexer.LexUntilEndOfStatement();
1032 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +00001033
Sean Callanan79ed1a82010-01-19 20:22:31 +00001034 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001035 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +00001036 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001037 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +00001038
Chris Lattnerff4bc462009-08-10 01:39:42 +00001039
1040 StringRef Segment, Section;
1041 unsigned TAA, StubSize;
1042 std::string ErrorStr =
1043 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1044 TAA, StubSize);
1045
1046 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001047 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +00001048
Chris Lattner56594f92009-07-31 17:47:16 +00001049 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001050 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001051 getStreamer().SwitchSection(Ctx.getMachOSection(
1052 Segment, Section, TAA, StubSize,
1053 isText ? SectionKind::getText()
1054 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001055 return false;
1056}
1057
Chris Lattnere15c2d72009-08-10 18:05:55 +00001058/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +00001059bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
1060 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +00001061 unsigned TAA, unsigned Align,
1062 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001063 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001064 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001065 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +00001066
Chris Lattner56594f92009-07-31 17:47:16 +00001067 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001068 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001069 getStreamer().SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001070 isText ? SectionKind::getText()
1071 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001072
1073 // Set the implicit alignment, if any.
1074 //
1075 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1076 // alignment on the section (e.g., if one manually inserts bytes into the
1077 // section, then just issueing the section switch directive will not realign
1078 // the section. However, this is arguably more reasonable behavior, and there
1079 // is no good reason for someone to intentionally emit incorrectly sized
1080 // values into the implicitly aligned sections.
1081 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001082 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001083
Chris Lattner529fb542009-06-24 05:13:15 +00001084 return false;
1085}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001086
Daniel Dunbar7a56fc22010-07-12 20:08:04 +00001087bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
1088 unsigned Flags, SectionKind Kind) {
1089 if (getLexer().isNot(AsmToken::EndOfStatement))
1090 return TokError("unexpected token in section switching directive");
1091 Lex();
1092
1093 getStreamer().SwitchSection(getContext().getELFSection(
1094 Section, Type, Flags, Kind));
1095
1096 return false;
1097}
1098
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001099bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001100 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001101
1102 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001103 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001104 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1105 if (Str[i] != '\\') {
1106 Data += Str[i];
1107 continue;
1108 }
1109
1110 // Recognize escaped characters. Note that this escape semantics currently
1111 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1112 ++i;
1113 if (i == e)
1114 return TokError("unexpected backslash at end of string");
1115
1116 // Recognize octal sequences.
1117 if ((unsigned) (Str[i] - '0') <= 7) {
1118 // Consume up to three octal characters.
1119 unsigned Value = Str[i] - '0';
1120
1121 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1122 ++i;
1123 Value = Value * 8 + (Str[i] - '0');
1124
1125 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1126 ++i;
1127 Value = Value * 8 + (Str[i] - '0');
1128 }
1129 }
1130
1131 if (Value > 255)
1132 return TokError("invalid octal escape sequence (out of range)");
1133
1134 Data += (unsigned char) Value;
1135 continue;
1136 }
1137
1138 // Otherwise recognize individual escapes.
1139 switch (Str[i]) {
1140 default:
1141 // Just reject invalid escape sequences for now.
1142 return TokError("invalid escape sequence (unrecognized character)");
1143
1144 case 'b': Data += '\b'; break;
1145 case 'f': Data += '\f'; break;
1146 case 'n': Data += '\n'; break;
1147 case 'r': Data += '\r'; break;
1148 case 't': Data += '\t'; break;
1149 case '"': Data += '"'; break;
1150 case '\\': Data += '\\'; break;
1151 }
1152 }
1153
1154 return false;
1155}
1156
Daniel Dunbara0d14262009-06-24 23:30:00 +00001157/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001158/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001159bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001160 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001161 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001162 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001163 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001164
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001165 std::string Data;
1166 if (ParseEscapedString(Data))
1167 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001168
1169 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001171 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1172
Sean Callanan79ed1a82010-01-19 20:22:31 +00001173 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001174
1175 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001176 break;
1177
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001178 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001179 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001180 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001181 }
1182 }
1183
Sean Callanan79ed1a82010-01-19 20:22:31 +00001184 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001185 return false;
1186}
1187
1188/// ParseDirectiveValue
1189/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1190bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001191 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001192 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001193 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001194 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001195 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001196 return true;
1197
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001198 // Special case constant expressions to match code generator.
1199 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001200 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001201 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001202 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001203
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001204 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001205 break;
1206
1207 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001208 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001209 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001210 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001211 }
1212 }
1213
Sean Callanan79ed1a82010-01-19 20:22:31 +00001214 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001215 return false;
1216}
1217
1218/// ParseDirectiveSpace
1219/// ::= .space expression [ , expression ]
1220bool AsmParser::ParseDirectiveSpace() {
1221 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001222 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001223 return true;
1224
1225 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001226 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1227 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001228 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001229 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001230
Daniel Dunbar475839e2009-06-29 20:37:27 +00001231 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001232 return true;
1233
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001234 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001235 return TokError("unexpected token in '.space' directive");
1236 }
1237
Sean Callanan79ed1a82010-01-19 20:22:31 +00001238 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001239
1240 if (NumBytes <= 0)
1241 return TokError("invalid number of bytes in '.space' directive");
1242
1243 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001244 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001245
1246 return false;
1247}
1248
1249/// ParseDirectiveFill
1250/// ::= .fill expression , expression , expression
1251bool AsmParser::ParseDirectiveFill() {
1252 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001253 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001254 return true;
1255
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001256 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001257 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001258 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001259
1260 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001261 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001262 return true;
1263
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001264 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001265 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001266 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001267
1268 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001269 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001270 return true;
1271
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001272 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001273 return TokError("unexpected token in '.fill' directive");
1274
Sean Callanan79ed1a82010-01-19 20:22:31 +00001275 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001276
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001277 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1278 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001279
1280 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001281 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001282
1283 return false;
1284}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001285
1286/// ParseDirectiveOrg
1287/// ::= .org expression [ , expression ]
1288bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001289 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001290 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001291 return true;
1292
1293 // Parse optional fill expression.
1294 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001295 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1296 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001297 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001298 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001299
Daniel Dunbar475839e2009-06-29 20:37:27 +00001300 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001301 return true;
1302
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001303 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001304 return TokError("unexpected token in '.org' directive");
1305 }
1306
Sean Callanan79ed1a82010-01-19 20:22:31 +00001307 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001308
1309 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1310 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001311 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001312
1313 return false;
1314}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001315
1316/// ParseDirectiveAlign
1317/// ::= {.align, ...} expression [ , expression [ , expression ]]
1318bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001319 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001320 int64_t Alignment;
1321 if (ParseAbsoluteExpression(Alignment))
1322 return true;
1323
1324 SMLoc MaxBytesLoc;
1325 bool HasFillExpr = false;
1326 int64_t FillExpr = 0;
1327 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001328 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1329 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001330 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001331 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001332
1333 // The fill expression can be omitted while specifying a maximum number of
1334 // alignment bytes, e.g:
1335 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001336 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001337 HasFillExpr = true;
1338 if (ParseAbsoluteExpression(FillExpr))
1339 return true;
1340 }
1341
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001342 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1343 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001344 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001345 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001346
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001347 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001348 if (ParseAbsoluteExpression(MaxBytesToFill))
1349 return true;
1350
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001351 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001352 return TokError("unexpected token in directive");
1353 }
1354 }
1355
Sean Callanan79ed1a82010-01-19 20:22:31 +00001356 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001357
Daniel Dunbar648ac512010-05-17 21:54:30 +00001358 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001359 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001360
1361 // Compute alignment in bytes.
1362 if (IsPow2) {
1363 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001364 if (Alignment >= 32) {
1365 Error(AlignmentLoc, "invalid alignment value");
1366 Alignment = 31;
1367 }
1368
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001369 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001370 }
1371
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001372 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001373 if (MaxBytesLoc.isValid()) {
1374 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001375 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1376 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001377 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001378 }
1379
1380 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001381 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1382 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001383 MaxBytesToFill = 0;
1384 }
1385 }
1386
Daniel Dunbar648ac512010-05-17 21:54:30 +00001387 // Check whether we should use optimal code alignment for this .align
1388 // directive.
1389 //
1390 // FIXME: This should be using a target hook.
1391 bool UseCodeAlign = false;
1392 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001393 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001394 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1395 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1396 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001397 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001398 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001399 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001400 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001401 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001402
1403 return false;
1404}
1405
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001406/// ParseDirectiveSymbolAttribute
1407/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001408bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001409 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001410 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001411 StringRef Name;
1412
1413 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001414 return TokError("expected identifier in directive");
1415
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001416 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001417
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001418 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001419
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001420 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001421 break;
1422
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001423 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001424 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001425 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001426 }
1427 }
1428
Sean Callanan79ed1a82010-01-19 20:22:31 +00001429 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001430 return false;
1431}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001432
Matt Fleming924c5e52010-05-21 11:36:59 +00001433/// ParseDirectiveELFType
1434/// ::= .type identifier , @attribute
1435bool AsmParser::ParseDirectiveELFType() {
1436 StringRef Name;
1437 if (ParseIdentifier(Name))
1438 return TokError("expected identifier in directive");
1439
1440 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001441 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001442
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001443 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001444 return TokError("unexpected token in '.type' directive");
1445 Lex();
1446
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001447 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001448 return TokError("expected '@' before type");
1449 Lex();
1450
1451 StringRef Type;
1452 SMLoc TypeLoc;
1453
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001454 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001455 if (ParseIdentifier(Type))
1456 return TokError("expected symbol type in directive");
1457
1458 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1459 .Case("function", MCSA_ELF_TypeFunction)
1460 .Case("object", MCSA_ELF_TypeObject)
1461 .Case("tls_object", MCSA_ELF_TypeTLS)
1462 .Case("common", MCSA_ELF_TypeCommon)
1463 .Case("notype", MCSA_ELF_TypeNoType)
1464 .Default(MCSA_Invalid);
1465
1466 if (Attr == MCSA_Invalid)
1467 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1468
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001469 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001470 return TokError("unexpected token in '.type' directive");
1471
1472 Lex();
1473
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001474 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001475
1476 return false;
1477}
1478
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001479/// ParseDirectiveDesc
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001480/// ::= .desc identifier , expression
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001481bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001482 StringRef Name;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001483 if (getParser().ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001484 return TokError("expected identifier in directive");
1485
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001486 // Handle the identifier as the key symbol.
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001487 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001488
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001489 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001490 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001491 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001492
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001493 int64_t DescValue;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001494 if (getParser().ParseAbsoluteExpression(DescValue))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001495 return true;
1496
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001497 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001498 return TokError("unexpected token in '.desc' directive");
1499
Sean Callanan79ed1a82010-01-19 20:22:31 +00001500 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001501
1502 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001503 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001504
1505 return false;
1506}
1507
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001508/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001509/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1510bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001511 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001512 StringRef Name;
1513 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001514 return TokError("expected identifier in directive");
1515
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001516 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001517 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001518
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001519 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001520 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001521 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001522
1523 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001524 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001525 if (ParseAbsoluteExpression(Size))
1526 return true;
1527
1528 int64_t Pow2Alignment = 0;
1529 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001530 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001531 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001532 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001533 if (ParseAbsoluteExpression(Pow2Alignment))
1534 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001535
1536 // If this target takes alignments in bytes (not log) validate and convert.
1537 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1538 if (!isPowerOf2_64(Pow2Alignment))
1539 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1540 Pow2Alignment = Log2_64(Pow2Alignment);
1541 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001542 }
1543
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001544 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001545 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001546
Sean Callanan79ed1a82010-01-19 20:22:31 +00001547 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001548
Chris Lattner1fc3d752009-07-09 17:25:12 +00001549 // NOTE: a size of zero for a .comm should create a undefined symbol
1550 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001551 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001552 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1553 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001554
Eric Christopherc260a3e2010-05-14 01:38:54 +00001555 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001556 // may internally end up wanting an alignment in bytes.
1557 // FIXME: Diagnose overflow.
1558 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001559 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1560 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001561
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001562 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001563 return Error(IDLoc, "invalid symbol redefinition");
1564
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001565 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001566 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001567 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001568 getStreamer().EmitZerofill(Ctx.getMachOSection(
1569 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1570 0, SectionKind::getBSS()),
1571 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001572 return false;
1573 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001574
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001575 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001576 return false;
1577}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001578
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001579/// ParseDirectiveZerofill
Chris Lattner9be3fee2009-07-10 22:20:30 +00001580/// ::= .zerofill segname , sectname [, identifier , size_expression [
1581/// , align_expression ]]
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001582bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001583 StringRef Segment;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001584 if (getParser().ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001585 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001586
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001587 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001588 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001589 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001590
1591 StringRef Section;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001592 if (getParser().ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001593 return TokError("expected section name after comma in '.zerofill' "
1594 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001595
Chris Lattner9be3fee2009-07-10 22:20:30 +00001596 // If this is the end of the line all that was wanted was to create the
1597 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001598 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001599 // Create the zerofill section but no symbol
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001600 getStreamer().EmitZerofill(getContext().getMachOSection(
1601 Segment, Section, MCSectionMachO::S_ZEROFILL,
1602 0, SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001603 return false;
1604 }
1605
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001606 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001607 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001608 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001609
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001610 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001611 StringRef IDStr;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001612 if (getParser().ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001613 return TokError("expected identifier in directive");
1614
1615 // handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001616 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001617
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001618 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001619 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001620 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001621
1622 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001623 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001624 if (getParser().ParseAbsoluteExpression(Size))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001625 return true;
1626
1627 int64_t Pow2Alignment = 0;
1628 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001629 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001630 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001631 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001632 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001633 return true;
1634 }
1635
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001636 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001637 return TokError("unexpected token in '.zerofill' directive");
1638
Sean Callanan79ed1a82010-01-19 20:22:31 +00001639 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001640
1641 if (Size < 0)
1642 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1643 "than zero");
1644
Eric Christopherc260a3e2010-05-14 01:38:54 +00001645 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001646 // may internally end up wanting an alignment in bytes.
1647 // FIXME: Diagnose overflow.
1648 if (Pow2Alignment < 0)
1649 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1650 "can't be less than zero");
1651
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001652 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001653 return Error(IDLoc, "invalid symbol redefinition");
1654
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001655 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001656 //
1657 // FIXME: Arch specific.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001658 getStreamer().EmitZerofill(getContext().getMachOSection(
1659 Segment, Section, MCSectionMachO::S_ZEROFILL,
1660 0, SectionKind::getBSS()),
1661 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001662
1663 return false;
1664}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001665
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001666/// ParseDirectiveTBSS
Eric Christopher482eba02010-05-14 01:50:28 +00001667/// ::= .tbss identifier, size, align
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001668bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001669 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001670 StringRef Name;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001671 if (getParser().ParseIdentifier(Name))
Eric Christopher482eba02010-05-14 01:50:28 +00001672 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001673
Eric Christopher482eba02010-05-14 01:50:28 +00001674 // Handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001675 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001676
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001677 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001678 return TokError("unexpected token in directive");
1679 Lex();
1680
1681 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001682 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001683 if (getParser().ParseAbsoluteExpression(Size))
Eric Christopher482eba02010-05-14 01:50:28 +00001684 return true;
1685
1686 int64_t Pow2Alignment = 0;
1687 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001688 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001689 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001690 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001691 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Eric Christopher482eba02010-05-14 01:50:28 +00001692 return true;
1693 }
1694
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001695 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001696 return TokError("unexpected token in '.tbss' directive");
1697
1698 Lex();
1699
1700 if (Size < 0)
1701 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1702 "zero");
1703
1704 // FIXME: Diagnose overflow.
1705 if (Pow2Alignment < 0)
1706 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1707 "than zero");
1708
1709 if (!Sym->isUndefined())
1710 return Error(IDLoc, "invalid symbol redefinition");
1711
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001712 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001713 "__DATA", "__thread_bss",
1714 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1715 0, SectionKind::getThreadBSS()),
1716 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001717
1718 return false;
1719}
1720
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001721/// ParseDirectiveSubsectionsViaSymbols
Kevin Enderbya5c78322009-07-13 21:03:15 +00001722/// ::= .subsections_via_symbols
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001723bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001724 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001725 return TokError("unexpected token in '.subsections_via_symbols' directive");
1726
Sean Callanan79ed1a82010-01-19 20:22:31 +00001727 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001728
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001729 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001730
1731 return false;
1732}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001733
1734/// ParseDirectiveAbort
1735/// ::= .abort [ "abort_string" ]
1736bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001737 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001738 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001739
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001740 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001741 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1742 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001743 return TokError("expected string in '.abort' directive");
1744
Sean Callanan18b83232010-01-19 21:44:56 +00001745 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001746
Sean Callanan79ed1a82010-01-19 20:22:31 +00001747 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001748 }
1749
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001750 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001751 return TokError("unexpected token in '.abort' directive");
1752
Sean Callanan79ed1a82010-01-19 20:22:31 +00001753 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001754
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001755 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001756 if (Str.empty())
1757 Error(Loc, ".abort detected. Assembly stopping.");
1758 else
1759 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001760
1761 return false;
1762}
Kevin Enderby71148242009-07-14 21:35:03 +00001763
1764/// ParseDirectiveLsym
1765/// ::= .lsym identifier , expression
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001766bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001767 StringRef Name;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001768 if (getParser().ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001769 return TokError("expected identifier in directive");
1770
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001771 // Handle the identifier as the key symbol.
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001772 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001773
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001774 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001775 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001776 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001777
Daniel Dunbar821e3332009-08-31 08:09:28 +00001778 const MCExpr *Value;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001779 if (getParser().ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001780 return true;
1781
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001782 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001783 return TokError("unexpected token in '.lsym' directive");
1784
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001786
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001787 // We don't currently support this directive.
1788 //
1789 // FIXME: Diagnostic location!
1790 (void) Sym;
1791 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001792}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001793
1794/// ParseDirectiveInclude
1795/// ::= .include "filename"
1796bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001797 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001798 return TokError("expected string in '.include' directive");
1799
Sean Callanan18b83232010-01-19 21:44:56 +00001800 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001801 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001802 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001803
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001804 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001805 return TokError("unexpected token in '.include' directive");
1806
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001807 // Strip the quotes.
1808 Filename = Filename.substr(1, Filename.size()-2);
1809
1810 // Attempt to switch the lexer to the included file before consuming the end
1811 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001812 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001813 PrintMessage(IncludeLoc,
1814 "Could not find include file '" + Filename + "'",
1815 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001816 return true;
1817 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001818
1819 return false;
1820}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001821
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001822/// ParseDirectiveDumpOrLoad
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001823/// ::= ( .dump | .load ) "filename"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001824bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1825 SMLoc IDLoc) {
1826 bool IsDump = Directive == ".dump";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001827 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001828 return TokError("expected string in '.dump' or '.load' directive");
1829
Sean Callanan79ed1a82010-01-19 20:22:31 +00001830 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001831
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001833 return TokError("unexpected token in '.dump' or '.load' directive");
1834
Sean Callanan79ed1a82010-01-19 20:22:31 +00001835 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001836
Kevin Enderby5026ae42009-07-20 20:25:37 +00001837 // FIXME: If/when .dump and .load are implemented they will be done in the
1838 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001839 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001840 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001841 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001842 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001843
1844 return false;
1845}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001846
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001847/// ParseDirectiveSecureLogUnique
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001848/// ::= .secure_log_unique "log message"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001849bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001850 std::string LogMessage;
1851
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001852 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001853 LogMessage = "";
1854 else{
1855 LogMessage = getTok().getString();
1856 Lex();
1857 }
1858
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001859 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001860 return TokError("unexpected token in '.secure_log_unique' directive");
1861
1862 if (getContext().getSecureLogUsed() != false)
1863 return Error(IDLoc, ".secure_log_unique specified multiple times");
1864
1865 char *SecureLogFile = getContext().getSecureLogFile();
1866 if (SecureLogFile == NULL)
1867 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1868 "environment variable unset.");
1869
1870 raw_ostream *OS = getContext().getSecureLog();
1871 if (OS == NULL) {
1872 std::string Err;
1873 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1874 if (!Err.empty()) {
1875 delete OS;
1876 return Error(IDLoc, Twine("can't open secure log file: ") +
1877 SecureLogFile + " (" + Err + ")");
1878 }
1879 getContext().setSecureLog(OS);
1880 }
1881
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001882 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
1883 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
1884 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001885 << LogMessage + "\n";
1886
1887 getContext().setSecureLogUsed(true);
1888
1889 return false;
1890}
1891
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001892/// ParseDirectiveSecureLogReset
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001893/// ::= .secure_log_reset
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001894bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001895 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001896 return TokError("unexpected token in '.secure_log_reset' directive");
1897
1898 Lex();
1899
1900 getContext().setSecureLogUsed(false);
1901
1902 return false;
1903}
1904
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001905/// ParseDirectiveIf
1906/// ::= .if expression
1907bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001908 TheCondStack.push_back(TheCondState);
1909 TheCondState.TheCond = AsmCond::IfCond;
1910 if(TheCondState.Ignore) {
1911 EatToEndOfStatement();
1912 }
1913 else {
1914 int64_t ExprValue;
1915 if (ParseAbsoluteExpression(ExprValue))
1916 return true;
1917
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001918 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001919 return TokError("unexpected token in '.if' directive");
1920
Sean Callanan79ed1a82010-01-19 20:22:31 +00001921 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001922
1923 TheCondState.CondMet = ExprValue;
1924 TheCondState.Ignore = !TheCondState.CondMet;
1925 }
1926
1927 return false;
1928}
1929
1930/// ParseDirectiveElseIf
1931/// ::= .elseif expression
1932bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1933 if (TheCondState.TheCond != AsmCond::IfCond &&
1934 TheCondState.TheCond != AsmCond::ElseIfCond)
1935 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1936 " an .elseif");
1937 TheCondState.TheCond = AsmCond::ElseIfCond;
1938
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001939 bool LastIgnoreState = false;
1940 if (!TheCondStack.empty())
1941 LastIgnoreState = TheCondStack.back().Ignore;
1942 if (LastIgnoreState || TheCondState.CondMet) {
1943 TheCondState.Ignore = true;
1944 EatToEndOfStatement();
1945 }
1946 else {
1947 int64_t ExprValue;
1948 if (ParseAbsoluteExpression(ExprValue))
1949 return true;
1950
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001951 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001952 return TokError("unexpected token in '.elseif' directive");
1953
Sean Callanan79ed1a82010-01-19 20:22:31 +00001954 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001955 TheCondState.CondMet = ExprValue;
1956 TheCondState.Ignore = !TheCondState.CondMet;
1957 }
1958
1959 return false;
1960}
1961
1962/// ParseDirectiveElse
1963/// ::= .else
1964bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001965 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001966 return TokError("unexpected token in '.else' directive");
1967
Sean Callanan79ed1a82010-01-19 20:22:31 +00001968 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001969
1970 if (TheCondState.TheCond != AsmCond::IfCond &&
1971 TheCondState.TheCond != AsmCond::ElseIfCond)
1972 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1973 ".elseif");
1974 TheCondState.TheCond = AsmCond::ElseCond;
1975 bool LastIgnoreState = false;
1976 if (!TheCondStack.empty())
1977 LastIgnoreState = TheCondStack.back().Ignore;
1978 if (LastIgnoreState || TheCondState.CondMet)
1979 TheCondState.Ignore = true;
1980 else
1981 TheCondState.Ignore = false;
1982
1983 return false;
1984}
1985
1986/// ParseDirectiveEndIf
1987/// ::= .endif
1988bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001989 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001990 return TokError("unexpected token in '.endif' directive");
1991
Sean Callanan79ed1a82010-01-19 20:22:31 +00001992 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001993
1994 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1995 TheCondStack.empty())
1996 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1997 ".else");
1998 if (!TheCondStack.empty()) {
1999 TheCondState = TheCondStack.back();
2000 TheCondStack.pop_back();
2001 }
2002
2003 return false;
2004}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002005
2006/// ParseDirectiveFile
2007/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002008bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002009 // FIXME: I'm not sure what this is.
2010 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00002011 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002012 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002013 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002014
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002015 if (FileNumber < 1)
2016 return TokError("file number less than one");
2017 }
2018
Daniel Dunbareceec052010-07-12 17:45:27 +00002019 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002020 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002021
Chris Lattnerd32e8032010-01-25 19:02:58 +00002022 StringRef Filename = getTok().getString();
2023 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002024 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002025
Daniel Dunbareceec052010-07-12 17:45:27 +00002026 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002027 return TokError("unexpected token in '.file' directive");
2028
Chris Lattnerd32e8032010-01-25 19:02:58 +00002029 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002030 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00002031 else
Daniel Dunbareceec052010-07-12 17:45:27 +00002032 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
2033
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002034 return false;
2035}
2036
2037/// ParseDirectiveLine
2038/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002039bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002040 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2041 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002042 return TokError("unexpected token in '.line' directive");
2043
Sean Callanan18b83232010-01-19 21:44:56 +00002044 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002045 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002046 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002047
2048 // FIXME: Do something with the .line.
2049 }
2050
Daniel Dunbareceec052010-07-12 17:45:27 +00002051 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002052 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002053
2054 return false;
2055}
2056
2057
2058/// ParseDirectiveLoc
2059/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002060bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002061 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002062 return TokError("unexpected token in '.loc' directive");
2063
2064 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002065 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002066 (void) FileNumber;
2067 // FIXME: Validate file.
2068
Sean Callanan79ed1a82010-01-19 20:22:31 +00002069 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002070 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2071 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002072 return TokError("unexpected token in '.loc' directive");
2073
Sean Callanan18b83232010-01-19 21:44:56 +00002074 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002075 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002076 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002077
Daniel Dunbareceec052010-07-12 17:45:27 +00002078 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2079 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002080 return TokError("unexpected token in '.loc' directive");
2081
Sean Callanan18b83232010-01-19 21:44:56 +00002082 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002083 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002084 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002085
2086 // FIXME: Do something with the .loc.
2087 }
2088 }
2089
Daniel Dunbareceec052010-07-12 17:45:27 +00002090 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002091 return TokError("unexpected token in '.file' directive");
2092
2093 return false;
2094}
2095