blob: 4ec5247d62eb8e9bf1464e04c99d455baca3b061 [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"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000016#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000017#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000018#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000019#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000020#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000021#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000024#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000025#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000027#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000028using namespace llvm;
29
Chris Lattneraaec2052010-01-19 19:46:13 +000030
31enum { DEFAULT_ADDRSPACE = 0 };
32
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000033// Mach-O section uniquing.
34//
35// FIXME: Figure out where this should live, it should be shared by
36// TargetLoweringObjectFile.
37typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
38
Chris Lattnerebb89b42009-09-27 21:16:52 +000039AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
40 const MCAsmInfo &_MAI)
Sean Callananfd0b0282010-01-21 00:19:58 +000041 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
42 CurBuffer(0), SectionUniquingMap(0) {
43 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
44
Chris Lattnerebb89b42009-09-27 21:16:52 +000045 // Debugging directives.
46 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
47 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
48 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
49}
50
51
52
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000053AsmParser::~AsmParser() {
54 // If we have the MachO uniquing map, free it.
55 delete (MachOUniqueMapTy*)SectionUniquingMap;
56}
57
58const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
59 const StringRef &Section,
60 unsigned TypeAndAttributes,
61 unsigned Reserved2,
62 SectionKind Kind) const {
63 // We unique sections by their segment/section pair. The returned section
64 // may not have the same flags as the requested section, if so this should be
65 // diagnosed by the client as an error.
66
67 // Create the map if it doesn't already exist.
68 if (SectionUniquingMap == 0)
69 SectionUniquingMap = new MachOUniqueMapTy();
70 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
71
72 // Form the name to look up.
73 SmallString<64> Name;
74 Name += Segment;
75 Name.push_back(',');
76 Name += Section;
77
78 // Do the lookup, if we have a hit, return it.
79 const MCSectionMachO *&Entry = Map[Name.str()];
80
81 // FIXME: This should validate the type and attributes.
82 if (Entry) return Entry;
83
84 // Otherwise, return a new section.
85 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
86 Reserved2, Kind, Ctx);
87}
88
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000089void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000090 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +000091}
92
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000093bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000094 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +000095 return true;
96}
97
98bool AsmParser::TokError(const char *Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +000099 PrintMessage(Lexer.getLoc(), Msg, "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000100 return true;
101}
102
Sean Callananbf2013e2010-01-20 23:19:55 +0000103void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
104 const char *Type) const {
105 SrcMgr.PrintMessage(Loc, Msg, Type);
106}
Sean Callananfd0b0282010-01-21 00:19:58 +0000107
108bool AsmParser::EnterIncludeFile(const std::string &Filename) {
109 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
110 if (NewBuf == -1)
111 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000112
Sean Callananfd0b0282010-01-21 00:19:58 +0000113 CurBuffer = NewBuf;
114
115 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
116
117 return false;
118}
119
120const AsmToken &AsmParser::Lex() {
121 const AsmToken *tok = &Lexer.Lex();
122
123 if (tok->is(AsmToken::Eof)) {
124 // If this is the end of an included file, pop the parent file off the
125 // include stack.
126 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
127 if (ParentIncludeLoc != SMLoc()) {
128 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
129 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
130 ParentIncludeLoc.getPointer());
131 tok = &Lexer.Lex();
132 }
133 }
134
135 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000136 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000137
Sean Callananfd0b0282010-01-21 00:19:58 +0000138 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000139}
140
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000141bool AsmParser::Run(bool NoInitialTextSection) {
142 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000143 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000144 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000145 if (!NoInitialTextSection)
146 Out.SwitchSection(getMachOSection("__TEXT", "__text",
147 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
148 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000149
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000150 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000151 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000152
Chris Lattnerb717fb02009-07-02 21:53:43 +0000153 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000154
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000155 AsmCond StartingCondState = TheCondState;
156
Chris Lattnerb717fb02009-07-02 21:53:43 +0000157 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000158 while (Lexer.isNot(AsmToken::Eof)) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000159 // Handle conditional assembly here before calling ParseStatement()
160 if (Lexer.getKind() == AsmToken::Identifier) {
161 // If we have an identifier, handle it as the key symbol.
Sean Callanan18b83232010-01-19 21:44:56 +0000162 AsmToken ID = getTok();
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000163 SMLoc IDLoc = ID.getLoc();
164 StringRef IDVal = ID.getString();
165
166 if (IDVal == ".if" ||
167 IDVal == ".elseif" ||
168 IDVal == ".else" ||
169 IDVal == ".endif") {
170 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
171 continue;
172 HadError = true;
173 EatToEndOfStatement();
174 continue;
175 }
176 }
177 if (TheCondState.Ignore) {
178 EatToEndOfStatement();
179 continue;
180 }
181
Chris Lattnerb717fb02009-07-02 21:53:43 +0000182 if (!ParseStatement()) continue;
183
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000184 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000185 HadError = true;
186 EatToEndOfStatement();
187 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000188
189 if (TheCondState.TheCond != StartingCondState.TheCond ||
190 TheCondState.Ignore != StartingCondState.Ignore)
191 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000192
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000193 if (!HadError)
194 Out.Finish();
195
Chris Lattnerb717fb02009-07-02 21:53:43 +0000196 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000197}
198
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000199/// ParseConditionalAssemblyDirectives - parse the conditional assembly
200/// directives
201bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
202 SMLoc DirectiveLoc) {
203 if (Directive == ".if")
204 return ParseDirectiveIf(DirectiveLoc);
205 if (Directive == ".elseif")
206 return ParseDirectiveElseIf(DirectiveLoc);
207 if (Directive == ".else")
208 return ParseDirectiveElse(DirectiveLoc);
209 if (Directive == ".endif")
210 return ParseDirectiveEndIf(DirectiveLoc);
211 return true;
212}
213
Chris Lattner2cf5f142009-06-22 01:29:09 +0000214/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
215void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000216 while (Lexer.isNot(AsmToken::EndOfStatement) &&
217 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000218 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000219
220 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000221 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000222 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000223}
224
Chris Lattnerc4193832009-06-22 05:51:26 +0000225
Chris Lattner74ec1a32009-06-22 06:32:03 +0000226/// ParseParenExpr - Parse a paren expression and return it.
227/// NOTE: This assumes the leading '(' has already been consumed.
228///
229/// parenexpr ::= expr)
230///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000231bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000232 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000233 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000234 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000235 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000236 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000237 return false;
238}
Chris Lattnerc4193832009-06-22 05:51:26 +0000239
Daniel Dunbar959fd882009-08-26 22:13:22 +0000240MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
Daniel Dunbar959fd882009-08-26 22:13:22 +0000241 // If the label starts with L it is an assembler temporary label.
242 if (Name.startswith("L"))
Chris Lattner00685bb2010-03-10 01:29:27 +0000243 return Ctx.GetOrCreateTemporarySymbol(Name);
244 return Ctx.GetOrCreateSymbol(Name);
Daniel Dunbar959fd882009-08-26 22:13:22 +0000245}
246
Chris Lattner74ec1a32009-06-22 06:32:03 +0000247/// ParsePrimaryExpr - Parse a primary expression and return it.
248/// primaryexpr ::= (parenexpr
249/// primaryexpr ::= symbol
250/// primaryexpr ::= number
251/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000252bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000253 switch (Lexer.getKind()) {
254 default:
255 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000256 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000257 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000258 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000259 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000260 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000261 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000262 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000263 case AsmToken::Identifier: {
264 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000265 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
266 MCSymbol *Sym = CreateSymbol(Split.first);
267
268 // Lookup the symbol variant if used.
269 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
270 if (Split.first.size() != getTok().getIdentifier().size())
271 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
272
Chris Lattnerb4307b32010-01-15 19:28:38 +0000273 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000274 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000275
276 // If this is an absolute variable reference, substitute it now to preserve
277 // semantics in the face of reassignment.
278 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000279 if (Variant)
280 return Error(EndLoc, "unexpected modified on variable reference");
281
Daniel Dunbarfffff912009-10-16 01:34:54 +0000282 Res = Sym->getValue();
283 return false;
284 }
285
286 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000287 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000288 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000289 }
Daniel Dunbar3f872332009-07-28 16:08:33 +0000290 case AsmToken::Integer:
Sean Callanan18b83232010-01-19 21:44:56 +0000291 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000292 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000293 Lex(); // Eat token.
Chris Lattnerc4193832009-06-22 05:51:26 +0000294 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000295 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000296 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000297 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000298 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000299 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000300 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000301 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000302 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000303 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000304 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000305 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000306 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000307 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000308 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000310 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000311 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000312 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000313 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000314 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000315 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000316 }
317}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000318
Chris Lattnerb4307b32010-01-15 19:28:38 +0000319bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000320 SMLoc EndLoc;
321 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000322}
323
Chris Lattner74ec1a32009-06-22 06:32:03 +0000324/// ParseExpression - Parse an expression and return it.
325///
326/// expr ::= expr +,- expr -> lowest.
327/// expr ::= expr |,^,&,! expr -> middle.
328/// expr ::= expr *,/,%,<<,>> expr -> highest.
329/// expr ::= primaryexpr
330///
Chris Lattner54482b42010-01-15 19:39:23 +0000331bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000332 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000333 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000334 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
335 return true;
336
337 // Try to constant fold it up front, if possible.
338 int64_t Value;
339 if (Res->EvaluateAsAbsolute(Value))
340 Res = MCConstantExpr::Create(Value, getContext());
341
342 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000343}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000344
Chris Lattnerb4307b32010-01-15 19:28:38 +0000345bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000346 Res = 0;
347 return ParseParenExpr(Res, EndLoc) ||
348 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000349}
350
Daniel Dunbar475839e2009-06-29 20:37:27 +0000351bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000352 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000353
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000354 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000355 if (ParseExpression(Expr))
356 return true;
357
Daniel Dunbare00b0112009-10-16 01:57:52 +0000358 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000359 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000360
361 return false;
362}
363
Daniel Dunbar3f872332009-07-28 16:08:33 +0000364static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000365 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000366 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000367 default:
368 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000369
370 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000371 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000372 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000373 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000374 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000375 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000376 return 1;
377
378 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000379 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000380 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000381 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000382 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000383 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000384 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000385 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000386 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000387 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000388 case AsmToken::ExclaimEqual:
389 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000390 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000391 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000392 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000393 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000394 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000395 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000396 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000397 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000398 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000399 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000400 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000401 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000402 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000403 return 2;
404
405 // Intermediate Precedence: |, &, ^
406 //
407 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000408 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000409 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000410 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000411 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000412 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000413 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000414 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000415 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000416 return 3;
417
418 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000419 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000420 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000421 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000422 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000423 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000424 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000425 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000426 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000427 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000428 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000429 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000430 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000431 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000432 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000433 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000434 }
435}
436
437
438/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
439/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000440bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
441 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000442 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000443 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000444 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000445
446 // If the next token is lower precedence than we are allowed to eat, return
447 // successfully with what we ate already.
448 if (TokPrec < Precedence)
449 return false;
450
Sean Callanan79ed1a82010-01-19 20:22:31 +0000451 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000452
453 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000454 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000455 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000456
457 // If BinOp binds less tightly with RHS than the operator after RHS, let
458 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000459 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000460 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000461 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000462 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000463 }
464
Daniel Dunbar475839e2009-06-29 20:37:27 +0000465 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000466 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000467 }
468}
469
Chris Lattnerc4193832009-06-22 05:51:26 +0000470
471
472
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000473/// ParseStatement:
474/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000475/// ::= Label* Directive ...Operands... EndOfStatement
476/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000477bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000478 if (Lexer.is(AsmToken::EndOfStatement)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +0000479 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000480 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000481 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000482
483 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000484 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000485 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000486 StringRef IDVal;
487 if (ParseIdentifier(IDVal))
488 return TokError("unexpected token at start of statement");
489
490 // FIXME: Recurse on local labels?
491
492 // See what kind of statement we have.
493 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000494 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000495 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000496 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000497
498 // Diagnose attempt to use a variable as a label.
499 //
500 // FIXME: Diagnostics. Note the location of the definition as a label.
501 // FIXME: This doesn't diagnose assignment to a symbol which has been
502 // implicitly marked as external.
Daniel Dunbar959fd882009-08-26 22:13:22 +0000503 MCSymbol *Sym = CreateSymbol(IDVal);
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000504 if (!Sym->isUndefined())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000505 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000506
Daniel Dunbar959fd882009-08-26 22:13:22 +0000507 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000508 Out.EmitLabel(Sym);
509
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000510 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000511 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000512
Daniel Dunbar3f872332009-07-28 16:08:33 +0000513 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000514 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000515 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000516
Daniel Dunbare2ace502009-08-31 08:09:09 +0000517 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000518
519 default: // Normal instruction or directive.
520 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000521 }
522
523 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000524 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000525 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000526 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000527 return ParseDirectiveDarwinSection();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000528 if (IDVal == ".text")
Chris Lattner529fb542009-06-24 05:13:15 +0000529 // FIXME: This changes behavior based on the -static flag to the
530 // assembler.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000531 return ParseDirectiveSectionSwitch("__TEXT", "__text",
532 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000533 if (IDVal == ".const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000534 return ParseDirectiveSectionSwitch("__TEXT", "__const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000535 if (IDVal == ".static_const")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000536 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000537 if (IDVal == ".cstring")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000538 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
539 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000540 if (IDVal == ".literal4")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000541 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000542 MCSectionMachO::S_4BYTE_LITERALS,
543 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000544 if (IDVal == ".literal8")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000545 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000546 MCSectionMachO::S_8BYTE_LITERALS,
547 8);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000548 if (IDVal == ".literal16")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000549 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000550 MCSectionMachO::S_16BYTE_LITERALS,
551 16);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000552 if (IDVal == ".constructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000553 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000554 if (IDVal == ".destructor")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000555 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000556 if (IDVal == ".fvmlib_init0")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000558 if (IDVal == ".fvmlib_init1")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000559 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
560
561 // FIXME: The assembler manual claims that this has the self modify code
562 // flag, at least on x86-32, but that does not appear to be correct.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000563 if (IDVal == ".symbol_stub")
564 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
565 MCSectionMachO::S_SYMBOL_STUBS |
Chris Lattnerff4bc462009-08-10 01:39:42 +0000566 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
567 // FIXME: Different on PPC and ARM.
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000568 0, 16);
569 // FIXME: PowerPC only?
570 if (IDVal == ".picsymbol_stub")
571 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
572 MCSectionMachO::S_SYMBOL_STUBS |
573 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
574 0, 26);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000575 if (IDVal == ".data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000576 return ParseDirectiveSectionSwitch("__DATA", "__data");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000577 if (IDVal == ".static_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000578 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
579
580 // FIXME: The section names of these two are misspelled in the assembler
581 // manual.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000582 if (IDVal == ".non_lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000583 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
584 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
585 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000586 if (IDVal == ".lazy_symbol_pointer")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000587 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
588 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
589 4);
590
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000591 if (IDVal == ".dyld")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000592 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000593 if (IDVal == ".mod_init_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000594 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000595 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
596 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000597 if (IDVal == ".mod_term_func")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000598 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000599 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
600 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000601 if (IDVal == ".const_data")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000602 return ParseDirectiveSectionSwitch("__DATA", "__const");
Chris Lattner529fb542009-06-24 05:13:15 +0000603
604
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000605 if (IDVal == ".objc_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000606 return ParseDirectiveSectionSwitch("__OBJC", "__class",
607 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000608 if (IDVal == ".objc_meta_class")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000609 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
610 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000611 if (IDVal == ".objc_cat_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000612 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
613 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000614 if (IDVal == ".objc_cat_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000615 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
616 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000617 if (IDVal == ".objc_protocol")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000618 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
619 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000620 if (IDVal == ".objc_string_object")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000621 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
622 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000623 if (IDVal == ".objc_cls_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000624 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
625 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000626 if (IDVal == ".objc_inst_meth")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000627 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
628 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000629 if (IDVal == ".objc_cls_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000630 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
631 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
632 MCSectionMachO::S_LITERAL_POINTERS,
633 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000634 if (IDVal == ".objc_message_refs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000635 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
636 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
637 MCSectionMachO::S_LITERAL_POINTERS,
638 4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000639 if (IDVal == ".objc_symbols")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000640 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
641 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000642 if (IDVal == ".objc_category")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000643 return ParseDirectiveSectionSwitch("__OBJC", "__category",
644 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000645 if (IDVal == ".objc_class_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000646 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
647 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000648 if (IDVal == ".objc_instance_vars")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000649 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
650 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000651 if (IDVal == ".objc_module_info")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000652 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
653 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000654 if (IDVal == ".objc_class_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000655 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
656 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000657 if (IDVal == ".objc_meth_var_types")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000658 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
659 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000660 if (IDVal == ".objc_meth_var_names")
Chris Lattnerff4bc462009-08-10 01:39:42 +0000661 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
662 MCSectionMachO::S_CSTRING_LITERALS);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000663 if (IDVal == ".objc_selector_strs")
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000664 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
665 MCSectionMachO::S_CSTRING_LITERALS);
Chris Lattner9a023f72009-06-24 04:43:34 +0000666
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000667 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000668 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000669 return ParseDirectiveSet();
670
Daniel Dunbara0d14262009-06-24 23:30:00 +0000671 // Data directives
672
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000673 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000674 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000675 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000676 return ParseDirectiveAscii(true);
677
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000678 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000679 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000680 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000681 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000682 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000683 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000684 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000685 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000686
687 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000688 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000689 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000690 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000691 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000692 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000693 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000694 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000695 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000696 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000697 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000698 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000699 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000700 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000701 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000702 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000703 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
704
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000705 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000706 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000707
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000708 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000709 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000710 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000711 return ParseDirectiveSpace();
712
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000713 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +0000714
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000715 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000716 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000717 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000718 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000719 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000720 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000721 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000722 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000723 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000724 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000725 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000726 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000727 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000728 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000729 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000730 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000731 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000732 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000733 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000734 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000735 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000736 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000737 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000738 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000739
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000740 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000741 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000742 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +0000743 return ParseDirectiveComm(/*IsLocal=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000744 if (IDVal == ".zerofill")
Chris Lattner9be3fee2009-07-10 22:20:30 +0000745 return ParseDirectiveDarwinZerofill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000746 if (IDVal == ".desc")
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000747 return ParseDirectiveDarwinSymbolDesc();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000748 if (IDVal == ".lsym")
Kevin Enderby71148242009-07-14 21:35:03 +0000749 return ParseDirectiveDarwinLsym();
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000750
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000751 if (IDVal == ".subsections_via_symbols")
Kevin Enderbya5c78322009-07-13 21:03:15 +0000752 return ParseDirectiveDarwinSubsectionsViaSymbols();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000753 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +0000754 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000755 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +0000756 return ParseDirectiveInclude();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000757 if (IDVal == ".dump")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000758 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000759 if (IDVal == ".load")
Kevin Enderby5026ae42009-07-20 20:25:37 +0000760 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
Kevin Enderbya5c78322009-07-13 21:03:15 +0000761
Chris Lattnerebb89b42009-09-27 21:16:52 +0000762 // Look up the handler in the handler table,
763 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
764 if (Handler)
765 return (this->*Handler)(IDVal, IDLoc);
766
Kevin Enderby9c656452009-09-10 20:51:44 +0000767 // Target hook for parsing target specific directives.
768 if (!getTargetParser().ParseDirective(ID))
769 return false;
770
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000771 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000772 EatToEndOfStatement();
773 return false;
774 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000775
Chris Lattner98986712010-01-14 22:21:20 +0000776
777 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
778 if (getTargetParser().ParseInstruction(IDVal, IDLoc, ParsedOperands))
779 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner29dfe7c2009-06-23 18:41:30 +0000780 return true;
Chris Lattner2cf5f142009-06-22 01:29:09 +0000781
Daniel Dunbar3f872332009-07-28 16:08:33 +0000782 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner98986712010-01-14 22:21:20 +0000783 // FIXME: Leaking ParsedOperands on failure.
Chris Lattner9a023f72009-06-24 04:43:34 +0000784 return TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +0000785
786 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000787 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000788
Chris Lattner98986712010-01-14 22:21:20 +0000789
790 MCInst Inst;
791
792 bool MatchFail = getTargetParser().MatchInstruction(ParsedOperands, Inst);
793
794 // Free any parsed operands.
795 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
796 delete ParsedOperands[i];
797
798 if (MatchFail) {
799 // FIXME: We should give nicer diagnostics about the exact failure.
800 Error(IDLoc, "unrecognized instruction");
801 return true;
802 }
803
Chris Lattner2cf5f142009-06-22 01:29:09 +0000804 // Instruction is good, process it.
Daniel Dunbar0eebb052009-07-01 06:35:48 +0000805 Out.EmitInstruction(Inst);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000806
807 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000808 return false;
809}
Chris Lattner9a023f72009-06-24 04:43:34 +0000810
Daniel Dunbare2ace502009-08-31 08:09:09 +0000811bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000812 // FIXME: Use better location, we should use proper tokens.
813 SMLoc EqualLoc = Lexer.getLoc();
814
Daniel Dunbar821e3332009-08-31 08:09:28 +0000815 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +0000816 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +0000817 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000818 return true;
819
Daniel Dunbar3f872332009-07-28 16:08:33 +0000820 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000821 return TokError("unexpected token in assignment");
822
823 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000824 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000825
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000826 // Validate that the LHS is allowed to be a variable (either it has not been
827 // used as a symbol, or it is an absolute symbol).
828 MCSymbol *Sym = getContext().LookupSymbol(Name);
829 if (Sym) {
830 // Diagnose assignment to a label.
831 //
832 // FIXME: Diagnostics. Note the location of the definition as a label.
833 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
834 if (!Sym->isUndefined() && !Sym->isAbsolute())
835 return Error(EqualLoc, "redefinition of '" + Name + "'");
836 else if (!Sym->isVariable())
837 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
838 else if (!isa<MCConstantExpr>(Sym->getValue()))
839 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
840 Name + "'");
841 } else
842 Sym = CreateSymbol(Name);
843
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000844 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000845
846 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +0000847 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000848
849 return false;
850}
851
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000852/// ParseIdentifier:
853/// ::= identifier
854/// ::= string
855bool AsmParser::ParseIdentifier(StringRef &Res) {
856 if (Lexer.isNot(AsmToken::Identifier) &&
857 Lexer.isNot(AsmToken::String))
858 return true;
859
Sean Callanan18b83232010-01-19 21:44:56 +0000860 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000861
Sean Callanan79ed1a82010-01-19 20:22:31 +0000862 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000863
864 return false;
865}
866
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000867/// ParseDirectiveSet:
868/// ::= .set identifier ',' expression
869bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000870 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000871
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000872 if (ParseIdentifier(Name))
873 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000874
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000875 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000876 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000877 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000878
Daniel Dunbare2ace502009-08-31 08:09:09 +0000879 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000880}
881
Chris Lattner9a023f72009-06-24 04:43:34 +0000882/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +0000883/// ::= .section identifier (',' identifier)*
884/// FIXME: This should actually parse out the segment, section, attributes and
885/// sizeof_stub fields.
886bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbarace63122009-08-11 03:42:33 +0000887 SMLoc Loc = Lexer.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000888
Daniel Dunbarace63122009-08-11 03:42:33 +0000889 StringRef SectionName;
890 if (ParseIdentifier(SectionName))
891 return Error(Loc, "expected identifier after '.section' directive");
892
893 // Verify there is a following comma.
894 if (!Lexer.is(AsmToken::Comma))
895 return TokError("unexpected token in '.section' directive");
896
Chris Lattnerff4bc462009-08-10 01:39:42 +0000897 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +0000898 SectionSpec += ",";
899
900 // Add all the tokens until the end of the line, ParseSectionSpecifier will
901 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000902 StringRef EOL = Lexer.LexUntilEndOfStatement();
903 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +0000904
Sean Callanan79ed1a82010-01-19 20:22:31 +0000905 Lex();
Daniel Dunbar3f872332009-07-28 16:08:33 +0000906 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +0000907 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000908 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +0000909
Chris Lattnerff4bc462009-08-10 01:39:42 +0000910
911 StringRef Segment, Section;
912 unsigned TAA, StubSize;
913 std::string ErrorStr =
914 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
915 TAA, StubSize);
916
917 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +0000918 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +0000919
Chris Lattner56594f92009-07-31 17:47:16 +0000920 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000921 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000922 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000923 isText ? SectionKind::getText()
924 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +0000925 return false;
926}
927
Chris Lattnere15c2d72009-08-10 18:05:55 +0000928/// ParseDirectiveSectionSwitch -
Chris Lattnerff4bc462009-08-10 01:39:42 +0000929bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
930 const char *Section,
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000931 unsigned TAA, unsigned Align,
932 unsigned StubSize) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000933 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +0000934 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +0000935 Lex();
Chris Lattner529fb542009-06-24 05:13:15 +0000936
Chris Lattner56594f92009-07-31 17:47:16 +0000937 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000938 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000939 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +0000940 isText ? SectionKind::getText()
941 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +0000942
943 // Set the implicit alignment, if any.
944 //
945 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
946 // alignment on the section (e.g., if one manually inserts bytes into the
947 // section, then just issueing the section switch directive will not realign
948 // the section. However, this is arguably more reasonable behavior, and there
949 // is no good reason for someone to intentionally emit incorrectly sized
950 // values into the implicitly aligned sections.
951 if (Align)
952 Out.EmitValueToAlignment(Align, 0, 1, 0);
953
Chris Lattner529fb542009-06-24 05:13:15 +0000954 return false;
955}
Daniel Dunbara0d14262009-06-24 23:30:00 +0000956
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000957bool AsmParser::ParseEscapedString(std::string &Data) {
958 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
959
960 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +0000961 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +0000962 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
963 if (Str[i] != '\\') {
964 Data += Str[i];
965 continue;
966 }
967
968 // Recognize escaped characters. Note that this escape semantics currently
969 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
970 ++i;
971 if (i == e)
972 return TokError("unexpected backslash at end of string");
973
974 // Recognize octal sequences.
975 if ((unsigned) (Str[i] - '0') <= 7) {
976 // Consume up to three octal characters.
977 unsigned Value = Str[i] - '0';
978
979 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
980 ++i;
981 Value = Value * 8 + (Str[i] - '0');
982
983 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
984 ++i;
985 Value = Value * 8 + (Str[i] - '0');
986 }
987 }
988
989 if (Value > 255)
990 return TokError("invalid octal escape sequence (out of range)");
991
992 Data += (unsigned char) Value;
993 continue;
994 }
995
996 // Otherwise recognize individual escapes.
997 switch (Str[i]) {
998 default:
999 // Just reject invalid escape sequences for now.
1000 return TokError("invalid escape sequence (unrecognized character)");
1001
1002 case 'b': Data += '\b'; break;
1003 case 'f': Data += '\f'; break;
1004 case 'n': Data += '\n'; break;
1005 case 'r': Data += '\r'; break;
1006 case 't': Data += '\t'; break;
1007 case '"': Data += '"'; break;
1008 case '\\': Data += '\\'; break;
1009 }
1010 }
1011
1012 return false;
1013}
1014
Daniel Dunbara0d14262009-06-24 23:30:00 +00001015/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001016/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001017bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001018 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001019 for (;;) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001020 if (Lexer.isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001021 return TokError("expected string in '.ascii' or '.asciz' directive");
1022
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001023 std::string Data;
1024 if (ParseEscapedString(Data))
1025 return true;
1026
Chris Lattneraaec2052010-01-19 19:46:13 +00001027 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001028 if (ZeroTerminated)
Chris Lattneraaec2052010-01-19 19:46:13 +00001029 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001030
Sean Callanan79ed1a82010-01-19 20:22:31 +00001031 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001032
Daniel Dunbar3f872332009-07-28 16:08:33 +00001033 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001034 break;
1035
Daniel Dunbar3f872332009-07-28 16:08:33 +00001036 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001037 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001038 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001039 }
1040 }
1041
Sean Callanan79ed1a82010-01-19 20:22:31 +00001042 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001043 return false;
1044}
1045
1046/// ParseDirectiveValue
1047/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1048bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001049 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001050 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001051 const MCExpr *Value;
Bill Wendling9bc0af82009-12-28 01:34:57 +00001052 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001053 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001054 return true;
1055
Chris Lattneraaec2052010-01-19 19:46:13 +00001056 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001057
Daniel Dunbar3f872332009-07-28 16:08:33 +00001058 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001059 break;
1060
1061 // FIXME: Improve diagnostic.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001062 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001063 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001064 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001065 }
1066 }
1067
Sean Callanan79ed1a82010-01-19 20:22:31 +00001068 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001069 return false;
1070}
1071
1072/// ParseDirectiveSpace
1073/// ::= .space expression [ , expression ]
1074bool AsmParser::ParseDirectiveSpace() {
1075 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001076 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001077 return true;
1078
1079 int64_t FillExpr = 0;
1080 bool HasFillExpr = false;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001081 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1082 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001083 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001084 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001085
Daniel Dunbar475839e2009-06-29 20:37:27 +00001086 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001087 return true;
1088
1089 HasFillExpr = true;
1090
Daniel Dunbar3f872332009-07-28 16:08:33 +00001091 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001092 return TokError("unexpected token in '.space' directive");
1093 }
1094
Sean Callanan79ed1a82010-01-19 20:22:31 +00001095 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001096
1097 if (NumBytes <= 0)
1098 return TokError("invalid number of bytes in '.space' directive");
1099
1100 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Chris Lattneraaec2052010-01-19 19:46:13 +00001101 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001102
1103 return false;
1104}
1105
1106/// ParseDirectiveFill
1107/// ::= .fill expression , expression , expression
1108bool AsmParser::ParseDirectiveFill() {
1109 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001110 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001111 return true;
1112
Daniel Dunbar3f872332009-07-28 16:08:33 +00001113 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001114 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001115 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001116
1117 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001118 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001119 return true;
1120
Daniel Dunbar3f872332009-07-28 16:08:33 +00001121 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001122 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001123 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001124
1125 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001126 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001127 return true;
1128
Daniel Dunbar3f872332009-07-28 16:08:33 +00001129 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001130 return TokError("unexpected token in '.fill' directive");
1131
Sean Callanan79ed1a82010-01-19 20:22:31 +00001132 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001133
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001134 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1135 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001136
1137 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Chris Lattneraaec2052010-01-19 19:46:13 +00001138 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1139 DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001140
1141 return false;
1142}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001143
1144/// ParseDirectiveOrg
1145/// ::= .org expression [ , expression ]
1146bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001147 const MCExpr *Offset;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001148 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001149 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001150 return true;
1151
1152 // Parse optional fill expression.
1153 int64_t FillExpr = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001154 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1155 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001156 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001157 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001158
Daniel Dunbar475839e2009-06-29 20:37:27 +00001159 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001160 return true;
1161
Daniel Dunbar3f872332009-07-28 16:08:33 +00001162 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001163 return TokError("unexpected token in '.org' directive");
1164 }
1165
Sean Callanan79ed1a82010-01-19 20:22:31 +00001166 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001167
1168 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1169 // has to be relative to the current section.
1170 Out.EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001171
1172 return false;
1173}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001174
1175/// ParseDirectiveAlign
1176/// ::= {.align, ...} expression [ , expression [ , expression ]]
1177bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001178 SMLoc AlignmentLoc = Lexer.getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001179 int64_t Alignment;
1180 if (ParseAbsoluteExpression(Alignment))
1181 return true;
1182
1183 SMLoc MaxBytesLoc;
1184 bool HasFillExpr = false;
1185 int64_t FillExpr = 0;
1186 int64_t MaxBytesToFill = 0;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001187 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1188 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001189 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001190 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001191
1192 // The fill expression can be omitted while specifying a maximum number of
1193 // alignment bytes, e.g:
1194 // .align 3,,4
Daniel Dunbar3f872332009-07-28 16:08:33 +00001195 if (Lexer.isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001196 HasFillExpr = true;
1197 if (ParseAbsoluteExpression(FillExpr))
1198 return true;
1199 }
1200
Daniel Dunbar3f872332009-07-28 16:08:33 +00001201 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1202 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001203 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001204 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001205
1206 MaxBytesLoc = Lexer.getLoc();
1207 if (ParseAbsoluteExpression(MaxBytesToFill))
1208 return true;
1209
Daniel Dunbar3f872332009-07-28 16:08:33 +00001210 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001211 return TokError("unexpected token in directive");
1212 }
1213 }
1214
Sean Callanan79ed1a82010-01-19 20:22:31 +00001215 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001216
1217 if (!HasFillExpr) {
1218 // FIXME: Sometimes fill with nop.
1219 FillExpr = 0;
1220 }
1221
1222 // Compute alignment in bytes.
1223 if (IsPow2) {
1224 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001225 if (Alignment >= 32) {
1226 Error(AlignmentLoc, "invalid alignment value");
1227 Alignment = 31;
1228 }
1229
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001230 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001231 }
1232
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001233 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001234 if (MaxBytesLoc.isValid()) {
1235 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001236 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1237 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001238 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001239 }
1240
1241 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001242 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1243 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001244 MaxBytesToFill = 0;
1245 }
1246 }
1247
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001248 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1249 // the TextAlignFillValue.
1250 if(Out.getCurrentSection()->getKind().isText() &&
1251 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1252 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1253 else
1254 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1255 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001256
1257 return false;
1258}
1259
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001260/// ParseDirectiveSymbolAttribute
1261/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001262bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001263 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001264 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001265 StringRef Name;
1266
1267 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001268 return TokError("expected identifier in directive");
1269
Daniel Dunbar959fd882009-08-26 22:13:22 +00001270 MCSymbol *Sym = CreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001271
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001272 Out.EmitSymbolAttribute(Sym, Attr);
1273
Daniel Dunbar3f872332009-07-28 16:08:33 +00001274 if (Lexer.is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001275 break;
1276
Daniel Dunbar3f872332009-07-28 16:08:33 +00001277 if (Lexer.isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001278 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001279 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001280 }
1281 }
1282
Sean Callanan79ed1a82010-01-19 20:22:31 +00001283 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001284 return false;
1285}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001286
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001287/// ParseDirectiveDarwinSymbolDesc
1288/// ::= .desc identifier , expression
1289bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001290 StringRef Name;
1291 if (ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001292 return TokError("expected identifier in directive");
1293
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001294 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001295 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001296
Daniel Dunbar3f872332009-07-28 16:08:33 +00001297 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001298 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001299 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001300
1301 SMLoc DescLoc = Lexer.getLoc();
1302 int64_t DescValue;
1303 if (ParseAbsoluteExpression(DescValue))
1304 return true;
1305
Daniel Dunbar3f872332009-07-28 16:08:33 +00001306 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001307 return TokError("unexpected token in '.desc' directive");
1308
Sean Callanan79ed1a82010-01-19 20:22:31 +00001309 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001310
1311 // Set the n_desc field of this Symbol to this DescValue
1312 Out.EmitSymbolDesc(Sym, DescValue);
1313
1314 return false;
1315}
1316
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001317/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001318/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1319bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001320 SMLoc IDLoc = Lexer.getLoc();
1321 StringRef Name;
1322 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001323 return TokError("expected identifier in directive");
1324
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001325 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001326 MCSymbol *Sym = CreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001327
Daniel Dunbar3f872332009-07-28 16:08:33 +00001328 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001329 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001330 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001331
1332 int64_t Size;
1333 SMLoc SizeLoc = Lexer.getLoc();
1334 if (ParseAbsoluteExpression(Size))
1335 return true;
1336
1337 int64_t Pow2Alignment = 0;
1338 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001339 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001340 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001341 Pow2AlignmentLoc = Lexer.getLoc();
1342 if (ParseAbsoluteExpression(Pow2Alignment))
1343 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001344
1345 // If this target takes alignments in bytes (not log) validate and convert.
1346 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1347 if (!isPowerOf2_64(Pow2Alignment))
1348 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1349 Pow2Alignment = Log2_64(Pow2Alignment);
1350 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001351 }
1352
Daniel Dunbar3f872332009-07-28 16:08:33 +00001353 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001354 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001355
Sean Callanan79ed1a82010-01-19 20:22:31 +00001356 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001357
Chris Lattner1fc3d752009-07-09 17:25:12 +00001358 // NOTE: a size of zero for a .comm should create a undefined symbol
1359 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001360 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001361 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1362 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001363
1364 // NOTE: The alignment in the directive is a power of 2 value, the assember
1365 // may internally end up wanting an alignment in bytes.
1366 // FIXME: Diagnose overflow.
1367 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001368 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1369 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001370
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001371 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001372 return Error(IDLoc, "invalid symbol redefinition");
1373
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001374 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001375 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001376 if (IsLocal) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +00001377 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1378 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001379 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001380 Sym, Size, 1 << Pow2Alignment);
1381 return false;
1382 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001383
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001384 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001385 return false;
1386}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001387
1388/// ParseDirectiveDarwinZerofill
1389/// ::= .zerofill segname , sectname [, identifier , size_expression [
1390/// , align_expression ]]
1391bool AsmParser::ParseDirectiveDarwinZerofill() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001392 // FIXME: Handle quoted names here.
1393
Daniel Dunbar3f872332009-07-28 16:08:33 +00001394 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001395 return TokError("expected segment name after '.zerofill' directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001396 StringRef Segment = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001397 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001398
Daniel Dunbar3f872332009-07-28 16:08:33 +00001399 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001400 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001401 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001402
Daniel Dunbar3f872332009-07-28 16:08:33 +00001403 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001404 return TokError("expected section name after comma in '.zerofill' "
1405 "directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001406 StringRef Section = getTok().getString();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001407 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001408
Chris Lattner9be3fee2009-07-10 22:20:30 +00001409 // If this is the end of the line all that was wanted was to create the
1410 // the section but with no symbol.
Daniel Dunbar3f872332009-07-28 16:08:33 +00001411 if (Lexer.is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001412 // Create the zerofill section but no symbol
Daniel Dunbar2e152922009-08-28 05:48:29 +00001413 Out.EmitZerofill(getMachOSection(Segment, Section,
1414 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001415 SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001416 return false;
1417 }
1418
Daniel Dunbar3f872332009-07-28 16:08:33 +00001419 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001420 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001421 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001422
Daniel Dunbar3f872332009-07-28 16:08:33 +00001423 if (Lexer.isNot(AsmToken::Identifier))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001424 return TokError("expected identifier in directive");
1425
1426 // handle the identifier as the key symbol.
1427 SMLoc IDLoc = Lexer.getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00001428 MCSymbol *Sym = CreateSymbol(getTok().getString());
Sean Callanan79ed1a82010-01-19 20:22:31 +00001429 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001430
Daniel Dunbar3f872332009-07-28 16:08:33 +00001431 if (Lexer.isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001432 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001433 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001434
1435 int64_t Size;
1436 SMLoc SizeLoc = Lexer.getLoc();
1437 if (ParseAbsoluteExpression(Size))
1438 return true;
1439
1440 int64_t Pow2Alignment = 0;
1441 SMLoc Pow2AlignmentLoc;
Daniel Dunbar3f872332009-07-28 16:08:33 +00001442 if (Lexer.is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001443 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001444 Pow2AlignmentLoc = Lexer.getLoc();
1445 if (ParseAbsoluteExpression(Pow2Alignment))
1446 return true;
1447 }
1448
Daniel Dunbar3f872332009-07-28 16:08:33 +00001449 if (Lexer.isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001450 return TokError("unexpected token in '.zerofill' directive");
1451
Sean Callanan79ed1a82010-01-19 20:22:31 +00001452 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001453
1454 if (Size < 0)
1455 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1456 "than zero");
1457
1458 // NOTE: The alignment in the directive is a power of 2 value, the assember
1459 // may internally end up wanting an alignment in bytes.
1460 // FIXME: Diagnose overflow.
1461 if (Pow2Alignment < 0)
1462 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1463 "can't be less than zero");
1464
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001465 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001466 return Error(IDLoc, "invalid symbol redefinition");
1467
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001468 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001469 //
1470 // FIXME: Arch specific.
1471 Out.EmitZerofill(getMachOSection(Segment, Section,
1472 MCSectionMachO::S_ZEROFILL, 0,
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001473 SectionKind::getBSS()),
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001474 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001475
1476 return false;
1477}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001478
1479/// ParseDirectiveDarwinSubsectionsViaSymbols
1480/// ::= .subsections_via_symbols
1481bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001482 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001483 return TokError("unexpected token in '.subsections_via_symbols' directive");
1484
Sean Callanan79ed1a82010-01-19 20:22:31 +00001485 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001486
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001487 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001488
1489 return false;
1490}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001491
1492/// ParseDirectiveAbort
1493/// ::= .abort [ "abort_string" ]
1494bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001495 // FIXME: Use loc from directive.
1496 SMLoc Loc = Lexer.getLoc();
1497
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001498 StringRef Str = "";
Daniel Dunbar3f872332009-07-28 16:08:33 +00001499 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1500 if (Lexer.isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001501 return TokError("expected string in '.abort' directive");
1502
Sean Callanan18b83232010-01-19 21:44:56 +00001503 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001504
Sean Callanan79ed1a82010-01-19 20:22:31 +00001505 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001506 }
1507
Daniel Dunbar3f872332009-07-28 16:08:33 +00001508 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001509 return TokError("unexpected token in '.abort' directive");
1510
Sean Callanan79ed1a82010-01-19 20:22:31 +00001511 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001512
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001513 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001514 if (Str.empty())
1515 Error(Loc, ".abort detected. Assembly stopping.");
1516 else
1517 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001518
1519 return false;
1520}
Kevin Enderby71148242009-07-14 21:35:03 +00001521
1522/// ParseDirectiveLsym
1523/// ::= .lsym identifier , expression
1524bool AsmParser::ParseDirectiveDarwinLsym() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001525 StringRef Name;
1526 if (ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001527 return TokError("expected identifier in directive");
1528
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001529 // Handle the identifier as the key symbol.
Daniel Dunbar959fd882009-08-26 22:13:22 +00001530 MCSymbol *Sym = CreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001531
Daniel Dunbar3f872332009-07-28 16:08:33 +00001532 if (Lexer.isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001533 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001534 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001535
Daniel Dunbar821e3332009-08-31 08:09:28 +00001536 const MCExpr *Value;
Daniel Dunbar883f9202009-08-31 08:08:50 +00001537 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001538 if (ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001539 return true;
1540
Daniel Dunbar3f872332009-07-28 16:08:33 +00001541 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001542 return TokError("unexpected token in '.lsym' directive");
1543
Sean Callanan79ed1a82010-01-19 20:22:31 +00001544 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001545
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001546 // We don't currently support this directive.
1547 //
1548 // FIXME: Diagnostic location!
1549 (void) Sym;
1550 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001551}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001552
1553/// ParseDirectiveInclude
1554/// ::= .include "filename"
1555bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001556 if (Lexer.isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001557 return TokError("expected string in '.include' directive");
1558
Sean Callanan18b83232010-01-19 21:44:56 +00001559 std::string Filename = getTok().getString();
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001560 SMLoc IncludeLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001561 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001562
Daniel Dunbar3f872332009-07-28 16:08:33 +00001563 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001564 return TokError("unexpected token in '.include' directive");
1565
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001566 // Strip the quotes.
1567 Filename = Filename.substr(1, Filename.size()-2);
1568
1569 // Attempt to switch the lexer to the included file before consuming the end
1570 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001571 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001572 PrintMessage(IncludeLoc,
1573 "Could not find include file '" + Filename + "'",
1574 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001575 return true;
1576 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001577
1578 return false;
1579}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001580
1581/// ParseDirectiveDarwinDumpOrLoad
1582/// ::= ( .dump | .load ) "filename"
Kevin Enderby5026ae42009-07-20 20:25:37 +00001583bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
Daniel Dunbar3f872332009-07-28 16:08:33 +00001584 if (Lexer.isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001585 return TokError("expected string in '.dump' or '.load' directive");
1586
Sean Callanan79ed1a82010-01-19 20:22:31 +00001587 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001588
Daniel Dunbar3f872332009-07-28 16:08:33 +00001589 if (Lexer.isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001590 return TokError("unexpected token in '.dump' or '.load' directive");
1591
Sean Callanan79ed1a82010-01-19 20:22:31 +00001592 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001593
Kevin Enderby5026ae42009-07-20 20:25:37 +00001594 // FIXME: If/when .dump and .load are implemented they will be done in the
1595 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001596 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001597 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001598 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001599 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001600
1601 return false;
1602}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001603
1604/// ParseDirectiveIf
1605/// ::= .if expression
1606bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1607 // Consume the identifier that was the .if directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001608 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001609
1610 TheCondStack.push_back(TheCondState);
1611 TheCondState.TheCond = AsmCond::IfCond;
1612 if(TheCondState.Ignore) {
1613 EatToEndOfStatement();
1614 }
1615 else {
1616 int64_t ExprValue;
1617 if (ParseAbsoluteExpression(ExprValue))
1618 return true;
1619
1620 if (Lexer.isNot(AsmToken::EndOfStatement))
1621 return TokError("unexpected token in '.if' directive");
1622
Sean Callanan79ed1a82010-01-19 20:22:31 +00001623 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001624
1625 TheCondState.CondMet = ExprValue;
1626 TheCondState.Ignore = !TheCondState.CondMet;
1627 }
1628
1629 return false;
1630}
1631
1632/// ParseDirectiveElseIf
1633/// ::= .elseif expression
1634bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1635 if (TheCondState.TheCond != AsmCond::IfCond &&
1636 TheCondState.TheCond != AsmCond::ElseIfCond)
1637 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1638 " an .elseif");
1639 TheCondState.TheCond = AsmCond::ElseIfCond;
1640
1641 // Consume the identifier that was the .elseif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001642 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001643
1644 bool LastIgnoreState = false;
1645 if (!TheCondStack.empty())
1646 LastIgnoreState = TheCondStack.back().Ignore;
1647 if (LastIgnoreState || TheCondState.CondMet) {
1648 TheCondState.Ignore = true;
1649 EatToEndOfStatement();
1650 }
1651 else {
1652 int64_t ExprValue;
1653 if (ParseAbsoluteExpression(ExprValue))
1654 return true;
1655
1656 if (Lexer.isNot(AsmToken::EndOfStatement))
1657 return TokError("unexpected token in '.elseif' directive");
1658
Sean Callanan79ed1a82010-01-19 20:22:31 +00001659 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001660 TheCondState.CondMet = ExprValue;
1661 TheCondState.Ignore = !TheCondState.CondMet;
1662 }
1663
1664 return false;
1665}
1666
1667/// ParseDirectiveElse
1668/// ::= .else
1669bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1670 // Consume the identifier that was the .else directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001671 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001672
1673 if (Lexer.isNot(AsmToken::EndOfStatement))
1674 return TokError("unexpected token in '.else' directive");
1675
Sean Callanan79ed1a82010-01-19 20:22:31 +00001676 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001677
1678 if (TheCondState.TheCond != AsmCond::IfCond &&
1679 TheCondState.TheCond != AsmCond::ElseIfCond)
1680 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1681 ".elseif");
1682 TheCondState.TheCond = AsmCond::ElseCond;
1683 bool LastIgnoreState = false;
1684 if (!TheCondStack.empty())
1685 LastIgnoreState = TheCondStack.back().Ignore;
1686 if (LastIgnoreState || TheCondState.CondMet)
1687 TheCondState.Ignore = true;
1688 else
1689 TheCondState.Ignore = false;
1690
1691 return false;
1692}
1693
1694/// ParseDirectiveEndIf
1695/// ::= .endif
1696bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1697 // Consume the identifier that was the .endif directive
Sean Callanan79ed1a82010-01-19 20:22:31 +00001698 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001699
1700 if (Lexer.isNot(AsmToken::EndOfStatement))
1701 return TokError("unexpected token in '.endif' directive");
1702
Sean Callanan79ed1a82010-01-19 20:22:31 +00001703 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00001704
1705 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1706 TheCondStack.empty())
1707 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1708 ".else");
1709 if (!TheCondStack.empty()) {
1710 TheCondState = TheCondStack.back();
1711 TheCondStack.pop_back();
1712 }
1713
1714 return false;
1715}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001716
1717/// ParseDirectiveFile
1718/// ::= .file [number] string
Chris Lattnerebb89b42009-09-27 21:16:52 +00001719bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001720 // FIXME: I'm not sure what this is.
1721 int64_t FileNumber = -1;
1722 if (Lexer.is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001723 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001724 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001725
1726 if (FileNumber < 1)
1727 return TokError("file number less than one");
1728 }
1729
1730 if (Lexer.isNot(AsmToken::String))
1731 return TokError("unexpected token in '.file' directive");
1732
Chris Lattnerd32e8032010-01-25 19:02:58 +00001733 StringRef Filename = getTok().getString();
1734 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00001735 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001736
1737 if (Lexer.isNot(AsmToken::EndOfStatement))
1738 return TokError("unexpected token in '.file' directive");
1739
Chris Lattnerd32e8032010-01-25 19:02:58 +00001740 if (FileNumber == -1)
1741 Out.EmitFileDirective(Filename);
1742 else
1743 Out.EmitDwarfFileDirective(FileNumber, Filename);
1744
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001745 return false;
1746}
1747
1748/// ParseDirectiveLine
1749/// ::= .line [number]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001750bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001751 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1752 if (Lexer.isNot(AsmToken::Integer))
1753 return TokError("unexpected token in '.line' directive");
1754
Sean Callanan18b83232010-01-19 21:44:56 +00001755 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001756 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001757 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001758
1759 // FIXME: Do something with the .line.
1760 }
1761
1762 if (Lexer.isNot(AsmToken::EndOfStatement))
1763 return TokError("unexpected token in '.file' directive");
1764
1765 return false;
1766}
1767
1768
1769/// ParseDirectiveLoc
1770/// ::= .loc number [number [number]]
Chris Lattnerebb89b42009-09-27 21:16:52 +00001771bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001772 if (Lexer.isNot(AsmToken::Integer))
1773 return TokError("unexpected token in '.loc' directive");
1774
1775 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00001776 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001777 (void) FileNumber;
1778 // FIXME: Validate file.
1779
Sean Callanan79ed1a82010-01-19 20:22:31 +00001780 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001781 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1782 if (Lexer.isNot(AsmToken::Integer))
1783 return TokError("unexpected token in '.loc' directive");
1784
Sean Callanan18b83232010-01-19 21:44:56 +00001785 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001786 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001787 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001788
1789 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1790 if (Lexer.isNot(AsmToken::Integer))
1791 return TokError("unexpected token in '.loc' directive");
1792
Sean Callanan18b83232010-01-19 21:44:56 +00001793 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001794 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00001795 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001796
1797 // FIXME: Do something with the .loc.
1798 }
1799 }
1800
1801 if (Lexer.isNot(AsmToken::EndOfStatement))
1802 return TokError("unexpected token in '.file' directive");
1803
1804 return false;
1805}
1806