blob: 1328ae28ba4d0ea532ee4ffd52e27d349d1d044e [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000021#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000022#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000026#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000028#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000029#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000030using namespace llvm;
31
Chris Lattneraaec2052010-01-19 19:46:13 +000032
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000033namespace {
34
35/// \brief Generic implementations of directive handling, etc. which is shared
36/// (or the default, at least) for all assembler parser.
37class GenericAsmParser : public MCAsmParserExtension {
38public:
39 GenericAsmParser() {}
40
41 virtual void Initialize(MCAsmParser &Parser) {
42 // Call the base implementation.
43 this->MCAsmParserExtension::Initialize(Parser);
44
45 // Debugging directives.
46 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
47 &GenericAsmParser::ParseDirectiveFile));
48 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
49 &GenericAsmParser::ParseDirectiveLine));
50 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
51 &GenericAsmParser::ParseDirectiveLoc));
52 }
53
54 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
55 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
56 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
57};
58
Daniel Dunbare4749702010-07-12 18:12:02 +000059/// \brief Implementation of directive handling which is shared across all
60/// Darwin targets.
61class DarwinAsmParser : public MCAsmParserExtension {
62public:
63 DarwinAsmParser() {}
64
65 virtual void Initialize(MCAsmParser &Parser) {
66 // Call the base implementation.
67 this->MCAsmParserExtension::Initialize(Parser);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000068
Daniel Dunbar492b7a22010-07-12 19:22:53 +000069 Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler(
70 &DarwinAsmParser::ParseDirectiveDesc));
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000071 Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler(
72 &DarwinAsmParser::ParseDirectiveLsym));
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000073 Parser.AddDirectiveHandler(this, ".subsections_via_symbols",
74 MCAsmParser::DirectiveHandler(
75 &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols));
76 Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler(
77 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
78 Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler(
79 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
80 Parser.AddDirectiveHandler(this, ".secure_log_unique",
81 MCAsmParser::DirectiveHandler(
82 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
83 Parser.AddDirectiveHandler(this, ".secure_log_reset",
84 MCAsmParser::DirectiveHandler(
85 &DarwinAsmParser::ParseDirectiveSecureLogReset));
Daniel Dunbarb6c3a602010-07-12 19:37:35 +000086 Parser.AddDirectiveHandler(this, ".tbss",
87 MCAsmParser::DirectiveHandler(
88 &DarwinAsmParser::ParseDirectiveTBSS));
89 Parser.AddDirectiveHandler(this, ".zerofill",
90 MCAsmParser::DirectiveHandler(
91 &DarwinAsmParser::ParseDirectiveZerofill));
Daniel Dunbare4749702010-07-12 18:12:02 +000092 }
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000093
Daniel Dunbar492b7a22010-07-12 19:22:53 +000094 bool ParseDirectiveDesc(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000095 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000096 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000097 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000098 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
99 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
Daniel Dunbarb6c3a602010-07-12 19:37:35 +0000100 bool ParseDirectiveTBSS(StringRef, SMLoc);
101 bool ParseDirectiveZerofill(StringRef, SMLoc);
Daniel Dunbare4749702010-07-12 18:12:02 +0000102};
103
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000104}
105
Chris Lattneraaec2052010-01-19 19:46:13 +0000106enum { DEFAULT_ADDRSPACE = 0 };
107
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000108AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
109 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000110 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000111 GenericParser(new GenericAsmParser), PlatformParser(0),
112 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000113 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000114
115 // Initialize the generic parser.
116 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000117
118 // Initialize the platform / file format parser.
119 //
120 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
121 // created.
122 if (_MAI.hasSubsectionsViaSymbols()) {
123 PlatformParser = new DarwinAsmParser;
124 PlatformParser->Initialize(*this);
125 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000126}
127
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000128AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000129 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000130 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000131}
132
Daniel Dunbar53131982010-07-12 17:27:45 +0000133void AsmParser::setTargetParser(TargetAsmParser &P) {
134 assert(!TargetParser && "Target parser is already initialized!");
135 TargetParser = &P;
136 TargetParser->Initialize(*this);
137}
138
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000139void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000140 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000141}
142
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000143bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000144 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000145 return true;
146}
147
Sean Callananbf2013e2010-01-20 23:19:55 +0000148void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
149 const char *Type) const {
150 SrcMgr.PrintMessage(Loc, Msg, Type);
151}
Sean Callananfd0b0282010-01-21 00:19:58 +0000152
153bool AsmParser::EnterIncludeFile(const std::string &Filename) {
154 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
155 if (NewBuf == -1)
156 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000157
Sean Callananfd0b0282010-01-21 00:19:58 +0000158 CurBuffer = NewBuf;
159
160 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
161
162 return false;
163}
164
165const AsmToken &AsmParser::Lex() {
166 const AsmToken *tok = &Lexer.Lex();
167
168 if (tok->is(AsmToken::Eof)) {
169 // If this is the end of an included file, pop the parent file off the
170 // include stack.
171 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
172 if (ParentIncludeLoc != SMLoc()) {
173 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
174 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
175 ParentIncludeLoc.getPointer());
176 tok = &Lexer.Lex();
177 }
178 }
179
180 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000181 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000182
Sean Callananfd0b0282010-01-21 00:19:58 +0000183 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000184}
185
Chris Lattner79180e22010-04-05 23:15:42 +0000186bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000187 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000188 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000189 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000190 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000191 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000192 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
193 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000194
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000195 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000196 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000197
Chris Lattnerb717fb02009-07-02 21:53:43 +0000198 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000199
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000200 AsmCond StartingCondState = TheCondState;
201
Chris Lattnerb717fb02009-07-02 21:53:43 +0000202 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000203 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000204 if (!ParseStatement()) continue;
205
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000206 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000207 HadError = true;
208 EatToEndOfStatement();
209 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000210
211 if (TheCondState.TheCond != StartingCondState.TheCond ||
212 TheCondState.Ignore != StartingCondState.Ignore)
213 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000214
Chris Lattner79180e22010-04-05 23:15:42 +0000215 // Finalize the output stream if there are no errors and if the client wants
216 // us to.
217 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000218 Out.Finish();
219
Chris Lattnerb717fb02009-07-02 21:53:43 +0000220 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000221}
222
Chris Lattner2cf5f142009-06-22 01:29:09 +0000223/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
224void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000225 while (Lexer.isNot(AsmToken::EndOfStatement) &&
226 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000227 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000228
229 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000230 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000231 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000232}
233
Chris Lattnerc4193832009-06-22 05:51:26 +0000234
Chris Lattner74ec1a32009-06-22 06:32:03 +0000235/// ParseParenExpr - Parse a paren expression and return it.
236/// NOTE: This assumes the leading '(' has already been consumed.
237///
238/// parenexpr ::= expr)
239///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000240bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000241 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000242 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000243 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000244 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000245 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000246 return false;
247}
Chris Lattnerc4193832009-06-22 05:51:26 +0000248
Chris Lattner74ec1a32009-06-22 06:32:03 +0000249/// ParsePrimaryExpr - Parse a primary expression and return it.
250/// primaryexpr ::= (parenexpr
251/// primaryexpr ::= symbol
252/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000253/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000254/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000255bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000256 switch (Lexer.getKind()) {
257 default:
258 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000259 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000260 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000261 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000262 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000263 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000264 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000265 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000266 case AsmToken::Identifier: {
267 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000268 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000269 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000270
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000271 // Mark the symbol as used in an expression.
272 Sym->setUsedInExpr(true);
273
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000274 // Lookup the symbol variant if used.
275 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
276 if (Split.first.size() != getTok().getIdentifier().size())
277 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
278
Chris Lattnerb4307b32010-01-15 19:28:38 +0000279 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000280 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000281
282 // If this is an absolute variable reference, substitute it now to preserve
283 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000284 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000285 if (Variant)
286 return Error(EndLoc, "unexpected modified on variable reference");
287
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000288 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000289 return false;
290 }
291
292 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000293 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000294 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000295 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000296 case AsmToken::Integer: {
297 SMLoc Loc = getTok().getLoc();
298 int64_t IntVal = getTok().getIntVal();
299 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000300 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000301 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000302 // Look for 'b' or 'f' following an Integer as a directional label
303 if (Lexer.getKind() == AsmToken::Identifier) {
304 StringRef IDVal = getTok().getString();
305 if (IDVal == "f" || IDVal == "b"){
306 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
307 IDVal == "f" ? 1 : 0);
308 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
309 getContext());
310 if(IDVal == "b" && Sym->isUndefined())
311 return Error(Loc, "invalid reference to undefined symbol");
312 EndLoc = Lexer.getLoc();
313 Lex(); // Eat identifier.
314 }
315 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000316 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000317 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000318 case AsmToken::Dot: {
319 // This is a '.' reference, which references the current PC. Emit a
320 // temporary label to the streamer and refer to it.
321 MCSymbol *Sym = Ctx.CreateTempSymbol();
322 Out.EmitLabel(Sym);
323 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
324 EndLoc = Lexer.getLoc();
325 Lex(); // Eat identifier.
326 return false;
327 }
328
Daniel Dunbar3f872332009-07-28 16:08:33 +0000329 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000330 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000331 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000332 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000333 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000334 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000335 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000336 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000337 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000338 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000339 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000340 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000341 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000342 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000343 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000344 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000345 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000346 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000347 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000348 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000349 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000350 }
351}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000352
Chris Lattnerb4307b32010-01-15 19:28:38 +0000353bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000354 SMLoc EndLoc;
355 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000356}
357
Chris Lattner74ec1a32009-06-22 06:32:03 +0000358/// ParseExpression - Parse an expression and return it.
359///
360/// expr ::= expr +,- expr -> lowest.
361/// expr ::= expr |,^,&,! expr -> middle.
362/// expr ::= expr *,/,%,<<,>> expr -> highest.
363/// expr ::= primaryexpr
364///
Chris Lattner54482b42010-01-15 19:39:23 +0000365bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000366 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000367 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000368 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
369 return true;
370
371 // Try to constant fold it up front, if possible.
372 int64_t Value;
373 if (Res->EvaluateAsAbsolute(Value))
374 Res = MCConstantExpr::Create(Value, getContext());
375
376 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000377}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000378
Chris Lattnerb4307b32010-01-15 19:28:38 +0000379bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000380 Res = 0;
381 return ParseParenExpr(Res, EndLoc) ||
382 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000383}
384
Daniel Dunbar475839e2009-06-29 20:37:27 +0000385bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000386 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000387
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000388 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000389 if (ParseExpression(Expr))
390 return true;
391
Daniel Dunbare00b0112009-10-16 01:57:52 +0000392 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000393 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394
395 return false;
396}
397
Daniel Dunbar3f872332009-07-28 16:08:33 +0000398static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000399 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000400 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000401 default:
402 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403
404 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000405 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000406 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000407 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000408 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000409 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000410 return 1;
411
412 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000413 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000414 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000415 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000416 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000417 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000418 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000420 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 case AsmToken::ExclaimEqual:
423 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000424 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000425 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000426 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000427 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000428 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000429 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000430 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000431 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000432 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000433 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000434 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000435 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000436 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000437 return 2;
438
439 // Intermediate Precedence: |, &, ^
440 //
441 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000442 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000443 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000444 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000445 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000446 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000447 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000448 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000449 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000450 return 3;
451
452 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000453 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000454 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000455 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000456 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000457 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000458 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000459 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000460 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000461 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000463 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000464 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000465 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000466 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000467 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000468 }
469}
470
471
472/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
473/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000474bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
475 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000476 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000477 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000478 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000479
480 // If the next token is lower precedence than we are allowed to eat, return
481 // successfully with what we ate already.
482 if (TokPrec < Precedence)
483 return false;
484
Sean Callanan79ed1a82010-01-19 20:22:31 +0000485 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000486
487 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000488 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000489 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000490
491 // If BinOp binds less tightly with RHS than the operator after RHS, let
492 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000493 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000494 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000495 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000496 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000497 }
498
Daniel Dunbar475839e2009-06-29 20:37:27 +0000499 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000500 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000501 }
502}
503
Chris Lattnerc4193832009-06-22 05:51:26 +0000504
505
506
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000507/// ParseStatement:
508/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000509/// ::= Label* Directive ...Operands... EndOfStatement
510/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000511bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000512 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000513 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000514 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000515 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000516 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000517
518 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000519 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000520 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000521 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000522 int64_t LocalLabelVal = -1;
523 // GUESS allow an integer followed by a ':' as a directional local label
524 if (Lexer.is(AsmToken::Integer)) {
525 LocalLabelVal = getTok().getIntVal();
526 if (LocalLabelVal < 0) {
527 if (!TheCondState.Ignore)
528 return TokError("unexpected token at start of statement");
529 IDVal = "";
530 }
531 else {
532 IDVal = getTok().getString();
533 Lex(); // Consume the integer token to be used as an identifier token.
534 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000535 if (!TheCondState.Ignore)
536 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000537 }
538 }
539 }
540 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000541 if (!TheCondState.Ignore)
542 return TokError("unexpected token at start of statement");
543 IDVal = "";
544 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000545
Chris Lattner7834fac2010-04-17 18:14:27 +0000546 // Handle conditional assembly here before checking for skipping. We
547 // have to do this so that .endif isn't skipped in a ".if 0" block for
548 // example.
549 if (IDVal == ".if")
550 return ParseDirectiveIf(IDLoc);
551 if (IDVal == ".elseif")
552 return ParseDirectiveElseIf(IDLoc);
553 if (IDVal == ".else")
554 return ParseDirectiveElse(IDLoc);
555 if (IDVal == ".endif")
556 return ParseDirectiveEndIf(IDLoc);
557
558 // If we are in a ".if 0" block, ignore this statement.
559 if (TheCondState.Ignore) {
560 EatToEndOfStatement();
561 return false;
562 }
563
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000564 // FIXME: Recurse on local labels?
565
566 // See what kind of statement we have.
567 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000568 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000569 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000570 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000571
572 // Diagnose attempt to use a variable as a label.
573 //
574 // FIXME: Diagnostics. Note the location of the definition as a label.
575 // FIXME: This doesn't diagnose assignment to a symbol which has been
576 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000577 MCSymbol *Sym;
578 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000579 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000580 else
581 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000582 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000583 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000584
Daniel Dunbar959fd882009-08-26 22:13:22 +0000585 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000586 Out.EmitLabel(Sym);
587
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000588 // Consume any end of statement token, if present, to avoid spurious
589 // AddBlankLine calls().
590 if (Lexer.is(AsmToken::EndOfStatement)) {
591 Lex();
592 if (Lexer.is(AsmToken::Eof))
593 return false;
594 }
595
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000596 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000597 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000598
Daniel Dunbar3f872332009-07-28 16:08:33 +0000599 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000600 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000601 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000602
Daniel Dunbare2ace502009-08-31 08:09:09 +0000603 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000604
605 default: // Normal instruction or directive.
606 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000607 }
608
609 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000610 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000611 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000612 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000613 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000615 // FIXME: This changes behavior based on the -static flag to the
616 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000617 return ParseDirectiveSectionSwitch("__TEXT", "__text",
618 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000619 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000620 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000621 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000622 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000624 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
625 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000627 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000628 MCSectionMachO::S_4BYTE_LITERALS,
629 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000630 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000631 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000632 MCSectionMachO::S_8BYTE_LITERALS,
633 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000635 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000636 MCSectionMachO::S_16BYTE_LITERALS,
637 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000638 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000639 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000640 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000641 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000643 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000644 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000645 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
646
647 // FIXME: The assembler manual claims that this has the self modify code
648 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000649 if (IDVal == ".symbol_stub")
650 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
651 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000652 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
653 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000654 0, 16);
655 // FIXME: PowerPC only?
656 if (IDVal == ".picsymbol_stub")
657 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
658 MCSectionMachO::S_SYMBOL_STUBS |
659 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
660 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000661 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000662 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000664 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
665
666 // FIXME: The section names of these two are misspelled in the assembler
667 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000669 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
670 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
671 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000672 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000673 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
674 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
675 4);
676
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000677 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000678 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000679 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000680 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000681 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
682 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000683 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000684 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000685 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
686 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000687 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000688 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000689
690
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000691 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000692 return ParseDirectiveSectionSwitch("__OBJC", "__class",
693 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000695 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
696 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000697 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000698 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
699 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000701 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
702 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000703 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000704 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
705 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000706 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000707 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
708 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000709 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000710 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
711 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000712 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000713 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
714 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000716 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
717 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
718 MCSectionMachO::S_LITERAL_POINTERS,
719 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000720 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000721 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
722 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
723 MCSectionMachO::S_LITERAL_POINTERS,
724 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000726 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
727 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000728 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000729 return ParseDirectiveSectionSwitch("__OBJC", "__category",
730 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000732 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
733 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000734 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000735 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
736 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000738 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
739 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000741 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
742 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000743 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000744 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
745 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000747 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
748 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000749 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000750 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
751 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000752
Eric Christopherc6177a42010-05-17 22:53:55 +0000753 if (IDVal == ".tdata")
754 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
755 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
756 if (IDVal == ".tlv")
757 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
758 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
759 if (IDVal == ".thread_init_func")
760 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
761 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
762
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000763 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000764 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000765 return ParseDirectiveSet();
766
Daniel Dunbara0d14262009-06-24 23:30:00 +0000767 // Data directives
768
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000769 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000770 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000771 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000772 return ParseDirectiveAscii(true);
773
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000774 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000775 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000776 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000777 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000778 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000779 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000780 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000781 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000782
783 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000784 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000785 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000786 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000787 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000788 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000789 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000790 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000791 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000792 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000793 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000794 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000795 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000796 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000797 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000798 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000799 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
800
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000801 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000802 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000803
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000804 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000805 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000806 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000807 return ParseDirectiveSpace();
808
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000809 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000810
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000811 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000812 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000813 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000814 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000815 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000816 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000817 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000818 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000819 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000820 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000821 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000822 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000823 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000824 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000825 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000826 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000827 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000828 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +0000829 if (IDVal == ".type")
830 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000831 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000832 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000833 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000834 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000835 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000836 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000837 if (IDVal == ".weak_def_can_be_hidden")
838 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000839
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000840 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000841 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000842 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000843 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000844
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000845 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000846 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000847 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000848 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +0000849
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000850 // Look up the handler in the handler table.
851 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
852 DirectiveMap.lookup(IDVal);
853 if (Handler.first)
854 return (Handler.first->*Handler.second)(IDVal, IDLoc);
855
Kevin Enderby9c656452009-09-10 20:51:44 +0000856 // Target hook for parsing target specific directives.
857 if (!getTargetParser().ParseDirective(ID))
858 return false;
859
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000860 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000861 EatToEndOfStatement();
862 return false;
863 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000864
Chris Lattnera7f13542010-05-19 23:34:33 +0000865 // Canonicalize the opcode to lower case.
866 SmallString<128> Opcode;
867 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
868 Opcode.push_back(tolower(IDVal[i]));
869
Chris Lattner98986712010-01-14 22:21:20 +0000870 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +0000871 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000872 ParsedOperands);
873 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
874 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000875
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000876 // If parsing succeeded, match the instruction.
877 if (!HadError) {
878 MCInst Inst;
879 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
880 // Emit the instruction on success.
881 Out.EmitInstruction(Inst);
882 } else {
883 // Otherwise emit a diagnostic about the match failure and set the error
884 // flag.
885 //
886 // FIXME: We should give nicer diagnostics about the exact failure.
887 Error(IDLoc, "unrecognized instruction");
888 HadError = true;
889 }
890 }
Chris Lattner98986712010-01-14 22:21:20 +0000891
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000892 // If there was no error, consume the end-of-statement token. Otherwise this
893 // will be done by our caller.
894 if (!HadError)
895 Lex();
Chris Lattner98986712010-01-14 22:21:20 +0000896
897 // Free any parsed operands.
898 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
899 delete ParsedOperands[i];
900
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +0000901 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +0000902}
Chris Lattner9a023f72009-06-24 04:43:34 +0000903
Daniel Dunbare2ace502009-08-31 08:09:09 +0000904bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000905 // FIXME: Use better location, we should use proper tokens.
906 SMLoc EqualLoc = Lexer.getLoc();
907
Daniel Dunbar821e3332009-08-31 08:09:28 +0000908 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +0000909 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000910 return true;
911
Daniel Dunbar3f872332009-07-28 16:08:33 +0000912 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000913 return TokError("unexpected token in assignment");
914
915 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000916 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000917
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000918 // Validate that the LHS is allowed to be a variable (either it has not been
919 // used as a symbol, or it is an absolute symbol).
920 MCSymbol *Sym = getContext().LookupSymbol(Name);
921 if (Sym) {
922 // Diagnose assignment to a label.
923 //
924 // FIXME: Diagnostics. Note the location of the definition as a label.
925 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000926 if (Sym->isUndefined() && !Sym->isUsedInExpr())
927 ; // Allow redefinitions of undefined symbols only used in directives.
928 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000929 return Error(EqualLoc, "redefinition of '" + Name + "'");
930 else if (!Sym->isVariable())
931 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000932 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000933 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
934 Name + "'");
935 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000936 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000937
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000938 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000939
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000940 Sym->setUsedInExpr(true);
941
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000942 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000943 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000944
945 return false;
946}
947
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000948/// ParseIdentifier:
949/// ::= identifier
950/// ::= string
951bool AsmParser::ParseIdentifier(StringRef &Res) {
952 if (Lexer.isNot(AsmToken::Identifier) &&
953 Lexer.isNot(AsmToken::String))
954 return true;
955
Sean Callanan18b83232010-01-19 21:44:56 +0000956 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000957
Sean Callanan79ed1a82010-01-19 20:22:31 +0000958 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000959
960 return false;
961}
962
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000963/// ParseDirectiveSet:
964/// ::= .set identifier ',' expression
965bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000966 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000967
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000968 if (ParseIdentifier(Name))
969 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000970
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000971 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000972 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000973 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000974
Daniel Dunbare2ace502009-08-31 08:09:09 +0000975 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000976}
977
Chris Lattner9a023f72009-06-24 04:43:34 +0000978/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000979/// ::= .section identifier (',' identifier)*
980/// FIXME: This should actually parse out the segment, section, attributes and
981/// sizeof_stub fields.
982bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000983 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000984
Daniel Dunbarace63122009-08-11 03:42:33 +0000985 StringRef SectionName;
986 if (ParseIdentifier(SectionName))
987 return Error(Loc, "expected identifier after '.section' directive");
988
989 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +0000990 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +0000991 return TokError("unexpected token in '.section' directive");
992
Chris Lattnerff4bc462009-08-10 01:39:42 +0000993 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000994 SectionSpec += ",";
995
996 // Add all the tokens until the end of the line, ParseSectionSpecifier will
997 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000998 StringRef EOL = Lexer.LexUntilEndOfStatement();
999 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +00001000
Sean Callanan79ed1a82010-01-19 20:22:31 +00001001 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001002 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +00001003 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001004 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +00001005
Chris Lattnerff4bc462009-08-10 01:39:42 +00001006
1007 StringRef Segment, Section;
1008 unsigned TAA, StubSize;
1009 std::string ErrorStr =
1010 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1011 TAA, StubSize);
1012
1013 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001014 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +00001015
Chris Lattner56594f92009-07-31 17:47:16 +00001016 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001017 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001018 getStreamer().SwitchSection(Ctx.getMachOSection(
1019 Segment, Section, TAA, StubSize,
1020 isText ? SectionKind::getText()
1021 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001022 return false;
1023}
1024
Chris Lattnere15c2d72009-08-10 18:05:55 +00001025/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +00001026bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
1027 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +00001028 unsigned TAA, unsigned Align,
1029 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001030 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001031 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001032 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +00001033
Chris Lattner56594f92009-07-31 17:47:16 +00001034 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001035 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001036 getStreamer().SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf0559e42010-04-08 20:30:37 +00001037 isText ? SectionKind::getText()
1038 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001039
1040 // Set the implicit alignment, if any.
1041 //
1042 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1043 // alignment on the section (e.g., if one manually inserts bytes into the
1044 // section, then just issueing the section switch directive will not realign
1045 // the section. However, this is arguably more reasonable behavior, and there
1046 // is no good reason for someone to intentionally emit incorrectly sized
1047 // values into the implicitly aligned sections.
1048 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001049 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001050
Chris Lattner529fb542009-06-24 05:13:15 +00001051 return false;
1052}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001053
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001054bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001055 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001056
1057 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001058 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001059 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1060 if (Str[i] != '\\') {
1061 Data += Str[i];
1062 continue;
1063 }
1064
1065 // Recognize escaped characters. Note that this escape semantics currently
1066 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1067 ++i;
1068 if (i == e)
1069 return TokError("unexpected backslash at end of string");
1070
1071 // Recognize octal sequences.
1072 if ((unsigned) (Str[i] - '0') <= 7) {
1073 // Consume up to three octal characters.
1074 unsigned Value = Str[i] - '0';
1075
1076 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1077 ++i;
1078 Value = Value * 8 + (Str[i] - '0');
1079
1080 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1081 ++i;
1082 Value = Value * 8 + (Str[i] - '0');
1083 }
1084 }
1085
1086 if (Value > 255)
1087 return TokError("invalid octal escape sequence (out of range)");
1088
1089 Data += (unsigned char) Value;
1090 continue;
1091 }
1092
1093 // Otherwise recognize individual escapes.
1094 switch (Str[i]) {
1095 default:
1096 // Just reject invalid escape sequences for now.
1097 return TokError("invalid escape sequence (unrecognized character)");
1098
1099 case 'b': Data += '\b'; break;
1100 case 'f': Data += '\f'; break;
1101 case 'n': Data += '\n'; break;
1102 case 'r': Data += '\r'; break;
1103 case 't': Data += '\t'; break;
1104 case '"': Data += '"'; break;
1105 case '\\': Data += '\\'; break;
1106 }
1107 }
1108
1109 return false;
1110}
1111
Daniel Dunbara0d14262009-06-24 23:30:00 +00001112/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001113/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001115 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001116 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001117 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001118 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001119
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001120 std::string Data;
1121 if (ParseEscapedString(Data))
1122 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001123
1124 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001125 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001126 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1127
Sean Callanan79ed1a82010-01-19 20:22:31 +00001128 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001129
1130 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001131 break;
1132
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001133 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001134 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001135 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001136 }
1137 }
1138
Sean Callanan79ed1a82010-01-19 20:22:31 +00001139 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001140 return false;
1141}
1142
1143/// ParseDirectiveValue
1144/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1145bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001146 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001147 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001148 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001149 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001150 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001151 return true;
1152
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001153 // Special case constant expressions to match code generator.
1154 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001155 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001156 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001157 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001158
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001159 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001160 break;
1161
1162 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001163 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001164 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001165 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001166 }
1167 }
1168
Sean Callanan79ed1a82010-01-19 20:22:31 +00001169 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001170 return false;
1171}
1172
1173/// ParseDirectiveSpace
1174/// ::= .space expression [ , expression ]
1175bool AsmParser::ParseDirectiveSpace() {
1176 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001177 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001178 return true;
1179
1180 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001181 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1182 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001183 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001184 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001185
Daniel Dunbar475839e2009-06-29 20:37:27 +00001186 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001187 return true;
1188
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001189 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001190 return TokError("unexpected token in '.space' directive");
1191 }
1192
Sean Callanan79ed1a82010-01-19 20:22:31 +00001193 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001194
1195 if (NumBytes <= 0)
1196 return TokError("invalid number of bytes in '.space' directive");
1197
1198 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001199 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001200
1201 return false;
1202}
1203
1204/// ParseDirectiveFill
1205/// ::= .fill expression , expression , expression
1206bool AsmParser::ParseDirectiveFill() {
1207 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001208 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001209 return true;
1210
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001211 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001212 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001213 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001214
1215 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001216 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001217 return true;
1218
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001219 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001220 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001221 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001222
1223 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001224 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001225 return true;
1226
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001227 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001228 return TokError("unexpected token in '.fill' directive");
1229
Sean Callanan79ed1a82010-01-19 20:22:31 +00001230 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001231
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001232 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1233 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001234
1235 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001236 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001237
1238 return false;
1239}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001240
1241/// ParseDirectiveOrg
1242/// ::= .org expression [ , expression ]
1243bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001244 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001245 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001246 return true;
1247
1248 // Parse optional fill expression.
1249 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001250 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1251 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001252 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001253 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001254
Daniel Dunbar475839e2009-06-29 20:37:27 +00001255 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001256 return true;
1257
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001258 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001259 return TokError("unexpected token in '.org' directive");
1260 }
1261
Sean Callanan79ed1a82010-01-19 20:22:31 +00001262 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001263
1264 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1265 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001266 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001267
1268 return false;
1269}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001270
1271/// ParseDirectiveAlign
1272/// ::= {.align, ...} expression [ , expression [ , expression ]]
1273bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001274 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001275 int64_t Alignment;
1276 if (ParseAbsoluteExpression(Alignment))
1277 return true;
1278
1279 SMLoc MaxBytesLoc;
1280 bool HasFillExpr = false;
1281 int64_t FillExpr = 0;
1282 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001283 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1284 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001285 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001286 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001287
1288 // The fill expression can be omitted while specifying a maximum number of
1289 // alignment bytes, e.g:
1290 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001291 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001292 HasFillExpr = true;
1293 if (ParseAbsoluteExpression(FillExpr))
1294 return true;
1295 }
1296
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001297 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1298 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001299 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001300 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001301
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001302 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001303 if (ParseAbsoluteExpression(MaxBytesToFill))
1304 return true;
1305
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001306 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001307 return TokError("unexpected token in directive");
1308 }
1309 }
1310
Sean Callanan79ed1a82010-01-19 20:22:31 +00001311 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001312
Daniel Dunbar648ac512010-05-17 21:54:30 +00001313 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001314 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001315
1316 // Compute alignment in bytes.
1317 if (IsPow2) {
1318 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001319 if (Alignment >= 32) {
1320 Error(AlignmentLoc, "invalid alignment value");
1321 Alignment = 31;
1322 }
1323
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001324 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001325 }
1326
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001327 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001328 if (MaxBytesLoc.isValid()) {
1329 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001330 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1331 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001332 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001333 }
1334
1335 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001336 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1337 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001338 MaxBytesToFill = 0;
1339 }
1340 }
1341
Daniel Dunbar648ac512010-05-17 21:54:30 +00001342 // Check whether we should use optimal code alignment for this .align
1343 // directive.
1344 //
1345 // FIXME: This should be using a target hook.
1346 bool UseCodeAlign = false;
1347 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001348 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001349 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1350 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1351 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001352 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001353 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001354 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001355 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001356 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001357
1358 return false;
1359}
1360
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001361/// ParseDirectiveSymbolAttribute
1362/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001363bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001364 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001365 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001366 StringRef Name;
1367
1368 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001369 return TokError("expected identifier in directive");
1370
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001371 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001372
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001373 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001374
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001375 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001376 break;
1377
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001378 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001379 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001380 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001381 }
1382 }
1383
Sean Callanan79ed1a82010-01-19 20:22:31 +00001384 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001385 return false;
1386}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001387
Matt Fleming924c5e52010-05-21 11:36:59 +00001388/// ParseDirectiveELFType
1389/// ::= .type identifier , @attribute
1390bool AsmParser::ParseDirectiveELFType() {
1391 StringRef Name;
1392 if (ParseIdentifier(Name))
1393 return TokError("expected identifier in directive");
1394
1395 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001396 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001397
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001398 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001399 return TokError("unexpected token in '.type' directive");
1400 Lex();
1401
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001402 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001403 return TokError("expected '@' before type");
1404 Lex();
1405
1406 StringRef Type;
1407 SMLoc TypeLoc;
1408
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001409 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001410 if (ParseIdentifier(Type))
1411 return TokError("expected symbol type in directive");
1412
1413 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1414 .Case("function", MCSA_ELF_TypeFunction)
1415 .Case("object", MCSA_ELF_TypeObject)
1416 .Case("tls_object", MCSA_ELF_TypeTLS)
1417 .Case("common", MCSA_ELF_TypeCommon)
1418 .Case("notype", MCSA_ELF_TypeNoType)
1419 .Default(MCSA_Invalid);
1420
1421 if (Attr == MCSA_Invalid)
1422 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1423
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001424 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001425 return TokError("unexpected token in '.type' directive");
1426
1427 Lex();
1428
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001429 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001430
1431 return false;
1432}
1433
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001434/// ParseDirectiveDesc
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001435/// ::= .desc identifier , expression
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001436bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001437 StringRef Name;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001438 if (getParser().ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001439 return TokError("expected identifier in directive");
1440
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001441 // Handle the identifier as the key symbol.
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001442 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001443
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001444 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001445 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001446 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001447
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001448 int64_t DescValue;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001449 if (getParser().ParseAbsoluteExpression(DescValue))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001450 return true;
1451
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001452 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001453 return TokError("unexpected token in '.desc' directive");
1454
Sean Callanan79ed1a82010-01-19 20:22:31 +00001455 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001456
1457 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001458 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001459
1460 return false;
1461}
1462
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001463/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001464/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1465bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001466 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001467 StringRef Name;
1468 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001469 return TokError("expected identifier in directive");
1470
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001471 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001472 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001473
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001474 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001475 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001476 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001477
1478 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001479 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001480 if (ParseAbsoluteExpression(Size))
1481 return true;
1482
1483 int64_t Pow2Alignment = 0;
1484 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001485 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001486 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001487 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001488 if (ParseAbsoluteExpression(Pow2Alignment))
1489 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001490
1491 // If this target takes alignments in bytes (not log) validate and convert.
1492 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1493 if (!isPowerOf2_64(Pow2Alignment))
1494 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1495 Pow2Alignment = Log2_64(Pow2Alignment);
1496 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001497 }
1498
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001499 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001500 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001501
Sean Callanan79ed1a82010-01-19 20:22:31 +00001502 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001503
Chris Lattner1fc3d752009-07-09 17:25:12 +00001504 // NOTE: a size of zero for a .comm should create a undefined symbol
1505 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001506 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001507 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1508 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001509
Eric Christopherc260a3e2010-05-14 01:38:54 +00001510 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001511 // may internally end up wanting an alignment in bytes.
1512 // FIXME: Diagnose overflow.
1513 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001514 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1515 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001516
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001517 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001518 return Error(IDLoc, "invalid symbol redefinition");
1519
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001520 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001521 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001522 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001523 getStreamer().EmitZerofill(Ctx.getMachOSection(
1524 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1525 0, SectionKind::getBSS()),
1526 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001527 return false;
1528 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001529
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001530 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001531 return false;
1532}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001533
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001534/// ParseDirectiveZerofill
Chris Lattner9be3fee2009-07-10 22:20:30 +00001535/// ::= .zerofill segname , sectname [, identifier , size_expression [
1536/// , align_expression ]]
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001537bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001538 StringRef Segment;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001539 if (getParser().ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001540 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001541
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001542 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001543 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001544 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001545
1546 StringRef Section;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001547 if (getParser().ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001548 return TokError("expected section name after comma in '.zerofill' "
1549 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001550
Chris Lattner9be3fee2009-07-10 22:20:30 +00001551 // If this is the end of the line all that was wanted was to create the
1552 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001553 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001554 // Create the zerofill section but no symbol
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001555 getStreamer().EmitZerofill(getContext().getMachOSection(
1556 Segment, Section, MCSectionMachO::S_ZEROFILL,
1557 0, SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001558 return false;
1559 }
1560
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001561 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001562 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001563 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001564
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001565 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001566 StringRef IDStr;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001567 if (getParser().ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001568 return TokError("expected identifier in directive");
1569
1570 // handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001571 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001572
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001573 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001574 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001575 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001576
1577 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001578 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001579 if (getParser().ParseAbsoluteExpression(Size))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001580 return true;
1581
1582 int64_t Pow2Alignment = 0;
1583 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001584 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001585 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001586 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001587 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001588 return true;
1589 }
1590
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001591 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001592 return TokError("unexpected token in '.zerofill' directive");
1593
Sean Callanan79ed1a82010-01-19 20:22:31 +00001594 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001595
1596 if (Size < 0)
1597 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1598 "than zero");
1599
Eric Christopherc260a3e2010-05-14 01:38:54 +00001600 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001601 // may internally end up wanting an alignment in bytes.
1602 // FIXME: Diagnose overflow.
1603 if (Pow2Alignment < 0)
1604 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1605 "can't be less than zero");
1606
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001607 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001608 return Error(IDLoc, "invalid symbol redefinition");
1609
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001610 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001611 //
1612 // FIXME: Arch specific.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001613 getStreamer().EmitZerofill(getContext().getMachOSection(
1614 Segment, Section, MCSectionMachO::S_ZEROFILL,
1615 0, SectionKind::getBSS()),
1616 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001617
1618 return false;
1619}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001620
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001621/// ParseDirectiveTBSS
Eric Christopher482eba02010-05-14 01:50:28 +00001622/// ::= .tbss identifier, size, align
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001623bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001624 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001625 StringRef Name;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001626 if (getParser().ParseIdentifier(Name))
Eric Christopher482eba02010-05-14 01:50:28 +00001627 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001628
Eric Christopher482eba02010-05-14 01:50:28 +00001629 // Handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001630 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001631
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001632 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001633 return TokError("unexpected token in directive");
1634 Lex();
1635
1636 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001637 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001638 if (getParser().ParseAbsoluteExpression(Size))
Eric Christopher482eba02010-05-14 01:50:28 +00001639 return true;
1640
1641 int64_t Pow2Alignment = 0;
1642 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001643 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001644 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001645 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001646 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Eric Christopher482eba02010-05-14 01:50:28 +00001647 return true;
1648 }
1649
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001650 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001651 return TokError("unexpected token in '.tbss' directive");
1652
1653 Lex();
1654
1655 if (Size < 0)
1656 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1657 "zero");
1658
1659 // FIXME: Diagnose overflow.
1660 if (Pow2Alignment < 0)
1661 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1662 "than zero");
1663
1664 if (!Sym->isUndefined())
1665 return Error(IDLoc, "invalid symbol redefinition");
1666
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001667 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001668 "__DATA", "__thread_bss",
1669 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1670 0, SectionKind::getThreadBSS()),
1671 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001672
1673 return false;
1674}
1675
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001676/// ParseDirectiveSubsectionsViaSymbols
Kevin Enderbya5c78322009-07-13 21:03:15 +00001677/// ::= .subsections_via_symbols
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001678bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001680 return TokError("unexpected token in '.subsections_via_symbols' directive");
1681
Sean Callanan79ed1a82010-01-19 20:22:31 +00001682 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001683
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001684 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001685
1686 return false;
1687}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001688
1689/// ParseDirectiveAbort
1690/// ::= .abort [ "abort_string" ]
1691bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001692 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001693 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001694
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001695 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001696 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1697 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001698 return TokError("expected string in '.abort' directive");
1699
Sean Callanan18b83232010-01-19 21:44:56 +00001700 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001701
Sean Callanan79ed1a82010-01-19 20:22:31 +00001702 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001703 }
1704
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001705 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001706 return TokError("unexpected token in '.abort' directive");
1707
Sean Callanan79ed1a82010-01-19 20:22:31 +00001708 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001709
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001710 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001711 if (Str.empty())
1712 Error(Loc, ".abort detected. Assembly stopping.");
1713 else
1714 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001715
1716 return false;
1717}
Kevin Enderby71148242009-07-14 21:35:03 +00001718
1719/// ParseDirectiveLsym
1720/// ::= .lsym identifier , expression
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001721bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001722 StringRef Name;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001723 if (getParser().ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001724 return TokError("expected identifier in directive");
1725
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001726 // Handle the identifier as the key symbol.
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001727 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001728
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001729 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001730 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001731 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001732
Daniel Dunbar821e3332009-08-31 08:09:28 +00001733 const MCExpr *Value;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001734 if (getParser().ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001735 return true;
1736
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001737 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001738 return TokError("unexpected token in '.lsym' directive");
1739
Sean Callanan79ed1a82010-01-19 20:22:31 +00001740 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001741
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001742 // We don't currently support this directive.
1743 //
1744 // FIXME: Diagnostic location!
1745 (void) Sym;
1746 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001747}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001748
1749/// ParseDirectiveInclude
1750/// ::= .include "filename"
1751bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001752 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001753 return TokError("expected string in '.include' directive");
1754
Sean Callanan18b83232010-01-19 21:44:56 +00001755 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001757 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001758
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001759 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001760 return TokError("unexpected token in '.include' directive");
1761
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001762 // Strip the quotes.
1763 Filename = Filename.substr(1, Filename.size()-2);
1764
1765 // Attempt to switch the lexer to the included file before consuming the end
1766 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001767 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001768 PrintMessage(IncludeLoc,
1769 "Could not find include file '" + Filename + "'",
1770 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001771 return true;
1772 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001773
1774 return false;
1775}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001776
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001777/// ParseDirectiveDumpOrLoad
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001778/// ::= ( .dump | .load ) "filename"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001779bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1780 SMLoc IDLoc) {
1781 bool IsDump = Directive == ".dump";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001782 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001783 return TokError("expected string in '.dump' or '.load' directive");
1784
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001786
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001787 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001788 return TokError("unexpected token in '.dump' or '.load' directive");
1789
Sean Callanan79ed1a82010-01-19 20:22:31 +00001790 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001791
Kevin Enderby5026ae42009-07-20 20:25:37 +00001792 // FIXME: If/when .dump and .load are implemented they will be done in the
1793 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001794 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001795 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001796 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001797 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001798
1799 return false;
1800}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001801
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001802/// ParseDirectiveSecureLogUnique
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001803/// ::= .secure_log_unique "log message"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001804bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001805 std::string LogMessage;
1806
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001807 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001808 LogMessage = "";
1809 else{
1810 LogMessage = getTok().getString();
1811 Lex();
1812 }
1813
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001814 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001815 return TokError("unexpected token in '.secure_log_unique' directive");
1816
1817 if (getContext().getSecureLogUsed() != false)
1818 return Error(IDLoc, ".secure_log_unique specified multiple times");
1819
1820 char *SecureLogFile = getContext().getSecureLogFile();
1821 if (SecureLogFile == NULL)
1822 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1823 "environment variable unset.");
1824
1825 raw_ostream *OS = getContext().getSecureLog();
1826 if (OS == NULL) {
1827 std::string Err;
1828 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1829 if (!Err.empty()) {
1830 delete OS;
1831 return Error(IDLoc, Twine("can't open secure log file: ") +
1832 SecureLogFile + " (" + Err + ")");
1833 }
1834 getContext().setSecureLog(OS);
1835 }
1836
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001837 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
1838 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
1839 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001840 << LogMessage + "\n";
1841
1842 getContext().setSecureLogUsed(true);
1843
1844 return false;
1845}
1846
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001847/// ParseDirectiveSecureLogReset
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001848/// ::= .secure_log_reset
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001849bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001850 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00001851 return TokError("unexpected token in '.secure_log_reset' directive");
1852
1853 Lex();
1854
1855 getContext().setSecureLogUsed(false);
1856
1857 return false;
1858}
1859
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001860/// ParseDirectiveIf
1861/// ::= .if expression
1862bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001863 TheCondStack.push_back(TheCondState);
1864 TheCondState.TheCond = AsmCond::IfCond;
1865 if(TheCondState.Ignore) {
1866 EatToEndOfStatement();
1867 }
1868 else {
1869 int64_t ExprValue;
1870 if (ParseAbsoluteExpression(ExprValue))
1871 return true;
1872
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001873 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001874 return TokError("unexpected token in '.if' directive");
1875
Sean Callanan79ed1a82010-01-19 20:22:31 +00001876 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001877
1878 TheCondState.CondMet = ExprValue;
1879 TheCondState.Ignore = !TheCondState.CondMet;
1880 }
1881
1882 return false;
1883}
1884
1885/// ParseDirectiveElseIf
1886/// ::= .elseif expression
1887bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1888 if (TheCondState.TheCond != AsmCond::IfCond &&
1889 TheCondState.TheCond != AsmCond::ElseIfCond)
1890 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1891 " an .elseif");
1892 TheCondState.TheCond = AsmCond::ElseIfCond;
1893
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001894 bool LastIgnoreState = false;
1895 if (!TheCondStack.empty())
1896 LastIgnoreState = TheCondStack.back().Ignore;
1897 if (LastIgnoreState || TheCondState.CondMet) {
1898 TheCondState.Ignore = true;
1899 EatToEndOfStatement();
1900 }
1901 else {
1902 int64_t ExprValue;
1903 if (ParseAbsoluteExpression(ExprValue))
1904 return true;
1905
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001906 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001907 return TokError("unexpected token in '.elseif' directive");
1908
Sean Callanan79ed1a82010-01-19 20:22:31 +00001909 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001910 TheCondState.CondMet = ExprValue;
1911 TheCondState.Ignore = !TheCondState.CondMet;
1912 }
1913
1914 return false;
1915}
1916
1917/// ParseDirectiveElse
1918/// ::= .else
1919bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001920 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001921 return TokError("unexpected token in '.else' directive");
1922
Sean Callanan79ed1a82010-01-19 20:22:31 +00001923 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001924
1925 if (TheCondState.TheCond != AsmCond::IfCond &&
1926 TheCondState.TheCond != AsmCond::ElseIfCond)
1927 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1928 ".elseif");
1929 TheCondState.TheCond = AsmCond::ElseCond;
1930 bool LastIgnoreState = false;
1931 if (!TheCondStack.empty())
1932 LastIgnoreState = TheCondStack.back().Ignore;
1933 if (LastIgnoreState || TheCondState.CondMet)
1934 TheCondState.Ignore = true;
1935 else
1936 TheCondState.Ignore = false;
1937
1938 return false;
1939}
1940
1941/// ParseDirectiveEndIf
1942/// ::= .endif
1943bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001944 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001945 return TokError("unexpected token in '.endif' directive");
1946
Sean Callanan79ed1a82010-01-19 20:22:31 +00001947 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001948
1949 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1950 TheCondStack.empty())
1951 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1952 ".else");
1953 if (!TheCondStack.empty()) {
1954 TheCondState = TheCondStack.back();
1955 TheCondStack.pop_back();
1956 }
1957
1958 return false;
1959}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001960
1961/// ParseDirectiveFile
1962/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001963bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001964 // FIXME: I'm not sure what this is.
1965 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00001966 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001967 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001968 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00001969
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001970 if (FileNumber < 1)
1971 return TokError("file number less than one");
1972 }
1973
Daniel Dunbareceec052010-07-12 17:45:27 +00001974 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001975 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00001976
Chris Lattnerd32e8032010-01-25 19:02:58 +00001977 StringRef Filename = getTok().getString();
1978 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001979 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001980
Daniel Dunbareceec052010-07-12 17:45:27 +00001981 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001982 return TokError("unexpected token in '.file' directive");
1983
Chris Lattnerd32e8032010-01-25 19:02:58 +00001984 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00001985 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00001986 else
Daniel Dunbareceec052010-07-12 17:45:27 +00001987 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
1988
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001989 return false;
1990}
1991
1992/// ParseDirectiveLine
1993/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001994bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00001995 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1996 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001997 return TokError("unexpected token in '.line' directive");
1998
Sean Callanan18b83232010-01-19 21:44:56 +00001999 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002000 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002001 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002002
2003 // FIXME: Do something with the .line.
2004 }
2005
Daniel Dunbareceec052010-07-12 17:45:27 +00002006 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002007 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002008
2009 return false;
2010}
2011
2012
2013/// ParseDirectiveLoc
2014/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002015bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002016 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002017 return TokError("unexpected token in '.loc' directive");
2018
2019 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002020 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002021 (void) FileNumber;
2022 // FIXME: Validate file.
2023
Sean Callanan79ed1a82010-01-19 20:22:31 +00002024 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2026 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002027 return TokError("unexpected token in '.loc' directive");
2028
Sean Callanan18b83232010-01-19 21:44:56 +00002029 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002030 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002031 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002032
Daniel Dunbareceec052010-07-12 17:45:27 +00002033 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2034 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002035 return TokError("unexpected token in '.loc' directive");
2036
Sean Callanan18b83232010-01-19 21:44:56 +00002037 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002038 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002039 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002040
2041 // FIXME: Do something with the .loc.
2042 }
2043 }
2044
Daniel Dunbareceec052010-07-12 17:45:27 +00002045 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002046 return TokError("unexpected token in '.file' directive");
2047
2048 return false;
2049}
2050